diff --git a/automl/package.json b/automl/package.json index 5f3c0b548df..3f6d9d5086b 100644 --- a/automl/package.json +++ b/automl/package.json @@ -12,12 +12,13 @@ "repository": "googleapis/nodejs-automl", "private": true, "scripts": { - "test": "mocha --timeout 600000" + "test": "mocha --timeout 600000 --recursive" }, "dependencies": { "@google-cloud/automl": "^0.2.0", "mathjs": "^5.5.0", - "yargs": "^13.2.1" + "yargs": "^13.2.1", + "execa":"^1.0.0" }, "devDependencies": { "mocha": "^6.0.1", diff --git a/automl/test/automlVisionObjectDetectionDataset.v1beta1.test.js b/automl/test/automlVisionObjectDetectionDataset.v1beta1.test.js new file mode 100644 index 00000000000..2c484873471 --- /dev/null +++ b/automl/test/automlVisionObjectDetectionDataset.v1beta1.test.js @@ -0,0 +1,82 @@ +/** + * Copyright 2019, 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +/** Tests for AutoML Vision Object Detection "Dataset API" sample. */ +// TODO(developer): Before running the test cases, +// set the environment variables PROJECT_ID, REGION_NAME and +// change the value of datasetId +const projectId = 'nodejs-docs-samples'; +const computeRegion = 'us-central1'; +const outputPath = 'gs://nodejs-docs-samples/VISION_OBJECT_DETECTION/'; +const datasetName = 'test_vision_create_dataset'; +const filter = 'imageObjectDetectionDatasetMetadata:*'; +const datasetId = 'ICN3946265060617537378'; +const importDataCsv = 'gs://nodejs-docs-samples-vcm/flowerTraindata20lines.csv'; + +describe('Vision Object Detection DatasetAPI', () => { + it.skip(`should create, import and delete a dataset`, async () => { + // Create dataset + let output = await execSync( + `node vision/object-detection/create-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${datasetName}"` + ); + const parsedOut = output.split('\n'); + const outputDatasetId = parsedOut[1].split(':')[1].trim(); + assert.match(output, /Dataset display name:/); + + // Import data + output = await execSync( + `node vision/object-detection/import-data.v1beta1.js "${projectId}" "${computeRegion}" "${outputDatasetId}" "${importDataCsv}"` + ); + assert.match(output, /Processing import.../); + + // Delete dataset + output = await execSync( + `node vision/object-detection/delete-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${outputDatasetId}"` + ); + assert.match(output, /Dataset delete details:/); + }); + + it.skip(`should list datasets`, async () => { + // List datasets + const output = await execSync( + `node vision/object-detection/list-datasets.v1beta1.js "${projectId}" "${computeRegion}" "${filter}"` + ); + assert.match(output, /List of datasets:/); + }); + + it.skip(`should get preexisting dataset`, async () => { + // Get dataset + const output = await execSync( + `node vision/object-detection/get-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}"` + ); + assert.match(output, /Dataset display name:/); + }); + + it.skip(`should export dataset`, async () => { + // Export data + const outputUri = outputPath + datasetId; + const output = await execSync( + `node vision/object-detection/export-data.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}" "${outputUri}"` + ); + assert.match(output, /Processing export.../); + }); +}); diff --git a/automl/test/automlVisionObjectDetectionModel.v1beta1.test.js b/automl/test/automlVisionObjectDetectionModel.v1beta1.test.js new file mode 100644 index 00000000000..183e1a2dce0 --- /dev/null +++ b/automl/test/automlVisionObjectDetectionModel.v1beta1.test.js @@ -0,0 +1,127 @@ +/** + * Copyright 2019, 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +/** Tests for AutoML Vision Object Detection "Model API" sample. */ +// TODO(developer): Before running the test cases, +// set the environment variables PROJECT_ID, REGION_NAME and +// change the value of datasetId, deployModelId and undeployModelId +const projectId = 'nodejs-docs-samples'; +const computeRegion = 'us-central1'; +const filter = 'imageObjectDetectionModelMetadata:*'; +const datasetId = 'ICN3217071205693347964'; +const testModelName = 'birds2_201804101601_base'; +const deployModelId = 'IOD1728502647608049664'; +const undeployModelId = 'IOD3348109663601164288'; + +describe(' Vision Object Detection ModelAPI', () => { + it.skip(`should create a model`, async () => { + let output = await execSync( + `node vision/object-detection/create-model.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}" "${testModelName}"` + ); + const operationName = output + .split('\n')[0] + .split(':')[1] + .trim(); + assert.match(output, /Training started.../); + + output = await execSync( + `node vision/object-detection/get-operation-status.v1beta1.js "${operationName}"` + ); + assert.match(output, /Operation details:/); + }); + + it.skip(`should list models, get and delete a model. list, get and display model + evaluations from preexisting models`, async () => { + // List models + let output = await execSync( + `node vision/object-detection/list-models.v1beta1.js "${projectId}" "${computeRegion}" "${filter}"` + ); + const parsedOut = output.split('\n'); + const outputModelId = parsedOut[3].split(':')[1].trim(); + assert.match(output, /List of models:/); + + // Get Model + output = await execSync( + `node vision/object-detection/get-model.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"` + ); + assert.match(output, /Model name:/); + + // List model evaluation + output = await execSync( + `node vision/object-detection/list-model-evaluations.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"` + ); + const parsedModelEvaluation = output.split('\n'); + const modelEvaluationId = parsedModelEvaluation[3].split(':')[1].trim(); + assert.match(output, /Model evaluation Id:/); + + // Get model evaluation + output = await execSync( + `node vision/object-detection/get-model-evaluation.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"` + + ` "${modelEvaluationId}"` + ); + assert.match(output, /Model evaluation Id:/); + + // Display evaluation + output = await execSync( + `node vision/object-detection/display-evaluation.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}" ` + ); + assert.match(output, /Model Evaluation ID:/); + + // Delete Model + output = await execSync( + `node vision/object-detection/delete-model.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"` + ); + assert.match(output, /Model delete details:/); + }); + + it.skip(`should list and get operation status`, async () => { + // List operation status + let output = await execSync( + `node vision/object-detection/list-operations-status.v1beta1.js` + ); + const parsedOut = output.split('\n'); + const operationFullId = parsedOut[3].split(':')[1].trim(); + + // Get operation status + // Poll operation status, here confirming that operation is not complete yet + output = await execSync( + `node vision/object-detection/get-operation-status.v1beta1.js "${operationFullId}"` + ); + assert.match(output, /Operation details:/); + }); + + it.skip(`should deploy the model`, async () => { + // Deploy the model + const output = await execSync( + `node vision/object-detection/deploy-model.v1beta1.js "${projectId}" "${computeRegion}" ${deployModelId}` + ); + assert.match(output, /Name:/); + }); + + it.skip(`should undeploy the model`, async () => { + // Undeploy the model + const output = await execSync( + `node vision/object-detection/undeploy-model.v1beta1.js "${projectId}" "${computeRegion}" ${undeployModelId}` + ); + assert.match(output, /Name:/); + }); +}); diff --git a/automl/test/automlVisionObjectDetectionPredict.v1beta1.test.js b/automl/test/automlVisionObjectDetectionPredict.v1beta1.test.js new file mode 100644 index 00000000000..9953227bb1b --- /dev/null +++ b/automl/test/automlVisionObjectDetectionPredict.v1beta1.test.js @@ -0,0 +1,40 @@ +/** + * Copyright 2019, 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const execa = require('execa'); + +/** Tests for AutoML Vision Object Detection "Prediction API" sample. */ + +// TODO(developer): Before running the test cases, +// set the environment variables PROJECT_ID, REGION_NAME and change the value of modelId +const projectId = 'nodejs-docs-samples'; +const computeRegion = 'us-central1'; +const modelId = ''; +const filePath = './resource/songbird.jpg'; + +const exec = async cmd => (await execa.shell(cmd)).stdout; + +describe.skip('Vision Object Detection PredictionAPI', () => { + it(`should run prediction from preexisting model`, async () => { + // Run prediction on 'salad.jpg' in resource folder + const output = await exec( + `node vision/object-detection/predict.v1beta1.js "${projectId}" "${computeRegion}" "${modelId}" "${filePath}"` + ); + assert.match(output, /Prediction results:/); + }); +}); diff --git a/automl/vision/object-detection/create-dataset.v1beta1.js b/automl/vision/object-detection/create-dataset.v1beta1.js new file mode 100644 index 00000000000..1e15b9737da --- /dev/null +++ b/automl/vision/object-detection/create-dataset.v1beta1.js @@ -0,0 +1,71 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetName = 'YOUR_DATASET_NAME' +) { + // [START automl_vision_object_detection_create_dataset] + /** + * Demonstrates using the AutoML client to create a dataset. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetName = '[DATASET_NAME]' e.g., "myDataset"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + const util = require(`util`); + + async function createDataset() { + // A resource that represents Google Cloud Platform location. + const projectLocation = automlClient.locationPath(projectId, computeRegion); + + // Set dataset name and metadata. + const myDataset = { + displayName: datasetName, + imageObjectDetectionDatasetMetadata: {}, + }; + + // Create a dataset with the dataset metadata in the region. + const [response] = await automlClient.createDataset({ + parent: projectLocation, + dataset: myDataset, + }); + + //const dataset = response[0]; + // Display the dataset information. + console.log(`Dataset name: ${response.name}`); + console.log(`Dataset Id: ${response.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${response.displayName}`); + console.log(`Dataset example count: ${response.exampleCount}`); + console.log( + `Image object detection dataset metadata: ${util.inspect( + response.imageObjectDetectionDatasetMetadata, + false, + null + )}` + ); + } + createDataset(); + // [END automl_vision_object_detection_create_dataset] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/create-model.v1beta1,js.js b/automl/vision/object-detection/create-model.v1beta1,js.js new file mode 100644 index 00000000000..4b2198f0bd8 --- /dev/null +++ b/automl/vision/object-detection/create-model.v1beta1,js.js @@ -0,0 +1,61 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + modelName = 'MODEL_NAME' +) { + // [START automl_vision_object_detection_create_model] + /** + * Demonstrates using the AutoML client to create a model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "IOD34216801856389120"; + // const modelName = '[MODEL_NAME]' e.g., "myModel"; + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + async function createModel() { + // A resource that represents Google Cloud Platform location. + const projectLocation = automlClient.locationPath(projectId, computeRegion); + + // Set datasetId, model name and model metadata for the dataset. + const myModel = { + displayName: modelName, + datasetId: datasetId, + imageObjectDetectionModelMetadata: {}, + }; + + // Create a model with the model metadata in the region. + const [response] = await automlClient.createModel({ + parent: projectLocation, + model: myModel, + }); + + const initialApiResponse = response[1]; + console.log(`Training operation name: ${initialApiResponse.name}`); + console.log(`Training started...`); + } + createModel(); + // [END automl_vision_object_detection_create_model] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/delete-dataset.v1beta1.js b/automl/vision/object-detection/delete-dataset.v1beta1.js new file mode 100644 index 00000000000..cd65f170e9f --- /dev/null +++ b/automl/vision/object-detection/delete-dataset.v1beta1.js @@ -0,0 +1,60 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_vision_object_detection_delete_dsataset] + /** + * Demonstrates using the AutoML client to delete a dataset. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "IOD34216801856389120"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + + async function deleteDataset() { + // Get the full path of the dataset. + const datasetFullId = automlClient.datasetPath( + projectId, + computeRegion, + datasetId + ); + + // Delete a dataset. + const [operation] = await automlClient.deleteDataset({name: datasetFullId}); + + const [response] = await operation.promise(); + const operationDetails = response[2]; + + // Get the Dataset delete details. + console.log('Dataset delete details:'); + console.log(` Operation details:`); + console.log(` Name: ${operationDetails.name}`); + console.log(` Done: ${operationDetails.done}`); + } + deleteDataset(); + // [END automl_vision_object_detection_delete_dataset] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/delete-model.v1beta1.js b/automl/vision/object-detection/delete-model.v1beta1.js new file mode 100644 index 00000000000..73a7e2f3a79 --- /dev/null +++ b/automl/vision/object-detection/delete-model.v1beta1.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_vision_object_detection_delete_model] + /** + * Demonstrates using the AutoML client to delete a model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "IOD1187015161160925184"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + async function deleteModel() { + // Get the full path of the model. + const modelFullId = automlClient.modelPath( + projectId, + computeRegion, + modelId + ); + + // Delete a model. + const [operation] = await automlClient.deleteModel({name: modelFullId}); + + const [response] = await operation.promise(); + const operationDetails = response[2]; + + // Get the Model delete details. + console.log('Model delete details:'); + console.log(` Operation details:`); + console.log(` Name: ${operationDetails.name}`); + console.log(` Done: ${operationDetails.done}`); + } + deleteModel(); + // [END automl_vision_object_detection_delete_model] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/deploy-model.v1beta1.js b/automl/vision/object-detection/deploy-model.v1beta1.js new file mode 100644 index 00000000000..803e39f8335 --- /dev/null +++ b/automl/vision/object-detection/deploy-model.v1beta1.js @@ -0,0 +1,58 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_vision_object_detection_deploy_model] + + /** + * Demonstrates using the AutoML client to deploy model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TEN5200971474357190656"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + async function deployModel() { + // Get the full path of the model. + const modelFullId = automlClient.modelPath( + projectId, + computeRegion, + modelId + ); + + // Deploy a model with the deploy model request. + const [operation] = await automlClient.deployModel({name: modelFullId}); + + const [response] = await operation.promise(); + console.log(`Deployment Details:`); + console.log(` Name: ${response.name}`); + console.log(` Metadata:`); + console.log(` Type Url: ${response.metadata.typeUrl}`); + console.log(` Done: ${response.done}`); + } + deployModel(); + // [END automl_vision_object_detection_deploy_model] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/display-evaluation.v1beta1.js b/automl/vision/object-detection/display-evaluation.v1beta1.js new file mode 100644 index 00000000000..838aeb8df85 --- /dev/null +++ b/automl/vision/object-detection/display-evaluation.v1beta1.js @@ -0,0 +1,119 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_vision_object_detection_display_evaluation] + /** + * Demonstrates using the AutoML client to display model evaluation. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "IOD2122286140026257408"; + // const filter = '[FILTER_EXPRESSIONS]' + // e.g., "imageObjectDetectionModelMetadata:*"; + + const math = require(`mathjs`); + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + + // Get the full path of the model. + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + automlClient + .listModelEvaluations({parent: modelFullId, filter: filter}) + .then(respond => { + const response = respond[0]; + // Iterate through the results. + let modelEvaluationId = ``; + for (const element of response) { + // There is evaluation for each class in a model and for overall model. + // Get only the evaluation of overall model. + if (!element.annotationSpecId) { + modelEvaluationId = element.name.split(`/`).pop(-1); + } + } + console.log(`Model Evaluation ID: ${modelEvaluationId}`); + + // Resource name for the model evaluation. + const modelEvaluationFullId = automlClient.modelEvaluationPath( + projectId, + computeRegion, + modelId, + modelEvaluationId + ); + + // Get a model evaluation. + automlClient + .getModelEvaluation({name: modelEvaluationFullId}) + .then(responses => { + const modelEvaluation = responses[0]; + const classMetrics = + modelEvaluation.imageObjectDetectionEvaluationMetrics; + const boundingBoxMetricsEntries = + classMetrics.boundingBoxMetricsEntries; + + for (const boundingBoxMetricsEntry of boundingBoxMetricsEntries) { + const confidenceMetricsEntries = + boundingBoxMetricsEntry.confidenceMetricsEntries; + // Showing model score based on threshold of 0.5 + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + if (confidenceMetricsEntry.confidenceThreshold === 0.5) { + console.log( + `Precision and recall are based on a score ` + + `threshold of 0.5` + ); + console.log( + `Model precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `Model recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `Model f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} %` + ); + } + } + } + }) + .catch(err => { + console.error(err); + }); + }) + .catch(err => { + console.error(err); + }); + // [END automl_vision_object_detection_display_evaluation] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/export-data.v1beta1.js b/automl/vision/object-detection/export-data.v1beta1.js new file mode 100644 index 00000000000..e5d3b190b5d --- /dev/null +++ b/automl/vision/object-detection/export-data.v1beta1.js @@ -0,0 +1,74 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + gcsOutputUri = 'GCS_DIRECTORY' +) { + // [START automl_vision_object_detection_export_data] + /** + * Demonstrates using the AutoML client to export a dataset to a + * Google Cloud Storage bucket. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g.,"IOD34216801856389120"; + // const gcsOutputUri = '[GCS_OUTPUT_URI]' e.g., "gs:///", + // `Google Cloud Storage URI for the export directory`; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + + async function exportData() { + // Get the full path of the dataset. + const datasetFullId = automlClient.datasetPath( + projectId, + computeRegion, + datasetId + ); + + // Set the output URI + const outputConfig = { + gcsDestination: { + outputUriPrefix: gcsOutputUri, + }, + }; + + // Set the request + const request = { + name: datasetFullId, + outputConfig: outputConfig, + }; + + // Export the data to the output URI. + const [operation] = await automlClient.exportData(request); + + const [response] = await operation.promise(); + console.log('Data export details:'); + console.log(` Operation details:`); + console.log(` Name: ${response.name}`); + console.log(` Done: ${response.done}`); + } + exportData(); + // [END automl_vision_object_detection_export_data] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/get-dataset.v1beta1.js b/automl/vision/object-detection/get-dataset.v1beta1.js new file mode 100644 index 00000000000..99c9d587e21 --- /dev/null +++ b/automl/vision/object-detection/get-dataset.v1beta1.js @@ -0,0 +1,62 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_vision_object_detection_get_dataset] + /** + * Demonstrates using the AutoML client to get a dataset by ID. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g.,"IOD34216801856389120"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + const util = require(`util`); + async function getDataset() { + // Get the full path of the dataset. + const datasetFullId = automlClient.datasetPath( + projectId, + computeRegion, + datasetId + ); + + // Get a dataset. + const [response] = await automlClient.getDataset({name: datasetFullId}); + console.log(`Got dataset: ${response.name}`); + console.log(`Dataset Id: ${response.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${response.displayName}`); + console.log(`Dataset example count: ${response.exampleCount}`); + console.log( + `Image object detection dataset metadata: ${util.inspect( + response.imageObjectDetectionDatasetMetadata, + false, + null + )}` + ); + } + getDataset(); + // [END automl_vision_object_detection_get_dataset] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/get-model-evaluations.v1beta1.js b/automl/vision/object-detection/get-model-evaluations.v1beta1.js new file mode 100644 index 00000000000..11a45bbd69d --- /dev/null +++ b/automl/vision/object-detection/get-model-evaluations.v1beta1.js @@ -0,0 +1,136 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + modelEvaluationId = 'MODEL_EVALUATION_ID' +) { + // [START automl_vision_object_detection_get_model_evaluation] + /** + * Demonstrates using the AutoML client to get model evaluations. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "IOD2122286140026257408"; + // const modelEvaluationId = '[MODEL_EVALUATION_ID]' + // e.g., "3806191078210741236"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + + const math = require(`mathjs`); + + async function getModelEvaluations() { + // Get the full path of the model evaluation. + const modelEvaluationFullId = automlClient.modelEvaluationPath( + projectId, + computeRegion, + modelId, + modelEvaluationId + ); + + // Get complete detail of the model evaluation. + const [response] = await automlClient.getModelEvaluation({ + name: modelEvaluationFullId, + }); + + const detectMetrics = response.imageObjectDetectionEvaluationMetrics; + const boundingBoxMetricsEntries = detectMetrics.boundingBoxMetricsEntries; + + // Display the model evaluations information. + console.log(`\nModel evaluation name: ${response.name}`); + console.log( + `Model evaluation Id: ${response.name + .split(`/`) + .slice(-1) + .pop()}` + ); + console.log( + `Model evaluation annotation spec Id: ${response.annotationSpecId}` + ); + console.log(`Model evaluation display name: ${response.displayName}`); + console.log( + `Model evaluation example count: ${response.evaluatedExampleCount}` + ); + console.log(`Image object detection evaluation metrics:`); + console.log( + ` Evaluated bounding box count: ${ + detectMetrics.evaluatedBoundingBoxCount + }` + ); + console.log( + ` Bounding box mean average precision: ${math.round( + detectMetrics.boundingBoxMeanAveragePrecision, + 6 + )}` + ); + + for (const boundingBoxMetricsEntry of boundingBoxMetricsEntries) { + console.log(`\tBounding box metrics entries:`); + console.log( + ` Iou threshold: ${math.round( + boundingBoxMetricsEntry.iouThreshold, + 2 + )}` + ); + console.log( + ` Mean average precision: ${math.round( + boundingBoxMetricsEntry.meanAveragePrecision, + 6 + )}` + ); + console.log(` Confidence metrics entries:`); + const confidenceMetricsEntries = + boundingBoxMetricsEntry.confidenceMetricsEntries; + + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + console.log( + ` Model confidence threshold: ${math.round( + confidenceMetricsEntry.confidenceThreshold * 100, + 6 + )}` + ); + console.log( + ` Model recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `\t\t\tModel precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + ` Model f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} % \n` + ); + } + } + } + getModelEvaluations(); + // [END automl_vision_object_detection_get_model_evaluation] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/get-model.v1beta1.js b/automl/vision/object-detection/get-model.v1beta1.js new file mode 100644 index 00000000000..b92e7b8cac9 --- /dev/null +++ b/automl/vision/object-detection/get-model.v1beta1.js @@ -0,0 +1,104 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_vision_object_detection_get_model] + /** + * Demonstrates using the AutoML client to get model details. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "IOD1187015161160925184"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + + async function getModel() { + // Get the full path of the model. + const modelFullId = automlClient.modelPath( + projectId, + computeRegion, + modelId + ); + + // Get complete detail of the model. + const [response] = await automlClient.getModel({name: modelFullId}); + + // Display the model information. + console.log(`Model name: ${response.name}`); + console.log(`Model Id: ${response.name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${response.displayName}`); + console.log(`Dataset Id: ${response.datasetId}`); + + if (response.modelMetadata === `translationModelMetadata`) { + console.log(`Translation model metadata:`); + console.log( + `\tBase model: ${response.translationModelMetadata.baseModel}` + ); + console.log( + `\tSource language code: ${ + response.translationModelMetadata.sourceLanguageCode + }` + ); + console.log( + `\tTarget language code: ${ + response.translationModelMetadata.targetLanguageCode + }` + ); + } else if (response.modelMetadata === `textClassificationModelMetadata`) { + console.log( + `Text classification model metadata: , ${ + response.textClassificationModelMetadata + }` + ); + } else if (response.modelMetadata === `imageClassificationModelMetadata`) { + console.log(`Image classification model metadata:`); + console.log( + `\tBase model Id: ${ + response.imageClassificationModelMetadata.baseModelId + }` + ); + console.log( + `\tTrain budget: ${ + response.imageClassificationModelMetadata.trainBudget + }` + ); + console.log( + `\tTrain cost: ${response.imageClassificationModelMetadata.trainCost}` + ); + console.log( + `\tStop reason: ${response.imageClassificationModelMetadata.stopReason}` + ); + } else if (response.modelMetadata === `imageObjectDetectionModelMetadata`) { + console.log(`Image Object Detection Model metadata:`); + console.log( + `\tModel Type: ${response.imageObjectDetectionModelMetadata.modelType}` + ); + } + console.log(`Model deployment state: ${response.deploymentState}`); + } + getModel(); + // [END automl_vision_object_detection_get_model] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/get-operation-status.v1beta1.js b/automl/vision/object-detection/get-operation-status.v1beta1.js new file mode 100644 index 00000000000..571f0eb936b --- /dev/null +++ b/automl/vision/object-detection/get-operation-status.v1beta1.js @@ -0,0 +1,57 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main(operationFullId = 'OPERATION_FULL_ID') { + // [START automl_vision_object_detection_get_operation_status] + /** + * Demonstrates using the AutoML client to get operation status. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const operationFullId = '[OPERATION_FULL_ID]' + // eg., "projects//locations/us-central1/operations/", + // `Full name of an operation`; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + async function getOperationStatus() { + // Get the latest state of a long-running operation. + const [response] = await automlClient.operationsClient.getOperation({ + name: operationFullId, + }); + console.log(`Operation details:`); + console.log(`\tName: ${response.name}`); + console.log(`\tMetadata:`); + console.log(`\t\tType Url: ${response.metadata.typeUrl}`); + console.log(`\tDone: ${response.done}`); + + if (response.response) { + console.log(`\tResponse:`); + console.log(`\t\tType Url: ${response.response.typeUrl}`); + } + + if (response.error) { + console.log(`\tResponse:`); + console.log(`\t\tError code: ${response.error.code}`); + console.log(`\t\tError message: ${response.error.message}`); + } + } + getOperationStatus(); + // [END automl_vision_object_detection_get_operation_status] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/import-data.v1beta1.js b/automl/vision/object-detection/import-data.v1beta1.js new file mode 100644 index 00000000000..d607f01bbd6 --- /dev/null +++ b/automl/vision/object-detection/import-data.v1beta1.js @@ -0,0 +1,70 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + gcsPath = 'GCS_PATH' +) { + // [START automl_vision_object_detection_import_data] + /** + * Demonstrates using the AutoML client to import labeled items. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g.,"IOD34216801856389120"; + // const gcsPath = '[GCS_PATH]' e.g., "gs:///", + // `.csv paths in AutoML Vision Object Detection CSV format`; + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + async function importData() { + // Get the full path of the dataset. + const datasetFullId = automlClient.datasetPath( + projectId, + computeRegion, + datasetId + ); + + // Get the multiple Google Cloud Storage URIs. + const inputUris = gcsPath.split(`,`); + const inputConfig = { + gcsSource: { + inputUris: inputUris, + }, + }; + + // Import the data from the input URI. + const [operation] = await automlClient.importData({ + name: datasetFullId, + inputConfig: inputConfig, + }); + + const [response] = await operation.promise(); + // Get the data import details. + console.log('Data import details:'); + console.log(` Operation details:`); + console.log(` Name: ${response.name}`); + console.log(` Done: ${response.done}`); + } + importData(); + // [END automl_vision_object_detection_import_data] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/list-datasets.v1beta1.js b/automl/vision/object-detection/list-datasets.v1beta1.js new file mode 100644 index 00000000000..b9eecc2ae1c --- /dev/null +++ b/automl/vision/object-detection/list-datasets.v1beta1.js @@ -0,0 +1,65 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_vision_object_detection_list_datasets] + /** + * Demonstrates using the AutoML client to list all Datasets. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter_ = '[FILTER_EXPRESSIONS]' + // e.g., "imageObjectDetectionDatasetMetadata:*"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + const util = require(`util`); + + async function listDatasets() { + const projectLocation = automlClient.locationPath(projectId, computeRegion); + + // List all the datasets available in the region by applying filter. + const [response] = await automlClient.listDatasets({ + parent: projectLocation, + filter: filter, + }); + console.log('List of datasets:'); + for (const dataset of response) { + console.log(`\nDataset name: ${dataset.name}`); + console.log(`Dataset Id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log( + `Image object detection dataset metadata: ${util.inspect( + dataset.imageObjectDetectionDatasetMetadata, + false, + null + )}` + ); + } + } + listDatasets(); + // [END automl_vision_object_detection_list_datasets] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/list-model-evaluations.v1beta1.js b/automl/vision/object-detection/list-model-evaluations.v1beta1.js new file mode 100644 index 00000000000..16bdf7612b3 --- /dev/null +++ b/automl/vision/object-detection/list-model-evaluations.v1beta1.js @@ -0,0 +1,138 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_vision_object_detection_list_model_evaluations] + /** + * Demonstrates using the AutoML client to list model evaluations. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "IOD1187015161160925184"; + // const filter_ = '[FILTER_EXPRESSIONS]' + // e.g., "imageObjectDetectionModelMetadata:*"; + + const math = require(`mathjs`); + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + + async function listModelEvaluations() { + // Get the full path of the model. + const modelFullId = automlClient.modelPath( + projectId, + computeRegion, + modelId + ); + + // List all the model evaluations in the model by applying filter. + const [response] = await automlClient.listModelEvaluations({ + parent: modelFullId, + filter: filter, + }); + console.log(`List of model evaluations:`); + for (const element of response) { + const detectMetrics = element.imageObjectDetectionEvaluationMetrics; + const boundingBoxMetricsEntries = detectMetrics.boundingBoxMetricsEntries; + + // Display the model evaluations information. + console.log(`\nModel evaluation name: ${element.name}`); + console.log( + `Model evaluation Id: ${element.name + .split(`/`) + .slice(-1) + .pop()}` + ); + console.log( + `Model evaluation annotation spec Id: ${element.annotationSpecId}` + ); + console.log(`Model evaluation display name: ${element.displayName}`); + console.log( + `Model evaluation example count: ${element.evaluatedExampleCount}` + ); + console.log(`Image object detection evaluation metrics:`); + console.log( + `Evaluated bounding box count: ${ + detectMetrics.evaluatedBoundingBoxCount + }` + ); + console.log( + `Bounding box mean average precision: ${math.round( + detectMetrics.boundingBoxMeanAveragePrecision, + 6 + )}` + ); + + for (const boundingBoxMetricsEntry of boundingBoxMetricsEntries) { + console.log(`\tBounding box metrics entries:`); + console.log( + `Iou threshold: ${math.round( + boundingBoxMetricsEntry.iouThreshold, + 2 + )}` + ); + console.log( + `Mean average precision: ${math.round( + boundingBoxMetricsEntry.meanAveragePrecision, + 6 + )}` + ); + console.log(`Confidence metrics entries:`); + const confidenceMetricsEntries = + boundingBoxMetricsEntry.confidenceMetricsEntries; + + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + console.log( + `Model confidence threshold: ${math.round( + confidenceMetricsEntry.confidenceThreshold * 100, + 6 + )}` + ); + console.log( + `\t\t\tModel recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `Model precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `Model f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} % \n` + ); + } + } + } + } + listModelEvaluations(); + // [END automl_vision_object_detection_list_model_evaluations] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/list-models.v1beta1.js b/automl/vision/object-detection/list-models.v1beta1.js new file mode 100644 index 00000000000..8b1b26c71fd --- /dev/null +++ b/automl/vision/object-detection/list-models.v1beta1.js @@ -0,0 +1,99 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_vision_object_detection_list_models] + /** + * Demonstrates using the AutoML client to list all models. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter_ = '[FILTER_EXPRESSIONS]' + // e.g., "imageObjectDetectionModelMetadata:*"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + async function listModels() { + // A resource that represents Google Cloud Platform location. + const projectLocation = automlClient.locationPath(projectId, computeRegion); + + // List all the models available in the region by applying filter. + const [response] = await automlClient.listModels({ + parent: projectLocation, + filter: filter, + }); + console.log(`List of models:`); + for (const model of response) { + console.log(`\nModel name: ${model.name}`); + console.log(`Model Id: ${model.name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Dataset Id: ${model.datasetId}`); + + if (model.modelMetadata === `translationModelMetadata`) { + console.log(`Translation model metadata:`); + console.log(`Base model: ${model.translationModelMetadata.baseModel}`); + console.log( + `Source language code: ${ + model.translationModelMetadata.sourceLanguageCode + }` + ); + console.log( + `Target language code: ${ + model.translationModelMetadata.targetLanguageCode + }` + ); + } else if (model.modelMetadata === `textClassificationModelMetadata`) { + console.log( + `Text classification model metadata: , ${ + model.textClassificationModelMetadata + }` + ); + } else if (model.modelMetadata === `imageClassificationModelMetadata`) { + console.log(`Image classification model metadata:`); + console.log( + `Base model Id: ${model.imageClassificationModelMetadata.baseModelId}` + ); + console.log( + `Train budget: ${model.imageClassificationModelMetadata.trainBudget}` + ); + console.log( + `Train cost: ${model.imageClassificationModelMetadata.trainCost}` + ); + console.log( + `Stop reason: ${model.imageClassificationModelMetadata.stopReason}` + ); + } else if (model.modelMetadata === `imageObjectDetectionModelMetadata`) { + console.log(`Image Object Detection Model metadata:`); + console.log( + `Model Type: ${model.imageObjectDetectionModelMetadata.modelType}` + ); + } + + console.log(`Model deployment state: ${model.deploymentState}`); + } + } + listModels(); + // [END automl_vision_object_detection_list_models] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/list-operation-status.v1beta1.js b/automl/vision/object-detection/list-operation-status.v1beta1.js new file mode 100644 index 00000000000..a7f3719f06b --- /dev/null +++ b/automl/vision/object-detection/list-operation-status.v1beta1.js @@ -0,0 +1,56 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_vision_object_detection_list_operations_status] + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter = '[FILTER_EXPRESSIONS]'; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + async function listOperationStatus() { + // A resource that represents Google Cloud Platform location. + const projectLocation = automlClient.locationPath(projectId, computeRegion); + + // List all the operations available in the region by applying filter. + const [operations] = await automlClient.operationsClient.listOperations({ + name: projectLocation, + filter: filter, + }); + for (const element of operations) { + console.log(`\nOperation details:`); + console.log(`Name: ${element.name}`); + console.log(`Metadata:`); + console.log(`Type Url: ${element.metadata.typeUrl}`); + console.log(`Done: ${element.done}`); + } + } + listOperationStatus(); + // [END automl_vision_object_detection_list_operations_status] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/predict.v1beta1.js b/automl/vision/object-detection/predict.v1beta1.js new file mode 100644 index 00000000000..040a002bbf3 --- /dev/null +++ b/automl/vision/object-detection/predict.v1beta1.js @@ -0,0 +1,88 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'YOUR_MODEL_ID', + filePath = 'GCS_PATH', + scoreThreshold = 'SCORE_THRESHOLD' +) { + // [START automl_vision_object_detection_predict] + + /** + * Demonstrates using the AutoML client to detect the object in an image. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "IOD1187015161160925184"; + // const filePath = '[GCS_PATH]' e.g., "/home/ubuntu/salad.jpg", + // `local text file path of content to be extracted`; + // const scoreThreshold = '[SCORE_THRESHOLD]', e.g, 0.50 , + // `Set the score threshold for Prediction of the created model`; + + //Imports the Google Cloud Automl library + const {PredictionServiceClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(); + + const fs = require(`fs`); + + async function predict() { + // Get the full path of the model. + const modelFullId = predictionServiceClient.modelPath( + projectId, + computeRegion, + modelId + ); + + // Read the file content for prediction. + const content = fs.readFileSync(filePath, `base64`); + let params = {}; + if (scoreThreshold) { + params = { + score_threshold: scoreThreshold, + }; + } + + // Set the payload by giving the content and type of the file. + const payload = { + image: { + imageBytes: content, + }, + }; + + // params is additional domain-specific parameters. + // currently there is no additional parameters supported. + const [response] = await predictionServiceClient.predict({ + name: modelFullId, + payload: payload, + params: params, + }); + console.log(`Prediction results:`); + for (const result of response[0].payload) { + console.log(`\nPredicted class name: ${result.displayName}`); + console.log( + `Predicted class score: ${result.imageObjectDetection.score}` + ); + } + } + predict(); + // [END automl_vision_object_detection_predict] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/object-detection/undeploy-model.v1beta1.js b/automl/vision/object-detection/undeploy-model.v1beta1.js new file mode 100644 index 00000000000..e3eba9c92b7 --- /dev/null +++ b/automl/vision/object-detection/undeploy-model.v1beta1.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, 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. + */ + +`use strict`; +function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_vision_object_detection_undeploy_model] + /** + * Demonstrates using the AutoML client to undeploy model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TEN5200971474357190656"; + + //Imports the Google Cloud Automl library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const automlClient = new AutoMlClient(); + + async function undeployModel() { + // Get the full path of the model. + const modelFullId = automlClient.modelPath( + projectId, + computeRegion, + modelId + ); + + // Deploy a model with the deploy model request. + const [operation] = await automlClient.undeployModel({name: modelFullId}); + const [response] = await operation.promise(); + for (const element of response) { + console.log(`Undeployment Details:`); + console.log(`\tName: ${element.name}`); + console.log(`\tMetadata:`); + console.log(`\t\tType Url: ${element.metadata.typeUrl}`); + console.log(`\tDone: ${element.done}`); + } + } + undeployModel(); + // [END automl_vision_object_detection_undeploy_model] +} +main(...process.argv.slice(2)); diff --git a/automl/vision/resources/songbird.jpg b/automl/vision/resources/songbird.jpg new file mode 100644 index 00000000000..f10312d084e Binary files /dev/null and b/automl/vision/resources/songbird.jpg differ