diff --git a/automl/test/automlTablesDataset.v1beta1.test.js b/automl/test/automlTablesDataset.v1beta1.test.js index 04d53539038..ab3e8d30973 100644 --- a/automl/test/automlTablesDataset.v1beta1.test.js +++ b/automl/test/automlTablesDataset.v1beta1.test.js @@ -84,10 +84,7 @@ describe('Tables DatasetAPI', () => { `${cmdDataset} list-table-specs` + ` "${datasetId}" "${filter}"` ); let parsedOut = output.split('\n'); - const outputTableId = parsedOut[1] - .split(':')[1] - .trim() - .split('/')[7]; + const outputTableId = parsedOut[1].split(':')[1].trim().split('/')[7]; assert.match(output, /Table Id:/); // Get table @@ -101,10 +98,7 @@ describe('Tables DatasetAPI', () => { `${cmdDataset} list-column-specs` + ` "${datasetId}" "${outputTableId}"` ); parsedOut = output.split('\n'); - const outputColumnId = parsedOut[1] - .split(':')[1] - .trim() - .split('/')[9]; + const outputColumnId = parsedOut[1].split(':')[1].trim().split('/')[9]; assert.match(output, /Column Id:/); // Get column diff --git a/automl/test/automlTablesModel.v1beta1.test.js b/automl/test/automlTablesModel.v1beta1.test.js index 7e31370157d..494e65d572b 100644 --- a/automl/test/automlTablesModel.v1beta1.test.js +++ b/automl/test/automlTablesModel.v1beta1.test.js @@ -46,10 +46,7 @@ describe('Tables ModelAPI', () => { `${cmdModel} create-model "${datasetId}" "${tableId}" "${columnId}"` + ` "${testModelName}" "${trainBudget}"` ); - const operationName = output - .split('\n')[0] - .split(':')[1] - .trim(); + const operationName = output.split('\n')[0].split(':')[1].trim(); assert.match(output, /Training started.../); output = exec(`${cmdModel} get-operation-status "${operationName}"`); @@ -112,10 +109,7 @@ describe('Tables ModelAPI', () => { it.skip('should list and get operation status', async () => { // List operations status let output = exec(`${cmdModel} list-operations-status `); - const operationFullId = output - .split('\n')[3] - .split(':')[1] - .trim(); + const operationFullId = output.split('\n')[3].split(':')[1].trim(); assert.match(output, /Operation details:/); // Get operation status diff --git a/automl/test/automlTranslation.v1beta1.test.js b/automl/test/automlTranslation.v1beta1.test.js index 38118c965c3..89387549c55 100644 --- a/automl/test/automlTranslation.v1beta1.test.js +++ b/automl/test/automlTranslation.v1beta1.test.js @@ -38,10 +38,7 @@ describe.skip('automl sample tests', () => { // Create dataset output = execSync(`${cmdDataset} create-dataset -n "${testDataSetName}"`); - const dataSetId = output - .split('\n')[1] - .split(':')[1] - .trim(); + const dataSetId = output.split('\n')[1].split(':')[1].trim(); assert.match( output, new RegExp(`Dataset display name: ${testDataSetName}`) @@ -60,10 +57,7 @@ describe.skip('automl sample tests', () => { // Create dataset output = execSync(`${cmdDataset} create-dataset -n "${dummyDataSet}"`); - const dataSetId = output - .split('\n')[1] - .split(':')[1] - .trim(); + const dataSetId = output.split('\n')[1].split(':')[1].trim(); assert.match(output, new RegExp(`Dataset display name: ${dummyDataSet}`)); // Import Data @@ -80,10 +74,7 @@ describe.skip('automl sample tests', () => { output = execSync( `${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"` ); - const operationName = output - .split('\n')[0] - .split(':')[1] - .trim(); + const operationName = output.split('\n')[0].split(':')[1].trim(); assert.match(output, 'Training started...'); // Poll operation status, here confirming that operation is not complete yet diff --git a/automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js b/automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js index 1c57c41277c..85ac1c4bca4 100644 --- a/automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js +++ b/automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js @@ -39,10 +39,7 @@ describe.skip('Video Intelligence ModelAPI', () => { let output = exec( `${cmdModel} create-model "${datasetId}" "${testModelName}"` ); - const operationName = output - .split('\n')[0] - .split(':')[1] - .trim(); + const operationName = output.split('\n')[0].split(':')[1].trim(); assert.match(output, /Training started.../); output = exec(`${cmdModel} get-operation-status "${operationName}"`); @@ -86,10 +83,7 @@ describe.skip('Video Intelligence ModelAPI', () => { it('should list and get operation status', async () => { // List operation status let output = exec(`${cmdModel} list-operations-status`); - const operationFullId = output - .split('\n')[3] - .split(':')[1] - .trim(); + const operationFullId = output.split('\n')[3].split(':')[1].trim(); assert.match(output, /Operation details:/); // Get operation status diff --git a/automl/test/automlVision.test.js b/automl/test/automlVision.test.js index dfff4a60ab9..f5de798c4b1 100644 --- a/automl/test/automlVision.test.js +++ b/automl/test/automlVision.test.js @@ -39,10 +39,7 @@ describe('auto ml vision', () => { // Create dataset output = execSync(`${cmdDataset} create-dataset -n "${testDataSetName}"`); - const dataSetId = output - .split('\n')[1] - .split(':')[1] - .trim(); + const dataSetId = output.split('\n')[1].split(':')[1].trim(); assert.match(output, new RegExp(testDataSetName)); // Delete dataset @@ -59,10 +56,7 @@ describe('auto ml vision', () => { // Create dataset output = execSync(`${cmdDataset} create-dataset -n "${dummyDataSet}"`); - const dataSetId = output - .split('\n')[1] - .split(':')[1] - .trim(); + const dataSetId = output.split('\n')[1].split(':')[1].trim(); assert.match(output, new RegExp(dummyDataSet)); // Import Data diff --git a/automl/test/delete-dataset.beta.test.js b/automl/test/delete-dataset.beta.test.js index 5239fcc9726..d91070d2850 100644 --- a/automl/test/delete-dataset.beta.test.js +++ b/automl/test/delete-dataset.beta.test.js @@ -32,10 +32,7 @@ describe('Automl Translate Delete Dataset Tests', () => { before('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; const request = { parent: client.locationPath(projectId, LOCATION), dataset: { diff --git a/automl/test/delete_dataset.test.js b/automl/test/delete_dataset.test.js index c610c4f7456..5a035c36f0f 100644 --- a/automl/test/delete_dataset.test.js +++ b/automl/test/delete_dataset.test.js @@ -32,10 +32,7 @@ describe('Automl Translate Delete Dataset Tests', () => { before('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; const request = { parent: client.locationPath(projectId, LOCATION), dataset: { diff --git a/automl/test/export_dataset.test.js b/automl/test/export_dataset.test.js index 3eb686a95a3..64963844ab8 100644 --- a/automl/test/export_dataset.test.js +++ b/automl/test/export_dataset.test.js @@ -33,7 +33,7 @@ describe('Automl Translate Dataset Tests', () => { const client = new AutoMlClient(); const prefix = 'TEST_EXPORT_OUTPUT'; - it('should export a datset', async function() { + it('should export a datset', async function () { this.retries(4); await delay(this.test); const projectId = await client.getProjectId(); @@ -58,10 +58,7 @@ describe('Automl Translate Dataset Tests', () => { .getFiles(options); for (const file of files) { - await storageClient - .bucket(`gs://${bucketName}`) - .file(file.name) - .delete(); + await storageClient.bucket(`gs://${bucketName}`).file(file.name).delete(); } }); }); diff --git a/automl/test/import_dataset.test.js b/automl/test/import_dataset.test.js index fb67829b231..afbb0fbe68d 100644 --- a/automl/test/import_dataset.test.js +++ b/automl/test/import_dataset.test.js @@ -69,14 +69,11 @@ describe('Automl Import Dataset Test', () => { } } - it('should create a dataset', async function() { + it('should create a dataset', async function () { this.retries(5); await delay(this.test); const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; const request = { parent: client.locationPath(projectId, LOCATION), dataset: { @@ -94,7 +91,7 @@ describe('Automl Import Dataset Test', () => { [response.name.split('/').length - 1].split('\n')[0]; }); - it('should import dataset', async function() { + it('should import dataset', async function () { this.retries(5); await delay(this.test); const projectId = await client.getProjectId(); @@ -105,7 +102,7 @@ describe('Automl Import Dataset Test', () => { assert.match(import_output, /Dataset imported/); }); - it('should delete created dataset', async function() { + it('should delete created dataset', async function () { this.retries(5); await delay(this.test); const projectId = await client.getProjectId(); diff --git a/automl/test/language_entity_extraction_create_dataset.test.js b/automl/test/language_entity_extraction_create_dataset.test.js index 945e88aec20..ed5dcd8e9c7 100644 --- a/automl/test/language_entity_extraction_create_dataset.test.js +++ b/automl/test/language_entity_extraction_create_dataset.test.js @@ -32,10 +32,7 @@ describe('Automl Natural Language Entity Extraction Create Dataset Test', () => it('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; // create const create_output = execSync( diff --git a/automl/test/language_sentiment_analysis_create_dataset.test.js b/automl/test/language_sentiment_analysis_create_dataset.test.js index 053bb0dd1ab..d0e8ccd2629 100644 --- a/automl/test/language_sentiment_analysis_create_dataset.test.js +++ b/automl/test/language_sentiment_analysis_create_dataset.test.js @@ -32,10 +32,7 @@ describe('Automl Natural Language Sentiment Analysis Create Dataset Test', () => it('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; // create const create_output = execSync( diff --git a/automl/test/language_text_classification_create_dataset.test.js b/automl/test/language_text_classification_create_dataset.test.js index 0467a70eecb..f913d91bba3 100644 --- a/automl/test/language_text_classification_create_dataset.test.js +++ b/automl/test/language_text_classification_create_dataset.test.js @@ -32,10 +32,7 @@ describe('Automl Natural Language Text Classification Create Dataset Test', () = it('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; // create const create_output = execSync( diff --git a/automl/test/translate_create_dataset.test.js b/automl/test/translate_create_dataset.test.js index 908dc8a794e..0d6ae52abe1 100644 --- a/automl/test/translate_create_dataset.test.js +++ b/automl/test/translate_create_dataset.test.js @@ -32,10 +32,7 @@ describe('Automl Translate Create Dataset Test', () => { it('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; // create const create_output = execSync( diff --git a/automl/test/translate_create_model.test.js b/automl/test/translate_create_model.test.js index 87f46a0c86a..2523de22ba8 100644 --- a/automl/test/translate_create_model.test.js +++ b/automl/test/translate_create_model.test.js @@ -32,7 +32,7 @@ describe('Automl Translate Create Model Tests', () => { const client = new AutoMlClient(); let operationId; - it('should create a model', async function() { + it('should create a model', async function () { this.retries(5); await delay(this.test); diff --git a/automl/test/video-classification-create-dataset.beta.test.js b/automl/test/video-classification-create-dataset.beta.test.js index 4e9addeecee..683bbeba4d1 100644 --- a/automl/test/video-classification-create-dataset.beta.test.js +++ b/automl/test/video-classification-create-dataset.beta.test.js @@ -32,10 +32,7 @@ describe('Automl Video Classification Create Dataset Test', () => { it('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; // create const create_output = execSync( diff --git a/automl/test/video-object-tracking-create-dataset.beta.test.js b/automl/test/video-object-tracking-create-dataset.beta.test.js index 63e6189c2b6..64cf1f3a629 100644 --- a/automl/test/video-object-tracking-create-dataset.beta.test.js +++ b/automl/test/video-object-tracking-create-dataset.beta.test.js @@ -32,10 +32,7 @@ describe('Automl Video Object Tracking Create Dataset Test', () => { it('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; // create const create_output = execSync( diff --git a/automl/test/vision_classification_create_dataset.test.js b/automl/test/vision_classification_create_dataset.test.js index 81cb4e1d407..cda62f9805a 100644 --- a/automl/test/vision_classification_create_dataset.test.js +++ b/automl/test/vision_classification_create_dataset.test.js @@ -32,10 +32,7 @@ describe('Automl Vision Classification Create Dataset Test', () => { it('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; // create const create_output = execSync( diff --git a/automl/test/vision_object_detection_create_dataset.test.js b/automl/test/vision_object_detection_create_dataset.test.js index b85c44c049e..5516af3997b 100644 --- a/automl/test/vision_object_detection_create_dataset.test.js +++ b/automl/test/vision_object_detection_create_dataset.test.js @@ -32,10 +32,7 @@ describe('Automl Vision Object Detection Create Dataset Test', () => { it('should create a dataset', async () => { const projectId = await client.getProjectId(); - const displayName = `test_${uuid - .v4() - .replace(/-/g, '_') - .substring(0, 26)}`; + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; // create const create_output = await execSync( diff --git a/automl/test/vision_object_detection_predict.test.js b/automl/test/vision_object_detection_predict.test.js index 1d1cceb8321..3842a2a403d 100644 --- a/automl/test/vision_object_detection_predict.test.js +++ b/automl/test/vision_object_detection_predict.test.js @@ -31,7 +31,7 @@ const {delay} = require('./util'); describe('Automl Vision Object Detection Predict Test', () => { const client = new AutoMlClient(); - it('should verify the model is deployed', async function() { + it('should verify the model is deployed', async function () { this.retries(5); await delay(this.test); const projectId = await client.getProjectId(); diff --git a/automl/video-intelligence/classification/get-model-evaluations.v1beta1.js b/automl/video-intelligence/classification/get-model-evaluations.v1beta1.js index c469b17609d..943897fb285 100644 --- a/automl/video-intelligence/classification/get-model-evaluations.v1beta1.js +++ b/automl/video-intelligence/classification/get-model-evaluations.v1beta1.js @@ -54,10 +54,7 @@ async function main( // Display the model evaluations information. console.log(`\nModel evaluation name: ${response.name}`); console.log( - `Model evaluation Id: ${response.name - .split('/') - .slice(-1) - .pop()}` + `Model evaluation Id: ${response.name.split('/').slice(-1).pop()}` ); console.log( `Model evaluation annotation spec Id: ${response.annotationSpecId}` diff --git a/automl/video-intelligence/classification/list-model-evaluations.v1beta1.js b/automl/video-intelligence/classification/list-model-evaluations.v1beta1.js index 3dfab37ee62..1197a356c60 100644 --- a/automl/video-intelligence/classification/list-model-evaluations.v1beta1.js +++ b/automl/video-intelligence/classification/list-model-evaluations.v1beta1.js @@ -50,10 +50,7 @@ async function main( // Display the model evaluations information. console.log(`\nModel evaluation name: ${element[i].name}`); console.log( - `Model evaluation Id: ${element[i].name - .split('/') - .slice(-1) - .pop()}` + `Model evaluation Id: ${element[i].name.split('/').slice(-1).pop()}` ); console.log( `Model evaluation annotation spec Id: ${element[i].annotationSpecId}` diff --git a/automl/vision/automlVisionDataset.js b/automl/vision/automlVisionDataset.js index cc4592dbdf6..b75adf668aa 100644 --- a/automl/vision/automlVisionDataset.js +++ b/automl/vision/automlVisionDataset.js @@ -1,290 +1,290 @@ -// Copyright 2018 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. - -/** - * This application demonstrates how to perform basic operations on dataset - * with the Google AutoML Vision API. - * - * For more information, see the documentation at - * https://cloud.google.com/vision/automl/docs/ - */ - -'use strict'; - -function createDataset(projectId, computeRegion, datasetName, multiLabel) { - // [START automl_vision_create_dataset] - async function automlVisionCreateDataset() { - const automl = require('@google-cloud/automl').v1beta1; - - const client = new automl.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const datasetName = `name of the dataset to create, e.g. “myDataset”`; - // const multiLabel = `type of classification problem, true for multilabel and false for multiclass e.g. "false"`; - - // A resource that represents Google Cloud Platform location. - const projectLocation = client.locationPath(projectId, computeRegion); - - // Classification type is assigned based on multilabel value. - let classificationType = 'MULTICLASS'; - if (multiLabel) { - classificationType = 'MULTILABEL'; - } - - // Specify the text classification type for the dataset. - const datasetMetadata = { - classificationType: classificationType, - }; - - // Set dataset name and metadata. - const myDataset = { - displayName: datasetName, - imageClassificationDatasetMetadata: datasetMetadata, - }; - - // Create a dataset with the dataset metadata in the region. - const [dataset] = await client.createDataset({ - parent: projectLocation, - dataset: myDataset, - }); - // Display the dataset information. - console.log(`Dataset 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 Classification type:'); - console.log( - `\t ${dataset.imageClassificationDatasetMetadata.classificationType}` - ); - console.log('Dataset create time:'); - console.log(`\tseconds: ${dataset.createTime.seconds}`); - console.log(`\tnanos: ${dataset.createTime.nanos}`); - } - - automlVisionCreateDataset().catch(console.error); - // [END automl_vision_create_dataset] -} - -function listDatasets(projectId, computeRegion, filter) { - // [START automl_vision_list_datasets] - async function automlVisionListDatasets() { - const automl = require('@google-cloud/automl').v1beta1; - - const client = new automl.AutoMlClient(); - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const filter = `filter expressions, must specify field e.g. “imageClassificationModelMetadata:*”`; - - // A resource that represents Google Cloud Platform location. - const projectLocation = client.locationPath(projectId, computeRegion); - - // List all the datasets available in the region by applying filter. - const [datasets] = await client.listDatasets({ - parent: projectLocation, - filter: filter, - }); - console.log('List of datasets:'); - datasets.forEach(dataset => { - console.log(`Dataset 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 Classification type:'); - console.log( - '\t', - dataset.imageClassificationDatasetMetadata.classificationType - ); - console.log('Dataset create time: '); - console.log(`\tseconds: ${dataset.createTime.seconds}`); - console.log(`\tnanos: ${dataset.createTime.nanos}`); - console.log('\n'); - }); - } - - automlVisionListDatasets().catch(console.error); - // [END automl_vision_list_datasets] -} - -function importData(projectId, computeRegion, datasetId, path) { - // [START automl_vision_import_data] - async function automlVisionImportData() { - const automl = require('@google-cloud/automl').v1beta1; - - const client = new automl.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const datasetId = `Id of the dataset`; - // const path = `string or array of .csv paths in AutoML Vision CSV format, e.g. “gs://myproject/traindata.csv”;` - - // Get the full path of the dataset. - const datasetFullId = client.datasetPath( - projectId, - computeRegion, - datasetId - ); - - // Get one or more Google Cloud Storage URI(s). - const inputUris = path.split(','); - const inputConfig = { - gcsSource: { - inputUris: inputUris, - }, - }; - - // Import the dataset from the input URI. - const [operation] = await client.importData({ - name: datasetFullId, - inputConfig: inputConfig, - }); - console.log('Processing import...'); - - const [, , response] = await operation.promise(); - - // The final result of the operation. - if (response.done) { - console.log('Data imported.'); - } - } - - automlVisionImportData().catch(console.error); - // [END automl_vision_import_data] -} - -function deleteDataset(projectId, computeRegion, datasetId) { - // [START automl_vision_delete_dataset] - async function automlVisionDeleteDataset() { - const automl = require('@google-cloud/automl').v1beta1; - - const client = new automl.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const datasetId = `Id of the dataset`; - - // Get the full path of the dataset. - const datasetFullId = client.datasetPath( - projectId, - computeRegion, - datasetId - ); - - // Delete a dataset. - const [operation] = await client.deleteDataset({name: datasetFullId}); - const [, , response] = await operation.promise(); - // The final result of the operation. - if (response.done) { - console.log('Dataset deleted.'); - } - } - - automlVisionDeleteDataset().catch(console.error); - // [END automl_vision_delete_dataset] -} - +// Copyright 2018 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. + +/** + * This application demonstrates how to perform basic operations on dataset + * with the Google AutoML Vision API. + * + * For more information, see the documentation at + * https://cloud.google.com/vision/automl/docs/ + */ + +'use strict'; + +function createDataset(projectId, computeRegion, datasetName, multiLabel) { + // [START automl_vision_create_dataset] + async function automlVisionCreateDataset() { + const automl = require('@google-cloud/automl').v1beta1; + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetName = `name of the dataset to create, e.g. “myDataset”`; + // const multiLabel = `type of classification problem, true for multilabel and false for multiclass e.g. "false"`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Classification type is assigned based on multilabel value. + let classificationType = 'MULTICLASS'; + if (multiLabel) { + classificationType = 'MULTILABEL'; + } + + // Specify the text classification type for the dataset. + const datasetMetadata = { + classificationType: classificationType, + }; + + // Set dataset name and metadata. + const myDataset = { + displayName: datasetName, + imageClassificationDatasetMetadata: datasetMetadata, + }; + + // Create a dataset with the dataset metadata in the region. + const [dataset] = await client.createDataset({ + parent: projectLocation, + dataset: myDataset, + }); + // Display the dataset information. + console.log(`Dataset 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 Classification type:'); + console.log( + `\t ${dataset.imageClassificationDatasetMetadata.classificationType}` + ); + console.log('Dataset create time:'); + console.log(`\tseconds: ${dataset.createTime.seconds}`); + console.log(`\tnanos: ${dataset.createTime.nanos}`); + } + + automlVisionCreateDataset().catch(console.error); + // [END automl_vision_create_dataset] +} + +function listDatasets(projectId, computeRegion, filter) { + // [START automl_vision_list_datasets] + async function automlVisionListDatasets() { + const automl = require('@google-cloud/automl').v1beta1; + + const client = new automl.AutoMlClient(); + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const filter = `filter expressions, must specify field e.g. “imageClassificationModelMetadata:*”`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the datasets available in the region by applying filter. + const [datasets] = await client.listDatasets({ + parent: projectLocation, + filter: filter, + }); + console.log('List of datasets:'); + datasets.forEach(dataset => { + console.log(`Dataset 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 Classification type:'); + console.log( + '\t', + dataset.imageClassificationDatasetMetadata.classificationType + ); + console.log('Dataset create time: '); + console.log(`\tseconds: ${dataset.createTime.seconds}`); + console.log(`\tnanos: ${dataset.createTime.nanos}`); + console.log('\n'); + }); + } + + automlVisionListDatasets().catch(console.error); + // [END automl_vision_list_datasets] +} + +function importData(projectId, computeRegion, datasetId, path) { + // [START automl_vision_import_data] + async function automlVisionImportData() { + const automl = require('@google-cloud/automl').v1beta1; + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetId = `Id of the dataset`; + // const path = `string or array of .csv paths in AutoML Vision CSV format, e.g. “gs://myproject/traindata.csv”;` + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath( + projectId, + computeRegion, + datasetId + ); + + // Get one or more Google Cloud Storage URI(s). + const inputUris = path.split(','); + const inputConfig = { + gcsSource: { + inputUris: inputUris, + }, + }; + + // Import the dataset from the input URI. + const [operation] = await client.importData({ + name: datasetFullId, + inputConfig: inputConfig, + }); + console.log('Processing import...'); + + const [, , response] = await operation.promise(); + + // The final result of the operation. + if (response.done) { + console.log('Data imported.'); + } + } + + automlVisionImportData().catch(console.error); + // [END automl_vision_import_data] +} + +function deleteDataset(projectId, computeRegion, datasetId) { + // [START automl_vision_delete_dataset] + async function automlVisionDeleteDataset() { + const automl = require('@google-cloud/automl').v1beta1; + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetId = `Id of the dataset`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath( + projectId, + computeRegion, + datasetId + ); + + // Delete a dataset. + const [operation] = await client.deleteDataset({name: datasetFullId}); + const [, , response] = await operation.promise(); + // The final result of the operation. + if (response.done) { + console.log('Dataset deleted.'); + } + } + + automlVisionDeleteDataset().catch(console.error); + // [END automl_vision_delete_dataset] +} + require(`yargs`) // eslint-disable-line - .demand(1) - .options({ - computeRegion: { - alias: 'c', - type: 'string', - default: 'us-central1', - requiresArg: true, - description: 'region name e.g. "us-central1"', - }, - datasetName: { - alias: 'n', - type: 'string', - default: 'testDataSet', - requiresArg: true, - description: 'Name of the Dataset', - }, - datasetId: { - alias: 'i', - type: 'string', - requiresArg: true, - description: 'Id of the dataset', - }, - filter: { - alias: 'f', - default: 'image_classification_dataset_metadata:*', - type: 'string', - requiresArg: false, - description: 'Name of the Dataset to search for', - }, - multilabel: { - alias: 'm', - type: 'string', - default: false, - requiresArg: true, - description: - 'Type of the classification problem, ' + - 'False - MULTICLASS, True - MULTILABEL.', - }, - path: { - alias: 'p', - type: 'string', - global: true, - default: 'gs://nodejs-docs-samples-vcm/flowerTraindataMini.csv', - requiresArg: true, - description: 'URI or local path to input .csv, or array of .csv paths', - }, - projectId: { - alias: 'z', - type: 'string', - default: process.env.GCLOUD_PROJECT, - requiresArg: true, - description: 'The GCLOUD_PROJECT string, e.g. "my-gcloud-project"', - }, - }) - .command('create-dataset', 'creates a new Dataset', {}, opts => - createDataset( - opts.projectId, - opts.computeRegion, - opts.datasetName, - opts.multilabel - ) - ) - .command('list-datasets', 'list all Datasets', {}, opts => - listDatasets(opts.projectId, opts.computeRegion, opts.filter) - ) - .command('delete-dataset', 'Delete a dataset', {}, opts => - deleteDataset(opts.projectId, opts.computeRegion, opts.datasetId) - ) - .command('import-data', 'Import labeled items into dataset', {}, opts => - importData(opts.projectId, opts.computeRegion, opts.datasetId, opts.path) - ) - .example('node $0 create-dataset -n "newDataSet"') - .example('node $0 list-datasets -f "image_classification_dataset_metadata:*"') - .example('node $0 delete-dataset -i "DATASETID"') - .example( - 'node $0 import-data -i "dataSetId" -p "gs://myproject/mytraindata.csv"' - ) - .wrap(120) - .recommendCommands() - .help() - .strict().argv; + .demand(1) + .options({ + computeRegion: { + alias: 'c', + type: 'string', + default: 'us-central1', + requiresArg: true, + description: 'region name e.g. "us-central1"', + }, + datasetName: { + alias: 'n', + type: 'string', + default: 'testDataSet', + requiresArg: true, + description: 'Name of the Dataset', + }, + datasetId: { + alias: 'i', + type: 'string', + requiresArg: true, + description: 'Id of the dataset', + }, + filter: { + alias: 'f', + default: 'image_classification_dataset_metadata:*', + type: 'string', + requiresArg: false, + description: 'Name of the Dataset to search for', + }, + multilabel: { + alias: 'm', + type: 'string', + default: false, + requiresArg: true, + description: + 'Type of the classification problem, ' + + 'False - MULTICLASS, True - MULTILABEL.', + }, + path: { + alias: 'p', + type: 'string', + global: true, + default: 'gs://nodejs-docs-samples-vcm/flowerTraindataMini.csv', + requiresArg: true, + description: 'URI or local path to input .csv, or array of .csv paths', + }, + projectId: { + alias: 'z', + type: 'string', + default: process.env.GCLOUD_PROJECT, + requiresArg: true, + description: 'The GCLOUD_PROJECT string, e.g. "my-gcloud-project"', + }, + }) + .command('create-dataset', 'creates a new Dataset', {}, opts => + createDataset( + opts.projectId, + opts.computeRegion, + opts.datasetName, + opts.multilabel + ) + ) + .command('list-datasets', 'list all Datasets', {}, opts => + listDatasets(opts.projectId, opts.computeRegion, opts.filter) + ) + .command('delete-dataset', 'Delete a dataset', {}, opts => + deleteDataset(opts.projectId, opts.computeRegion, opts.datasetId) + ) + .command('import-data', 'Import labeled items into dataset', {}, opts => + importData(opts.projectId, opts.computeRegion, opts.datasetId, opts.path) + ) + .example('node $0 create-dataset -n "newDataSet"') + .example('node $0 list-datasets -f "image_classification_dataset_metadata:*"') + .example('node $0 delete-dataset -i "DATASETID"') + .example( + 'node $0 import-data -i "dataSetId" -p "gs://myproject/mytraindata.csv"' + ) + .wrap(120) + .recommendCommands() + .help() + .strict().argv; diff --git a/automl/vision/automlVisionModel.js b/automl/vision/automlVisionModel.js index 4e6e421bf62..65a8fdf9489 100644 --- a/automl/vision/automlVisionModel.js +++ b/automl/vision/automlVisionModel.js @@ -1,489 +1,489 @@ -// Copyright 2018 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. - -/** - * This application demonstrates how to perform basic operations on dataset - * with the Google AutoML Vision API. - * - * For more information, see the documentation at - * https://cloud.google.com/vision/automl/docs/ - */ - -'use strict'; - -function createModel( - projectId, - computeRegion, - datasetId, - modelName, - trainBudget -) { - // [START automl_vision_create_model] - async function automlVisionCreateModel() { - const automl = require('@google-cloud/automl').v1beta1; - - const client = new automl.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const datasetId = `Id of the dataset`; - // const modelName = `Name of the model, e.g. "myModel"`; - // const trainBudget = `Budget for training model, e.g. 50`; - - // A resource that represents Google Cloud Platform location. - const projectLocation = client.locationPath(projectId, computeRegion); - - // Check train budget condition. - if (trainBudget === 0) { - trainBudget = {}; - } else { - trainBudget = {trainBudget: trainBudget}; - } - - // Set model name and model metadata for the dataset. - const myModel = { - displayName: modelName, - datasetId: datasetId, - imageClassificationModelMetadata: trainBudget, - }; - - // Create a model with the model metadata in the region. - const [operation, initialApiResponse] = await client.createModel({ - parent: projectLocation, - model: myModel, - }); - console.log('Training operation name: ', initialApiResponse.name); - console.log('Training started...'); - const [model] = await operation.promise(); - - // Retrieve deployment state. - let deploymentState = ''; - if (model.deploymentState === 1) { - deploymentState = 'deployed'; - } else if (model.deploymentState === 2) { - deploymentState = 'undeployed'; - } - - // Display the model information. - console.log(`Model name: ${model.name}`); - console.log(`Model id: ${model.name.split('/').pop(-1)}`); - console.log(`Model display name: ${model.displayName}`); - console.log('Model create time:'); - console.log(`\tseconds: ${model.createTime.seconds}`); - console.log(`\tnanos: ${model.createTime.nanos}`); - console.log(`Model deployment state: ${deploymentState}`); - } - - automlVisionCreateModel().catch(console.error); - // [END automl_vision_create_model] -} - -function listModels(projectId, computeRegion, filter) { - // [START automl_vision_list_models] - async function automlVisinListModels() { - const automl = require('@google-cloud/automl'); - - const client = new automl.v1beta1.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const filter = `filter expressions, must specify field, e.g. “imageClassificationModelMetadata:*”`; - - // A resource that represents Google Cloud Platform location. - const projectLocation = client.locationPath(projectId, computeRegion); - - // List all the models available in the region by applying filter. - const [models] = await client.listModels({ - parent: projectLocation, - filter: filter, - }); - - // Display the model information. - console.log('List of models:'); - models.forEach(model => { - console.log(`Model name: ${model.name}`); - console.log(`Model id: ${model.name.split('/').pop(-1)}`); - console.log(`Model display name: ${model.displayName}`); - console.log(`Model dataset id: ${model.datasetId}`); - if (model.modelMetadata === 'translationModelMetadata') { - console.log('Translation model metadata:'); - console.log( - `\tBase model: ${model.translationModelMetadata.baseModel}` - ); - console.log( - `\tSource language code: ${model.translationModelMetadata.sourceLanguageCode}` - ); - console.log( - `\tTarget 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( - `\tBase model id: ${model.imageClassificationModelMetadata.baseModelId}` - ); - console.log( - `\tTrain budget: ${model.imageClassificationModelMetadata.trainBudget}` - ); - console.log( - `\tTrain cost: ${model.imageClassificationModelMetadata.trainCost}` - ); - console.log( - `\tStop reason: ${model.imageClassificationModelMetadata.stopReason}` - ); - } - console.log('Model create time:'); - console.log(`\tseconds: ${model.createTime.seconds}`); - console.log(`\tnanos: ${model.createTime.nanos}`); - console.log('Model update time:'); - console.log(`\tseconds: ${model.updateTime.seconds}`); - console.log(`\tnanos: ${model.updateTime.nanos}`); - console.log(`Model deployment state: ${model.deploymentState}`); - console.log('\n'); - }); - } - - automlVisinListModels().catch(console.error); - // [END automl_vision_list_models] -} - -function getModel(projectId, computeRegion, modelId) { - // [START automl_vision_get_model] - - async function automlVisionGetModel() { - const automl = require('@google-cloud/automl').v1beta1; - - const client = new automl.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const modelId = `id of the model, e.g. “ICN12345”`; - - // Get the full path of the model. - const modelFullId = client.modelPath(projectId, computeRegion, modelId); - - // Get complete detail of the model. - const [model] = await client.getModel({name: modelFullId}); - - // Display the model information. - console.log(`Model name: ${model.name}`); - console.log(`Model id: ${model.name.split('/').pop(-1)}`); - console.log(`Model display name: ${model.displayName}`); - console.log(`Model dataset id: ${model.datasetId}`); - if (model.modelMetadata === 'translationModelMetadata') { - console.log('Translation model metadata:'); - console.log(`\tBase model: ${model.translationModelMetadata.baseModel}`); - console.log( - `\tSource language code: ${model.translationModelMetadata.sourceLanguageCode}` - ); - console.log( - `\tTarget 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( - `\tBase model id: ${model.imageClassificationModelMetadata.baseModelId}` - ); - console.log( - `\tTrain budget: ${model.imageClassificationModelMetadata.trainBudget}` - ); - console.log( - `\tTrain cost: ${model.imageClassificationModelMetadata.trainCost}` - ); - console.log( - `\tStop reason: ${model.imageClassificationModelMetadata.stopReason}` - ); - } - console.log('Model create time:'); - console.log(`\tseconds: ${model.createTime.seconds}`); - console.log(`\tnanos: ${model.createTime.nanos}`); - console.log('Model update time:'); - console.log(`\tseconds: ${model.updateTime.seconds}`); - console.log(`\tnanos: ${model.updateTime.nanos}`); - console.log(`Model deployment state: ${model.deploymentState}`); - } - - automlVisionGetModel().catch(console.error); - // [END automl_vision_get_model] -} - -function listModelEvaluations(projectId, computeRegion, modelId, filter) { - // [START automl_vision_list_model_evaluations] - async function automlVisionListModelEvalution() { - const automl = require('@google-cloud/automl').v1beta1; - const util = require('util'); - - const client = new automl.AutoMlClient(); - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const modelId = `id of the model, e.g. “ICN12345”`; - // const filter = `filter expressions, must specify field, e.g. “imageClassificationModelMetadata:*”`; - - // Get the full path of the model. - const modelFullId = client.modelPath(projectId, computeRegion, modelId); - - // List all the model evaluations in the model by applying filter. - const [elements] = await client.listModelEvaluations({ - parent: modelFullId, - filter: filter, - }); - elements.forEach(element => { - console.log(util.inspect(element, false, null)); - }); - } - - automlVisionListModelEvalution().catch(console.error); - // [END automl_vision_list_model_evaluations] -} - -function displayEvaluation(projectId, computeRegion, modelId, filter) { - // [START automl_vision_display_evaluation] - async function automlVisionDisplayEvalution() { - const automl = require('@google-cloud/automl').v1beta1; - const math = require('mathjs'); - - const client = new automl.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const modelId = `id of the model, e.g. “ICN12345”`; - // const filter = `filter expressions, must specify field, e.g. “imageClassificationModelMetadata:*”`; - - // Get the full path of the model. - const modelFullId = client.modelPath(projectId, computeRegion, modelId); - - // List all the model evaluations in the model by applying filter. - const [response] = await client.listModelEvaluations({ - parent: modelFullId, - filter: filter, - }); - response.forEach(async element => { - // There is evaluation for each class in a model and for overall model. - // Get only the evaluation of overall model. - if (!element.annotationSpecId) { - const modelEvaluationId = element.name.split('/').pop(-1); - - // Resource name for the model evaluation. - const modelEvaluationFullId = client.modelEvaluationPath( - projectId, - computeRegion, - modelId, - modelEvaluationId - ); - - let modelEvaluation = null; - - (async () => { - [modelEvaluation] = await client.getModelEvaluation({ - name: modelEvaluationFullId, - }); - })(); - const classMetrics = modelEvaluation.classificationEvaluationMetrics; - const confidenceMetricsEntries = classMetrics.confidenceMetricsEntry; - - // Showing model score based on threshold of 0.5 - confidenceMetricsEntries.forEach(confidenceMetricsEntry => { - 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) - ); - console.log( - 'Model Precision@1: %', - math.round(confidenceMetricsEntry.precisionAt1 * 100, 2) - ); - console.log( - 'Model Recall@1: %', - math.round(confidenceMetricsEntry.recallAt1 * 100, 2) - ); - console.log( - 'Model F1 score@1: %', - math.round(confidenceMetricsEntry.f1ScoreAt1 * 100, 2) - ); - } - }); - } - }); - } - - automlVisionDisplayEvalution().catch(console.error); - // [END automl_vision_display_evaluation] -} - -function deleteModel(projectId, computeRegion, modelId) { - // [START automl_vision_delete_model] - async function automlVisionDeleteModel() { - const automl = require('@google-cloud/automl').v1beta1; - - const client = new automl.AutoMlClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const modelId = `id of the model, e.g. “ICN12345”`; - - // Get the full path of the model. - const modelFullId = client.modelPath(projectId, computeRegion, modelId); - - // Delete a model. - const [operation] = await client.deleteModel({name: modelFullId}); - const [, , response] = await operation.promise(); - // The final result of the operation. - if (response.done) { - console.log('Model deleted.'); - } - } - - automlVisionDeleteModel().catch(console.error); - // [END automl_vision_delete_model] -} - +// Copyright 2018 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. + +/** + * This application demonstrates how to perform basic operations on dataset + * with the Google AutoML Vision API. + * + * For more information, see the documentation at + * https://cloud.google.com/vision/automl/docs/ + */ + +'use strict'; + +function createModel( + projectId, + computeRegion, + datasetId, + modelName, + trainBudget +) { + // [START automl_vision_create_model] + async function automlVisionCreateModel() { + const automl = require('@google-cloud/automl').v1beta1; + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetId = `Id of the dataset`; + // const modelName = `Name of the model, e.g. "myModel"`; + // const trainBudget = `Budget for training model, e.g. 50`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Check train budget condition. + if (trainBudget === 0) { + trainBudget = {}; + } else { + trainBudget = {trainBudget: trainBudget}; + } + + // Set model name and model metadata for the dataset. + const myModel = { + displayName: modelName, + datasetId: datasetId, + imageClassificationModelMetadata: trainBudget, + }; + + // Create a model with the model metadata in the region. + const [operation, initialApiResponse] = await client.createModel({ + parent: projectLocation, + model: myModel, + }); + console.log('Training operation name: ', initialApiResponse.name); + console.log('Training started...'); + const [model] = await operation.promise(); + + // Retrieve deployment state. + let deploymentState = ''; + if (model.deploymentState === 1) { + deploymentState = 'deployed'; + } else if (model.deploymentState === 2) { + deploymentState = 'undeployed'; + } + + // Display the model information. + console.log(`Model name: ${model.name}`); + console.log(`Model id: ${model.name.split('/').pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log('Model create time:'); + console.log(`\tseconds: ${model.createTime.seconds}`); + console.log(`\tnanos: ${model.createTime.nanos}`); + console.log(`Model deployment state: ${deploymentState}`); + } + + automlVisionCreateModel().catch(console.error); + // [END automl_vision_create_model] +} + +function listModels(projectId, computeRegion, filter) { + // [START automl_vision_list_models] + async function automlVisinListModels() { + const automl = require('@google-cloud/automl'); + + const client = new automl.v1beta1.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const filter = `filter expressions, must specify field, e.g. “imageClassificationModelMetadata:*”`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the models available in the region by applying filter. + const [models] = await client.listModels({ + parent: projectLocation, + filter: filter, + }); + + // Display the model information. + console.log('List of models:'); + models.forEach(model => { + console.log(`Model name: ${model.name}`); + console.log(`Model id: ${model.name.split('/').pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Model dataset id: ${model.datasetId}`); + if (model.modelMetadata === 'translationModelMetadata') { + console.log('Translation model metadata:'); + console.log( + `\tBase model: ${model.translationModelMetadata.baseModel}` + ); + console.log( + `\tSource language code: ${model.translationModelMetadata.sourceLanguageCode}` + ); + console.log( + `\tTarget 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( + `\tBase model id: ${model.imageClassificationModelMetadata.baseModelId}` + ); + console.log( + `\tTrain budget: ${model.imageClassificationModelMetadata.trainBudget}` + ); + console.log( + `\tTrain cost: ${model.imageClassificationModelMetadata.trainCost}` + ); + console.log( + `\tStop reason: ${model.imageClassificationModelMetadata.stopReason}` + ); + } + console.log('Model create time:'); + console.log(`\tseconds: ${model.createTime.seconds}`); + console.log(`\tnanos: ${model.createTime.nanos}`); + console.log('Model update time:'); + console.log(`\tseconds: ${model.updateTime.seconds}`); + console.log(`\tnanos: ${model.updateTime.nanos}`); + console.log(`Model deployment state: ${model.deploymentState}`); + console.log('\n'); + }); + } + + automlVisinListModels().catch(console.error); + // [END automl_vision_list_models] +} + +function getModel(projectId, computeRegion, modelId) { + // [START automl_vision_get_model] + + async function automlVisionGetModel() { + const automl = require('@google-cloud/automl').v1beta1; + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get complete detail of the model. + const [model] = await client.getModel({name: modelFullId}); + + // Display the model information. + console.log(`Model name: ${model.name}`); + console.log(`Model id: ${model.name.split('/').pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Model dataset id: ${model.datasetId}`); + if (model.modelMetadata === 'translationModelMetadata') { + console.log('Translation model metadata:'); + console.log(`\tBase model: ${model.translationModelMetadata.baseModel}`); + console.log( + `\tSource language code: ${model.translationModelMetadata.sourceLanguageCode}` + ); + console.log( + `\tTarget 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( + `\tBase model id: ${model.imageClassificationModelMetadata.baseModelId}` + ); + console.log( + `\tTrain budget: ${model.imageClassificationModelMetadata.trainBudget}` + ); + console.log( + `\tTrain cost: ${model.imageClassificationModelMetadata.trainCost}` + ); + console.log( + `\tStop reason: ${model.imageClassificationModelMetadata.stopReason}` + ); + } + console.log('Model create time:'); + console.log(`\tseconds: ${model.createTime.seconds}`); + console.log(`\tnanos: ${model.createTime.nanos}`); + console.log('Model update time:'); + console.log(`\tseconds: ${model.updateTime.seconds}`); + console.log(`\tnanos: ${model.updateTime.nanos}`); + console.log(`Model deployment state: ${model.deploymentState}`); + } + + automlVisionGetModel().catch(console.error); + // [END automl_vision_get_model] +} + +function listModelEvaluations(projectId, computeRegion, modelId, filter) { + // [START automl_vision_list_model_evaluations] + async function automlVisionListModelEvalution() { + const automl = require('@google-cloud/automl').v1beta1; + const util = require('util'); + + const client = new automl.AutoMlClient(); + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + // const filter = `filter expressions, must specify field, e.g. “imageClassificationModelMetadata:*”`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + const [elements] = await client.listModelEvaluations({ + parent: modelFullId, + filter: filter, + }); + elements.forEach(element => { + console.log(util.inspect(element, false, null)); + }); + } + + automlVisionListModelEvalution().catch(console.error); + // [END automl_vision_list_model_evaluations] +} + +function displayEvaluation(projectId, computeRegion, modelId, filter) { + // [START automl_vision_display_evaluation] + async function automlVisionDisplayEvalution() { + const automl = require('@google-cloud/automl').v1beta1; + const math = require('mathjs'); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + // const filter = `filter expressions, must specify field, e.g. “imageClassificationModelMetadata:*”`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + const [response] = await client.listModelEvaluations({ + parent: modelFullId, + filter: filter, + }); + response.forEach(async element => { + // There is evaluation for each class in a model and for overall model. + // Get only the evaluation of overall model. + if (!element.annotationSpecId) { + const modelEvaluationId = element.name.split('/').pop(-1); + + // Resource name for the model evaluation. + const modelEvaluationFullId = client.modelEvaluationPath( + projectId, + computeRegion, + modelId, + modelEvaluationId + ); + + let modelEvaluation = null; + + (async () => { + [modelEvaluation] = await client.getModelEvaluation({ + name: modelEvaluationFullId, + }); + })(); + const classMetrics = modelEvaluation.classificationEvaluationMetrics; + const confidenceMetricsEntries = classMetrics.confidenceMetricsEntry; + + // Showing model score based on threshold of 0.5 + confidenceMetricsEntries.forEach(confidenceMetricsEntry => { + 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) + ); + console.log( + 'Model Precision@1: %', + math.round(confidenceMetricsEntry.precisionAt1 * 100, 2) + ); + console.log( + 'Model Recall@1: %', + math.round(confidenceMetricsEntry.recallAt1 * 100, 2) + ); + console.log( + 'Model F1 score@1: %', + math.round(confidenceMetricsEntry.f1ScoreAt1 * 100, 2) + ); + } + }); + } + }); + } + + automlVisionDisplayEvalution().catch(console.error); + // [END automl_vision_display_evaluation] +} + +function deleteModel(projectId, computeRegion, modelId) { + // [START automl_vision_delete_model] + async function automlVisionDeleteModel() { + const automl = require('@google-cloud/automl').v1beta1; + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Delete a model. + const [operation] = await client.deleteModel({name: modelFullId}); + const [, , response] = await operation.promise(); + // The final result of the operation. + if (response.done) { + console.log('Model deleted.'); + } + } + + automlVisionDeleteModel().catch(console.error); + // [END automl_vision_delete_model] +} + require(`yargs`) // eslint-disable-line - .demand(1) - .options({ - computeRegion: { - alias: 'c', - type: 'string', - default: 'us-central1', - requiresArg: true, - description: 'region name e.g. "us-central1"', - }, - datasetId: { - alias: 'i', - type: 'string', - requiresArg: true, - description: 'Id of the dataset', - }, - filter: { - alias: 'f', - default: '', - type: 'string', - requiresArg: true, - description: 'Name of the Dataset to search for', - }, - modelName: { - alias: 'm', - type: 'string', - default: false, - requiresArg: true, - description: 'Name of the model', - }, - modelId: { - alias: 'a', - type: 'string', - default: '', - requiresArg: true, - description: 'Id of the model', - }, - modelEvaluationId: { - alias: 'e', - type: 'string', - default: '', - requiresArg: true, - description: 'Id of the model evaluation', - }, - projectId: { - alias: 'z', - type: 'string', - default: process.env.GCLOUD_PROJECT, - requiresArg: true, - description: 'The GCLOUD_PROJECT string, e.g. "my-gcloud-project"', - }, - trainBudget: { - alias: 't', - type: 'string', - default: '', - requiresArg: true, - description: 'Budget for training the model', - }, - }) - .command('create-model', 'creates a new Model', {}, opts => - createModel( - opts.projectId, - opts.computeRegion, - opts.datasetId, - opts.modelName, - opts.trainBudget - ) - ) - .command('list-models', 'list all Models', {}, opts => - listModels(opts.projectId, opts.computeRegion, opts.filter) - ) - .command('get-model', 'Get a Model', {}, opts => - getModel(opts.projectId, opts.computeRegion, opts.modelId) - ) - .command('list-model-evaluations', 'List model evaluations', {}, opts => - listModelEvaluations( - opts.projectId, - opts.computeRegion, - opts.modelId, - opts.filter - ) - ) - .command('display-evaluation', 'Display evaluation', {}, opts => - displayEvaluation( - opts.projectId, - opts.computeRegion, - opts.modelId, - opts.filter - ) - ) - .command('delete-model', 'Delete a Model', {}, opts => - deleteModel(opts.projectId, opts.computeRegion, opts.modelId) - ) - .example('node $0 create-model -i "datasetId" -m "myModelName" -t "2"') - .example('node $0 list-models -f "image_classification_dataset_metadata:*"') - .example('node $0 get-model -a "ModelID"') - .example('node $0 list-model-evaluations -a "ModelID"') - .example('node $0 display-evaluation -a "ModelId"') - .example('node $0 delete-model -a "ModelID"') - .wrap(120) - .recommendCommands() - .help() - .strict().argv; + .demand(1) + .options({ + computeRegion: { + alias: 'c', + type: 'string', + default: 'us-central1', + requiresArg: true, + description: 'region name e.g. "us-central1"', + }, + datasetId: { + alias: 'i', + type: 'string', + requiresArg: true, + description: 'Id of the dataset', + }, + filter: { + alias: 'f', + default: '', + type: 'string', + requiresArg: true, + description: 'Name of the Dataset to search for', + }, + modelName: { + alias: 'm', + type: 'string', + default: false, + requiresArg: true, + description: 'Name of the model', + }, + modelId: { + alias: 'a', + type: 'string', + default: '', + requiresArg: true, + description: 'Id of the model', + }, + modelEvaluationId: { + alias: 'e', + type: 'string', + default: '', + requiresArg: true, + description: 'Id of the model evaluation', + }, + projectId: { + alias: 'z', + type: 'string', + default: process.env.GCLOUD_PROJECT, + requiresArg: true, + description: 'The GCLOUD_PROJECT string, e.g. "my-gcloud-project"', + }, + trainBudget: { + alias: 't', + type: 'string', + default: '', + requiresArg: true, + description: 'Budget for training the model', + }, + }) + .command('create-model', 'creates a new Model', {}, opts => + createModel( + opts.projectId, + opts.computeRegion, + opts.datasetId, + opts.modelName, + opts.trainBudget + ) + ) + .command('list-models', 'list all Models', {}, opts => + listModels(opts.projectId, opts.computeRegion, opts.filter) + ) + .command('get-model', 'Get a Model', {}, opts => + getModel(opts.projectId, opts.computeRegion, opts.modelId) + ) + .command('list-model-evaluations', 'List model evaluations', {}, opts => + listModelEvaluations( + opts.projectId, + opts.computeRegion, + opts.modelId, + opts.filter + ) + ) + .command('display-evaluation', 'Display evaluation', {}, opts => + displayEvaluation( + opts.projectId, + opts.computeRegion, + opts.modelId, + opts.filter + ) + ) + .command('delete-model', 'Delete a Model', {}, opts => + deleteModel(opts.projectId, opts.computeRegion, opts.modelId) + ) + .example('node $0 create-model -i "datasetId" -m "myModelName" -t "2"') + .example('node $0 list-models -f "image_classification_dataset_metadata:*"') + .example('node $0 get-model -a "ModelID"') + .example('node $0 list-model-evaluations -a "ModelID"') + .example('node $0 display-evaluation -a "ModelId"') + .example('node $0 delete-model -a "ModelID"') + .wrap(120) + .recommendCommands() + .help() + .strict().argv; diff --git a/automl/vision/automlVisionPredict.js b/automl/vision/automlVisionPredict.js index 40d30c22e7c..ad835c83d00 100644 --- a/automl/vision/automlVisionPredict.js +++ b/automl/vision/automlVisionPredict.js @@ -1,133 +1,133 @@ -// Copyright 2018 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. - -/** - * This application demonstrates how to perform basic operations on dataset - * with the Google AutoML Natural Language API. - * - * For more information, see the documentation at - * https://cloud.google.com/natural-language/automl/docs/ - */ - -'use strict'; - -function predict(projectId, computeRegion, modelId, filePath, scoreThreshold) { - // [START automl_vision_predict] - async function automlVisionPredict() { - const automl = require('@google-cloud/automl').v1beta1; - const fs = require('fs'); - - // Create client for prediction service. - const client = new automl.PredictionServiceClient(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; - // const computeRegion = `region-name, e.g. "us-central1"`; - // const modelId = `id of the model, e.g. “ICN12345”`; - // const filePath = `local text file path of content to be classified, e.g. "./resources/test.txt"`; - // const scoreThreshold = `value between 0.0 and 1.0, e.g. "0.5"`; - - // Get the full path of the model. - const modelFullId = client.modelPath(projectId, computeRegion, modelId); - - // Read the file content for prediction. - const content = fs.readFileSync(filePath, 'base64'); - - const params = {}; - - if (scoreThreshold) { - params.score_threshold = scoreThreshold; - } - - // Set the payload by giving the content and type of the file. - const payload = {}; - payload.image = {imageBytes: content}; - - // params is additional domain-specific parameters. - // currently there is no additional parameters supported. - const [response] = await client.predict({ - name: modelFullId, - payload: payload, - params: params, - }); - console.log('Prediction results:'); - response.payload.forEach(result => { - console.log(`Predicted class name: ${result.displayName}`); - console.log(`Predicted class score: ${result.classification.score}`); - }); - } - - automlVisionPredict().catch(console.error); - // [END automl_vision_predict] -} - +// Copyright 2018 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. + +/** + * This application demonstrates how to perform basic operations on dataset + * with the Google AutoML Natural Language API. + * + * For more information, see the documentation at + * https://cloud.google.com/natural-language/automl/docs/ + */ + +'use strict'; + +function predict(projectId, computeRegion, modelId, filePath, scoreThreshold) { + // [START automl_vision_predict] + async function automlVisionPredict() { + const automl = require('@google-cloud/automl').v1beta1; + const fs = require('fs'); + + // Create client for prediction service. + const client = new automl.PredictionServiceClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + // const filePath = `local text file path of content to be classified, e.g. "./resources/test.txt"`; + // const scoreThreshold = `value between 0.0 and 1.0, e.g. "0.5"`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Read the file content for prediction. + const content = fs.readFileSync(filePath, 'base64'); + + const params = {}; + + if (scoreThreshold) { + params.score_threshold = scoreThreshold; + } + + // Set the payload by giving the content and type of the file. + const payload = {}; + payload.image = {imageBytes: content}; + + // params is additional domain-specific parameters. + // currently there is no additional parameters supported. + const [response] = await client.predict({ + name: modelFullId, + payload: payload, + params: params, + }); + console.log('Prediction results:'); + response.payload.forEach(result => { + console.log(`Predicted class name: ${result.displayName}`); + console.log(`Predicted class score: ${result.classification.score}`); + }); + } + + automlVisionPredict().catch(console.error); + // [END automl_vision_predict] +} + require(`yargs`) // eslint-disable-line - .demand(1) - .options({ - computeRegion: { - alias: 'c', - type: 'string', - default: 'us-central1', - requiresArg: true, - description: 'region name e.g. "us-central1"', - }, - filePath: { - alias: 'f', - default: './resources/testImage.jpg', - type: 'string', - requiresArg: true, - description: 'local text file path of the content to be classified', - }, - modelId: { - alias: 'i', - //default: ``, - type: 'string', - requiresArg: true, - description: 'Id of the model which will be used for text classification', - }, - projectId: { - alias: 'z', - type: 'string', - default: process.env.GCLOUD_PROJECT, - requiresArg: true, - description: 'The GCLOUD_PROJECT string, e.g. "my-gcloud-project"', - }, - scoreThreshold: { - alias: 's', - type: 'string', - default: '0.5', - requiresArg: true, - description: - 'A value from 0.0 to 1.0. When the model makes predictions for an image it will' + - 'only produce results that have at least this confidence score threshold. Default is .5', - }, - }) - .command('predict', 'classify the content', {}, opts => - predict( - opts.projectId, - opts.computeRegion, - opts.modelId, - opts.filePath, - opts.scoreThreshold - ) - ) - .example( - 'node $0 predict -i "modelId" -f "./resources/testImage.jpg" -s "0.5"' - ) - .wrap(120) - .recommendCommands() - .help() - .strict().argv; + .demand(1) + .options({ + computeRegion: { + alias: 'c', + type: 'string', + default: 'us-central1', + requiresArg: true, + description: 'region name e.g. "us-central1"', + }, + filePath: { + alias: 'f', + default: './resources/testImage.jpg', + type: 'string', + requiresArg: true, + description: 'local text file path of the content to be classified', + }, + modelId: { + alias: 'i', + //default: ``, + type: 'string', + requiresArg: true, + description: 'Id of the model which will be used for text classification', + }, + projectId: { + alias: 'z', + type: 'string', + default: process.env.GCLOUD_PROJECT, + requiresArg: true, + description: 'The GCLOUD_PROJECT string, e.g. "my-gcloud-project"', + }, + scoreThreshold: { + alias: 's', + type: 'string', + default: '0.5', + requiresArg: true, + description: + 'A value from 0.0 to 1.0. When the model makes predictions for an image it will' + + 'only produce results that have at least this confidence score threshold. Default is .5', + }, + }) + .command('predict', 'classify the content', {}, opts => + predict( + opts.projectId, + opts.computeRegion, + opts.modelId, + opts.filePath, + opts.scoreThreshold + ) + ) + .example( + 'node $0 predict -i "modelId" -f "./resources/testImage.jpg" -s "0.5"' + ) + .wrap(120) + .recommendCommands() + .help() + .strict().argv;