This repository was archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
samples: adds custom model, action recognition samples and tests #111
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a818952
samples: adds custom mode, action recognition samples and tests
telpirion 43f24be
fix: per reviewer
telpirion 1fc163a
fix: linter
telpirion 46f224e
fix: per linter
telpirion e8830e8
chore: adds env vars
telpirion 3c9e5d1
fix: more linter
telpirion de00a7c
fix: remove env vars
telpirion cd6657d
fix: adds env vars correct way, fixes to deploy model sample
telpirion f1fe3df
Merge branch 'master' into additional-samples
telpirion 1ea481e
fix: loads env vars
telpirion 891048c
fix: refactor to use Gson
telpirion d60a3c3
Merge branch 'master' into additional-samples
telpirion e562308
fix: lint
telpirion 318d911
fix: per reviewer
telpirion 6a0d859
fix:lint
telpirion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
108 changes: 108 additions & 0 deletions
108
samples/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,108 @@ | ||
/* | ||
* 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.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 modelName, | ||
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(); | ||
BatchPredictionJob batchPredictionJob = | ||
BatchPredictionJob.newBuilder() | ||
.setDisplayName(displayName) | ||
// Format: 'projects/{project}/locations/{location}/models/{model_id}' | ||
.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] |
124 changes: 124 additions & 0 deletions
124
samples/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,124 @@ | ||
/* | ||
* 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.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 modelName, | ||
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(); | ||
BatchPredictionJob batchPredictionJob = | ||
BatchPredictionJob.newBuilder() | ||
.setDisplayName(displayName) | ||
// Format: 'projects/{project}/locations/{location}/models/{model_id}' | ||
.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] |
96 changes: 96 additions & 0 deletions
96
...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,96 @@ | ||
/* | ||
* 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.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(); | ||
BatchPredictionJob batchPredictionJob = | ||
BatchPredictionJob.newBuilder() | ||
.setDisplayName(displayName) | ||
// Format: 'projects/{project}/locations/{location}/models/{model_id}' | ||
.setModel(model) | ||
.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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For any resource name, we should probably demonstrate the resource name helpers:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.