SageMaker AI 이미지 분류 - TensorFlow 알고리즘을 사용하는 방법 - Amazon SageMaker AI

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

SageMaker AI 이미지 분류 - 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 예측기를 구성합니다.

먼저 도커 이미지 URI, 훈련 스크립트 URI, 사전 훈련 모델 URI를 검색하세요. 그런 다음 상황에 맞게 하이퍼파라미터를 변경하세요. hyperparameters.retrieve_default를 사용하면 모든 가용 하이퍼파라미터와 해당 하이퍼파라미터의 기본값으로 구성된 Python 사전을 볼 수 있습니다. 자세한 내용은 이미지 분류 - 텐서플로우 하이퍼파라미터 단원을 참조하십시오. 이 값을 사용하여 SageMaker AI 예측기를 구성합니다.

참고

하이퍼파라미터의 기본값은 모델마다 다릅니다. 대형 모델의 경우 기본 배치 크기가 더 작고 train_only_top_layer 하이퍼파라미터는 "True"로 설정됩니다.

이 예제에서는 다섯 개의 꽃 이미지 클래스가 포함된 tf_flowers 데이터세트를 사용합니다. TensorFlow에서 Apache 2.0 라이선스에 따라 데이터세트를 사전 다운로드하고 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)