培訓您的模型 - Amazon Lookout for Vision

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

培訓您的模型

建立資料集並標記影像之後,您可以訓練模型。在訓練過程中,會使用測試資料集。如果您有單一資料集專案,資料集中的影像會自動分割為測試資料集和訓練資料集,做為訓練程序的一部分。如果您的專案有訓練和測試資料集,則會使用這些資料集來分別訓練和測試資料集。

訓練完成後,您可以評估模型的效能並進行任何必要的改進。如需詳細資訊,請參閱改善您的 Amazon Lookout for Vision 模型

為了訓練您的模型,Amazon Lookout 視覺會製作一份來源訓練和測試影像的副本。依預設,複製的映像會使用AWS擁有和管理的金鑰加密。您也可以選擇使用自己的AWS金鑰管理服務 (KMS) 金鑰。如需詳細資訊,請參閱AWS金鑰管理服務概念。您的來源圖像不會受到影響。

您可以使用標籤形式將中繼資料指派給模型。如需詳細資訊,請參閱標記模型

每次訓練模型時,都會建立模型的新版本。如果您不再需要模型的版本,可以將其刪除。如需詳細資訊,請參閱刪除模型

您需支付成功訓練模型所花費的時間費用。如需詳細資訊,請參閱訓練時數

若要檢視專案中的現有模型,請執行檢視您的模型

注意

如果您剛剛完成建立資料集將影像新增至資料集. 主控台目前應該會顯示您的模型儀表板,而您不需要執行步驟 1 至 4。

訓練模型(控制台)

下列程序說明如何使用主控台訓練模型。

培訓您的模型(主控台)
  1. 打開 Amazon Lookout for Vision 控制台 https://console.aws.amazon.com/lookoutvision/

  2. 在左側導覽視窗中,選擇 專案

  3. 專案 頁面,選擇包含要培訓的模型的專案。

  4. 在專案詳細資訊頁面上,選擇 [訓練模型]。如果您有足夠的標籤影像來訓練模型,則可以使用「訓練模型」按鈕。如果該按鈕不可用,請添加更多圖像,直到您有足夠的標記圖像。

  5. (選擇性) 如果您要使用自己的AWSKMS加密金鑰,請執行下列動作:

    1. 圖像資料加密 中選擇 自訂加密配置 (進階)

    2. 加密 .aws_kms_key 時,請輸入金鑰的 Amazon 資源名稱 (ARN),或選擇現有的金鑰。AWS KMS若要建立新的金鑰,請選擇 [建立AWSIMS金鑰]。

  6. (可選) 如果您要新增標籤到模型,請執行以下操作:

    1. 標籤 區域,選擇 新增

    2. 輸入下列資料:

      1. 金鑰 中的金鑰名稱。

      2. 中的鍵/值。

    3. 若要新增更多標籤,請重複步驟 6a 和 6b。

    4. (選用) 如果您要移除標籤,請選擇要刪除的標籤旁邊的刪除。如果您要移除先前儲存的標籤,則會在您儲存變更時移除該標籤。

  7. 選擇訓練模型

  8. 您是否要訓練模型?的對話框中,選擇訓練模型

  9. 在「模」(Model) 檢視中,您可以看到訓練已經開始,而且您可以檢視模型版本的Status欄來檢查目前的狀態。培訓模型需要一段時間才能完成。

  10. 訓練完成後,您可以評估其效能。如需詳細資訊,請參閱改善您的 Amazon Lookout for Vision 模型

訓練模型(SDK)

您可以使用此CreateModel作業來開始模型的訓練、測試和評估。Amazon Lookout for Vision 使用與該項目關聯的培訓和測試數據集來訓練模型。如需詳細資訊,請參閱建立專案 (SDK)

每次呼叫時CreateModel,都會建立模型的新版本。來自的回應CreateModel包括模型的版本。

每次成功的模型訓練都會向您收取費用。使用 ClientToken input 參數可協助避免因使用者不必要或意外重複模型訓練而產生的費用。 ClientToken是冪等輸入參數,可確保CreateModel僅針對特定參數集完成一次 — 具有相同ClientTokenCreateModel的重複呼叫可確保不會重複訓練。如果您沒有提供值ClientToken,則AWSSDK您正在使用的值會為您插入一個值。這樣可以防止網路錯誤後重試啟動多個訓練工作,但您需要為自己的使用案例提供自己的價值。如需詳細資訊,請參閱CreateModel

培訓需要一段時間才能完成。要檢查當前狀態,請調用DescribeModel並傳遞項目名稱(在調用對象中指定CreateProject)和模型版本。此status欄位會指出模型訓練的目前狀態。如需範例程式碼,請參閱 檢視您的模型 (SDK)

如果訓練成功,您可以評估模型。如需詳細資訊,請參閱改善您的 Amazon Lookout for Vision 模型

若要檢視您在專案中建立的模型,請呼叫ListModels。如需範例程式碼,請參閱 檢視您的模型

訓練模型(SDK)
  1. 如果您尚未這樣做,請安裝並配置 AWS CLI 和 AWS SDKs。如需詳細資訊,請參閱步驟 4:設定 AWS CLI 以及 AWS SDKs

  2. 使用下列範例程式碼來訓練模型。

    CLI

    變更下列值:

    • project-name至包含您要建立之模型的專案名稱。

    • output-config到您要儲存訓練結果的位置。取代以下的值:

      • output bucket與 Amazon S3 存儲桶的名稱,其中 Amazon Lookout for Vision 保存培訓結果。

      • output folder以您要儲存訓練結果的資料夾名稱。

      • Key帶有標籤鍵的名稱。

      • Value與要關聯的值tag_key

    aws lookoutvision create-model --project-name "project name"\ --output-config '{ "S3Location": { "Bucket": "output bucket", "Prefix": "output folder" } }'\ --tags '[{"Key":"Key","Value":"Value"}]' \ --profile lookoutvision-access
    Python

    此代碼取自 AWS 文檔SDK示例 GitHub 存儲庫。請參閱此處的完整範例。

    @staticmethod def create_model( lookoutvision_client, project_name, training_results, tag_key=None, tag_key_value=None, ): """ Creates a version of a Lookout for Vision model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project in which you want to create a model. :param training_results: The Amazon S3 location where training results are stored. :param tag_key: The key for a tag to add to the model. :param tag_key_value - A value associated with the tag_key. return: The model status and version. """ try: logger.info("Training model...") output_bucket, output_folder = training_results.replace("s3://", "").split( "/", 1 ) output_config = { "S3Location": {"Bucket": output_bucket, "Prefix": output_folder} } tags = [] if tag_key is not None: tags = [{"Key": tag_key, "Value": tag_key_value}] response = lookoutvision_client.create_model( ProjectName=project_name, OutputConfig=output_config, Tags=tags ) logger.info("ARN: %s", response["ModelMetadata"]["ModelArn"]) logger.info("Version: %s", response["ModelMetadata"]["ModelVersion"]) logger.info("Started training...") print("Training started. Training might take several hours to complete.") # Wait until training completes. finished = False status = "UNKNOWN" while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=response["ModelMetadata"]["ModelVersion"], ) status = model_description["ModelDescription"]["Status"] if status == "TRAINING": logger.info("Model training in progress...") time.sleep(600) continue if status == "TRAINED": logger.info("Model was successfully trained.") else: logger.info( "Model training failed: %s ", model_description["ModelDescription"]["StatusMessage"], ) finished = True except ClientError: logger.exception("Couldn't train model.") raise else: return status, response["ModelMetadata"]["ModelVersion"]
    Java V2

    此代碼取自 AWS 文檔SDK示例 GitHub 存儲庫。請參閱此處的完整範例。

    /** * Creates an Amazon Lookout for Vision model. The function returns after model * training completes. Model training can take multiple hours to complete. * You are charged for the amount of time it takes to successfully train a model. * Returns after Lookout for Vision creates the dataset. * * @param lfvClient An Amazon Lookout for Vision client. * @param projectName The name of the project in which you want to create a * model. * @param description A description for the model. * @param bucket The S3 bucket in which Lookout for Vision stores the * training results. * @param folder The location of the training results within the S3 * bucket. * @return ModelDescription The description of the created model. */ public static ModelDescription createModel(LookoutVisionClient lfvClient, String projectName, String description, String bucket, String folder) throws LookoutVisionException, InterruptedException { logger.log(Level.INFO, "Creating model for project: {0}.", new Object[] { projectName }); // Setup input parameters. S3Location s3Location = S3Location.builder() .bucket(bucket) .prefix(folder) .build(); OutputConfig config = OutputConfig.builder() .s3Location(s3Location) .build(); CreateModelRequest createModelRequest = CreateModelRequest.builder() .projectName(projectName) .description(description) .outputConfig(config) .build(); // Create and train the model. CreateModelResponse response = lfvClient.createModel(createModelRequest); String modelVersion = response.modelMetadata().modelVersion(); boolean finished = false; DescribeModelResponse descriptionResponse = null; // Wait until training finishes or fails. do { DescribeModelRequest describeModelRequest = DescribeModelRequest.builder() .projectName(projectName) .modelVersion(modelVersion) .build(); descriptionResponse = lfvClient.describeModel(describeModelRequest); switch (descriptionResponse.modelDescription().status()) { case TRAINED: logger.log(Level.INFO, "Model training completed for project {0} version {1}.", new Object[] { projectName, modelVersion }); finished = true; break; case TRAINING: logger.log(Level.INFO, "Model training in progress for project {0} version {1}.", new Object[] { projectName, modelVersion }); TimeUnit.SECONDS.sleep(60); break; case TRAINING_FAILED: logger.log(Level.SEVERE, "Model training failed for for project {0} version {1}.", new Object[] { projectName, modelVersion }); finished = true; break; default: logger.log(Level.SEVERE, "Unexpected error when training model project {0} version {1}: {2}.", new Object[] { projectName, modelVersion, descriptionResponse.modelDescription() .status() }); finished = true; break; } } while (!finished); return descriptionResponse.modelDescription(); }
  3. 訓練完成後,您可以評估其效能。如需詳細資訊,請參閱改善您的 Amazon Lookout for Vision 模型