.. _sec_imgquick: Image Prediction - Quick Start ============================== In this quick start, we'll use the task of image classification to illustrate how to use AutoGluon’s APIs. This tutorial demonstrates how to load images and 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 <../../api/autogluon.predictor.html#autogluon.vision.ImagePredictor.fit>`__ function, AutoGluon automatically trains many models with different hyperparameter configurations and returns the model that achieved the highest level of accuracy. .. code:: python import autogluon.core as ag from autogluon.vision import ImagePredictor Create Image 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 can load a dataset by downloading a url data automatically: .. code:: python train_dataset, _, test_dataset = ImagePredictor.Dataset.from_folders('https://autogluon.s3.amazonaws.com/datasets/shopee-iet.zip') print(train_dataset) .. parsed-literal:: :class: output data/ ├── test/ └── train/ image label 0 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 0 1 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 0 2 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 0 3 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 0 4 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 0 .. ... ... 795 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 3 796 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 3 797 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 3 798 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 3 799 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 3 [800 rows x 2 columns] Use AutoGluon to Fit Models --------------------------- Now, we fit a classifier using AutoGluon as follows: .. code:: python predictor = ImagePredictor() # since the original dataset does not provide validation split, the `fit` function splits it randomly with 90/10 ratio predictor.fit(train_dataset, hyperparameters={'epochs': 2}) # you can trust the default config, we reduce the # epoch to save some build time .. parsed-literal:: :class: output INFO:root:`time_limit=auto` set to `time_limit=7200`. WARNING:gluoncv.auto.tasks.image_classification:The number of requested GPUs is greater than the number of available GPUs.Reduce the number to 1 INFO:gluoncv.auto.tasks.image_classification:Randomly split train_data into train[723]/validation[77] splits. INFO:gluoncv.auto.tasks.image_classification:Starting fit without HPO INFO:ImageClassificationEstimator:modified configs( != ): { INFO:ImageClassificationEstimator:root.valid.batch_size 128 != 16 INFO:ImageClassificationEstimator:root.valid.num_workers 4 != 8 INFO:ImageClassificationEstimator:root.train.batch_size 128 != 16 INFO:ImageClassificationEstimator:root.train.rec_val ~/.mxnet/datasets/imagenet/rec/val.rec != auto INFO:ImageClassificationEstimator:root.train.epochs 10 != 2 INFO:ImageClassificationEstimator:root.train.rec_val_idx ~/.mxnet/datasets/imagenet/rec/val.idx != auto INFO:ImageClassificationEstimator:root.train.num_workers 4 != 8 INFO:ImageClassificationEstimator:root.train.rec_train_idx ~/.mxnet/datasets/imagenet/rec/train.idx != auto INFO:ImageClassificationEstimator:root.train.lr 0.1 != 0.01 INFO:ImageClassificationEstimator:root.train.num_training_samples 1281167 != -1 INFO:ImageClassificationEstimator:root.train.data_dir ~/.mxnet/datasets/imagenet != auto INFO:ImageClassificationEstimator:root.train.rec_train ~/.mxnet/datasets/imagenet/rec/train.rec != auto INFO:ImageClassificationEstimator:root.img_cls.model resnet50_v1 != resnet50_v1b INFO:ImageClassificationEstimator:} INFO:ImageClassificationEstimator:Saved config to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-image-classification-v3/docs/_build/eval/tutorials/image_prediction/4357e3b8/.trial_0/config.yaml INFO:ImageClassificationEstimator:Start training from [Epoch 0] INFO:ImageClassificationEstimator:Epoch[0] Batch [49] Speed: 100.685474 samples/sec accuracy=0.522500 lr=0.010000 INFO:ImageClassificationEstimator:[Epoch 0] training: accuracy=0.522500 INFO:ImageClassificationEstimator:[Epoch 0] speed: 98 samples/sec time cost: 10.833059 INFO:ImageClassificationEstimator:[Epoch 0] validation: top1=0.760000 top5=1.000000 INFO:ImageClassificationEstimator:[Epoch 0] Current best top-1: 0.760000 vs previous 0.000000, saved to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-image-classification-v3/docs/_build/eval/tutorials/image_prediction/4357e3b8/.trial_0/best_checkpoint.pkl INFO:ImageClassificationEstimator:Pickled to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-image-classification-v3/docs/_build/eval/tutorials/image_prediction/4357e3b8/.trial_0/best_checkpoint.pkl INFO:ImageClassificationEstimator:Epoch[1] Batch [49] Speed: 103.524639 samples/sec accuracy=0.703750 lr=0.010000 INFO:ImageClassificationEstimator:[Epoch 1] training: accuracy=0.703750 INFO:ImageClassificationEstimator:[Epoch 1] speed: 101 samples/sec time cost: 10.651478 INFO:ImageClassificationEstimator:[Epoch 1] validation: top1=0.842500 top5=1.000000 INFO:ImageClassificationEstimator:[Epoch 1] Current best top-1: 0.842500 vs previous 0.760000, saved to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-image-classification-v3/docs/_build/eval/tutorials/image_prediction/4357e3b8/.trial_0/best_checkpoint.pkl INFO:ImageClassificationEstimator:Pickled to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-image-classification-v3/docs/_build/eval/tutorials/image_prediction/4357e3b8/.trial_0/best_checkpoint.pkl INFO:gluoncv.auto.tasks.image_classification:Finished, total runtime is 30.83 s INFO:gluoncv.auto.tasks.image_classification:{ 'best_config': { 'batch_size': 16, 'dist_ip_addrs': None, 'epochs': 2, 'estimator': , 'final_fit': False, 'gpus': [0], 'log_dir': '/var/lib/jenkins/workspace/workspace/autogluon-tutorial-image-classification-v3/docs/_build/eval/tutorials/image_prediction/4357e3b8', 'lr': 0.01, 'model': 'resnet50_v1b', 'ngpus_per_trial': 8, 'nthreads_per_trial': 128, 'num_trials': 1, 'num_workers': 8, 'search_strategy': 'random', 'seed': 744, 'time_limits': 7200, 'wall_clock_tick': 1615358054.3473246}, 'total_time': 23.46466612815857, 'train_acc': 0.70375, 'valid_acc': 0.8425} .. parsed-literal:: :class: output 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: .. code:: python fit_result = predictor.fit_summary() print('Top-1 train acc: %.3f, val acc: %.3f' %(fit_result['train_acc'], fit_result['valid_acc'])) .. parsed-literal:: :class: output Top-1 train acc: 0.704, val acc: 0.843 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 denoted as ``score``): .. code:: python image_path = test_dataset.iloc[0]['image'] result = predictor.predict(image_path) print(result) .. parsed-literal:: :class: output class score id 0 BabyShirt 0.760976 1 If probabilities of all categories are needed, you can call ``predict_proba``: .. code:: python proba = predictor.predict_proba(image_path) print(proba) .. parsed-literal:: :class: output class score id 0 BabyShirt 0.760976 1 1 BabyPants 0.174474 0 2 womencasualshoes 0.033775 2 3 womenchiffontop 0.030775 3 You can also feed in multiple images all together, let's use images in test dataset as an example: .. code:: python bulk_result = predictor.predict(test_dataset) print(bulk_result) .. parsed-literal:: :class: output class score id \ 0 BabyShirt 0.760976 1 1 BabyShirt 0.938339 1 2 BabyShirt 0.514371 1 3 BabyShirt 0.477294 1 4 BabyShirt 0.593985 1 .. ... ... .. 75 womenchiffontop 0.634051 3 76 BabyPants 0.361681 0 77 womenchiffontop 0.866246 3 78 BabyPants 0.450829 0 79 womenchiffontop 0.543045 3 image 0 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 1 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 2 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 3 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 4 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... .. ... 75 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 76 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 77 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 78 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... 79 /var/lib/jenkins/.gluoncv/datasets/shopee-iet/... [80 rows x 4 columns] An extra column will be included in bulk prediction, indicate the corresponding image for the row. There will be (# image) rows in the result, each row includes ``class``, ``score``, ``id`` and ``image`` for prediction class, prediction confidence, class id, and image path respectively. Generate image features with a classifier ----------------------------------------- Extracting representation from the whole image learned by a model is also very useful. We provide ``predict_feature`` function to allow predictor to return the N-dimensional image feature where ``N`` depends on the model(usually a 512 to 2048 length vector) .. code:: python image_path = test_dataset.iloc[0]['image'] feature = predictor.predict_feature(image_path) print(feature) .. parsed-literal:: :class: output image_feature 0 [0.20994957, 0.0, 0.4686559, 2.1155107, 0.3233... Evaluate on Test Dataset ------------------------ You can evaluate the classifier on a test dataset rather than retrieving the predictions. The validation and test top-1 accuracy are: .. code:: python test_acc, _ = predictor.evaluate(test_dataset) print('Top-1 test acc: %.3f' % test_acc) .. parsed-literal:: :class: output Top-1 test acc: 0.762 Save and load classifiers ------------------------- You can directly save the instances of classifiers: .. code:: python filename = 'predictor.ag' predictor.save(filename) predictor_loaded = ImagePredictor.load(filename) # use predictor_loaded as usual result = predictor_loaded.predict(image_path) print(result) .. parsed-literal:: :class: output class score id 0 BabyShirt 0.760976 1