Image Classification - Quick Start

In this quick start, we’ll use the task of image classification to illustrate how to use AutoGluon’s APIs.

In this tutorial, we load images and the corresponding labels into AutoGluon and use this data to obtain a neural network that can classify new images. This is different from traditional machine learning where we need to manually define the neural network and then specify the hyperparameters in the training process. Instead, with just a single call to AutoGluon’s fit function, AutoGluon automatically trains many models with different hyperparameter configurations and returns the model that achieved the highest level of accuracy.

We begin by specifying ImageClassification as our task of interest as follows:

import autogluon as ag
from autogluon import ImageClassification as task

Create AutoGluon Dataset

For demonstration purposes, we use a subset of the Shopee-IET dataset from Kaggle. Each image in this data depicts a clothing item and the corresponding label specifies its clothing category. Our subset of the data contains the following possible labels: BabyPants, BabyShirt, womencasualshoes, womenchiffontop.

We download the data subset and unzip it using the following commands:

filename = ag.download('https://autogluon.s3.amazonaws.com/datasets/shopee-iet.zip')
ag.unzip(filename)
Downloading shopee-iet.zip from https://autogluon.s3.amazonaws.com/datasets/shopee-iet.zip...
100%|██████████| 40895/40895 [00:00<00:00, 70511.50KB/s]
'data'

After the dataset is downloaded, we load it into a `Dataset </api/autogluon.task.html#autogluon.task.ImageClassification.Dataset>`__ object:

dataset = task.Dataset('data/train')

Load the test dataset as follows:

test_dataset = task.Dataset('data/test', train=False)

If you don’t have a GPU, change the dataset to ‘FashionMNIST’ to ensure that it doesn’t take too long to run:

if ag.get_gpu_count() == 0:
    dataset = task.Dataset(name='FashionMNIST')
    test_dataset = task.Dataset(name='FashionMNIST', train=False)

Use AutoGluon to Fit Models

Now, we fit a classifier using AutoGluon as follows:

classifier = task.fit(dataset,
                      epochs=5,
                      ngpus_per_trial=1,
                      verbose=False)
Starting Experiments
Num of Finished Tasks is 0
Num of Pending Tasks is 2
scheduler: FIFOScheduler(
DistributedResourceManager{
(Remote: Remote REMOTE_ID: 0,
    <Remote: 'inproc://172.31.35.213/14964/1' processes=1 threads=8, memory=33.24 GB>, Resource: NodeResourceManager(8 CPUs, 1 GPUs))
})
HBox(children=(FloatProgress(value=0.0, max=2.0), HTML(value='')))
  0%|          | 0/5 [00:00<?, ?it/s]
[Epoch 1] training: accuracy=0.269:   0%|          | 0/5 [00:08<?, ?it/s]
[Epoch 1] Validation: 0.394:   0%|          | 0/5 [00:09<?, ?it/s]       
[Epoch 1] Validation: 0.394:  20%|██        | 1/5 [00:09<00:39,  9.76s/it]
[Epoch 2] training: accuracy=0.395:  20%|██        | 1/5 [00:18<00:39,  9.76s/it]
[Epoch 2] Validation: 0.481:  20%|██        | 1/5 [00:19<00:39,  9.76s/it]       
[Epoch 2] Validation: 0.481:  40%|████      | 2/5 [00:19<00:29,  9.73s/it]
[Epoch 3] training: accuracy=0.500:  40%|████      | 2/5 [00:27<00:29,  9.73s/it]
[Epoch 3] Validation: 0.475:  40%|████      | 2/5 [00:29<00:29,  9.73s/it]       
[Epoch 3] Validation: 0.475:  60%|██████    | 3/5 [00:29<00:19,  9.73s/it]
[Epoch 4] training: accuracy=0.522:  60%|██████    | 3/5 [00:37<00:19,  9.73s/it]
[Epoch 4] Validation: 0.519:  60%|██████    | 3/5 [00:38<00:19,  9.73s/it]       
[Epoch 4] Validation: 0.519:  80%|████████  | 4/5 [00:38<00:09,  9.73s/it]
[Epoch 5] training: accuracy=0.545:  80%|████████  | 4/5 [00:47<00:09,  9.73s/it]
[Epoch 5] Validation: 0.481: 100%|██████████| 5/5 [00:48<00:00,  9.74s/it]Finished Task with config: {'net.choice': 0, 'optimizer.learning_rate': 0.0031622777, 'optimizer.wd': 0.0003162278} and reward: 0.48125

[Epoch 5] Validation: 0.481: 100%|██████████| 5/5 [00:48<00:00,  9.73s/it]
[Epoch 5] Validation: 0.688: 100%|██████████| 5/5 [00:48<00:00,  9.79s/it]
Finished Task with config: {'net.choice': 0, 'optimizer.learning_rate': 0.007012677325899843, 'optimizer.wd': 0.0003264188681299316} and reward: 0.6875
[Epoch 5] training: accuracy=0.628:  80%|████████  | 4/5 [00:52<00:10, 10.44s/it]
[Epoch 5] training: accuracy=0.628: 100%|██████████| 5/5 [00:52<00:00, 10.46s/it]

Within fit, the dataset is automatically split into training and validation sets. The model with the best hyperparameter configuration is selected based on its performance on the validation set. The best model is finally retrained on our entire dataset (i.e., merging training+validation) using the best configuration.

The best Top-1 accuracy achieved on the validation set is as follows:

print('Top-1 val acc: %.3f' % classifier.results['best_reward'])
Top-1 val acc: 0.688

Predict on a New Image

Given an example image, we can easily use the final model to predict the label (and the conditional class-probability):

# skip this if training FashionMNIST on CPU.
if ag.get_gpu_count() > 0:
    image = 'data/test/BabyShirt/BabyShirt_323.jpg'
    ind, prob, _ = classifier.predict(image, plot=True)

    print('The input picture is classified as [%s], with probability %.2f.' %
          (dataset.init().classes[ind.asscalar()], prob.asscalar()))

    image = 'data/test/womenchiffontop/womenchiffontop_184.jpg'
    ind, prob, _ = classifier.predict(image, plot=True)

    print('The input picture is classified as [%s], with probability %.2f.' %
          (dataset.init().classes[ind.asscalar()], prob.asscalar()))
../../_images/output_beginner_7db196_15_0.png
The input picture is classified as [BabyShirt], with probability 0.71.
../../_images/output_beginner_7db196_15_2.png
The input picture is classified as [womenchiffontop], with probability 0.36.

Evaluate on Test Dataset

We now evaluate the classifier on a test dataset.

The validation and test top-1 accuracy are:

test_acc = classifier.evaluate(test_dataset)
print('Top-1 test acc: %.3f' % test_acc)
accuracy: 0.8125: 100%|██████████| 1/1 [00:04<00:00,  4.43s/it]
Top-1 test acc: 0.812