如何使用node.js客户端库以编程方式从google-cloud-automl获取模型ID [英] How to programmatically get model id from google-cloud-automl with node.js client library
问题描述
现在,我可以使用autoML node.js客户端库在google-cloud-automl上训练模型.
Now i can use autoML node.js client library to train the model on google-cloud-automl.
问::完成模型训练后,如何以编程方式获取模型ID?
Q: How can i programmatically get the model id when finished training the model?.
目标:我将使用该ID来部署没有Web界面的模型.
Goal: I will use that id to deploy the model without web interface.
尝试过:首先,我认为这是训练模型(operation.name)时的响应方式.但是operation.name显示 projects/$ {projectId}/locations/$ {location}/operations/$ {operationId} ,其中不包含模型ID.所以我不知道如何以编程方式获取模型ID.
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.
任何建议将不胜感激.
培训代码,来自: 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();
用于部署的代码来自: https://cloud.google.com/vision/automl/docs/deploy (必须提供型号ID)
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();
推荐答案
创建模型是一项长期运行(LRO)操作,因此响应不会包含模型元数据,而是包含有关将创建的操作的信息模型:
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();
上面的Node.js代码来自使用文档中长期运行的操作部分.
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"
}
}
然后您可以从响应中获取 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
这篇关于如何使用node.js客户端库以编程方式从google-cloud-automl获取模型ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!