终止支持通知:2025年10月31日, AWS 将停止对亚马逊 Lookout for Vision 的支持。2025 年 10 月 31 日之后,你将无法再访问 Lookout for Vision 主机或 Lookout for Vision 资源。如需更多信息,请访问此博客文章。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
训练您的模型
在创建数据集后并标注图像后,您便可以训练模型。作为训练过程的一部分,将会使用测试数据集。如果您具有单数据集项目,则作为训练过程的一部分,数据集中的图像会自动拆分为测试数据集和训练数据集。如果您的项目有训练数据集和测试数据集,则它们会分别用来训练和测试数据集。
训练完成后,您可以评估模型的性能并做出必要的改进。有关更多信息,请参阅 改进您的 Amazon Lookout for Vision 模型。
为了训练您的模型,Amazon Lookout for Vision 会复制您的源训练图像和测试图像。默认情况下,所复制的图像会使用亚马逊云科技拥有和管理的密钥进行静态加密。您也可以选择使用自己的 Amazon Key Management Service(KMS)密钥。有关更多信息,请参阅 Amazon Key Management Service 概念。源图像不受影响。
您可以将元数据以标签的形式分配给自己的模型。有关更多信息,请参阅 标记模型。
每次训练模型时,都会创建一个新的模型版本。如果您不再需要某个模型版本,可以将其删除。有关更多信息,请参阅 删除模型。
您需要按照成功训练模型所花费的时间量付费。有关更多信息,请参阅训练时长。
要查看项目中的现有模型,请 查看您的模型。
训练模型(控制台)
下面的过程演示如何使用控制台训练您的模型。
训练您的模型(控制台)
-
打开亚马逊 Lookout for Vision 控制台 https://console.aws.amazon.com/lookoutvision/,网址为。
-
在左侧导航窗格中,选择项目。
-
在项目页面上,选择包含要训练的模型的项目。
-
在项目详细信息页面上,选择训练模型。如果您有足够多的已标注图像来训练模型,则训练模型按钮可供使用。如果该按钮不可用,请添加更多图像,直到有足够多的已标注图像为止。
-
(可选)如果要使用自己的 Amazon KMS 加密密钥,请执行以下操作:
-
在图像数据加密中,选择自定义加密设置(高级)。
-
在 encryption.aws_kms_key 中,输入您的密钥的 Amazon 资源名称 (ARN),或者选择现有的 AWS KMS 密钥。要创建新密钥,请选择创建 AWS IMS 密钥。
-
(可选)如果要向模型添加标签,请执行以下操作:
-
在标签部分,选择添加新标签。
-
输入以下信息:
-
在键中输入键名称。
-
在值中输入键值。
-
要添加更多标签,请重复步骤 6a 和 6b。
-
(可选)如果要移除标签,请选择要移除的标签旁的移除。如果要移除先前保存的标签,则会在保存所做的更改时将其移除。
-
选择训练模型。
-
在是否要训练您的模型?对话框中,选择训练模型。
-
在模型视图中,您可以看到训练已开始,通过查看模型版本的 Status
列可以检查当前状态。训练模型需要一些时间才能完成。
-
训练结束后,您可以评估它的性能。有关更多信息,请参阅 改进您的 Amazon Lookout for Vision 模型。
训练模型(SDK)
您可以使用该CreateModel操作开始模型的训练、测试和评估。Amazon Lookout for Vision 使用与项目关联的训练和测试数据集来训练模型。有关更多信息,请参阅 创建项目(SDK)。
每次调用 CreateModel
时,都会创建一个新的模型版本。CreateModel
的响应中包含模型的版本。
您需要为每次成功训练模型付费。使用 ClientToken
输入参数,有助于防止因用户不必要或意外重复进行模型训练而产生费用。ClientToken
是一个幂等输入参数,可确保一组特定的参数仅会完成一次 CreateModel
。使用相同的 ClientToken
值重复调用 CreateModel
时,可确保不会重复训练。如果不为 ClientToken
提供值,则您使用的 Amazon SDK 会为您插入一个值。这样在网络错误后,可以防止重试操作启动多个训练作业,但您需要为自己的使用场景提供自己的价值。有关更多信息,请参阅 CreateModel。
训练需要一段时间才能完成。要检查当前状态,请调用 DescribeModel
并传递项目名称(在调用 CreateProject
时指定)和模型版本。status
字段用于指示模型训练的当前状态。有关代码示例,请参阅 查看您的模型 (SDK)。
如果训练成功,您便可以评估模型。有关更多信息,请参阅 改进您的 Amazon Lookout for Vision 模型。
要查看您在项目中已创建的模型,请调用 ListModels
。有关代码示例,请参阅 查看您的模型。
训练模型(SDK)
-
如果您尚未这样做,请安装并配置 AWS CLI 和 AWS SDKs。有关更多信息,请参阅 第 4 步:设置 AWS CLI 和 AWS SDKs。
-
使用以下示例代码训练模型。
- CLI
-
更改以下值:
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();
}
-
训练结束后,您可以评估它的性能。有关更多信息,请参阅 改进您的 Amazon Lookout for Vision 模型。