

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

# 標記模型
<a name="tm-tagging-model"></a>

您可以使用標籤來識別、整理、搜尋和篩選 Amazon Rekognition 模型。每個標籤都是由使用者定義的金鑰和值組成的標籤。例如，為有助於確定模型的計費方式，請使用 `Cost center` 金鑰標記模型，並新增適當的成本中心編號作為值。如需詳細資訊，請參閱[標記 AWS 資源](https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html)。

使用標籤來：
+ 使用成本分配標籤追蹤模型的計費。如需詳細資訊，請參閱[使用成本分配標籤](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html)。
+ 使用 Identity and Access Management (IAM) 控制對模型的存取。如需詳細資訊，請參閱[使用資源標籤來控制對 AWS 資源的存取權](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_tags.html)。
+ 自動化模型管理。例如，您可以執行自動化啟動或停止指令碼，在非上班時間關閉開發模型以降低成本。如需詳細資訊，請參閱[執行培訓過的 Amazon Rekognition 自訂標籤模型](running-model.md)。

您可以使用 Amazon Rekognition 主控台或使用 AWS SDKs 來標記模型。

**Topics**
+ [標記模型 (主控台)](#tm-tagging-model-console)
+ [檢視模型標籤](#tm-tagging-model-viewing-console)
+ [標記模型 (SDK)](#tm-tagging-model-sdk)

## 標記模型 (主控台)
<a name="tm-tagging-model-console"></a>

您可以使用 Rekognition 主控台為模型新增標籤、檢視連接至模型的標籤，以及移除標籤。

### 新增或移除標籤
<a name="tm-tagging-model-add-remove-console"></a>

此程序會說明如何新增標籤至現有模型或從中移除標籤。您也可以在訓練過模型後將標籤新增到新模型。如需詳細資訊，請參閱[培訓 Amazon Rekognition 自訂標籤模型](training-model.md)。

**使用主控台將標籤新增至現有模型或從中移除標籤**

1. 開啟 Amazon Rekognition 主控台：[https://console.aws.amazon.com/rekognition/]( https://console.aws.amazon.com/rekognition/)。

1. 選擇**開始使用**。

1. 在導覽窗格中，選擇**專案**。

1. 在**專案**資源頁面上，選擇包含您要標記的模型的專案。

1. 在導覽窗格中，於您先前選擇的專案下選擇**模型**。

1. 在**模型**區段中，選擇您要對其新增標籤的模型。

1. 在模型詳細資訊頁面上，選擇**標籤**索引標籤。

1. 在**標籤**區段中，選擇**管理標籤**。

1. 在**管理標籤** 頁面上，選擇**新增標籤**。

1. 輸入金鑰和值。

   1. 在**金鑰**中，輸入金鑰名稱。

   1. 在**值**中，輸入值。

1. 若要新增更多標籤，請重複步驟 9 和 10。

1. (選用) 若要移除標籤，請選擇要移除的標籤旁的**移除**。如果您要移除先前儲存的標籤，則會在您儲存變更時移除該標籤。

1. 請選擇**儲存變更**，以儲存您所做的變更。

## 檢視模型標籤
<a name="tm-tagging-model-viewing-console"></a>

您可以使用 Amazon Rekognition 主控台來檢視連接至模型的標籤。

若要檢視連接至*專案內所有模型*的標籤，您必須使用 AWS SDK。如需詳細資訊，請參閱[列出模型標籤](#listing-model-tags-sdk)。

**檢視連接至模型的標籤**

1. 開啟 Amazon Rekognition 主控台：[https://console.aws.amazon.com/rekognition/]( https://console.aws.amazon.com/rekognition/)。

1. 選擇**開始使用**。

1. 在導覽窗格中，選擇**專案**。

1. 在**專案**資源頁面上，選擇包含您要檢視的模型的專案。

1. 在導覽窗格中，於您先前選擇的專案下選擇**模型**。

1. 在**模型**區段中，選擇您要檢視其標籤的模型。

1. 在模型詳細資訊頁面上，選擇**標籤**索引標籤。**標籤**區段中隨即顯示標籤。

## 標記模型 (SDK)
<a name="tm-tagging-model-sdk"></a>

您可以使用 AWS SDK 來：
+ 將標籤新增到新模型
+ 將標籤新增到現有模型
+ 列出連接至模型的標籤。
+ 從模型移除標籤 

下列 AWS CLI 範例中的標籤格式如下。

```
--tags '{"key1":"value1","key2":"value2"}' 
```

或者，您可以使用此格式。

```
--tags key1=value1,key2=value2
```

如果您尚未安裝 AWS CLI，請參閱 [步驟 4：設定 AWS CLI 和 AWS SDKs](su-awscli-sdk.md)。

### 將標籤新增到新模型
<a name="tagging-new-model-sdk"></a>

當您使用 [CreateProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_CreateProjectVersion.html) 操作建立模型時，您可以將標籤新增至該模型。在 `Tags` 陣列輸入參數中指定一或多個標籤。

```
aws rekognition create-project-version --project-arn project arn \
  --version-name version_name \
  --output-config '{ "S3Location": { "Bucket": "output bucket", "Prefix":  "output folder" } }' \
  --tags '{"key1":"value1","key2":"value2"}' \
  --profile custom-labels-access
```

如需建立及訓練模型的資訊，請參閱 [培訓模型 (SDK)](training-model.md#tm-sdk)。

### 將標籤新增到現有模型
<a name="tagging-new-model-sdk"></a>

若要將一或多個標籤新增到現有模型，請使用 [TagResource](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_TagResource) 操作。指定模型的 Amazon Resource Name (ARN) (`ResourceArn`) 和您要新增的標籤 (`Tags`)。下列範例會說明如何新增兩個標籤。

```
aws rekognition tag-resource --resource-arn resource-arn \
  --tags '{"key1":"value1","key2":"value2"}' \
  --profile custom-labels-access
```

呼叫 [CreateProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_CreateProjectVersion)，即可取得模型的 ARN。

### 列出模型標籤
<a name="listing-model-tags-sdk"></a>

若要列出連接至模型的標籤，請使用 [ListTagsForResource](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_ListTagsForResource) 操作並指定模型的 ARN (`ResourceArn`)。回應是連接至指定模型之標籤金鑰和值的對應。

```
aws rekognition list-tags-for-resource --resource-arn resource-arn \
  --profile custom-labels-access
```

輸出會顯示連接至模型的標籤清單。

```
{
    "Tags": {
        "Dept": "Engineering",
        "Name": "Ana Silva Carolina",
        "Role": "Developer"
    }
}
```

若要查看專案中哪些模型具有特定標籤，請呼叫 `DescribeProjectVersions` 以取得模型清單。然後在 `DescribeProjectVersions` 的回應中呼叫每個模型的 `ListTagsForResource`。檢查 `ListTagsForResource` 的回應，以查看是否存在所需的標籤。

下面的 Python 3 範例會說明如何搜尋所有專案的特定標籤金鑰和值。輸出包括專案 ARN 和模型 ARN，其中找到一個相符的金鑰。

**搜尋標籤值**

1. 將下列程式碼儲存到名為 `find_tag.py` 的檔案。

   ```
   # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   # SPDX-License-Identifier: Apache-2.0
   """
   Purpose
   Shows how to find a tag value that's associated with models within
   your Amazon Rekognition Custom Labels projects.
   """
   import logging
   import argparse
   import boto3
   
   from botocore.exceptions import ClientError
   
   
   logger = logging.getLogger(__name__)
   
   
   def find_tag_in_projects(rekognition_client, key, value):
       """
       Finds Amazon Rekognition Custom Label models tagged with the supplied key and key value.
       :param rekognition_client: An Amazon Rekognition boto3 client.
       :param key: The tag key to find.
       :param value: The value of the tag that you want to find.
       return: A list of matching model versions (and model projects) that were found.
       """
       try:
   
           found_tags = []
           found = False
   
           projects = rekognition_client.describe_projects()
           # Iterate through each project and models within a project.
           for project in projects["ProjectDescriptions"]:
               logger.info("Searching project: %s ...", project["ProjectArn"])
   
               models = rekognition_client.describe_project_versions(
                   ProjectArn=(project["ProjectArn"])
               )
   
               for model in models["ProjectVersionDescriptions"]:
                   logger.info("Searching model %s", model["ProjectVersionArn"])
   
                   tags = rekognition_client.list_tags_for_resource(
                       ResourceArn=model["ProjectVersionArn"]
                   )
   
                   logger.info(
                       "\tSearching model: %s for tag: %s value: %s.",
                       model["ProjectVersionArn"],
                       key,
                       value,
                   )
                   # Check if tag exists.
   
                   if key in tags["Tags"]:
                       if tags["Tags"][key] == value:
                           found = True
                           logger.info(
                               "\t\tMATCH: Project: %s: model version %s",
                               project["ProjectArn"],
                               model["ProjectVersionArn"],
                           )
                           found_tags.append(
                               {
                                   "Project": project["ProjectArn"],
                                   "ModelVersion": model["ProjectVersionArn"],
                               }
                           )
   
           if found is False:
               logger.info("No match for Tag %s with value %s.", key, value)
           return found_tags
       except ClientError as err:
           logger.info("Problem finding tags: %s. ", format(err))
           raise
   
   
   def main():
       """
       Entry point for example.
       """
       logging.basicConfig(level=logging.INFO,
                           format="%(levelname)s: %(message)s")
   
       # Set up command line arguments.
       parser = argparse.ArgumentParser(usage=argparse.SUPPRESS)
   
       parser.add_argument("tag", help="The tag that you want to find.")
       parser.add_argument("value", help="The tag value that you want to find.")
   
       args = parser.parse_args()
       key = args.tag
       value = args.value
   
       print(f"Searching your models for tag: {key} with value: {value}.")
   
   
       session = boto3.Session(profile_name='custom-labels-access')
       rekognition_client = session.client("rekognition")
   
       # Get tagged models for all projects.
       tagged_models = find_tag_in_projects(rekognition_client, key, value)
   
       print("Matched models\n--------------")
       if len(tagged_models) > 0:
           for model in tagged_models:
               print(
                   "Project: {project}\nModel version: {version}\n".format(
                       project=model["Project"], version=model["ModelVersion"]
                   )
               )
   
       else:
           print("No matches found.")
   
       print("Done.")
   
   
   if __name__ == "__main__":
       main()
   ```

1. 在命令提示中，輸入以下內容。使用您要尋找的金鑰名稱和金鑰值取代*金鑰*和*值*。

   ```
   python find_tag.py key value
   ```

### 從模型刪除標籤
<a name="tm-removing-a-tag-sdk"></a>

若要從模型移除一或多個標籤，請使用 [UntagResource](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_UntagResource) 操作。指定要移除的模型 (`ResourceArn`) 和標籤金鑰 (`Tag-Keys`) 的 ARN。

```
aws rekognition untag-resource --resource-arn resource-arn \
  --tag-keys '["key1","key2"]' \
  --profile custom-labels-access
```

或者，您可以指定此格式中的 `tag-keys`。

```
--tag-keys key1,key2 
```