【问题标题】:How to programmatically get model id from google-cloud-automl with node.js client library如何使用 node.js 客户端库以编程方式从 google-cloud-automl 获取模型 ID
【发布时间】:2020-06-30 02:29:05
【问题描述】:

现在我可以使用 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();

【问题讨论】:

    标签: javascript node.js google-cloud-platform google-cloud-automl


    【解决方案1】:

    创建模型是一项长期运行操作 (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
    

    【讨论】:

    • 答案非常明确,我现在明白了。非常感谢您的关注!。
    • 如果你不介意我问,你怎么知道这些反应?我的意思是有什么文件我应该看一下。我是新手,任何建议将不胜感激。
    • 响应位于“REST & CMD LINE”选项卡下的“处理长时间运行的操作”部分。见cloud.google.com/vision/automl/docs/…。他们在每个选项卡下都有不同的信息并没有真正的帮助,但这就是文档的状态。因此,例如,如果您在“Node.js”选项卡下查看,您将看不到该响应对象。
    • 我明白了!是的,正如你提到的,我感到困惑,并认为它们是分开的。非常感谢您的帮助。祝你有美好的一天兄弟:)。
    猜你喜欢
    • 2020-11-07
    • 1970-01-01
    • 2019-06-24
    • 2013-03-02
    • 2012-07-05
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多