

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# SageMaker 画像分類 - TensorFlow アルゴリズムの使用方法
<a name="IC-TF-how-to-use"></a>

画像分類 - TensorFlow は Amazon SageMaker AI の組み込みアルゴリズムとして使用できます。次のセクションでは、SageMaker AI Python SDK で画像分類 - TensorFlow を使用する方法について説明します。Amazon SageMaker Studio Classic UI から画像分類 - 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 を取得します。次に、必要に応じてハイパーパラメータを変更します。使用可能なすべてのハイパーパラメータとそのデフォルト値の Python ディクショナリは、`hyperparameters.retrieve_default` で確認できます。詳細については、「[画像分類 - TensorFlow ハイパーパラメータ](IC-TF-Hyperparameter.md)」を参照してください。これらの値を使用して SageMaker AI 推定器を構築します。

**注記**  
デフォルトのハイパーパラメータ値はモデルによって異なります。モデルが大きくなると、デフォルトのバッチサイズは小さくなり、`train_only_top_layer` ハイパーパラメータは `"True"` に設定されます。

この例では、5 つのクラスの花の画像を含む [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)
```