Skip to content

Commit d6602ce

Browse files
authored
samples: adds custom model, action recognition samples and tests (#111)
* samples: adds custom mode, action recognition samples and tests * fix: refactor to use Gson
1 parent fc550d9 commit d6602ce

File tree

61 files changed

+3184
-101
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3184
-101
lines changed

aiplatform/snippets/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<artifactId>protobuf-java-util</artifactId>
4141
<version>4.0.0-rc-2</version>
4242
</dependency>
43+
<dependency>
44+
<groupId>com.google.code.gson</groupId>
45+
<artifactId>gson</artifactId>
46+
<version>2.8.6</version>
47+
</dependency>
4348
<dependency>
4449
<groupId>junit</groupId>
4550
<artifactId>junit</artifactId>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package aiplatform;
18+
19+
// [START aiplatform_create_batch_prediction_job_bigquery_sample]
20+
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob;
21+
import com.google.cloud.aiplatform.v1beta1.BigQueryDestination;
22+
import com.google.cloud.aiplatform.v1beta1.BigQuerySource;
23+
import com.google.cloud.aiplatform.v1beta1.JobServiceClient;
24+
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings;
25+
import com.google.cloud.aiplatform.v1beta1.LocationName;
26+
import com.google.cloud.aiplatform.v1beta1.ModelName;
27+
import com.google.gson.JsonObject;
28+
import com.google.protobuf.Value;
29+
import com.google.protobuf.util.JsonFormat;
30+
import java.io.IOException;
31+
32+
public class CreateBatchPredictionJobBigquerySample {
33+
34+
public static void main(String[] args) throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String project = "PROJECT";
37+
String displayName = "DISPLAY_NAME";
38+
String modelName = "MODEL_NAME";
39+
String instancesFormat = "INSTANCES_FORMAT";
40+
String bigquerySourceInputUri = "BIGQUERY_SOURCE_INPUT_URI";
41+
String predictionsFormat = "PREDICTIONS_FORMAT";
42+
String bigqueryDestinationOutputUri = "BIGQUERY_DESTINATION_OUTPUT_URI";
43+
createBatchPredictionJobBigquerySample(
44+
project,
45+
displayName,
46+
modelName,
47+
instancesFormat,
48+
bigquerySourceInputUri,
49+
predictionsFormat,
50+
bigqueryDestinationOutputUri);
51+
}
52+
53+
static void createBatchPredictionJobBigquerySample(
54+
String project,
55+
String displayName,
56+
String model,
57+
String instancesFormat,
58+
String bigquerySourceInputUri,
59+
String predictionsFormat,
60+
String bigqueryDestinationOutputUri)
61+
throws IOException {
62+
JobServiceSettings settings =
63+
JobServiceSettings.newBuilder()
64+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
65+
.build();
66+
String location = "us-central1";
67+
68+
// Initialize client that will be used to send requests. This client only needs to be created
69+
// once, and can be reused for multiple requests. After completing all of your requests, call
70+
// the "close" method on the client to safely clean up any remaining background resources.
71+
try (JobServiceClient client = JobServiceClient.create(settings)) {
72+
JsonObject jsonModelParameters = new JsonObject();
73+
Value.Builder modelParametersBuilder = Value.newBuilder();
74+
JsonFormat.parser().merge(jsonModelParameters.toString(), modelParametersBuilder);
75+
Value modelParameters = modelParametersBuilder.build();
76+
BigQuerySource bigquerySource =
77+
BigQuerySource.newBuilder().setInputUri(bigquerySourceInputUri).build();
78+
BatchPredictionJob.InputConfig inputConfig =
79+
BatchPredictionJob.InputConfig.newBuilder()
80+
.setInstancesFormat(instancesFormat)
81+
.setBigquerySource(bigquerySource)
82+
.build();
83+
BigQueryDestination bigqueryDestination =
84+
BigQueryDestination.newBuilder().setOutputUri(bigqueryDestinationOutputUri).build();
85+
BatchPredictionJob.OutputConfig outputConfig =
86+
BatchPredictionJob.OutputConfig.newBuilder()
87+
.setPredictionsFormat(predictionsFormat)
88+
.setBigqueryDestination(bigqueryDestination)
89+
.build();
90+
String modelName = ModelName.of(project, location, model).toString();
91+
BatchPredictionJob batchPredictionJob =
92+
BatchPredictionJob.newBuilder()
93+
.setDisplayName(displayName)
94+
.setModel(modelName)
95+
.setModelParameters(modelParameters)
96+
.setInputConfig(inputConfig)
97+
.setOutputConfig(outputConfig)
98+
// optional
99+
.setGenerateExplanation(true)
100+
.build();
101+
LocationName parent = LocationName.of(project, location);
102+
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob);
103+
System.out.format("response: %s\n", response);
104+
System.out.format("\tName: %s\n", response.getName());
105+
}
106+
}
107+
}
108+
109+
// [END aiplatform_create_batch_prediction_job_bigquery_sample]
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package aiplatform;
18+
19+
// [START aiplatform_create_batch_prediction_job_sample]
20+
import com.google.cloud.aiplatform.v1beta1.AcceleratorType;
21+
import com.google.cloud.aiplatform.v1beta1.BatchDedicatedResources;
22+
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob;
23+
import com.google.cloud.aiplatform.v1beta1.GcsDestination;
24+
import com.google.cloud.aiplatform.v1beta1.GcsSource;
25+
import com.google.cloud.aiplatform.v1beta1.JobServiceClient;
26+
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings;
27+
import com.google.cloud.aiplatform.v1beta1.LocationName;
28+
import com.google.cloud.aiplatform.v1beta1.MachineSpec;
29+
import com.google.cloud.aiplatform.v1beta1.ModelName;
30+
import com.google.gson.JsonObject;
31+
import com.google.protobuf.Value;
32+
import com.google.protobuf.util.JsonFormat;
33+
import java.io.IOException;
34+
35+
public class CreateBatchPredictionJobSample {
36+
37+
public static void main(String[] args) throws IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String project = "PROJECT";
40+
String displayName = "DISPLAY_NAME";
41+
String modelName = "MODEL_NAME";
42+
String instancesFormat = "INSTANCES_FORMAT";
43+
String gcsSourceUri = "GCS_SOURCE_URI";
44+
String predictionsFormat = "PREDICTIONS_FORMAT";
45+
String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX";
46+
createBatchPredictionJobSample(
47+
project,
48+
displayName,
49+
modelName,
50+
instancesFormat,
51+
gcsSourceUri,
52+
predictionsFormat,
53+
gcsDestinationOutputUriPrefix);
54+
}
55+
56+
static void createBatchPredictionJobSample(
57+
String project,
58+
String displayName,
59+
String model,
60+
String instancesFormat,
61+
String gcsSourceUri,
62+
String predictionsFormat,
63+
String gcsDestinationOutputUriPrefix)
64+
throws IOException {
65+
JobServiceSettings settings =
66+
JobServiceSettings.newBuilder()
67+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
68+
.build();
69+
String location = "us-central1";
70+
71+
// Initialize client that will be used to send requests. This client only needs to be created
72+
// once, and can be reused for multiple requests. After completing all of your requests, call
73+
// the "close" method on the client to safely clean up any remaining background resources.
74+
try (JobServiceClient client = JobServiceClient.create(settings)) {
75+
76+
// Passing in an empty Value object for model parameters
77+
JsonObject jsonModelParameters = new JsonObject();
78+
Value.Builder modelParametersBuilder = Value.newBuilder();
79+
JsonFormat.parser().merge(jsonModelParameters.toString(), modelParametersBuilder);
80+
Value modelParameters = modelParametersBuilder.build();
81+
82+
GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build();
83+
BatchPredictionJob.InputConfig inputConfig =
84+
BatchPredictionJob.InputConfig.newBuilder()
85+
.setInstancesFormat(instancesFormat)
86+
.setGcsSource(gcsSource)
87+
.build();
88+
GcsDestination gcsDestination =
89+
GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build();
90+
BatchPredictionJob.OutputConfig outputConfig =
91+
BatchPredictionJob.OutputConfig.newBuilder()
92+
.setPredictionsFormat(predictionsFormat)
93+
.setGcsDestination(gcsDestination)
94+
.build();
95+
MachineSpec machineSpec =
96+
MachineSpec.newBuilder()
97+
.setMachineType("n1-standard-2")
98+
.setAcceleratorType(AcceleratorType.NVIDIA_TESLA_K80)
99+
.setAcceleratorCount(1)
100+
.build();
101+
BatchDedicatedResources dedicatedResources =
102+
BatchDedicatedResources.newBuilder()
103+
.setMachineSpec(machineSpec)
104+
.setStartingReplicaCount(1)
105+
.setMaxReplicaCount(1)
106+
.build();
107+
String modelName = ModelName.of(project, location, model).toString();
108+
BatchPredictionJob batchPredictionJob =
109+
BatchPredictionJob.newBuilder()
110+
.setDisplayName(displayName)
111+
.setModel(modelName)
112+
.setModelParameters(modelParameters)
113+
.setInputConfig(inputConfig)
114+
.setOutputConfig(outputConfig)
115+
.setDedicatedResources(dedicatedResources)
116+
.build();
117+
LocationName parent = LocationName.of(project, location);
118+
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob);
119+
System.out.format("response: %s\n", response);
120+
System.out.format("\tName: %s\n", response.getName());
121+
}
122+
}
123+
}
124+
125+
// [END aiplatform_create_batch_prediction_job_sample]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package aiplatform;
18+
19+
// [START aiplatform_create_batch_prediction_job_video_action_recognition_sample]
20+
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob;
21+
import com.google.cloud.aiplatform.v1beta1.GcsDestination;
22+
import com.google.cloud.aiplatform.v1beta1.GcsSource;
23+
import com.google.cloud.aiplatform.v1beta1.JobServiceClient;
24+
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings;
25+
import com.google.cloud.aiplatform.v1beta1.LocationName;
26+
import com.google.cloud.aiplatform.v1beta1.ModelName;
27+
import com.google.gson.JsonObject;
28+
import com.google.protobuf.Value;
29+
import com.google.protobuf.util.JsonFormat;
30+
import java.io.IOException;
31+
32+
public class CreateBatchPredictionJobVideoActionRecognitionSample {
33+
34+
public static void main(String[] args) throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String project = "PROJECT";
37+
String displayName = "DISPLAY_NAME";
38+
String model = "MODEL";
39+
String gcsSourceUri = "GCS_SOURCE_URI";
40+
String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX";
41+
createBatchPredictionJobVideoActionRecognitionSample(
42+
project, displayName, model, gcsSourceUri, gcsDestinationOutputUriPrefix);
43+
}
44+
45+
static void createBatchPredictionJobVideoActionRecognitionSample(
46+
String project,
47+
String displayName,
48+
String model,
49+
String gcsSourceUri,
50+
String gcsDestinationOutputUriPrefix)
51+
throws IOException {
52+
JobServiceSettings settings =
53+
JobServiceSettings.newBuilder()
54+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
55+
.build();
56+
String location = "us-central1";
57+
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests. After completing all of your requests, call
60+
// the "close" method on the client to safely clean up any remaining background resources.
61+
try (JobServiceClient client = JobServiceClient.create(settings)) {
62+
JsonObject jsonModelParameters = new JsonObject();
63+
jsonModelParameters.addProperty("confidenceThreshold", 0.5);
64+
Value.Builder modelParametersBuilder = Value.newBuilder();
65+
JsonFormat.parser().merge(jsonModelParameters.toString(), modelParametersBuilder);
66+
Value modelParameters = modelParametersBuilder.build();
67+
GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build();
68+
BatchPredictionJob.InputConfig inputConfig =
69+
BatchPredictionJob.InputConfig.newBuilder()
70+
.setInstancesFormat("jsonl")
71+
.setGcsSource(gcsSource)
72+
.build();
73+
GcsDestination gcsDestination =
74+
GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build();
75+
BatchPredictionJob.OutputConfig outputConfig =
76+
BatchPredictionJob.OutputConfig.newBuilder()
77+
.setPredictionsFormat("jsonl")
78+
.setGcsDestination(gcsDestination)
79+
.build();
80+
81+
String modelName = ModelName.of(project, location, model).toString();
82+
83+
BatchPredictionJob batchPredictionJob =
84+
BatchPredictionJob.newBuilder()
85+
.setDisplayName(displayName)
86+
.setModel(modelName)
87+
.setModelParameters(modelParameters)
88+
.setInputConfig(inputConfig)
89+
.setOutputConfig(outputConfig)
90+
.build();
91+
LocationName parent = LocationName.of(project, location);
92+
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob);
93+
System.out.format("response: %s\n", response);
94+
System.out.format("\tName: %s\n", response.getName());
95+
}
96+
}
97+
}
98+
99+
// [END aiplatform_create_batch_prediction_job_video_action_recognition_sample]

0 commit comments

Comments
 (0)