Prenos učenja za modele klasifikacije slik TensorFlow v Amazon SageMaker

Amazon SageMaker ponuja zbirko vgrajeni algoritmi, predhodno usposobljeni modeliin vnaprej izdelane predloge rešitev za pomoč podatkovnim znanstvenikom in izvajalcem strojnega učenja (ML), da hitro začnejo z usposabljanjem in uvajanjem modelov ML. Te algoritme in modele lahko uporabite za nadzorovano in nenadzorovano učenje. Obdelujejo lahko različne vrste vhodnih podatkov, vključno s tabelarnimi, slikovnimi in besedilnimi.

Starting today, SageMaker provides a new built-in algorithm for image classification: Image Classification – TensorFlow. It is a supervised learning algorithm that supports transfer learning for many pre-trained models available in Središče TensorFlow. It takes an image as input and outputs probability for each of the class labels. You can fine-tune these pre-trained models using transfer learning even when a large number of training images aren’t available. It’s available through the SageMaker vgrajeni algoritmi kot tudi prek Uporabniški vmesnik SageMaker JumpStart v notranjosti Amazon SageMaker Studio. For more information, refer to its documentation Image Classification – TensorFlow in primer zvezka Introduction to SageMaker TensorFlow – Image Classification.

Image classification with TensorFlow in SageMaker provides transfer learning on many pre-trained models available in TensorFlow Hub. According to the number of class labels in the training data, a classification layer is attached to the pre-trained TensorFlow Hub model. The classification layer consists of a dropout layer and a dense layer, which is a fully connected layer with 2-norm regularizer that is initialized with random weights. The model training has hyperparameters for the dropout rate of the dropout layer and the L2 regularization factor for the dense layer. Then either the whole network, including the pre-trained model, or only the top classification layer can be fine-tuned on the new training data. In this transfer learning mode, you can achieve training even with a smaller dataset.

How to use the new TensorFlow image classification algorithm

This section describes how to use the TensorFlow image classification algorithm with the SDK SageMaker Python. Za informacije o tem, kako ga uporabljati iz uporabniškega vmesnika Studio, glejte SageMaker JumpStart.

Algoritem podpira prenos učenja za predhodno usposobljene modele, navedene v TensorFlow Hub Models. Vsak model je označen z edinstveno oznako model_id. The following code shows how to fine-tune MobileNet V2 1.00 224 identified by model_id tensorflow-ic-imagenet-mobilenet-v2-100-224-classification-4 na naboru podatkov za usposabljanje po meri. Za vsakogar model_id, da bi prek Ocenjevalnik class of the SageMaker Python SDK, you need to fetch the Docker image URI, training script URI, and pre-trained model URI through the utility functions provided in SageMaker. The training script URI contains all the necessary code for data processing, loading the pre-trained model, model training, and saving the trained model for inference. The pre-trained model URI contains the pre-trained model architecture definition and the model parameters. Note that the Docker image URI and the training script URI are the same for all the TensorFlow image classification models. The pre-trained model URI is specific to the particular model. The pre-trained model tarballs have been pre-downloaded from TensorFlow Hub and saved with the appropriate model signature in Preprosta storitev shranjevanja Amazon (Amazon S3), tako da se usposabljanje izvaja v izolaciji omrežja. Oglejte si naslednjo kodo:

from sagemaker import image_uris, model_uris, script_uris
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 pre-trained model tarball for transfer learning
train_model_uri = model_uris.retrieve(model_id=model_id, model_version=model_version, model_scope="training")

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

S temi vadbenimi artefakti, specifičnimi za model, lahko sestavite predmet Ocenjevalnik razred:

# 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,
)

Nato boste za prenos učenja na svojem naboru podatkov po meri morda morali spremeniti privzete vrednosti hiperparametrov usposabljanja, ki so navedeni v Hiperparametri. S klicem lahko pridobite slovar Python teh hiperparametrov z njihovimi privzetimi vrednostmi hyperparameters.retrieve_default, jih po potrebi posodobite in jih nato posredujte razredu Estimator. Upoštevajte, da so privzete vrednosti nekaterih hiperparametrov različne za različne modele. Pri velikih modelih je privzeta velikost serije manjša in train_only_top_layer hiperparameter je nastavljen na True. Hiperparameter Train_only_top_layer določa, kateri parametri modela se spreminjajo med postopkom natančnega prilagajanja. če train_only_top_layer is True, se parametri klasifikacijskih slojev spremenijo, preostali parametri pa med postopkom natančnega prilagajanja ostanejo nespremenjeni. Po drugi strani, če train_only_top_layer is False, so vsi parametri modela natančno nastavljeni. Oglejte si naslednjo kodo:

from sagemaker import hyperparameters
# 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 following code provides a default training dataset hosted in S3 buckets. We provide the tf_flowers dataset as a default dataset for fine-tuning the models. The dataset comprises images of five types of flowers. The dataset has been downloaded from TensorFlow pod Licenca Apache 2.0.

# Sample training data is available in this 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}"

Končno, če želite zagnati nalogo usposabljanja SageMaker za natančno nastavitev modela, pokličite .fit na objektu razreda Estimator, medtem ko posreduje lokacijo S3 nabora podatkov za usposabljanje:

# Launch a SageMaker Training job by passing s3 path of the training data
tf_ic_estimator.fit({"training": training_dataset_s3_path}, logs=True)

For more information about how to use the new SageMaker TensorFlow image classification algorithm for transfer learning on a custom dataset, deploy the fine-tuned model, run inference on the deployed model, and deploy the pre-trained model as is without first fine-tuning on a custom dataset, see the following example notebook: Introduction to SageMaker TensorFlow – Image Classification.

Input/output interface for the TensorFlow image classification algorithm

Vsak od predhodno usposobljenih modelov, navedenih v TensorFlow Hub Models kateremu koli danemu naboru podatkov, ki vsebuje slike, ki pripadajo poljubnemu številu razredov. Cilj je zmanjšati napako napovedi vhodnih podatkov. Model, vrnjen s fino nastavitvijo, je mogoče nadalje uporabiti za sklepanje. Sledijo navodila, kako naj bodo podatki o usposabljanju oblikovani za vnos v model:

  • vhod – A directory with as many sub-directories as the number of classes. Each sub-directory should have images belonging to that class in .jpg, .jpeg, or .png format.
  • izhod – A fine-tuned model that can be deployed for inference or can be further trained using incremental training. A preprocessing and postprocessing signature is added to the fine-tuned model such that it takes raw .jpg image as input and returns class probabilities. A file mapping class indexes to class labels is saved along with the models.

The input directory should look like the following example if the training data contains images from two classes: roses in dandelion. The S3 path should look like s3://bucket_name/input_directory/. Upoštevajte zaostanek / is required. The names of the folders and roses, dandelion, and the .jpg filenames can be anything. The label mapping file that is saved along with the trained model on the S3 bucket maps the folder names roses and dandelion to the indexes in the list of class probabilities the model outputs. The mapping follows alphabetical ordering of the folder names. In the following example, index 0 in the model output list corresponds to dandelion, and index 1 corresponds to roses.

input_directory
    |--roses
        |--abc.jpg
        |--def.jpg
    |--dandelion
        |--ghi.jpg
        |--jkl.jpg

Inference with the TensorFlow image classification algorithm

Ustvarjene modele je mogoče gostiti za sklepanje in podpirati kodirane slikovne formate .jpg, .jpeg in .png kot application/x-image content type. The input image is resized automatically. The output contains the probability values, the class labels for all classes, and the predicted label corresponding to the class index with the highest probability, encoded in JSON format. The TensorFlow image classification model processes a single image per request and outputs only one line in the JSON. The following is an example of a response in JSON:

accept: application/json;verbose

 {"probabilities": [prob_0, prob_1, prob_2, ...],
  "labels":        [label_0, label_1, label_2, ...],
  "predicted_label": predicted_label}

If accept nastavljena na application/json, then the model only outputs probabilities. For more details on training and inference, see the sample notebook Introduction to SageMaker TensorFlow – Image Classification.

Uporabite vgrajene algoritme SageMaker prek uporabniškega vmesnika JumpStart

You can also use SageMaker TensorFlow image classification and any of the other built-in algorithms with a few clicks via the JumpStart UI. JumpStart is a SageMaker feature that allows you to train and deploy built-in algorithms and pre-trained models from various ML frameworks and model hubs through a graphical interface. It also allows you to deploy fully fledged ML solutions that string together ML models and various other AWS services to solve a targeted use case. Check out Zaženite klasifikacijo besedila z Amazon SageMaker JumpStart z uporabo modelov TensorFlow Hub in Hugging Face to find out how to use JumpStart to train an algorithm or pre-trained model in a few clicks.

zaključek

In this post, we announced the launch of the SageMaker TensorFlow image classification built-in algorithm. We provided example code on how to do transfer learning on a custom dataset using a pre-trained model from TensorFlow Hub using this algorithm. For more information, check out Dokumentacija in primer zvezek.


O avtorjih

Transfer learning for TensorFlow image classification models in Amazon SageMaker PlatoBlockchain Data Intelligence. Vertical Search. Ai.Dr. Ashish Khetan je višji aplikativni znanstvenik z Vgrajeni algoritmi Amazon SageMaker and helps develop machine learning algorithms. He got his PhD from University of Illinois Urbana-Champaign. He is an active researcher in machine learning and statistical inference, and has published many papers in NeurIPS, ICML, ICLR, JMLR, ACL, and EMNLP conferences.

Transfer learning for TensorFlow image classification models in Amazon SageMaker PlatoBlockchain Data Intelligence. Vertical Search. Ai.dr. Vivek Madan je uporabni znanstvenik z Ekipa Amazon SageMaker JumpStart. He got his PhD from University of Illinois Urbana-Champaign and was a Post Doctoral Researcher at Georgia Tech. He is an active researcher in machine learning and algorithm design, and has published papers in EMNLP, ICLR, COLT, FOCS, and SODA conferences.

Transfer learning for TensorFlow image classification models in Amazon SageMaker PlatoBlockchain Data Intelligence. Vertical Search. Ai.João Moura je specialist za rešitve AI/ML pri Amazon Web Services. Večinoma se osredotoča na primere uporabe NLP in pomaga strankam optimizirati usposabljanje in uvajanje modela globokega učenja. Je tudi aktiven zagovornik rešitev ML z nizko kodo in strojne opreme, specializirane za ML.

Transfer learning for TensorFlow image classification models in Amazon SageMaker PlatoBlockchain Data Intelligence. Vertical Search. Ai.Raju Penmatcha je višji arhitekt AI / ML specialističnih rešitev pri AWS. Sodeluje z izobraževalnimi, vladnimi in neprofitnimi strankami pri projektih strojnega učenja in umetne inteligence ter jim pomaga pri gradnji rešitev z uporabo AWS. Kadar strankam ne pomaga, rad potuje v nove kraje.

Časovni žig:

Več od Strojno učenje AWS