gpt4 book ai didi

javascript - 如何使用 node.js 客户端库以编程方式从 google-cloud-automl 获取模型 ID

转载 作者:行者123 更新时间:2023-12-04 14:15:22 25 4
gpt4 key购买 nike

现在我可以使用 autoML node.js 客户端库在 google-cloud-automl 上训练模型。

问:如何在完成模型训练后以编程方式获取模型 ID?。

目标:我将使用该 ID 在没有 Web 界面的情况下部署模型。

尝试过:起初,我认为它是在训练模型(operation.name)时的响应中。但是 operation.name 显示projects/${projectId}/locations/${location}/operations/${operationId},不包括模型 ID。所以我不知道如何以编程方式获取模型 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)


/**
* 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),因此响应不会包含模型元数据,而是包含有关将创建模型的操作的信息:

{
"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": {}
}
}

您可以随时检索操作以查看它是否已完成:

/**
* 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代码来自Working with long-running operations文档部分。

对于已完成的创建模型操作,您应该会看到类似于以下内容的输出:

{
"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:

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

关于javascript - 如何使用 node.js 客户端库以编程方式从 google-cloud-automl 获取模型 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60748968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com