训练您的模型 - Amazon Lookout for Vision

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

训练您的模型

在创建数据集后并标注图像后,您便可以训练模型。作为训练过程的一部分,将会使用测试数据集。如果您具有单数据集项目,则作为训练过程的一部分,数据集中的图像会自动拆分为测试数据集和训练数据集。如果您的项目有训练数据集和测试数据集,则它们会分别用来训练和测试数据集。

训练完成后,您可以评估模型的性能并做出必要的改进。有关更多信息,请参阅 改进您的 Amazon Lookout for Vision 模型

为了训练您的模型,Amazon Lookout for Vision 会复制您的源训练图像和测试图像。默认情况下,复制的图像使用AWS拥有和管理的密钥进行加密。您也可以选择使用自己的AWS密钥管理服务 (KMS) 密钥。有关更多信息,请参阅AWS密钥管理服务概念。您的源图像不受影响。

您可以将元数据以标签的形式分配给自己的模型。有关更多信息,请参阅 标记模型

每次训练模型时,都会创建一个新的模型版本。如果您不再需要某个模型版本,可以将其删除。有关更多信息,请参阅 删除模型

您需要按照成功训练模型所花费的时间量付费。有关更多信息,请参阅训练时长

要查看项目中的现有模型,请 查看您的模型

注意

如果您刚完成 创建您的数据集向您的数据集中添加图像,则控制台此时应该会显示您的模型控制面板,并且您无需执行步骤 1-4。

训练模型(控制台)

下面的过程演示如何使用控制台训练您的模型。

训练您的模型(控制台)
  1. 打开亚马逊 Lookout for Vision 控制台 https://console.aws.amazon.com/lookoutvision/,网址为。

  2. 在左侧导航窗格中,选择项目

  3. 项目页面上,选择包含要训练的模型的项目。

  4. 在项目详细信息页面上,选择训练模型。如果您有足够多的已标注图像来训练模型,则训练模型按钮可供使用。如果该按钮不可用,请添加更多图像,直到有足够多的已标注图像为止。

  5. (可选)如果要使用自己的AWSKMS加密密钥,请执行以下操作:

    1. 图像数据加密中,选择自定义加密设置(高级)

    2. encryption.aws_kms_key 中输入密钥的亚马逊资源名称 ARN (),或者选择现有密钥。AWS KMS要创建新密钥,请选择创建AWSIMS密钥

  6. (可选)如果要向模型添加标签,请执行以下操作:

    1. 标签部分,选择添加新标签

    2. 输入以下信息:

      1. 中输入键名称。

      2. 中输入键值。

    3. 要添加更多标签,请重复步骤 6a 和 6b。

    4. (可选)如果要移除标签,请选择要移除的标签旁的移除。如果要移除先前保存的标签,则会在保存所做的更改时将其移除。

  7. 选择训练模型

  8. 是否要训练您的模型?对话框中,选择训练模型

  9. 模型视图中,您可以看到训练已开始,通过查看模型版本的 Status 列可以检查当前状态。训练模型需要一些时间才能完成。

  10. 训练结束后,您可以评估它的性能。有关更多信息,请参阅 改进您的 Amazon Lookout for Vision 模型

训练模型 (SDK)

您可以使用该CreateModel操作开始模型的训练、测试和评估。Amazon Lookout for Vision 使用与项目关联的训练和测试数据集来训练模型。有关更多信息,请参阅 创建项目 (SDK)

每次调用 CreateModel 时,都会创建一个新的模型版本。CreateModel 的响应中包含模型的版本。

您需要为每次成功训练模型付费。使用 ClientToken 输入参数,有助于防止因用户不必要或意外重复进行模型训练而产生费用。ClientToken 是一个幂等输入参数,可确保一组特定的参数仅会完成一次 CreateModel。使用相同的 ClientToken 值重复调用 CreateModel 时,可确保不会重复训练。如果您没有为提供值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 模型