.. _sec_imgquick: 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: .. code:: python import autogluon.core as ag from autogluon.vision 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: .. code:: python filename = ag.download('https://autogluon.s3.amazonaws.com/datasets/shopee-iet.zip') ag.unzip(filename) .. parsed-literal:: :class: output 100%|██████████| 40895/40895 [00:01<00:00, 39821.37KB/s] .. parsed-literal:: :class: output 'data' After the dataset is downloaded, we load it into a `Dataset `__ object: .. code:: python dataset = task.Dataset('data/train') Load the test dataset as follows: .. code:: python 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: .. code:: python 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: .. code:: python classifier = task.fit(dataset, epochs=5, ngpus_per_trial=1, verbose=False) .. parsed-literal:: :class: output scheduler: FIFOScheduler( DistributedResourceManager{ (Remote: Remote REMOTE_ID: 0, , Resource: NodeResourceManager(8 CPUs, 1 GPUs)) }) .. parsed-literal:: :class: output HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=2.0), HTML(value=''))) .. parsed-literal:: :class: output 0%| | 0/5 [00:00 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())) .. figure:: output_beginner_7db196_15_0.png .. parsed-literal:: :class: output The input picture is classified as [BabyShirt], with probability 0.55. .. figure:: output_beginner_7db196_15_2.png .. parsed-literal:: :class: output The input picture is classified as [BabyShirt], with probability 0.37. Evaluate on Test Dataset ------------------------ We now evaluate the classifier on a test dataset. The validation and test top-1 accuracy are: .. code:: python test_acc = classifier.evaluate(test_dataset) print('Top-1 test acc: %.3f' % test_acc) .. parsed-literal:: :class: output accuracy: 0.71875: 100%|██████████| 1/1 [00:01<00:00, 1.74s/it] .. parsed-literal:: :class: output Top-1 test acc: 0.719 .. parsed-literal:: :class: output