簡體   English   中英

如何使用 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 上訓練模型。

問:完成模型訓練后,如何以編程方式獲取模型 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 代碼來自文檔的使用長時間運行的操作部分。

對於已完成的創建模型操作,您應該會看到類似於以下內容的輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM