Skip to content

Commit ea5e774

Browse files
authored
feat(samples): adds samples for enhanced version of library (#16)
1 parent 9138e20 commit ea5e774

40 files changed

Lines changed: 2868 additions & 39 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
* https://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+
'use strict';
18+
19+
async function main(
20+
batchPredictionDisplayName,
21+
modelId,
22+
gcsSourceUri,
23+
gcsDestinationOutputUriPrefix,
24+
project,
25+
location = 'us-central1'
26+
) {
27+
// [START aiplatform_create_batch_prediction_job_video_classification]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.\
30+
* (Not necessary if passing values as arguments)
31+
*/
32+
33+
// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
34+
// const modelId = 'YOUR_MODEL_ID';
35+
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
36+
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
37+
// eg. "gs://<your-gcs-bucket>/destination_path"
38+
// const project = 'YOUR_PROJECT_ID';
39+
// const location = 'YOUR_PROJECT_LOCATION';
40+
const aiplatform = require('@google-cloud/aiplatform');
41+
const {
42+
params,
43+
} = aiplatform.protos.google.cloud.aiplatform.v1beta1.schema.predict;
44+
45+
// Imports the Google Cloud Job Service Client library
46+
const {JobServiceClient} = require('@google-cloud/aiplatform');
47+
48+
// Specifies the location of the api endpoint
49+
const clientOptions = {
50+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
51+
};
52+
53+
// Instantiates a client
54+
const jobServiceClient = new JobServiceClient(clientOptions);
55+
56+
async function createBatchPredictionJobVideoClassification() {
57+
// Configure the parent resource
58+
const parent = `projects/${project}/locations/${location}`;
59+
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;
60+
61+
// For more information on how to configure the model parameters object, see
62+
// https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions
63+
const modelParamsObj = new params.VideoClassificationPredictionParams({
64+
confidenceThreshold: 0.5,
65+
maxPredictions: 1000,
66+
segmentClassification: true,
67+
shotClassification: true,
68+
oneSecIntervalClassification: true,
69+
});
70+
71+
const modelParameters = modelParamsObj.toValue();
72+
73+
const inputConfig = {
74+
instancesFormat: 'jsonl',
75+
gcsSource: {uris: [gcsSourceUri]},
76+
};
77+
const outputConfig = {
78+
predictionsFormat: 'jsonl',
79+
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
80+
};
81+
const batchPredictionJob = {
82+
displayName: batchPredictionDisplayName,
83+
model: modelName,
84+
modelParameters,
85+
inputConfig,
86+
outputConfig,
87+
};
88+
const request = {
89+
parent,
90+
batchPredictionJob,
91+
};
92+
93+
// Create batch prediction job request
94+
const [response] = await jobServiceClient.createBatchPredictionJob(request);
95+
96+
console.log('Create batch prediction job video classification response');
97+
console.log(`Name : ${response.name}`);
98+
console.log('Raw response:');
99+
console.log(JSON.stringify(response, null, 2));
100+
}
101+
createBatchPredictionJobVideoClassification();
102+
// [END aiplatform_create_batch_prediction_job_video_classification]
103+
}
104+
105+
process.on('unhandledRejection', err => {
106+
console.error(err.message);
107+
process.exitCode = 1;
108+
});
109+
110+
main(...process.argv.slice(2));
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
* https://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+
'use strict';
18+
19+
async function main(
20+
batchPredictionDisplayName,
21+
modelId,
22+
gcsSourceUri,
23+
gcsDestinationOutputUriPrefix,
24+
project,
25+
location = 'us-central1'
26+
) {
27+
// [START aiplatform_create_batch_prediction_job_video_object_tracking]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.\
30+
* (Not necessary if passing values as arguments)
31+
*/
32+
33+
// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
34+
// const modelId = 'YOUR_MODEL_ID';
35+
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
36+
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
37+
// eg. "gs://<your-gcs-bucket>/destination_path"
38+
// const project = 'YOUR_PROJECT_ID';
39+
// const location = 'YOUR_PROJECT_LOCATION';
40+
const aiplatform = require('@google-cloud/aiplatform');
41+
const {
42+
params,
43+
} = aiplatform.protos.google.cloud.aiplatform.v1beta1.schema.predict;
44+
45+
// Imports the Google Cloud Job Service Client library
46+
const {JobServiceClient} = require('@google-cloud/aiplatform');
47+
48+
// Specifies the location of the api endpoint
49+
const clientOptions = {
50+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
51+
};
52+
53+
// Instantiates a client
54+
const jobServiceClient = new JobServiceClient(clientOptions);
55+
56+
async function createBatchPredictionJobVideoObjectTracking() {
57+
// Configure the parent resource
58+
const parent = `projects/${project}/locations/${location}`;
59+
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;
60+
61+
// For more information on how to configure the model parameters object, see
62+
// https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions
63+
const modelParamsObj = new params.VideoObjectTrackingPredictionParams({
64+
confidenceThreshold: 0.5,
65+
});
66+
67+
const modelParameters = modelParamsObj.toValue();
68+
69+
const inputConfig = {
70+
instancesFormat: 'jsonl',
71+
gcsSource: {uris: [gcsSourceUri]},
72+
};
73+
const outputConfig = {
74+
predictionsFormat: 'jsonl',
75+
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
76+
};
77+
const batchPredictionJob = {
78+
displayName: batchPredictionDisplayName,
79+
model: modelName,
80+
modelParameters,
81+
inputConfig,
82+
outputConfig,
83+
};
84+
const request = {
85+
parent,
86+
batchPredictionJob,
87+
};
88+
89+
// Create batch prediction job request
90+
const [response] = await jobServiceClient.createBatchPredictionJob(request);
91+
92+
console.log('Create batch prediction job video object tracking response');
93+
console.log(`Name : ${response.name}`);
94+
console.log('Raw response:');
95+
console.log(JSON.stringify(response, null, 2));
96+
}
97+
createBatchPredictionJobVideoObjectTracking();
98+
// [END aiplatform_create_batch_prediction_job_video_object_tracking]
99+
}
100+
101+
process.on('unhandledRejection', err => {
102+
console.error(err.message);
103+
process.exitCode = 1;
104+
});
105+
106+
main(...process.argv.slice(2));

ai-platform/snippets/create-training-pipeline-image-classification.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,9 @@ function main(
9292
);
9393

9494
console.log('Create training pipeline image classification response');
95-
console.log(`\tName : ${response.name}`);
96-
console.log(`\tDisplay Name : ${response.displayName}`);
97-
console.log(
98-
`\tTraining task definition : ${response.trainingTaskDefinition}`
99-
);
100-
console.log(
101-
`\tTraining task inputs : \
102-
${JSON.stringify(response.trainingTaskInputs)}`
103-
);
104-
console.log(
105-
`\tTraining task metadata : \
106-
${JSON.stringify(response.trainingTaskMetadata)}`
107-
);
108-
console.log(`\tState ; ${response.state}`);
109-
console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`);
110-
console.log(`\tStart time : ${JSON.stringify(response.startTime)}`);
111-
console.log(`\tEnd time : ${JSON.stringify(response.endTime)}`);
112-
console.log(`\tUpdate time : ${JSON.stringify(response.updateTime)}`);
113-
console.log(`\tLabels : ${JSON.stringify(response.labels)}`);
114-
115-
const error = response.error;
116-
console.log('\tError');
117-
if (error === null) {
118-
console.log('\t\tCode : {}');
119-
console.log('\t\tMessage : {}');
120-
} else {
121-
console.log(`\t\tCode : ${error.code}`);
122-
console.log(`\t\tMessage : ${error.message}`);
123-
}
95+
console.log(`Name : ${response.name}`);
96+
console.log('Raw response:');
97+
console.log(JSON.stringify(response, null, 2));
12498
}
12599

126100
createTrainingPipelineImageClassification();
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
* https://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+
'use strict';
18+
19+
async function main(
20+
datasetId,
21+
modelDisplayName,
22+
trainingPipelineDisplayName,
23+
project,
24+
location = 'us-central1'
25+
) {
26+
// [START aiplatform_create_training_pipeline_image_object_detection]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.\
29+
* (Not necessary if passing values as arguments)
30+
*/
31+
32+
// const datasetId = 'YOUR_DATASET_ID';
33+
// const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME';
34+
// const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME';
35+
// const project = 'YOUR_PROJECT_ID';
36+
// const location = 'YOUR_PROJECT_LOCATION';
37+
38+
const aiplatform = require('@google-cloud/aiplatform');
39+
const {
40+
definition,
41+
} = aiplatform.protos.google.cloud.aiplatform.v1beta1.schema.trainingjob;
42+
const ModelType = definition.AutoMlImageObjectDetectionInputs.ModelType;
43+
44+
// Imports the Google Cloud Pipeline Service Client library
45+
const {PipelineServiceClient} = aiplatform;
46+
47+
// Specifies the location of the api endpoint
48+
const clientOptions = {
49+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
50+
};
51+
52+
// Instantiates a client
53+
const pipelineServiceClient = new PipelineServiceClient(clientOptions);
54+
55+
async function createTrainingPipelineImageObjectDetection() {
56+
// Configure the parent resource
57+
const parent = `projects/${project}/locations/${location}`;
58+
59+
const trainingTaskInputsObj = new definition.AutoMlImageObjectDetectionInputs(
60+
{
61+
disableEarlyStopping: false,
62+
modelType: ModelType.CLOUD_HIGH_ACCURACY_1,
63+
budgetMilliNodeHours: 20000,
64+
}
65+
);
66+
67+
const trainingTaskInputs = trainingTaskInputsObj.toValue();
68+
const modelToUpload = {displayName: modelDisplayName};
69+
const inputDataConfig = {datasetId: datasetId};
70+
const trainingPipeline = {
71+
displayName: trainingPipelineDisplayName,
72+
trainingTaskDefinition:
73+
'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_object_detection_1.0.0.yaml',
74+
trainingTaskInputs,
75+
inputDataConfig,
76+
modelToUpload,
77+
};
78+
const request = {
79+
parent,
80+
trainingPipeline,
81+
};
82+
83+
// Create training pipeline request
84+
const [response] = await pipelineServiceClient.createTrainingPipeline(
85+
request
86+
);
87+
88+
console.log('Create training pipeline image object detection response');
89+
console.log(`Name : ${response.name}`);
90+
console.log('Raw response:');
91+
console.log(JSON.stringify(response, null, 2));
92+
}
93+
createTrainingPipelineImageObjectDetection();
94+
// [END aiplatform_create_training_pipeline_image_object_detection]
95+
}
96+
97+
process.on('unhandledRejection', err => {
98+
console.error(err.message);
99+
process.exitCode = 1;
100+
});
101+
102+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)