Skip to content

Commit

Permalink
feat(samples): adds samples for enhanced version of library (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
telpirion authored Jan 13, 2021
1 parent 9138e20 commit ea5e774
Show file tree
Hide file tree
Showing 40 changed files with 2,868 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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
*
* https://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.
*/

'use strict';

async function main(
batchPredictionDisplayName,
modelId,
gcsSourceUri,
gcsDestinationOutputUriPrefix,
project,
location = 'us-central1'
) {
// [START aiplatform_create_batch_prediction_job_video_classification]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/

// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
// const modelId = 'YOUR_MODEL_ID';
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
// eg. "gs://<your-gcs-bucket>/destination_path"
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
const aiplatform = require('@google-cloud/aiplatform');
const {
params,
} = aiplatform.protos.google.cloud.aiplatform.v1beta1.schema.predict;

// Imports the Google Cloud Job Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function createBatchPredictionJobVideoClassification() {
// Configure the parent resource
const parent = `projects/${project}/locations/${location}`;
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;

// For more information on how to configure the model parameters object, see
// https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions
const modelParamsObj = new params.VideoClassificationPredictionParams({
confidenceThreshold: 0.5,
maxPredictions: 1000,
segmentClassification: true,
shotClassification: true,
oneSecIntervalClassification: true,
});

const modelParameters = modelParamsObj.toValue();

const inputConfig = {
instancesFormat: 'jsonl',
gcsSource: {uris: [gcsSourceUri]},
};
const outputConfig = {
predictionsFormat: 'jsonl',
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
};
const batchPredictionJob = {
displayName: batchPredictionDisplayName,
model: modelName,
modelParameters,
inputConfig,
outputConfig,
};
const request = {
parent,
batchPredictionJob,
};

// Create batch prediction job request
const [response] = await jobServiceClient.createBatchPredictionJob(request);

console.log('Create batch prediction job video classification response');
console.log(`Name : ${response.name}`);
console.log('Raw response:');
console.log(JSON.stringify(response, null, 2));
}
createBatchPredictionJobVideoClassification();
// [END aiplatform_create_batch_prediction_job_video_classification]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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
*
* https://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.
*/

'use strict';

async function main(
batchPredictionDisplayName,
modelId,
gcsSourceUri,
gcsDestinationOutputUriPrefix,
project,
location = 'us-central1'
) {
// [START aiplatform_create_batch_prediction_job_video_object_tracking]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/

// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
// const modelId = 'YOUR_MODEL_ID';
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
// eg. "gs://<your-gcs-bucket>/destination_path"
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
const aiplatform = require('@google-cloud/aiplatform');
const {
params,
} = aiplatform.protos.google.cloud.aiplatform.v1beta1.schema.predict;

// Imports the Google Cloud Job Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function createBatchPredictionJobVideoObjectTracking() {
// Configure the parent resource
const parent = `projects/${project}/locations/${location}`;
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;

// For more information on how to configure the model parameters object, see
// https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions
const modelParamsObj = new params.VideoObjectTrackingPredictionParams({
confidenceThreshold: 0.5,
});

const modelParameters = modelParamsObj.toValue();

const inputConfig = {
instancesFormat: 'jsonl',
gcsSource: {uris: [gcsSourceUri]},
};
const outputConfig = {
predictionsFormat: 'jsonl',
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
};
const batchPredictionJob = {
displayName: batchPredictionDisplayName,
model: modelName,
modelParameters,
inputConfig,
outputConfig,
};
const request = {
parent,
batchPredictionJob,
};

// Create batch prediction job request
const [response] = await jobServiceClient.createBatchPredictionJob(request);

console.log('Create batch prediction job video object tracking response');
console.log(`Name : ${response.name}`);
console.log('Raw response:');
console.log(JSON.stringify(response, null, 2));
}
createBatchPredictionJobVideoObjectTracking();
// [END aiplatform_create_batch_prediction_job_video_object_tracking]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,9 @@ function main(
);

console.log('Create training pipeline image classification response');
console.log(`\tName : ${response.name}`);
console.log(`\tDisplay Name : ${response.displayName}`);
console.log(
`\tTraining task definition : ${response.trainingTaskDefinition}`
);
console.log(
`\tTraining task inputs : \
${JSON.stringify(response.trainingTaskInputs)}`
);
console.log(
`\tTraining task metadata : \
${JSON.stringify(response.trainingTaskMetadata)}`
);
console.log(`\tState ; ${response.state}`);
console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`);
console.log(`\tStart time : ${JSON.stringify(response.startTime)}`);
console.log(`\tEnd time : ${JSON.stringify(response.endTime)}`);
console.log(`\tUpdate time : ${JSON.stringify(response.updateTime)}`);
console.log(`\tLabels : ${JSON.stringify(response.labels)}`);

const error = response.error;
console.log('\tError');
if (error === null) {
console.log('\t\tCode : {}');
console.log('\t\tMessage : {}');
} else {
console.log(`\t\tCode : ${error.code}`);
console.log(`\t\tMessage : ${error.message}`);
}
console.log(`Name : ${response.name}`);
console.log('Raw response:');
console.log(JSON.stringify(response, null, 2));
}

createTrainingPipelineImageClassification();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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
*
* https://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.
*/

'use strict';

async function main(
datasetId,
modelDisplayName,
trainingPipelineDisplayName,
project,
location = 'us-central1'
) {
// [START aiplatform_create_training_pipeline_image_object_detection]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/

// const datasetId = 'YOUR_DATASET_ID';
// const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME';
// const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

const aiplatform = require('@google-cloud/aiplatform');
const {
definition,
} = aiplatform.protos.google.cloud.aiplatform.v1beta1.schema.trainingjob;
const ModelType = definition.AutoMlImageObjectDetectionInputs.ModelType;

// Imports the Google Cloud Pipeline Service Client library
const {PipelineServiceClient} = aiplatform;

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const pipelineServiceClient = new PipelineServiceClient(clientOptions);

async function createTrainingPipelineImageObjectDetection() {
// Configure the parent resource
const parent = `projects/${project}/locations/${location}`;

const trainingTaskInputsObj = new definition.AutoMlImageObjectDetectionInputs(
{
disableEarlyStopping: false,
modelType: ModelType.CLOUD_HIGH_ACCURACY_1,
budgetMilliNodeHours: 20000,
}
);

const trainingTaskInputs = trainingTaskInputsObj.toValue();
const modelToUpload = {displayName: modelDisplayName};
const inputDataConfig = {datasetId: datasetId};
const trainingPipeline = {
displayName: trainingPipelineDisplayName,
trainingTaskDefinition:
'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_object_detection_1.0.0.yaml',
trainingTaskInputs,
inputDataConfig,
modelToUpload,
};
const request = {
parent,
trainingPipeline,
};

// Create training pipeline request
const [response] = await pipelineServiceClient.createTrainingPipeline(
request
);

console.log('Create training pipeline image object detection response');
console.log(`Name : ${response.name}`);
console.log('Raw response:');
console.log(JSON.stringify(response, null, 2));
}
createTrainingPipelineImageObjectDetection();
// [END aiplatform_create_training_pipeline_image_object_detection]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
Loading

0 comments on commit ea5e774

Please sign in to comment.