3

Now i can use autoML node.js client library to train the model on google-cloud-automl.

Q: How can i programmatically get the model id when finished training the model?.

Goal: I will use that id to deploy the model without web interface.

Tried: At first, i thought it is in the response when training the model (operation.name). But the operation.name showed projects/${projectId}/locations/${location}/operations/${operationId}, which is not include model id. So i have no idea how to programmatically get the model id.

Any suggestion will be grateful.

code for training from : https://cloud.google.com/vision/automl/docs/train-edge

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const dataset_id = 'YOUR_DATASET_ID';
// const displayName = 'YOUR_DISPLAY_NAME';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function createModel() {
  // Construct request
  const request = {
    parent: client.locationPath(projectId, location),
    model: {
      displayName: displayName,
      datasetId: datasetId,
      imageClassificationModelMetadata: {
        trainBudgetMilliNodeHours: 24000,
      },
    },
  };

  // Don't wait for the LRO
  const [operation] = await client.createModel(request);
  console.log(`Training started... ${operation}`);
  console.log(`Training operation name: ${operation.name}`);
}

createModel();

code for deploy from: https://cloud.google.com/vision/automl/docs/deploy (model id is required)


/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function deployModel() {
  // Construct request
  const request = {
    name: client.modelPath(projectId, location, modelId),
  };

  const [operation] = await client.deployModel(request);

  // Wait for operation to complete.
  const [response] = await operation.promise();
  console.log(`Model deployment finished. ${response}`);
}

deployModel();
SanyapongY
  • 78
  • 5

1 Answers1

3

Creating the model is a Long Running Operation (LRO) so the response is not going to contain the model metadata, but instead contains information about the operation that will create the model:

{
  "name": "projects/project-id/locations/us-central1/operations/ICN2106290444865378475",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1.OperationMetadata",
    "createTime": "2019-10-30T20:06:08.253243Z",
    "updateTime": "2019-10-30T20:06:08.253243Z",
    "createModelDetails": {}
  }
}

You can retrieve the operation at any point to see if it has completed:

/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const operationId = 'YOUR_OPERATION_ID'; // e.g. ICN2106290444865378475

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function getOperationStatus() {
  // Construct request
  const request = {
    name: `projects/${projectId}/locations/${location}/operations/${operationId}`,
  };

  const [response] = await client.operationsClient.getOperation(request);

  console.log(`Name: ${response.name}`);
  console.log(`Operation details:`);
  console.log(`${response}`);
}

getOperationStatus();

The above Node.js code is from the Working with long-running operations section of the documentation.

You should see output similar to the following for a completed create model operation:

{
  "name": "projects/project-id/locations/us-central1/operations/operation-id",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1.OperationMetadata",
    "createTime": "2019-07-22T18:35:06.881193Z",
    "updateTime": "2019-07-22T19:58:44.972235Z",
    "createModelDetails": {}
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.automl.v1.Model",
    "name": "projects/project-id/locations/us-central1/models/model-id"
  }
}

You could then get the model-id from the response:

console.log(response.response.name); // Full model path
console.log(response.response.name.replace(/projects\/[a-zA-Z0-9-]*\/locations\/[a-zA-Z0-9-]*\/models\//,'')); // Just the model-id
Daniel Bank
  • 3,581
  • 3
  • 39
  • 50
  • It is very clear answer and i got the point now. Thank you very much for your attention!. – SanyapongY Mar 19 '20 at 02:52
  • If you don't mind i am asking, how do you know those response?. I means are there any document should i take a look. I'm newbie any suggestion will be grateful. – SanyapongY Mar 20 '20 at 00:33
  • 2
    The response is in the "Working with Long Running Operations" section under the "REST & CMD LINE" Tab. See https://cloud.google.com/vision/automl/docs/train-edge#automl_get_operation_status-drest. It's not really helpful that they have different information under each tab, but that's the state of the documentation. So for instance if you were looking under the "Node.js" Tab, you would not see that response object. – Daniel Bank Mar 20 '20 at 01:28
  • I got it!. Yep, as you mentioned i get confused and thought they are separated. Thank you very much for your help. Have a nice day bro :). – SanyapongY Mar 20 '20 at 03:04