翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
SageMaker 画像分類 - TensorFlow アルゴリズムの使用方法
画像分類 - TensorFlow は Amazon SageMaker AI の組み込みアルゴリズムとして使用できます。次のセクションでは、SageMaker AI Python SDK で画像分類 - TensorFlow を使用する方法について説明します。Amazon SageMaker Studio Classic UI から画像分類 - TensorFlow を使用する方法については、「SageMaker JumpStart の事前トレーニング済みモデル」を参照してください。
画像分類 - TensorFlow アルゴリズムは、互換性のある事前トレーニング済みの TensorFlow Hub モデルのいずれかを使用した転移学習をサポートします。使用可能なすべての事前トレーニング済みモデルのリストについては、「TensorFlow Hub モデル」を参照してください。事前トレーニング済みのモデルには一意の model_id があります。次の例では、MobileNet V2 1.00 224 (model_id: tensorflow-ic-imagenet-mobilenet-v2-100-224-classification-4) を使用してカスタムデータセットを微調整しています。事前トレーニング済みのモデルはすべて TensorFlow Hub から事前にダウンロードされ、Amazon S3 バケットに保存されるため、トレーニングジョブはネットワークから切り離された状態で実行できます。これらの事前に生成されたモデルトレーニングアーティファクトを使用して、SageMaker AI ModelTrainer を構築します。
まず、Docker イメージ URI、トレーニングスクリプト URI、および事前トレーニング済みのモデル URI を取得します。次に、必要に応じてハイパーパラメータを変更します。使用可能なすべてのハイパーパラメータとそのデフォルト値の Python ディクショナリは、hyperparameters.retrieve_default で確認できます。詳細については、「画像分類 - TensorFlow ハイパーパラメータ」を参照してください。これらの値を使用して SageMaker AI ModelTrainer を作成します。
注記
デフォルトのハイパーパラメータ値はモデルによって異なります。モデルが大きくなると、デフォルトのバッチサイズは小さくなり、train_only_top_layer ハイパーパラメータは "True" に設定されます。
この例では、5 つのクラスの花の画像を含む tf_flowers.fit を呼び出します。
from sagemaker.core import image_uris from sagemaker.core import model_uris, script_uris, hyperparameters from sagemaker.train import ModelTrainer from sagemaker.train.configs import InputData from sagemaker.train.configs import SourceCode, Compute, StoppingCondition, OutputDataConfig 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 ModelTrainer instance tf_ic_model_trainer = ModelTrainer( role=aws_role, training_image=train_image_uri, source_code=SourceCode(source_dir=train_source_uri, entry_script="transfer_learning.py"), # In V3, pre-trained model artifacts are passed via input_data_config compute=Compute(instance_type=training_instance_type, instance_count=1), stopping_condition=StoppingCondition(max_runtime_in_seconds=360000), hyperparameters=hyperparameters, output_data_config=OutputDataConfig(s3_output_path=s3_output_location), ) # Use S3 path of the training data to launch SageMaker TrainingJob tf_ic_model_trainer.train( input_data_config=[ InputData(channel_name="training", data_source=training_dataset_s3_path), InputData(channel_name="model", data_source=train_model_uri), ] )