

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

# SageMaker AI オブジェクト検出 - TensorFlow アルゴリズムの使用方法
<a name="object-detection-tensorflow-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 モデルのいずれかを使用した転移学習をサポートします。使用可能なすべての事前トレーニング済みモデルのリストについては、「[TensorFlow モデル](object-detection-tensorflow-Models.md)」を参照してください。事前トレーニング済みのモデルにはそれぞれ一意の `model_id` があります。次の例では、ResNet50 (`model_id`: `tensorflow-od1-ssd-resnet50-v1-fpn-640x640-coco17-tpu-8`) を使用してカスタムデータセットを微調整しています。事前トレーニング済みモデルはすべて TensorFlow ハブから事前にダウンロードされ、Amazon S3 バケットに保存されているため、トレーニングジョブはネットワーク分離状態で実行できます。事前に生成されたこれらのモデルトレーニングアーティファクトを使用して SageMaker AI 推定器を構築します。

まず、Docker イメージ URI、トレーニングスクリプト URI、および事前トレーニング済みのモデル URI を取得します。次に、必要に応じてハイパーパラメータを変更します。使用可能なすべてのハイパーパラメータとそのデフォルト値の Python ディクショナリは、`hyperparameters.retrieve_default` で確認できます。詳細については、「[オブジェクト検出 - TensorFlow ハイパーパラメータ](object-detection-tensorflow-Hyperparameter.md)」を参照してください。これらの値を使用して SageMaker AI 推定器を構築します。

**注記**  
デフォルトのハイパーパラメータ値はモデルによって異なります。例えば、モデルが大きくなると、デフォルトのエポック数は小さくなります。

この例では、道を歩く歩行者の画像を含む [https://www.cis.upenn.edu/~jshi/ped_html/#pub1](https://www.cis.upenn.edu/~jshi/ped_html/#pub1) データセットを使用しています。データセットを事前にダウンロードして、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-od1-ssd-resnet50-v1-fpn-640x640-coco17-tpu-8", "*"
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 hyperparameters 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"

# Sample training data is available in this bucket
training_data_bucket = f"jumpstart-cache-prod-{aws_region}"
training_data_prefix = "training-datasets/PennFudanPed_COCO_format/"

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

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

# Create an Estimator instance
tf_od_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,
)

# Launch a training job
tf_od_estimator.fit({"training": training_dataset_s3_path}, logs=True)
```

SageMaker AI オブジェクト検出 - TensorFlow アルゴリズムを使用してカスタムデータセットの転移学習を行う方法の詳細については、「[Introduction to SageMaker TensorFlow - Object Detection](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/object_detection_tensorflow/Amazon_Tensorflow_Object_Detection.ipynb)」ノートブックを参照してください。