

# Image Classification - TensorFlow
<a name="image-classification-tensorflow"></a>

The Amazon SageMaker Image Classification - TensorFlow algorithm is a supervised learning algorithm that supports transfer learning with many pretrained models from the [TensorFlow Hub](https://tfhub.dev/s?fine-tunable=yes&module-type=image-classification&subtype=module,placeholder&tf-version=tf2). Use transfer learning to fine-tune one of the available pretrained models on your own dataset, even if a large amount of image data is not available. The image classification algorithm takes an image as input and outputs a probability for each provided class label. Training datasets must consist of images in .jpg, .jpeg, or .png format. This page includes information about Amazon EC2 instance recommendations and sample notebooks for Image Classification - TensorFlow.

**Topics**
+ [How to use the SageMaker Image Classification - TensorFlow algorithm](IC-TF-how-to-use.md)
+ [Input and output interface for the Image Classification - TensorFlow algorithm](IC-TF-inputoutput.md)
+ [Amazon EC2 instance recommendation for the Image Classification - TensorFlow algorithm](#IC-TF-instances)
+ [Image Classification - TensorFlow sample notebooks](#IC-TF-sample-notebooks)
+ [How Image Classification - TensorFlow Works](IC-TF-HowItWorks.md)
+ [TensorFlow Hub Models](IC-TF-Models.md)
+ [Image Classification - TensorFlow Hyperparameters](IC-TF-Hyperparameter.md)
+ [Tune an Image Classification - TensorFlow model](IC-TF-tuning.md)

# How to use the SageMaker Image Classification - TensorFlow algorithm
<a name="IC-TF-how-to-use"></a>

You can use Image Classification - TensorFlow as an Amazon SageMaker AI built-in algorithm. The following section describes how to use Image Classification - TensorFlow with the SageMaker AI Python SDK. For information on how to use Image Classification - TensorFlow from the Amazon SageMaker Studio Classic UI, see [SageMaker JumpStart pretrained models](studio-jumpstart.md).

The Image Classification - TensorFlow algorithm supports transfer learning using any of the compatible pretrained TensorFlow Hub models. For a list of all available pretrained models, see [TensorFlow Hub Models](IC-TF-Models.md). Every pretrained model has a unique `model_id`. The following example uses MobileNet V2 1.00 224 (`model_id`: `tensorflow-ic-imagenet-mobilenet-v2-100-224-classification-4`) to fine-tune on a custom dataset. The pretrained models are all pre-downloaded from the TensorFlow Hub and stored in Amazon S3 buckets so that training jobs can run in network isolation. Use these pre-generated model training artifacts to construct a SageMaker AI Estimator.

First, retrieve the Docker image URI, training script URI, and pretrained model URI. Then, change the hyperparameters as you see fit. You can see a Python dictionary of all available hyperparameters and their default values with `hyperparameters.retrieve_default`. For more information, see [Image Classification - TensorFlow Hyperparameters](IC-TF-Hyperparameter.md). Use these values to construct a SageMaker AI Estimator.

**Note**  
Default hyperparameter values are different for different models. For larger models, the default batch size is smaller and the `train_only_top_layer` hyperparameter is set to `"True"`.

This example uses the [https://www.tensorflow.org/datasets/catalog/tf_flowers](https://www.tensorflow.org/datasets/catalog/tf_flowers) dataset, which contains five classes of flower images. We pre-downloaded the dataset from TensorFlow under the Apache 2.0 license and made it available with Amazon S3. To fine-tune your model, call `.fit` using the Amazon S3 location of your training dataset.

```
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)
```

# Input and output interface for the Image Classification - TensorFlow algorithm
<a name="IC-TF-inputoutput"></a>

Each of the pretrained models listed in TensorFlow Hub Models can be fine-tuned to any dataset with any number of image classes. Be mindful of how to format your training data for input to the Image Classification - TensorFlow model.
+ **Training data input format:** Your training data should be a directory with as many subdirectories as the number of classes. Each subdirectory should contain images belonging to that class in .jpg, .jpeg, or .png format.

The following is an example of an input directory structure. This example dataset has two classes: `roses` and `dandelion`. The image files in each class folder can have any name. The input directory should be hosted in an Amazon S3 bucket with a path similar to the following: `s3://bucket_name/input_directory/`. Note that the trailing `/` is required.

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

Trained models output label mapping files that map class folder names to the indices in the list of output class probabilities. This mapping is in alphabetical order. For example, in the preceding example, the dandelion class is index 0 and the roses class is index 1. 

After training, you have a fine-tuned model that you can further train using incremental training or deploy for inference. The Image Classification - TensorFlow algorithm automatically adds a pre-processing and post-processing signature to the fine-tuned model so that it can take in images as input and return class probabilities. The file mapping class indices to class labels is saved along with the models. 

## Incremental training
<a name="IC-TF-incremental-training"></a>

You can seed the training of a new model with artifacts from a model that you trained previously with SageMaker AI. Incremental training saves training time when you want to train a new model with the same or similar data.

**Note**  
You can only seed a SageMaker Image Classification - TensorFlow model with another Image Classification - TensorFlow model trained in SageMaker AI. 

You can use any dataset for incremental training, as long as the set of classes remains the same. The incremental training step is similar to the fine-tuning step, but instead of starting with a pretrained model, you start with an existing fine-tuned model. For an example of incremental training with the SageMaker AI Image Classification - TensorFlow algorithm, see the [Introduction to SageMaker TensorFlow - Image Classification](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/image_classification_tensorflow/Amazon_TensorFlow_Image_Classification.ipynb) sample notebook.

## Inference with the Image Classification - TensorFlow algorithm
<a name="IC-TF-inference"></a>

You can host the fine-tuned model that results from your TensorFlow Image Classification training for inference. Any input image for inference must be in `.jpg`, .`jpeg`, or `.png` format and be content type `application/x-image`. The Image Classification - TensorFlow algorithm resizes input images automatically. 

Running inference results in probability values, class labels for all classes, and the predicted label corresponding to the class index with the highest probability encoded in JSON format. The Image Classification - TensorFlow model processes a single image per request and outputs only one line. The following is an example of a JSON format response:

```
accept: application/json;verbose

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

If `accept` is set to `application/json`, then the model only outputs probabilities. For more information on training and inference with the Image Classification - TensorFlow algorithm, see the [Introduction to SageMaker TensorFlow - Image Classification](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/image_classification_tensorflow/Amazon_TensorFlow_Image_Classification.ipynb) sample notebook.

## Amazon EC2 instance recommendation for the Image Classification - TensorFlow algorithm
<a name="IC-TF-instances"></a>

The Image Classification - TensorFlow algorithm supports all CPU and GPU instances for training, including:
+ `ml.p2.xlarge`
+ `ml.p2.16xlarge`
+ `ml.p3.2xlarge`
+ `ml.p3.16xlarge`
+ `ml.g4dn.xlarge`
+ `ml.g4dn.16.xlarge`
+ `ml.g5.xlarge`
+ `ml.g5.48xlarge`

We recommend GPU instances with more memory for training with large batch sizes. Both CPU (such as M5) and GPU (P2, P3, G4dn, or G5) instances can be used for inference.

## Image Classification - TensorFlow sample notebooks
<a name="IC-TF-sample-notebooks"></a>

For more information about how to use the SageMaker Image Classification - TensorFlow algorithm for transfer learning on a custom dataset, see the [Introduction to SageMaker TensorFlow - Image Classification](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/image_classification_tensorflow/Amazon_TensorFlow_Image_Classification.ipynb) notebook.

For instructions how to create and access Jupyter notebook instances that you can use to run the example in SageMaker AI, see [Amazon SageMaker notebook instances](nbi.md). After you have created a notebook instance and opened it, select the **SageMaker AI Examples** tab to see a list of all the SageMaker AI samples. To open a notebook, choose its **Use** tab and choose **Create copy**.

# How Image Classification - TensorFlow Works
<a name="IC-TF-HowItWorks"></a>

The Image Classification - TensorFlow algorithm takes an image as input and classifies it into one of the output class labels. Various deep learning networks such as MobileNet, ResNet, Inception, and EfficientNet are highly accurate for image classification. There are also deep learning networks that are trained on large image datasets, such as ImageNet, which has over 11 million images and almost 11,000 classes. After a network is trained with ImageNet data, you can then fine-tune the network on a dataset with a particular focus to perform more specific classification tasks. The Amazon SageMaker Image Classification - TensorFlow algorithm supports transfer learning on many pretrained models that are available in the TensorFlow Hub.

According to the number of class labels in your training data, a classification layer is attached to the pretrained TensorFlow Hub model of your choice. The classification layer consists of a dropout layer, a dense layer, and a fully-connected layer with 2-norm regularizer that is initialized with random weights. The model has hyperparameters for the dropout rate of the dropout layer and the L2 regularization factor for the dense layer. You can then fine-tune either the entire network (including the pretrained model) or only the top classification layer on new training data. With this method of transfer learning, training with smaller datasets is possible.

# TensorFlow Hub Models
<a name="IC-TF-Models"></a>

The following pretrained models are available to use for transfer learning with the Image Classification - TensorFlow algorithm. 

The following models vary significantly in size, number of model parameters, training time, and inference latency for any given dataset. The best model for your use case depends on the complexity of your fine-tuning dataset and any requirements that you have on training time, inference latency, or model accuracy.


| Model Name | `model_id` | Source | 
| --- | --- | --- | 
| MobileNet V2 1.00 224 | `tensorflow-ic-imagenet-mobilenet-v2-100-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/4) | 
| MobileNet V2 0.75 224 | `tensorflow-ic-imagenet-mobilenet-v2-075-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v2_075_224/classification/4) | 
| MobileNet V2 0.50 224 | `tensorflow-ic-imagenet-mobilenet-v2-050-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v2_050_224/classification/4) | 
| MobileNet V2 0.35 224 | `tensorflow-ic-imagenet-mobilenet-v2-035-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v2_035_224/classification/4) | 
| MobileNet V2 1.40 224 | `tensorflow-ic-imagenet-mobilenet-v2-140-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/4) | 
| MobileNet V2 1.30 224 | `tensorflow-ic-imagenet-mobilenet-v2-130-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4) | 
| MobileNet V2 | `tensorflow-ic-tf2-preview-mobilenet-v2-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4) | 
| Inception V3 | `tensorflow-ic-imagenet-inception-v3-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/inception_v3/classification/4) | 
| Inception V2 | `tensorflow-ic-imagenet-inception-v2-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/inception_v2/classification/4) | 
| Inception V1 | `tensorflow-ic-imagenet-inception-v1-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/inception_v1/classification/4) | 
| Inception V3 Preview | `tensorflow-ic-tf2-preview-inception-v3-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/tf2-preview/inception_v3/classification/4) | 
| Inception ResNet V2 | `tensorflow-ic-imagenet-inception-resnet-v2-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/inception_resnet_v2/classification/4) | 
| ResNet V2 50 | `tensorflow-ic-imagenet-resnet-v2-50-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/resnet_v2_50/classification/4) | 
| ResNet V2 101 | `tensorflow-ic-imagenet-resnet-v2-101-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/resnet_v2_101/classification/4) | 
| ResNet V2 152 | `tensorflow-ic-imagenet-resnet-v2-152-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/resnet_v2_152/classification/4) | 
| ResNet V1 50 | `tensorflow-ic-imagenet-resnet-v1-50-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/resnet_v1_50/classification/4) | 
| ResNet V1 101 | `tensorflow-ic-imagenet-resnet-v1-101-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/resnet_v1_101/classification/4) | 
| ResNet V1 152 | `tensorflow-ic-imagenet-resnet-v1-152-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/resnet_v1_152/classification/4) | 
| ResNet 50 | `tensorflow-ic-imagenet-resnet-50-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/resnet_50/classification/1) | 
| EfficientNet B0 | `tensorflow-ic-efficientnet-b0-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/efficientnet/b0/classification/1) | 
| EfficientNet B1 | `tensorflow-ic-efficientnet-b1-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/efficientnet/b1/classification/1) | 
| EfficientNet B2 | `tensorflow-ic-efficientnet-b2-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/efficientnet/b2/classification/1) | 
| EfficientNet B3 | `tensorflow-ic-efficientnet-b3-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/efficientnet/b3/classification/1) | 
| EfficientNet B4 | `tensorflow-ic-efficientnet-b4-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/efficientnet/b4/classification/1) | 
| EfficientNet B5 | `tensorflow-ic-efficientnet-b5-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/efficientnet/b5/classification/1) | 
| EfficientNet B6 | `tensorflow-ic-efficientnet-b6-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/efficientnet/b6/classification/1) | 
| EfficientNet B7 | `tensorflow-ic-efficientnet-b7-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/efficientnet/b7/classification/1) | 
| EfficientNet B0 Lite | `tensorflow-ic-efficientnet-lite0-classification-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/efficientnet/lite0/classification/2) | 
| EfficientNet B1 Lite | `tensorflow-ic-efficientnet-lite1-classification-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/efficientnet/lite1/classification/2) | 
| EfficientNet B2 Lite | `tensorflow-ic-efficientnet-lite2-classification-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/efficientnet/lite2/classification/2) | 
| EfficientNet B3 Lite | `tensorflow-ic-efficientnet-lite3-classification-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/efficientnet/lite3/classification/2) | 
| EfficientNet B4 Lite | `tensorflow-ic-efficientnet-lite4-classification-2` | [TensorFlow Hub link](https://tfhub.dev/tensorflow/efficientnet/lite4/classification/2) | 
| MobileNet V1 1.00 224 | `tensorflow-ic-imagenet-mobilenet-v1-100-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/classification/4) | 
| MobileNet V1 1.00 192 | `tensorflow-ic-imagenet-mobilenet-v1-100-192-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_100_192/classification/4) | 
| MobileNet V1 1.00 160 | `tensorflow-ic-imagenet-mobilenet-v1-100-160-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_100_160/classification/4) | 
| MobileNet V1 1.00 128 | `tensorflow-ic-imagenet-mobilenet-v1-100-128-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_100_128/classification/4) | 
| MobileNet V1 0.75 224 | `tensorflow-ic-imagenet-mobilenet-v1-075-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_075_224/classification/4) | 
| MobileNet V1 0.75 192 | `tensorflow-ic-imagenet-mobilenet-v1-075-192-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_075_192/classification/4) | 
| MobileNet V1 0.75 160 | `tensorflow-ic-imagenet-mobilenet-v1-075-160-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_075_160/classification/4) | 
| MobileNet V1 0.75 128 | `tensorflow-ic-imagenet-mobilenet-v1-075-128-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_075_128/classification/4) | 
| MobileNet V1 0.50 224 | `tensorflow-ic-imagenet-mobilenet-v1-050-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_050_224/classification/4) | 
| MobileNet V1 0.50 192 | `tensorflow-ic-imagenet-mobilenet-v1-050-192-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_050_192/classification/4) | 
| MobileNet V1 1.00 160 | `tensorflow-ic-imagenet-mobilenet-v1-050-160-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_050_160/classification/4) | 
| MobileNet V1 0.50 128 | `tensorflow-ic-imagenet-mobilenet-v1-050-128-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_050_128/classification/4) | 
| MobileNet V1 0.25 224 | `tensorflow-ic-imagenet-mobilenet-v1-025-224-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_025_224/classification/4) | 
| MobileNet V1 0.25 192 | `tensorflow-ic-imagenet-mobilenet-v1-025-192-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_025_192/classification/4) | 
| MobileNet V1 0.25 160 | `tensorflow-ic-imagenet-mobilenet-v1-025-160-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_025_160/classification/4) | 
| MobileNet V1 0.25 128 | `tensorflow-ic-imagenet-mobilenet-v1-025-128-classification-4` | [TensorFlow Hub link](https://tfhub.dev/google/imagenet/mobilenet_v1_025_128/classification/4) | 
| BiT-S R50x1 | `tensorflow-ic-bit-s-r50x1-ilsvrc2012-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/s-r50x1/ilsvrc2012_classification/1) | 
| BiT-S R50x3 | `tensorflow-ic-bit-s-r50x3-ilsvrc2012-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/s-r50x3/ilsvrc2012_classification/1) | 
| BiT-S R101x1 | `tensorflow-ic-bit-s-r101x1-ilsvrc2012-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/s-r101x1/ilsvrc2012_classification/1) | 
| BiT-S R101x3 | `tensorflow-ic-bit-s-r101x3-ilsvrc2012-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/s-r101x3/ilsvrc2012_classification/1) | 
| BiT-M R50x1 | `tensorflow-ic-bit-m-r50x1-ilsvrc2012-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/m-r50x1/ilsvrc2012_classification/1) | 
| BiT-M R50x3 | `tensorflow-ic-bit-m-r50x3-ilsvrc2012-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/m-r50x3/ilsvrc2012_classification/1) | 
| BiT-M R101x1 | `tensorflow-ic-bit-m-r101x1-ilsvrc2012-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/m-r101x1/ilsvrc2012_classification/1) | 
| BiT-M R101x3 | `tensorflow-ic-bit-m-r101x3-ilsvrc2012-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/m-r101x3/ilsvrc2012_classification/1) | 
| BiT-M R50x1 ImageNet-21k | `tensorflow-ic-bit-m-r50x1-imagenet21k-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/m-r50x1/imagenet21k_classification/1) | 
| BiT-M R50x3 ImageNet-21k | `tensorflow-ic-bit-m-r50x3-imagenet21k-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/m-r50x3/imagenet21k_classification/1) | 
| BiT-M R101x1 ImageNet-21k | `tensorflow-ic-bit-m-r101x1-imagenet21k-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/m-r101x1/imagenet21k_classification/1) | 
| BiT-M R101x3 ImageNet-21k | `tensorflow-ic-bit-m-r101x3-imagenet21k-classification-1` | [TensorFlow Hub link](https://tfhub.dev/google/bit/m-r101x3/imagenet21k_classification/1) | 

# Image Classification - TensorFlow Hyperparameters
<a name="IC-TF-Hyperparameter"></a>

Hyperparameters are parameters that are set before a machine learning model begins learning. The following hyperparameters are supported by the Amazon SageMaker AI built-in Image Classification - TensorFlow algorithm. See [Tune an Image Classification - TensorFlow model](IC-TF-tuning.md) for information on hyperparameter tuning. 


| Parameter Name | Description | 
| --- | --- | 
| augmentation |  Set to `"True"` to apply `augmentation_random_flip`, `augmentation_random_rotation`, and `augmentation_random_zoom` to the training data.  Valid values: string, either: (`"True"` or `"False"`). Default value: `"False"`.  | 
| augmentation\$1random\$1flip |  Indicates which flip mode to use for data augmentation when `augmentation` is set to `"True"`. For more information, see [RandomFlip](https://www.tensorflow.org/api_docs/python/tf/keras/layers/RandomFlip) in the TensorFlow documentation. Valid values: string, any of the following: (`"horizontal_and_vertical"`, `"vertical"`, or `"None"`). Default value: `"horizontal_and_vertical"`.  | 
| augmentation\$1random\$1rotation |  Indicates how much rotation to use for data augmentation when `augmentation` is set to `"True"`. Values represent a fraction of 2π. Positive values rotate counterclockwise while negative values rotate clockwise. `0` means no rotation. For more information, see [RandomRotation](https://www.tensorflow.org/api_docs/python/tf/keras/layers/RandomRotation) in the TensorFlow documentation. Valid values: float, range: [`-1.0`, `1.0`]. Default value: `0.2`.  | 
| augmentation\$1random\$1zoom |  Indicates how much vertical zoom to use for data augmentation when `augmentation` is set to `"True"`. Positive values zoom out while negative values zoom in. `0` means no zoom. For more information, see [RandomZoom](https://www.tensorflow.org/api_docs/python/tf/keras/layers/RandomZoom) in the TensorFlow documentation. Valid values: float, range: [`-1.0`, `1.0`]. Default value: `0.1`.  | 
| batch\$1size |  The batch size for training. For training on instances with multiple GPUs, this batch size is used across the GPUs.  Valid values: positive integer. Default value: `32`.  | 
| beta\$11 |  The beta1 for the `"adam"` optimizer. Represents the exponential decay rate for the first moment estimates. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.9`.  | 
| beta\$12 |  The beta2 for the `"adam"` optimizer. Represents the exponential decay rate for the second moment estimates. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.999`.  | 
| binary\$1mode |  When `binary_mode` is set to `"True"`, the model returns a single probability number for the positive class and can use additional `eval_metric` options. Use only for binary classification problems. Valid values: string, either: (`"True"` or `"False"`). Default value: `"False"`.  | 
| dropout\$1rate | The dropout rate for the dropout layer in the top classification layer. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.2` | 
| early\$1stopping |  Set to `"True"` to use early stopping logic during training. If `"False"`, early stopping is not used. Valid values: string, either: (`"True"` or `"False"`). Default value: `"False"`.  | 
| early\$1stopping\$1min\$1delta | The minimum change needed to qualify as an improvement. An absolute change less than the value of early\$1stopping\$1min\$1delta does not qualify as improvement. Used only when early\$1stopping is set to "True".Valid values: float, range: [`0.0`, `1.0`].Default value: `0.0`. | 
| early\$1stopping\$1patience |  The number of epochs to continue training with no improvement. Used only when `early_stopping` is set to `"True"`. Valid values: positive integer. Default value: `5`.  | 
| epochs |  The number of training epochs. Valid values: positive integer. Default value: `3`.  | 
| epsilon |  The epsilon for `"adam"`, `"rmsprop"`, `"adadelta"`, and `"adagrad"` optimizers. Usually set to a small value to avoid division by 0. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `1e-7`.  | 
| eval\$1metric |  If `binary_mode` is set to `"False"`, `eval_metric` can only be `"accuracy"`. If `binary_mode` is `"True"`, select any of the valid values. For more information, see [Metrics](https://www.tensorflow.org/api_docs/python/tf/keras/metrics) in the TensorFlow documentation. Valid values: string, any of the following: (`"accuracy"`, `"precision"`, `"recall"`, `"auc"`, or `"prc"`). Default value: `"accuracy"`.  | 
| image\$1resize\$1interpolation |  Indicates interpolation method used when resizing images. For more information, see [image.resize](https://www.tensorflow.org/api_docs/python/tf/image/resize) in the TensorFlow documentation. Valid values: string, any of the following: (`"bilinear"`, `"nearest"`, `"bicubic"`, `"area"`,` "lanczos3"` , `"lanczos5"`, `"gaussian"`, or `"mitchellcubic"`). Default value: `"bilinear"`.  | 
| initial\$1accumulator\$1value |  The starting value for the accumulators, or the per-parameter momentum values, for the `"adagrad"` optimizer. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.0001`.  | 
| label\$1smoothing |  Indicates how much to relax the confidence on label values. For example, if `label_smoothing` is `0.1`, then non-target labels are `0.1/num_classes `and target labels are `0.9+0.1/num_classes`.  Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.1`.  | 
| learning\$1rate | The optimizer learning rate. Valid values: float, range: [`0.0`, `1.0`].Default value: `0.001`. | 
| momentum |  The momentum for `"sgd"`, `"nesterov"`, and `"rmsprop"` optimizers. Ignored for other optimizers. Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.9`.  | 
| optimizer |  The optimizer type. For more information, see [Optimizers](https://www.tensorflow.org/api_docs/python/tf/keras/optimizers) in the TensorFlow documentation. Valid values: string, any of the following: (`"adam"`, `"sgd"`, `"nesterov"`, `"rmsprop"`,` "adagrad"` , `"adadelta"`). Default value: `"adam"`.  | 
| regularizers\$1l2 |  The L2 regularization factor for the dense layer in the classification layer.  Valid values: float, range: [`0.0`, `1.0`]. Default value: `.0001`.  | 
| reinitialize\$1top\$1layer |  If set to `"Auto"`, the top classification layer parameters are re-initialized during fine-tuning. For incremental training, top classification layer parameters are not re-initialized unless set to `"True"`. Valid values: string, any of the following: (`"Auto"`, `"True"` or `"False"`). Default value: `"Auto"`.  | 
| rho |  The discounting factor for the gradient of the `"adadelta"` and `"rmsprop"` optimizers. Ignored for other optimizers.  Valid values: float, range: [`0.0`, `1.0`]. Default value: `0.95`.  | 
| train\$1only\$1top\$1layer |  If `"True"`, only the top classification layer parameters are fine-tuned. If `"False"`, all model parameters are fine-tuned. Valid values: string, either: (`"True"` or `"False"`). Default value: `"False"`.  | 

# Tune an Image Classification - TensorFlow model
<a name="IC-TF-tuning"></a>

*Automatic model tuning*, also known as hyperparameter tuning, finds the best version of a model by running many jobs that test a range of hyperparameters on your dataset. You choose the tunable hyperparameters, a range of values for each, and an objective metric. You choose the objective metric from the metrics that the algorithm computes. Automatic model tuning searches the hyperparameters chosen to find the combination of values that result in the model that optimizes the objective metric.

For more information about model tuning, see [Automatic model tuning with SageMaker AI](automatic-model-tuning.md).

## Metrics computed by the Image Classification - TensorFlow algorithm
<a name="IC-TF-metrics"></a>

The image classification algorithm is a supervised algorithm. It reports an accuracy metric that is computed during training. When tuning the model, choose this metric as the objective metric.


| Metric Name | Description | Optimization Direction | 
| --- | --- | --- | 
| validation:accuracy | The ratio of the number of correct predictions to the total number of predictions made. | Maximize | 

## Tunable Image Classification - TensorFlow hyperparameters
<a name="IC-TF-tunable-hyperparameters"></a>

Tune an image classification model with the following hyperparameters. The hyperparameters that have the greatest impact on image classification objective metrics are: `batch_size`, `learning_rate`, and `optimizer`. Tune the optimizer-related hyperparameters, such as `momentum`, `regularizers_l2`, `beta_1`, `beta_2`, and `eps` based on the selected `optimizer`. For example, use `beta_1` and `beta_2` only when `adam` is the `optimizer`.

For more information about which hyperparameters are used for each `optimizer`, see [Image Classification - TensorFlow Hyperparameters](IC-TF-Hyperparameter.md).


| Parameter Name | Parameter Type | Recommended Ranges | 
| --- | --- | --- | 
| batch\$1size | IntegerParameterRanges | MinValue: 8, MaxValue: 512 | 
| beta\$11 | ContinuousParameterRanges | MinValue: 1e-6, MaxValue: 0.999 | 
| beta\$12 | ContinuousParameterRanges | MinValue: 1e-6, MaxValue: 0.999 | 
| eps | ContinuousParameterRanges | MinValue: 1e-8, MaxValue: 1.0 | 
| learning\$1rate | ContinuousParameterRanges | MinValue: 1e-6, MaxValue: 0.5 | 
| momentum | ContinuousParameterRanges | MinValue: 0.0, MaxValue: 0.999 | 
| optimizer | CategoricalParameterRanges | ['sgd', ‘adam’, ‘rmsprop’, 'nesterov', 'adagrad', 'adadelta'] | 
| regularizers\$1l2 | ContinuousParameterRanges | MinValue: 0.0, MaxValue: 0.999 | 
| train\$1only\$1top\$1layer | ContinuousParameterRanges | ['True', 'False'] | 