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)
/var/lib/jenkins/miniconda3/envs/autogluon_docs-v0_0_14/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: should_run_async will not call transform_cell automatically in the future. Please pass the result to transformed_cell argument and any exception that happen during thetransform in preprocessing_exc_tuple in IPython 7.17 and above. and should_run_async(code) Downloading shopee-iet.zip from https://autogluon.s3.amazonaws.com/datasets/shopee-iet.zip... 100%|██████████| 40895/40895 [00:01<00:00, 37302.04KB/s]
'data'
After the dataset is downloaded, we load it into a 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)
scheduler_options: Key 'training_history_callback_delta_secs': Imputing default value 60
scheduler_options: Key 'delay_get_config': Imputing default value True
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.38.222/29784/1' processes=1 threads=8, memory=33.24 GB>, Resource: NodeResourceManager(8 CPUs, 1 GPUs))
})
HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=2.0), HTML(value='')))
0%| | 0/5 [00:00<?, ?it/s][A
[Epoch 1] training: accuracy=0.281: 0%| | 0/5 [00:08<?, ?it/s][A
[Epoch 1] Validation: 0.381: 0%| | 0/5 [00:09<?, ?it/s] [A
[A
[Epoch 2] training: accuracy=0.417: 20%|██ | 1/5 [00:18<00:38, 9.73s/it][A
[Epoch 2] Validation: 0.425: 20%|██ | 1/5 [00:19<00:38, 9.73s/it] [A
[Epoch 2] Validation: 0.425: 40%|████ | 2/5 [00:19<00:28, 9.64s/it][A
[Epoch 3] training: accuracy=0.484: 40%|████ | 2/5 [00:27<00:28, 9.64s/it][A
[Epoch 3] Validation: 0.456: 40%|████ | 2/5 [00:28<00:28, 9.64s/it]
[Epoch 3] Validation: 0.456: 60%|██████ | 3/5 [00:28<00:19, 9.61s/it][A
[Epoch 4] training: accuracy=0.514: 60%|██████ | 3/5 [00:37<00:19, 9.61s/it][A
[Epoch 4] Validation: 0.475: 60%|██████ | 3/5 [00:38<00:19, 9.61s/it] [A
[A
[Epoch 5] training: accuracy=0.565: 80%|████████ | 4/5 [00:46<00:09, 9.58s/it][A
[Epoch 5] Validation: 0.506: 80%|████████ | 4/5 [00:47<00:09, 9.58s/it] [A
[Epoch 5] Validation: 0.506: 100%|██████████| 5/5 [00:47<00:00, 9.55s/it]
[Epoch 5] Validation: 0.525: 80%|████████ | 4/5 [00:48<00:09, 9.70s/it]
[Epoch 5] training: accuracy=0.543: 80%|████████ | 4/5 [00:51<00:10, 10.40s/it]
[Epoch 5] training: accuracy=0.543: 100%|██████████| 5/5 [00:51<00:00, 10.38s/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.525
/var/lib/jenkins/miniconda3/envs/autogluon_docs-v0_0_14/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: should_run_async will not call transform_cell automatically in the future. Please pass the result to transformed_cell argument and any exception that happen during thetransform in preprocessing_exc_tuple in IPython 7.17 and above. and should_run_async(code)
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()))

The input picture is classified as [BabyShirt], with probability 0.54.

The input picture is classified as [BabyShirt], with probability 0.33.
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.65625: 100%|██████████| 1/1 [00:01<00:00, 1.77s/it]
Top-1 test acc: 0.656