diff --git a/automl/vision/automlVisionDataset.js b/automl/vision/automlVisionDataset.js index 67cfa5d6f89..aec1b6dbc8f 100644 --- a/automl/vision/automlVisionDataset.js +++ b/automl/vision/automlVisionDataset.js @@ -1,17 +1,16 @@ -/** - * 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 - * - * 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. - */ +// 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 @@ -23,238 +22,273 @@ `use strict`; -async function createDataset( - projectId, - computeRegion, - datasetName, - multiLabel -) { +function createDataset(projectId, computeRegion, datasetName, multiLabel) { // [START automl_vision_create_dataset] - 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`; + 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}`); } - // 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] } -async function listDatasets(projectId, computeRegion, filter) { +function listDatasets(projectId, computeRegion, filter) { // [START automl_vision_list_datasets] - 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 => { + 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 getDataset(projectId, computeRegion, datasetId) { + // [START automl_vision_get_dataset] + async function automlVisionGetDataset() { + 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 + ); + + // Get complete detail of the dataset. + const [dataset] = await client.getDataset({name: datasetFullId}); + // 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 + `Classification type: ${dataset.imageClassificationDatasetMetadata.classificationType}` ); console.log(`Dataset create time: `); console.log(`\tseconds: ${dataset.createTime.seconds}`); console.log(`\tnanos: ${dataset.createTime.nanos}`); - console.log(`\n`); - }); - // [END automl_vision_list_datasets] -} + } -async function getDataset(projectId, computeRegion, datasetId) { - // [START automl_vision_get_dataset] - 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); - - // Get complete detail of the dataset. - const [dataset] = await client.getDataset({name: datasetFullId}); - // 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( - `Classification type: ${dataset.imageClassificationDatasetMetadata.classificationType}` - ); - console.log(`Dataset create time: `); - console.log(`\tseconds: ${dataset.createTime.seconds}`); - console.log(`\tnanos: ${dataset.createTime.nanos}`); + automlVisionGetDataset().catch(console.error); // [END automl_vision_get_dataset] } -async function importData(projectId, computeRegion, datasetId, path) { +function importData(projectId, computeRegion, datasetId, path) { // [START automl_vision_import_data] - 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(); + 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 + ); - // The final result of the operation. - if (response.done) { - console.log(`Data imported.`); + // 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] } -async function exportData(projectId, computeRegion, datasetId, outputUri) { +function exportData(projectId, computeRegion, datasetId, outputUri) { // [START automl_vision_export_data] - 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 outputUri = `Google Cloud Storage URI for the export directory, e.g. “gs://myproject/output”;` - - // Get the full path of the dataset. - const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + async function automlVisionExportData() { + 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 outputUri = `Google Cloud Storage URI for the export directory, e.g. “gs://myproject/output”;` + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath( + projectId, + computeRegion, + datasetId + ); - // Set the output URI - const outputConfig = { - gcsDestination: { - outputUriPrefix: outputUri, - }, - }; - - // Export the data to the output URI. - const [operation] = await client.exportData({ - name: datasetFullId, - outputConfig: outputConfig, - }); - const [, , response] = await operation.promise(); - - // The final result of the operation. - if (response.done) { - console.log(`Data exported.`); + // Set the output URI + const outputConfig = { + gcsDestination: { + outputUriPrefix: outputUri, + }, + }; + + // Export the data to the output URI. + const [operation] = await client.exportData({ + name: datasetFullId, + outputConfig: outputConfig, + }); + const [, , response] = await operation.promise(); + + // The final result of the operation. + if (response.done) { + console.log(`Data exported.`); + } } + + automlVisionExportData().catch(console.error); // [END automl_vision_export_data] } -async function deleteDataset(projectId, computeRegion, datasetId) { +function deleteDataset(projectId, computeRegion, datasetId) { // [START automl_vision_delete_dataset] - 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.`); + 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] } diff --git a/automl/vision/automlVisionModel.js b/automl/vision/automlVisionModel.js index 96c52c84c02..ff04194f93e 100644 --- a/automl/vision/automlVisionModel.js +++ b/automl/vision/automlVisionModel.js @@ -1,17 +1,16 @@ -/** - * 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 - * - * 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. - */ +// 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 @@ -23,7 +22,7 @@ `use strict`; -async function createModel( +function createModel( projectId, computeRegion, datasetId, @@ -31,108 +30,188 @@ async function createModel( trainBudget ) { // [START automl_vision_create_model] - const automl = require(`@google-cloud/automl`).v1beta1; - - const client = new automl.AutoMlClient(); + 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}; + } - /** - * 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`; + } - // 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}`); } - // 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] } -async function getOperationStatus(operationFullId) { +function getOperationStatus(operationFullId) { // [START automl_vision_get_operation_status] - const automl = require(`@google-cloud/automl`).v1beta1; + async function automlVisionGetOperationStatus() { + const automl = require(`@google-cloud/automl`).v1beta1; - const client = new automl.AutoMlClient(); + const client = new automl.AutoMlClient(); - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const operationFullId = `Full name of an operation, eg. “Projects//locations/us-central1/operations/ + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const operationFullId = `Full name of an operation, eg. “Projects//locations/us-central1/operations/ - // Get the latest state of a long-running operation. - const [response] = await client.operationsClient.getOperation( - operationFullId - ); - console.log(`Operation status: `, response); + // Get the latest state of a long-running operation. + const [response] = await client.operationsClient.getOperation( + operationFullId + ); + console.log(`Operation status: `, response); + } + + automlVisionGetOperationStatus().catch(console.error); // [END automl_vision_get_operation_status] } -async function listModels(projectId, computeRegion, filter) { +function listModels(projectId, computeRegion, filter) { // [START automl_vision_list_models] - const automl = require(`@google-cloud/automl`); + 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`); + }); + } - const client = new automl.v1beta1.AutoMlClient(); + automlVisinListModels().catch(console.error); + // [END automl_vision_list_models] +} - /** - * 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:*”`; +function getModel(projectId, computeRegion, modelId) { + // [START automl_vision_get_model] - // A resource that represents Google Cloud Platform location. - const projectLocation = client.locationPath(projectId, computeRegion); + async function automlVisionGetModel() { + const automl = require(`@google-cloud/automl`).v1beta1; - // List all the models available in the region by applying filter. - const [models] = await client.listModels({ - parent: projectLocation, - filter: filter, - }); + 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); - // Display the model information. - console.log(`List of models:`); - models.forEach(model => { + // 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}`); @@ -172,98 +251,41 @@ async function listModels(projectId, computeRegion, filter) { console.log(`\tseconds: ${model.updateTime.seconds}`); console.log(`\tnanos: ${model.updateTime.nanos}`); console.log(`Model deployment state: ${model.deploymentState}`); - console.log(`\n`); - }); - // [END automl_vision_list_models] -} - -async function getModel(projectId, computeRegion, modelId) { - // [START automl_vision_get_model] - 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] } -async function listModelEvaluations(projectId, computeRegion, modelId, filter) { +function listModelEvaluations(projectId, computeRegion, modelId, filter) { // [START automl_vision_list_model_evaluations] - 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); + 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)); + }); + } - // 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] } @@ -303,110 +325,121 @@ async function getModelEvaluation( // [END automl_vision_get_model_evaluation] } -async function displayEvaluation(projectId, computeRegion, modelId, filter) { +function displayEvaluation(projectId, computeRegion, modelId, filter) { // [START automl_vision_display_evaluation] - 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 - ); + 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) + ); + } + }); + } + }); + } - const [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] } -async function deleteModel(projectId, computeRegion, modelId) { +function deleteModel(projectId, computeRegion, modelId) { // [START automl_vision_delete_model] - 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.`); + 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] } diff --git a/automl/vision/automlVisionPredict.js b/automl/vision/automlVisionPredict.js index 50f7db73590..d48cdfeded2 100644 --- a/automl/vision/automlVisionPredict.js +++ b/automl/vision/automlVisionPredict.js @@ -1,17 +1,16 @@ -/** - * 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 - * - * 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. - */ +// 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 @@ -23,57 +22,55 @@ 'use strict'; -async function predict( - projectId, - computeRegion, - modelId, - filePath, - scoreThreshold -) { +function predict(projectId, computeRegion, modelId, filePath, scoreThreshold) { // [START automl_vision_predict] - const automl = require('@google-cloud/automl').v1beta1; - const fs = require('fs'); + async function automlVisionPredict() { + const automl = require('@google-cloud/automl').v1beta1; + const fs = require('fs'); - // Create client for prediction service. - const client = new automl.PredictionServiceClient(); + // 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"`; + /** + * 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); + // 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'); + // Read the file content for prediction. + const content = fs.readFileSync(filePath, 'base64'); - const params = {}; + const params = {}; - if (scoreThreshold) { - params.score_threshold = scoreThreshold; - } + if (scoreThreshold) { + params.score_threshold = scoreThreshold; + } + + // Set the payload by giving the content and type of the file. + const payload = {}; + payload.image = {imageBytes: content}; - // 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}`); + }); + } - // 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] }