

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

# 如何使用 SageMaker 影像分類 - TensorFlow 演算法
<a name="IC-TF-how-to-use"></a>

您可以使用影像分類 - TensorFlow 做為 Amazon SageMaker AI 內建演算法。下一節描述如何將影像分類 - TensorFlow 搭配 SageMaker AI Python SDK 使用。如需如何從 Amazon SageMaker Studio Classic 使用者介面使用影像分類 - TensorFlow 的資訊，請參閱[SageMaker JumpStart 預先訓練模型](studio-jumpstart.md)。

影像分類 - TensorFlow 演算法支援使用所有相容的 TensorFlow Hub 預先訓練模型進行傳輸學習。如需所有可用的預先訓練模型清單，請參閱[TensorFlow Hub 模型](IC-TF-Models.md)。每個預先訓練的模型都有獨特的 `model_id`。下列範例會使用 MobileNet V2 1.00 224 (`model_id`:`tensorflow-ic-imagenet-mobilenet-v2-100-224-classification-4`) 以自訂資料集進行微調。預先訓練模型都是從 TensorFlow Hub 預先下載，並儲存在 Amazon S3 儲存貯體中，以便訓練工作可以在網路隔離下執行。使用這些預先產生的模型訓練成品，來建構 SageMaker AI 估算器。

首先，檢索 Docker 映像 URI，訓練指令碼 URI 和預先訓練的模型 URI。然後，視需要變更超參數。您可以使用 `hyperparameters.retrieve_default` 查看所有可用超參數及其預設數值的 Python 字典。如需詳細資訊，請參閱[影像分類 - TensorFlow 參數](IC-TF-Hyperparameter.md)。使用這些值來建構 SageMaker AI 估算器。

**注意**  
預設超參數值依不同的模型而異。對於較大的模型，預設批次大小較小，且超參數 `train_only_top_layer` 設定為 `"True"`。

此範例使用 [https://www.tensorflow.org/datasets/catalog/tf_flowers](https://www.tensorflow.org/datasets/catalog/tf_flowers) 資料集，其中包含五類花朵影像。我們根據 Apache 2.0 授權從 TensorFlow 預先下載資料集，並在 Amazon S3 上提供該資料集。若要微調模型，請使用您的訓練資料集的 Amazon S3 位置呼叫 `.fit`。

```
from sagemaker import image_uris, model_uris, script_uris, hyperparameters
from sagemaker.estimator import Estimator

model_id, model_version = {{"tensorflow-ic-imagenet-mobilenet-v2-100-224-classification-4"}}, "*"
training_instance_type = {{"ml.p3.2xlarge"}}

# Retrieve the Docker image
train_image_uri = image_uris.retrieve(model_id=model_id,model_version=model_version,image_scope="training",instance_type=training_instance_type,region=None,framework=None)

# Retrieve the training script
train_source_uri = script_uris.retrieve(model_id=model_id, model_version=model_version, script_scope="training")

# Retrieve the pretrained model tarball for transfer learning
train_model_uri = model_uris.retrieve(model_id=model_id, model_version=model_version, model_scope="training")

# Retrieve the default hyper-parameters for fine-tuning the model
hyperparameters = hyperparameters.retrieve_default(model_id=model_id, model_version=model_version)

# [Optional] Override default hyperparameters with custom values
hyperparameters["epochs"] = "5"

# The sample training data is available in the following S3 bucket
training_data_bucket = f"jumpstart-cache-prod-{aws_region}"
training_data_prefix = "training-datasets/tf_flowers/"

training_dataset_s3_path = f"s3://{training_data_bucket}/{training_data_prefix}"

output_bucket = sess.default_bucket()
output_prefix = "jumpstart-example-ic-training"
s3_output_location = f"s3://{output_bucket}/{output_prefix}/output"

# Create SageMaker Estimator instance
tf_ic_estimator = Estimator(
    role=aws_role,
    image_uri=train_image_uri,
    source_dir=train_source_uri,
    model_uri=train_model_uri,
    entry_point="transfer_learning.py",
    instance_count=1,
    instance_type=training_instance_type,
    max_run=360000,
    hyperparameters=hyperparameters,
    output_path=s3_output_location,
)

# Use S3 path of the training data to launch SageMaker TrainingJob
tf_ic_estimator.fit({"training": training_dataset_s3_path}, logs=True)
```