-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: adds custom model, action recognition samples and tests (#111)
* samples: adds custom mode, action recognition samples and tests * fix: refactor to use Gson
- Loading branch information
Showing
61 changed files
with
3,184 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
aiplatform/snippets/src/main/java/aiplatform/CreateBatchPredictionJobBigquerySample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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_batch_prediction_job_bigquery_sample] | ||
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob; | ||
import com.google.cloud.aiplatform.v1beta1.BigQueryDestination; | ||
import com.google.cloud.aiplatform.v1beta1.BigQuerySource; | ||
import com.google.cloud.aiplatform.v1beta1.JobServiceClient; | ||
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings; | ||
import com.google.cloud.aiplatform.v1beta1.LocationName; | ||
import com.google.cloud.aiplatform.v1beta1.ModelName; | ||
import com.google.gson.JsonObject; | ||
import com.google.protobuf.Value; | ||
import com.google.protobuf.util.JsonFormat; | ||
import java.io.IOException; | ||
|
||
public class CreateBatchPredictionJobBigquerySample { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String project = "PROJECT"; | ||
String displayName = "DISPLAY_NAME"; | ||
String modelName = "MODEL_NAME"; | ||
String instancesFormat = "INSTANCES_FORMAT"; | ||
String bigquerySourceInputUri = "BIGQUERY_SOURCE_INPUT_URI"; | ||
String predictionsFormat = "PREDICTIONS_FORMAT"; | ||
String bigqueryDestinationOutputUri = "BIGQUERY_DESTINATION_OUTPUT_URI"; | ||
createBatchPredictionJobBigquerySample( | ||
project, | ||
displayName, | ||
modelName, | ||
instancesFormat, | ||
bigquerySourceInputUri, | ||
predictionsFormat, | ||
bigqueryDestinationOutputUri); | ||
} | ||
|
||
static void createBatchPredictionJobBigquerySample( | ||
String project, | ||
String displayName, | ||
String model, | ||
String instancesFormat, | ||
String bigquerySourceInputUri, | ||
String predictionsFormat, | ||
String bigqueryDestinationOutputUri) | ||
throws IOException { | ||
JobServiceSettings settings = | ||
JobServiceSettings.newBuilder() | ||
.setEndpoint("us-central1-aiplatform.googleapis.com:443") | ||
.build(); | ||
String location = "us-central1"; | ||
|
||
// 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 (JobServiceClient client = JobServiceClient.create(settings)) { | ||
JsonObject jsonModelParameters = new JsonObject(); | ||
Value.Builder modelParametersBuilder = Value.newBuilder(); | ||
JsonFormat.parser().merge(jsonModelParameters.toString(), modelParametersBuilder); | ||
Value modelParameters = modelParametersBuilder.build(); | ||
BigQuerySource bigquerySource = | ||
BigQuerySource.newBuilder().setInputUri(bigquerySourceInputUri).build(); | ||
BatchPredictionJob.InputConfig inputConfig = | ||
BatchPredictionJob.InputConfig.newBuilder() | ||
.setInstancesFormat(instancesFormat) | ||
.setBigquerySource(bigquerySource) | ||
.build(); | ||
BigQueryDestination bigqueryDestination = | ||
BigQueryDestination.newBuilder().setOutputUri(bigqueryDestinationOutputUri).build(); | ||
BatchPredictionJob.OutputConfig outputConfig = | ||
BatchPredictionJob.OutputConfig.newBuilder() | ||
.setPredictionsFormat(predictionsFormat) | ||
.setBigqueryDestination(bigqueryDestination) | ||
.build(); | ||
String modelName = ModelName.of(project, location, model).toString(); | ||
BatchPredictionJob batchPredictionJob = | ||
BatchPredictionJob.newBuilder() | ||
.setDisplayName(displayName) | ||
.setModel(modelName) | ||
.setModelParameters(modelParameters) | ||
.setInputConfig(inputConfig) | ||
.setOutputConfig(outputConfig) | ||
// optional | ||
.setGenerateExplanation(true) | ||
.build(); | ||
LocationName parent = LocationName.of(project, location); | ||
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob); | ||
System.out.format("response: %s\n", response); | ||
System.out.format("\tName: %s\n", response.getName()); | ||
} | ||
} | ||
} | ||
|
||
// [END aiplatform_create_batch_prediction_job_bigquery_sample] |
125 changes: 125 additions & 0 deletions
125
aiplatform/snippets/src/main/java/aiplatform/CreateBatchPredictionJobSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* 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_batch_prediction_job_sample] | ||
import com.google.cloud.aiplatform.v1beta1.AcceleratorType; | ||
import com.google.cloud.aiplatform.v1beta1.BatchDedicatedResources; | ||
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob; | ||
import com.google.cloud.aiplatform.v1beta1.GcsDestination; | ||
import com.google.cloud.aiplatform.v1beta1.GcsSource; | ||
import com.google.cloud.aiplatform.v1beta1.JobServiceClient; | ||
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings; | ||
import com.google.cloud.aiplatform.v1beta1.LocationName; | ||
import com.google.cloud.aiplatform.v1beta1.MachineSpec; | ||
import com.google.cloud.aiplatform.v1beta1.ModelName; | ||
import com.google.gson.JsonObject; | ||
import com.google.protobuf.Value; | ||
import com.google.protobuf.util.JsonFormat; | ||
import java.io.IOException; | ||
|
||
public class CreateBatchPredictionJobSample { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String project = "PROJECT"; | ||
String displayName = "DISPLAY_NAME"; | ||
String modelName = "MODEL_NAME"; | ||
String instancesFormat = "INSTANCES_FORMAT"; | ||
String gcsSourceUri = "GCS_SOURCE_URI"; | ||
String predictionsFormat = "PREDICTIONS_FORMAT"; | ||
String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX"; | ||
createBatchPredictionJobSample( | ||
project, | ||
displayName, | ||
modelName, | ||
instancesFormat, | ||
gcsSourceUri, | ||
predictionsFormat, | ||
gcsDestinationOutputUriPrefix); | ||
} | ||
|
||
static void createBatchPredictionJobSample( | ||
String project, | ||
String displayName, | ||
String model, | ||
String instancesFormat, | ||
String gcsSourceUri, | ||
String predictionsFormat, | ||
String gcsDestinationOutputUriPrefix) | ||
throws IOException { | ||
JobServiceSettings settings = | ||
JobServiceSettings.newBuilder() | ||
.setEndpoint("us-central1-aiplatform.googleapis.com:443") | ||
.build(); | ||
String location = "us-central1"; | ||
|
||
// 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 (JobServiceClient client = JobServiceClient.create(settings)) { | ||
|
||
// Passing in an empty Value object for model parameters | ||
JsonObject jsonModelParameters = new JsonObject(); | ||
Value.Builder modelParametersBuilder = Value.newBuilder(); | ||
JsonFormat.parser().merge(jsonModelParameters.toString(), modelParametersBuilder); | ||
Value modelParameters = modelParametersBuilder.build(); | ||
|
||
GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build(); | ||
BatchPredictionJob.InputConfig inputConfig = | ||
BatchPredictionJob.InputConfig.newBuilder() | ||
.setInstancesFormat(instancesFormat) | ||
.setGcsSource(gcsSource) | ||
.build(); | ||
GcsDestination gcsDestination = | ||
GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build(); | ||
BatchPredictionJob.OutputConfig outputConfig = | ||
BatchPredictionJob.OutputConfig.newBuilder() | ||
.setPredictionsFormat(predictionsFormat) | ||
.setGcsDestination(gcsDestination) | ||
.build(); | ||
MachineSpec machineSpec = | ||
MachineSpec.newBuilder() | ||
.setMachineType("n1-standard-2") | ||
.setAcceleratorType(AcceleratorType.NVIDIA_TESLA_K80) | ||
.setAcceleratorCount(1) | ||
.build(); | ||
BatchDedicatedResources dedicatedResources = | ||
BatchDedicatedResources.newBuilder() | ||
.setMachineSpec(machineSpec) | ||
.setStartingReplicaCount(1) | ||
.setMaxReplicaCount(1) | ||
.build(); | ||
String modelName = ModelName.of(project, location, model).toString(); | ||
BatchPredictionJob batchPredictionJob = | ||
BatchPredictionJob.newBuilder() | ||
.setDisplayName(displayName) | ||
.setModel(modelName) | ||
.setModelParameters(modelParameters) | ||
.setInputConfig(inputConfig) | ||
.setOutputConfig(outputConfig) | ||
.setDedicatedResources(dedicatedResources) | ||
.build(); | ||
LocationName parent = LocationName.of(project, location); | ||
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob); | ||
System.out.format("response: %s\n", response); | ||
System.out.format("\tName: %s\n", response.getName()); | ||
} | ||
} | ||
} | ||
|
||
// [END aiplatform_create_batch_prediction_job_sample] |
99 changes: 99 additions & 0 deletions
99
...ippets/src/main/java/aiplatform/CreateBatchPredictionJobVideoActionRecognitionSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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_batch_prediction_job_video_action_recognition_sample] | ||
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob; | ||
import com.google.cloud.aiplatform.v1beta1.GcsDestination; | ||
import com.google.cloud.aiplatform.v1beta1.GcsSource; | ||
import com.google.cloud.aiplatform.v1beta1.JobServiceClient; | ||
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings; | ||
import com.google.cloud.aiplatform.v1beta1.LocationName; | ||
import com.google.cloud.aiplatform.v1beta1.ModelName; | ||
import com.google.gson.JsonObject; | ||
import com.google.protobuf.Value; | ||
import com.google.protobuf.util.JsonFormat; | ||
import java.io.IOException; | ||
|
||
public class CreateBatchPredictionJobVideoActionRecognitionSample { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String project = "PROJECT"; | ||
String displayName = "DISPLAY_NAME"; | ||
String model = "MODEL"; | ||
String gcsSourceUri = "GCS_SOURCE_URI"; | ||
String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX"; | ||
createBatchPredictionJobVideoActionRecognitionSample( | ||
project, displayName, model, gcsSourceUri, gcsDestinationOutputUriPrefix); | ||
} | ||
|
||
static void createBatchPredictionJobVideoActionRecognitionSample( | ||
String project, | ||
String displayName, | ||
String model, | ||
String gcsSourceUri, | ||
String gcsDestinationOutputUriPrefix) | ||
throws IOException { | ||
JobServiceSettings settings = | ||
JobServiceSettings.newBuilder() | ||
.setEndpoint("us-central1-aiplatform.googleapis.com:443") | ||
.build(); | ||
String location = "us-central1"; | ||
|
||
// 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 (JobServiceClient client = JobServiceClient.create(settings)) { | ||
JsonObject jsonModelParameters = new JsonObject(); | ||
jsonModelParameters.addProperty("confidenceThreshold", 0.5); | ||
Value.Builder modelParametersBuilder = Value.newBuilder(); | ||
JsonFormat.parser().merge(jsonModelParameters.toString(), modelParametersBuilder); | ||
Value modelParameters = modelParametersBuilder.build(); | ||
GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build(); | ||
BatchPredictionJob.InputConfig inputConfig = | ||
BatchPredictionJob.InputConfig.newBuilder() | ||
.setInstancesFormat("jsonl") | ||
.setGcsSource(gcsSource) | ||
.build(); | ||
GcsDestination gcsDestination = | ||
GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build(); | ||
BatchPredictionJob.OutputConfig outputConfig = | ||
BatchPredictionJob.OutputConfig.newBuilder() | ||
.setPredictionsFormat("jsonl") | ||
.setGcsDestination(gcsDestination) | ||
.build(); | ||
|
||
String modelName = ModelName.of(project, location, model).toString(); | ||
|
||
BatchPredictionJob batchPredictionJob = | ||
BatchPredictionJob.newBuilder() | ||
.setDisplayName(displayName) | ||
.setModel(modelName) | ||
.setModelParameters(modelParameters) | ||
.setInputConfig(inputConfig) | ||
.setOutputConfig(outputConfig) | ||
.build(); | ||
LocationName parent = LocationName.of(project, location); | ||
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob); | ||
System.out.format("response: %s\n", response); | ||
System.out.format("\tName: %s\n", response.getName()); | ||
} | ||
} | ||
} | ||
|
||
// [END aiplatform_create_batch_prediction_job_video_action_recognition_sample] |
Oops, something went wrong.