支援終止通知:在 2025 年 10 月 31 日, AWS 將停止對 Amazon Lookout for Vision 的支援。2025 年 10 月 31 日之後,您將無法再存取 Lookout for Vision 主控台或 Lookout for Vision 資源。如需詳細資訊,請造訪此部落格文章。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
培訓您的模型
建立資料集並標記影像後,您可以訓練模型。在訓練過程中,會使用測試資料集。如果您有單一資料集專案,資料集中的影像會自動分割為測試資料集和訓練資料集,做為訓練程序的一部分。如果您的專案有訓練和測試資料集,它們會用來分別訓練和測試資料集。
訓練完成後,您可以評估模型的效能並進行任何必要的改善。如需詳細資訊,請參閱改善您的 Amazon Lookout for Vision 模型。
為了訓練模型,Amazon Lookout for Vision 會複製您的來源訓練和測試映像。根據預設,複製的影像會使用AWS擁有和管理的金鑰進行加密。您也可以選擇使用自己的 AWS Key Management Service (KMS) 金鑰。如需詳細資訊,請參閱 AWS Key Management Service 概念。您的來源圖像不會受到影響。
您可以將中繼資料以標籤的形式指派給模型。如需詳細資訊,請參閱標記模型。
每次訓練模型時,都會建立新的模型版本。如果您不再需要模型的版本,則可以將其刪除。如需詳細資訊,請參閱刪除模型。
您需要支付成功訓練模型所需的時間。如需詳細資訊,請參閱訓練時數。
若要檢視專案中的現有模型,請檢視模型。
訓練模型 (主控台)
下列程序說明如何使用 主控台訓練模型。
培訓您的模型(主控台)
-
在 開啟 Amazon Lookout for Vision https://console.aws.amazon.com/lookoutvision/主控台。
-
在左側導覽視窗中,選擇 專案。
-
在 專案 頁面,選擇包含要培訓的模型的專案。
-
在專案詳細資訊頁面上,選擇訓練模型。如果您有足夠的標籤影像來訓練模型,即可使用訓練模型按鈕。如果按鈕無法使用,請新增更多影像,直到您有足夠的標籤影像為止。
-
(選用) 如果您想要使用自己的AWSKMS加密金鑰,請執行下列動作:
-
在 圖像資料加密 中選擇 自訂加密配置 (進階)。
-
在 encryption.aws_kms_key 中,輸入金鑰的 Amazon Resource Name (ARN),或選擇現有的AWSKMS金鑰。若要建立新的金鑰,請選擇建立AWSIMS金鑰。
-
(可選) 如果您要新增標籤到模型,請執行以下操作:
-
在 標籤 區域,選擇 新增。
-
輸入下列資料:
-
金鑰 中的金鑰名稱。
-
值 中的鍵/值。
-
若要新增更多標籤,請重複步驟 6a 和 6b。
-
(選用) 如果您要移除標籤,請選擇要刪除的標籤旁邊的刪除。如果您要移除先前儲存的標籤,則會在您儲存變更時移除該標籤。
-
選擇訓練模型。
-
在您是否要訓練模型?的對話框中,選擇訓練模型。
-
在模型檢視中,您可以看到訓練已開始,而且您可以檢視模型版本的 Status
欄來檢查目前狀態。培訓模型需要一段時間才能完成。
-
訓練完成後,您可以評估其效能。如需詳細資訊,請參閱改善您的 Amazon Lookout for Vision 模型。
訓練模型 (SDK)
您可以使用 CreateModel操作來開始模型的訓練、測試和評估。Amazon Lookout for Vision 會使用與專案相關聯的訓練和測試資料集來訓練模型。如需詳細資訊,請參閱建立專案 (SDK)。
每次呼叫 時CreateModel
,都會建立新的模型版本。的回應CreateModel
包含模型的 版本。
每個成功的模型訓練都會向您收取費用。使用 ClientToken
輸入參數協助防止使用者重複不必要的或意外的模型訓練而產生費用。 ClientToken
是一種等冪輸入參數,可確保特定參數集CreateModel
只完成一次 — CreateModel
具有相同ClientToken
值的 重複呼叫可確保訓練不會重複。如果您未提供 的值ClientToken
,AWSSDK則您使用的 會為您插入值。這可防止網路錯誤開始多個訓練任務後重試,但您需要為自己的使用案例提供自己的值。如需詳細資訊,請參閱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 模型。