Skip to content

Commit

Permalink
samples: ucaip batch samples 4 of 6 (#19)
Browse files Browse the repository at this point in the history
* samples: ucaip batch samples 4 of 6

* made requested changes

* fixed prediction test

* added missing resources
  • Loading branch information
munkhuushmgl authored Nov 6, 2020
1 parent 413b908 commit a662f6b
Show file tree
Hide file tree
Showing 11 changed files with 1,042 additions and 0 deletions.
1 change: 1 addition & 0 deletions aiplatform/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@
<version>1.1</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Binary file added aiplatform/snippets/resources/daisy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package aiplatform;

// [START aiplatform_create_training_pipeline_sample]

import com.google.cloud.aiplatform.v1beta1.DeployedModelRef;
import com.google.cloud.aiplatform.v1beta1.EnvVar;
import com.google.cloud.aiplatform.v1beta1.ExplanationMetadata;
import com.google.cloud.aiplatform.v1beta1.ExplanationParameters;
import com.google.cloud.aiplatform.v1beta1.ExplanationSpec;
import com.google.cloud.aiplatform.v1beta1.FilterSplit;
import com.google.cloud.aiplatform.v1beta1.FractionSplit;
import com.google.cloud.aiplatform.v1beta1.InputDataConfig;
import com.google.cloud.aiplatform.v1beta1.LocationName;
import com.google.cloud.aiplatform.v1beta1.Model;
import com.google.cloud.aiplatform.v1beta1.Model.ExportFormat;
import com.google.cloud.aiplatform.v1beta1.ModelContainerSpec;
import com.google.cloud.aiplatform.v1beta1.PipelineServiceClient;
import com.google.cloud.aiplatform.v1beta1.PipelineServiceSettings;
import com.google.cloud.aiplatform.v1beta1.Port;
import com.google.cloud.aiplatform.v1beta1.PredefinedSplit;
import com.google.cloud.aiplatform.v1beta1.PredictSchemata;
import com.google.cloud.aiplatform.v1beta1.SampledShapleyAttribution;
import com.google.cloud.aiplatform.v1beta1.TimestampSplit;
import com.google.cloud.aiplatform.v1beta1.TrainingPipeline;
import com.google.protobuf.Any;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import com.google.rpc.Status;
import java.io.IOException;
import java.util.List;

public class CreateTrainingPipelineSample {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String trainingPipelineDisplayName = "YOUR_TRAINING_PIPELINE_DISPLAY_NAME";
String project = "YOUR_PROJECT_ID";
String datasetId = "YOUR_DATASET_ID";
String trainingTaskDefinition = "YOUR_TRAINING_TASK_DEFINITION";
String modelDisplayName = "YOUR_MODEL_DISPLAY_NAME";
createTrainingPipelineSample(
project, trainingPipelineDisplayName, datasetId, trainingTaskDefinition, modelDisplayName);
}

static void createTrainingPipelineSample(
String project,
String trainingPipelineDisplayName,
String datasetId,
String trainingTaskDefinition,
String modelDisplayName)
throws IOException {
PipelineServiceSettings pipelineServiceSettings =
PipelineServiceSettings.newBuilder()
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
.build();

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (PipelineServiceClient pipelineServiceClient =
PipelineServiceClient.create(pipelineServiceSettings)) {
String location = "us-central1";
LocationName locationName = LocationName.of(project, location);

String jsonString =
"{\"multiLabel\": false, \"modelType\": \"CLOUD\", \"budgetMilliNodeHours\": 8000,"
+ " \"disableEarlyStopping\": false}";
Value.Builder trainingTaskInputs = Value.newBuilder();
JsonFormat.parser().merge(jsonString, trainingTaskInputs);

InputDataConfig trainingInputDataConfig =
InputDataConfig.newBuilder().setDatasetId(datasetId).build();
Model model = Model.newBuilder().setDisplayName(modelDisplayName).build();
TrainingPipeline trainingPipeline =
TrainingPipeline.newBuilder()
.setDisplayName(trainingPipelineDisplayName)
.setTrainingTaskDefinition(trainingTaskDefinition)
.setTrainingTaskInputs(trainingTaskInputs)
.setInputDataConfig(trainingInputDataConfig)
.setModelToUpload(model)
.build();

TrainingPipeline trainingPipelineResponse =
pipelineServiceClient.createTrainingPipeline(locationName, trainingPipeline);

System.out.println("Create Training Pipeline Response");
System.out.format("Name: %s\n", trainingPipelineResponse.getName());
System.out.format("Display Name: %s\n", trainingPipelineResponse.getDisplayName());

System.out.format(
"Training Task Definition %s\n", trainingPipelineResponse.getTrainingTaskDefinition());
System.out.format(
"Training Task Inputs: %s\n", trainingPipelineResponse.getTrainingTaskInputs());
System.out.format(
"Training Task Metadata: %s\n", trainingPipelineResponse.getTrainingTaskMetadata());
System.out.format("State: %s\n", trainingPipelineResponse.getState());

System.out.format("Create Time: %s\n", trainingPipelineResponse.getCreateTime());
System.out.format("StartTime %s\n", trainingPipelineResponse.getStartTime());
System.out.format("End Time: %s\n", trainingPipelineResponse.getEndTime());
System.out.format("Update Time: %s\n", trainingPipelineResponse.getUpdateTime());
System.out.format("Labels: %s\n", trainingPipelineResponse.getLabelsMap());

InputDataConfig inputDataConfig = trainingPipelineResponse.getInputDataConfig();
System.out.println("Input Data Config");
System.out.format("Dataset Id: %s", inputDataConfig.getDatasetId());
System.out.format("Annotations Filter: %s\n", inputDataConfig.getAnnotationsFilter());

FractionSplit fractionSplit = inputDataConfig.getFractionSplit();
System.out.println("Fraction Split");
System.out.format("Training Fraction: %s\n", fractionSplit.getTrainingFraction());
System.out.format("Validation Fraction: %s\n", fractionSplit.getValidationFraction());
System.out.format("Test Fraction: %s\n", fractionSplit.getTestFraction());

FilterSplit filterSplit = inputDataConfig.getFilterSplit();
System.out.println("Filter Split");
System.out.format("Training Filter: %s\n", filterSplit.getTrainingFilter());
System.out.format("Validation Filter: %s\n", filterSplit.getValidationFilter());
System.out.format("Test Filter: %s\n", filterSplit.getTestFilter());

PredefinedSplit predefinedSplit = inputDataConfig.getPredefinedSplit();
System.out.println("Predefined Split");
System.out.format("Key: %s\n", predefinedSplit.getKey());

TimestampSplit timestampSplit = inputDataConfig.getTimestampSplit();
System.out.println("Timestamp Split");
System.out.format("Training Fraction: %s\n", timestampSplit.getTrainingFraction());
System.out.format("Validation Fraction: %s\n", timestampSplit.getValidationFraction());
System.out.format("Test Fraction: %s\n", timestampSplit.getTestFraction());
System.out.format("Key: %s\n", timestampSplit.getKey());

Model modelResponse = trainingPipelineResponse.getModelToUpload();
System.out.println("Model To Upload");
System.out.format("Name: %s\n", modelResponse.getName());
System.out.format("Display Name: %s\n", modelResponse.getDisplayName());
System.out.format("Description: %s\n", modelResponse.getDescription());

System.out.format("Metadata Schema Uri: %s\n", modelResponse.getMetadataSchemaUri());
System.out.format("Metadata: %s\n", modelResponse.getMetadata());
System.out.format("Training Pipeline: %s\n", modelResponse.getTrainingPipeline());
System.out.format("Artifact Uri: %s\n", modelResponse.getArtifactUri());

System.out.format(
"Supported Deployment Resources Types: %s\n",
modelResponse.getSupportedDeploymentResourcesTypesList());
System.out.format(
"Supported Input Storage Formats: %s\n",
modelResponse.getSupportedInputStorageFormatsList());
System.out.format(
"Supported Output Storage Formats: %s\n",
modelResponse.getSupportedOutputStorageFormatsList());

System.out.format("Create Time: %s\n", modelResponse.getCreateTime());
System.out.format("Update Time: %s\n", modelResponse.getUpdateTime());
System.out.format("Labels: %sn\n", modelResponse.getLabelsMap());

PredictSchemata predictSchemata = modelResponse.getPredictSchemata();
System.out.println("Predict Schemata");
System.out.format("Instance Schema Uri: %s\n", predictSchemata.getInstanceSchemaUri());
System.out.format("Parameters Schema Uri: %s\n", predictSchemata.getParametersSchemaUri());
System.out.format("Prediction Schema Uri: %s\n", predictSchemata.getPredictionSchemaUri());

for (ExportFormat exportFormat : modelResponse.getSupportedExportFormatsList()) {
System.out.println("Supported Export Format");
System.out.format("Id: %s\n", exportFormat.getId());
}

ModelContainerSpec modelContainerSpec = modelResponse.getContainerSpec();
System.out.println("Container Spec");
System.out.format("Image Uri: %s\n", modelContainerSpec.getImageUri());
System.out.format("Command: %s\n", modelContainerSpec.getCommandList());
System.out.format("Args: %s\n", modelContainerSpec.getArgsList());
System.out.format("Predict Route: %s\n", modelContainerSpec.getPredictRoute());
System.out.format("Health Route: %s\n", modelContainerSpec.getHealthRoute());

for (EnvVar envVar : modelContainerSpec.getEnvList()) {
System.out.println("Env");
System.out.format("Name: %s\n", envVar.getName());
System.out.format("Value: %s\n", envVar.getValue());
}

for (Port port : modelContainerSpec.getPortsList()) {
System.out.println("Port");
System.out.format("Container Port: %s\n", port.getContainerPort());
}

for (DeployedModelRef deployedModelRef : modelResponse.getDeployedModelsList()) {
System.out.println("Deployed Model");
System.out.format("Endpoint: %s\n", deployedModelRef.getEndpoint());
System.out.format("Deployed Model Id: %s\n", deployedModelRef.getDeployedModelId());
}

ExplanationSpec explanationSpec = modelResponse.getExplanationSpec();
System.out.println("Explanation Spec");

ExplanationParameters explanationParameters = explanationSpec.getParameters();
System.out.println("Parameters");

SampledShapleyAttribution sampledShapleyAttribution =
explanationParameters.getSampledShapleyAttribution();
System.out.println("Sampled Shapley Attribution");
System.out.format("Path Count: %s\n", sampledShapleyAttribution.getPathCount());

ExplanationMetadata explanationMetadata = explanationSpec.getMetadata();
System.out.println("Metadata");
System.out.format("Inputs: %s\n", explanationMetadata.getInputsMap());
System.out.format("Outputs: %s\n", explanationMetadata.getOutputsMap());
System.out.format(
"Feature Attributions Schema_uri: %s\n",
explanationMetadata.getFeatureAttributionsSchemaUri());

Status status = trainingPipelineResponse.getError();
System.out.println("Error");
System.out.format("Code: %s\n", status.getCode());
System.out.format("Message: %s\n", status.getMessage());
}
}
}
// [END aiplatform_create_training_pipeline_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package aiplatform;

// [START aiplatform_delete_model_sample]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1beta1.DeleteOperationMetadata;
import com.google.cloud.aiplatform.v1beta1.ModelName;
import com.google.cloud.aiplatform.v1beta1.ModelServiceClient;
import com.google.cloud.aiplatform.v1beta1.ModelServiceSettings;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeleteModelSample {
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String modelId = "YOUR_MODEL_ID";
deleteModel(project, modelId);
}

static void deleteModel(String project, String modelId)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
ModelServiceSettings modelServiceSettings =
ModelServiceSettings.newBuilder()
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
.build();

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ModelServiceClient modelServiceClient = ModelServiceClient.create(modelServiceSettings)) {
String location = "us-central1";
ModelName modelName = ModelName.of(project, location, modelId);
OperationFuture<Empty, DeleteOperationMetadata> operationFuture =
modelServiceClient.deleteModelAsync(modelName);
System.out.format("Operation name: %s\n", operationFuture.getInitialFuture().get().getName());
System.out.println("Waiting for operation to finish...");
operationFuture.get(300, TimeUnit.SECONDS);
System.out.format("Deleted Model.");
}
}
}
// [END aiplatform_delete_model_sample]
Loading

0 comments on commit a662f6b

Please sign in to comment.