本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
刪除 Amazon Rekognition 自訂標籤模型
您可以使用 Amazon Rekognition 自訂標籤主控台或使用 DeleteProjectVersionAPI 來刪除模型。如果模型正在執行中或正在訓練,則無法刪除該模型。若要停止執行中的模型,請使用 StopProjectVersionAPI。如需詳細資訊,請參閱停止 Amazon Rekognition 自訂標籤模型 (SDK)。如果模型正在訓練,請等到完成後再刪除模型。
刪除的模型無法取消刪除。
刪除 Amazon Rekognition 自訂標籤模型 (主控台)
下列程序說明如何從專案詳細資訊頁面中刪除模型。您也可以從模型的詳細資訊頁面中刪除模型。
若要刪除模型 (主控台)
開啟亞馬遜重新認知主控台,網址為 https://console.aws.amazon.com/rekognition/。
-
選擇「使用自訂標籤」。
-
選擇 Get started (開始使用)。
-
在左側導覽窗格中選擇 Project (專案)。
-
選擇包含您要刪除之模型的專案。專案詳細資訊頁面隨即開啟。
-
在「模型」區段中,選取您要刪除的模型。
如果無法選取模型,表示模型正在執行或正在訓練,且無法刪除。檢查「狀態」(Status) 欄位,並在停止執行中的模型後再試一次,或等到訓練完成。
-
選擇刪除模型,將顯示刪除模型對話方塊。
-
輸入刪除以確認刪除。
-
選擇 Delete (刪除),刪除模型。刪除模型可能需要一段時間才能完成。
如果您在刪除模型期間關閉對話方塊,模型仍會被刪除。
刪除 Amazon Rekognition 自訂標籤模型 (SDK)
您可以呼叫DeleteProjectVersion並提供您想要刪除的模型的 Amazon Resource Name (ARN),以刪除 Amazon Resource Name (ARN)。您可以從 Amazon Rekognition 自訂標籤主控台的模型詳細資料頁面的「使用您的模型」區段取得模型 ARN。或者,調用DescribeProjectVersions並提供以下內容。
模型 ARN 是ProjectVersionDescription對象中的ProjectVersionArn
字段,來自DescribeProjectVersions
響應。
如果模型正在執行中或正在訓練,則無法刪除該模型。若要判斷模型是否正在執行或訓練,請呼叫DescribeProjectVersions並檢查模型ProjectVersionDescription物件的Status
欄位。若要停止執行中的模型,請使用 StopProjectVersionAPI。如需詳細資訊,請參閱停止 Amazon Rekognition 自訂標籤模型 (SDK)。您必須等待模型完成訓練,然後才能夠將其刪除。
若要刪除模型 (SDK)
-
若您尚未這樣做,請安裝AWS CLI並設定和AWS SDK。如需詳細資訊,請參閱步驟 4:設定 AWS CLI 以及 AWS SDKs。
-
使用下列程式碼刪除模型。
- AWS CLI
-
project-version-arn
將的值變更為您想要刪除的專案的名稱。
aws rekognition delete-project-version --project-version-arn model_arn
\
--profile custom-labels-access
- Python
-
提供下列命令列參數
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Purpose
Shows how to delete an existing Amazon Rekognition Custom Labels model.
"""
import argparse
import logging
import time
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
def find_forward_slash(input_string, n):
"""
Returns the location of '/' after n number of occurences.
:param input_string: The string you want to search
: n: the occurence that you want to find.
"""
position = input_string.find('/')
while position >= 0 and n > 1:
position = input_string.find('/', position + 1)
n -= 1
return position
def delete_model(rek_client, project_arn, model_arn):
"""
Deletes an Amazon Rekognition Custom Labels model.
:param rek_client: The Amazon Rekognition Custom Labels Boto3 client.
:param model_arn: The ARN of the model version that you want to delete.
"""
try:
# Delete the model
logger.info("Deleting dataset: {%s}", model_arn)
rek_client.delete_project_version(ProjectVersionArn=model_arn)
# Get the model version name
start = find_forward_slash(model_arn, 3) + 1
end = find_forward_slash(model_arn, 4)
version_name = model_arn[start:end]
deleted = False
# model might not be deleted yet, so wait deletion finishes.
while deleted is False:
describe_response = rek_client.describe_project_versions(ProjectArn=project_arn,
VersionNames=[version_name])
if len(describe_response['ProjectVersionDescriptions']) == 0:
deleted = True
else:
logger.info("Waiting for model deletion %s", model_arn)
time.sleep(5)
logger.info("model deleted: %s", model_arn)
return True
except ClientError as err:
logger.exception("Couldn't delete model - %s: %s",
model_arn, err.response['Error']['Message'])
raise
def add_arguments(parser):
"""
Adds command line arguments to the parser.
:param parser: The command line parser.
"""
parser.add_argument(
"project_arn", help="The ARN of the project that contains the model that you want to delete."
)
parser.add_argument(
"model_arn", help="The ARN of the model version that you want to delete."
)
def confirm_model_deletion(model_arn):
"""
Confirms deletion of the model. Returns True if delete entered.
:param model_arn: The ARN of the model that you want to delete.
"""
print(f"Are you sure you wany to delete model {model_arn} ?\n", model_arn)
start = input("Enter delete to delete your model: ")
if start == "delete":
return True
else:
return False
def main():
logging.basicConfig(level=logging.INFO,
format="%(levelname)s: %(message)s")
try:
# Get command line arguments.
parser = argparse.ArgumentParser(usage=argparse.SUPPRESS)
add_arguments(parser)
args = parser.parse_args()
if confirm_model_deletion(args.model_arn) is True:
print(f"Deleting model: {args.model_arn}")
# Delete the model.
session = boto3.Session(profile_name='custom-labels-access')
rekognition_client = session.client("rekognition")
delete_model(rekognition_client,
args.project_arn,
args.model_arn)
print(f"Finished deleting model: {args.model_arn}")
else:
print(f"Not deleting model {args.model_arn}")
except ClientError as err:
print(f"Problem deleting model: {err}")
if __name__ == "__main__":
main()
- Java V2
-
//Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-custom-labels-developer-guide/blob/master/LICENSE-SAMPLECODE.)
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
import software.amazon.awssdk.services.rekognition.model.DeleteProjectVersionRequest;
import software.amazon.awssdk.services.rekognition.model.DeleteProjectVersionResponse;
import software.amazon.awssdk.services.rekognition.model.DescribeProjectVersionsRequest;
import software.amazon.awssdk.services.rekognition.model.DescribeProjectVersionsResponse;
import software.amazon.awssdk.services.rekognition.model.RekognitionException;
public class DeleteModel {
public static final Logger logger = Logger.getLogger(DeleteModel.class.getName());
public static int findForwardSlash(String modelArn, int n) {
int start = modelArn.indexOf('/');
while (start >= 0 && n > 1) {
start = modelArn.indexOf('/', start + 1);
n -= 1;
}
return start;
}
public static void deleteMyModel(RekognitionClient rekClient, String projectArn, String modelArn)
throws InterruptedException {
try {
logger.log(Level.INFO, "Deleting model: {0}", projectArn);
// Delete the model
DeleteProjectVersionRequest deleteProjectVersionRequest = DeleteProjectVersionRequest.builder()
.projectVersionArn(modelArn).build();
DeleteProjectVersionResponse response =
rekClient.deleteProjectVersion(deleteProjectVersionRequest);
logger.log(Level.INFO, "Status: {0}", response.status());
// Get the model version
int start = findForwardSlash(modelArn, 3) + 1;
int end = findForwardSlash(modelArn, 4);
String versionName = modelArn.substring(start, end);
Boolean deleted = false;
DescribeProjectVersionsRequest describeProjectVersionsRequest = DescribeProjectVersionsRequest.builder()
.projectArn(projectArn).versionNames(versionName).build();
// Wait until model is deleted.
do {
DescribeProjectVersionsResponse describeProjectVersionsResponse = rekClient
.describeProjectVersions(describeProjectVersionsRequest);
if (describeProjectVersionsResponse.projectVersionDescriptions().size()==0) {
logger.log(Level.INFO, "Waiting for model deletion: {0}", modelArn);
Thread.sleep(5000);
} else {
deleted = true;
logger.log(Level.INFO, "Model deleted: {0}", modelArn);
}
} while (Boolean.FALSE.equals(deleted));
logger.log(Level.INFO, "Model deleted: {0}", modelArn);
} catch (
RekognitionException e) {
logger.log(Level.SEVERE, "Client error occurred: {0}", e.getMessage());
throw e;
}
}
public static void main(String args[]) {
final String USAGE = "\n" + "Usage: " + "<project_arn> <model_arn>\n\n" + "Where:\n"
+ " project_arn - The ARN of the project that contains the model that you want to delete.\n\n"
+ " model_version - The ARN of the model that you want to delete.\n\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String projectArn = args[0];
String modelVersion = args[1];
try {
RekognitionClient rekClient = RekognitionClient.builder().build();
// Delete the model
deleteMyModel(rekClient, projectArn, modelVersion);
System.out.println(String.format("model deleted: %s", modelVersion));
rekClient.close();
} catch (RekognitionException rekError) {
logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage());
System.exit(1);
}
catch (InterruptedException intError) {
logger.log(Level.SEVERE, "Exception while sleeping: {0}", intError.getMessage());
System.exit(1);
}
}
}