.. _sec_object_detection_quick: Object Detection - Quick Start ============================== Object detection is the process of identifying and localizing objects in an image and is an important task in computer vision. Follow this tutorial to learn how to use AutoGluon for object detection. **Tip**: If you are new to AutoGluon, review :ref:`sec_imgquick` first to learn the basics of the AutoGluon API. Our goal is to detect motorbike in images by `YOLOv3 model `__. A tiny dataset is collected from VOC dataset, which only contains the motorbike category. The model pretrained on the COCO dataset is used to fine-tune our small dataset. With the help of AutoGluon, we are able to try many models with different hyperparameters automatically, and return the best one as our final model. To start, import autogluon.vision and ObjectDetector: .. code:: python import autogluon.core as ag from autogluon.vision import ObjectDetector .. parsed-literal:: :class: output /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/core/src/autogluon/core/scheduler/jobs.py:132: SyntaxWarning: "is" with a literal. Did you mean "=="? file = sys.stderr if out is 'err' else sys.stdout Tiny\_motorbike Dataset ----------------------- We collect a toy dataset for detecting motorbikes in images. From the VOC dataset, images are randomly selected for training, validation, and testing - 120 images for training, 50 images for validation, and 50 for testing. This tiny dataset follows the same format as VOC. Using the commands below, we can download this dataset, which is only 23M. The name of unzipped folder is called ``tiny_motorbike``. Anyway, the task dataset helper can perform the download and extraction automatically, and load the dataset according to the detection formats. .. code:: python url = 'https://autogluon.s3.amazonaws.com/datasets/tiny_motorbike.zip' dataset_train = ObjectDetector.Dataset.from_voc(url, splits='trainval') .. parsed-literal:: :class: output tiny_motorbike/ ├── Annotations/ ├── ImageSets/ └── JPEGImages/ Fit Models by AutoGluon ----------------------- In this section, we demonstrate how to apply AutoGluon to fit our detection models. We use mobilenet as the backbone for the YOLOv3 model. Two different learning rates are used to fine-tune the network. The best model is the one that obtains the best performance on the validation dataset. You can also try using more networks and hyperparameters to create a larger searching space. We ``fit`` a classifier using AutoGluon as follows. In each experiment (one trial in our searching space), we train the model for 5 epochs to avoid bursting our tutorial runtime. .. code:: python time_limit = 60*30 # at most 0.5 hour detector = ObjectDetector() hyperparameters = {'epochs': 5, 'batch_size': 8} hyperparamter_tune_kwargs={'num_trials': 2} detector.fit(dataset_train, time_limit=time_limit, hyperparameters=hyperparameters, hyperparamter_tune_kwargs=hyperparamter_tune_kwargs) .. parsed-literal:: :class: output WARNING:gluoncv.auto.tasks.object_detection:The number of requested GPUs is greater than the number of available GPUs.Reduce the number to 1 INFO:gluoncv.auto.tasks.object_detection:Randomly split train_data into train[153]/validation[17] splits. INFO:gluoncv.auto.tasks.object_detection:Starting fit without HPO INFO:SSDEstimator:modified configs( != ): { INFO:SSDEstimator:root.valid.batch_size 16 != 8 INFO:SSDEstimator:root.num_workers 4 != 8 INFO:SSDEstimator:root.dataset_root ~/.mxnet/datasets/ != auto INFO:SSDEstimator:root.train.seed 233 != 518 INFO:SSDEstimator:root.train.batch_size 16 != 8 INFO:SSDEstimator:root.train.epochs 20 != 5 INFO:SSDEstimator:root.dataset voc_tiny != auto INFO:SSDEstimator:root.gpus (0, 1, 2, 3) != (0,) INFO:SSDEstimator:root.ssd.data_shape 300 != 512 INFO:SSDEstimator:root.ssd.base_network vgg16_atrous != resnet50_v1 INFO:SSDEstimator:} INFO:SSDEstimator:Saved config to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/config.yaml INFO:SSDEstimator:Using transfer learning from ssd_512_resnet50_v1_coco, the other network parameters are ignored. INFO:SSDEstimator:Start training from [Epoch 0] INFO:SSDEstimator:[Epoch 0] Training cost: 10.095721, CrossEntropy=3.505902, SmoothL1=1.049549 INFO:SSDEstimator:[Epoch 0] Validation: dog=1.0000000000000002 chair=0.0 cow=0.909090909090909 bicycle=0.06436337901306054 bus=0.31818181818181823 person=0.7422855054197233 motorbike=0.6067141109174189 pottedplant=0.0 boat=1.0000000000000002 car=0.7809041688253078 mAP=0.5421539891448239 INFO:SSDEstimator:[Epoch 0] Current best map: 0.542154 vs previous 0.000000, saved to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/best_checkpoint.pkl INFO:SSDEstimator:Pickled to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/best_checkpoint.pkl INFO:SSDEstimator:[Epoch 1] Training cost: 9.202750, CrossEntropy=2.617644, SmoothL1=1.162525 INFO:SSDEstimator:[Epoch 1] Validation: dog=1.0000000000000002 chair=0.10000000000000002 cow=1.0000000000000002 bicycle=0.36366578819435563 bus=0.6363636363636365 person=0.7353234805617082 motorbike=0.8644176832704251 pottedplant=0.0 boat=1.0000000000000002 car=0.6797905525846702 mAP=0.6379561140974797 INFO:SSDEstimator:[Epoch 1] Current best map: 0.637956 vs previous 0.542154, saved to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/best_checkpoint.pkl INFO:SSDEstimator:Pickled to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/best_checkpoint.pkl INFO:SSDEstimator:[Epoch 2] Training cost: 8.853812, CrossEntropy=2.517284, SmoothL1=1.173569 INFO:SSDEstimator:[Epoch 2] Validation: dog=1.0000000000000002 chair=1.0000000000000002 cow=0.5587583148558757 bicycle=0.44866373296125367 bus=0.6472727272727272 person=0.7827678247643114 motorbike=0.8525577209333013 pottedplant=0.017595307917888565 boat=0.009433962264150943 car=0.8111666954171699 mAP=0.6128216286386678 INFO:SSDEstimator:[Epoch 3] Training cost: 9.413587, CrossEntropy=2.267903, SmoothL1=1.077149 INFO:SSDEstimator:[Epoch 3] Validation: dog=1.0000000000000002 chair=1.0000000000000002 cow=0.909090909090909 bicycle=0.5454545454545455 bus=0.5454545454545454 person=0.7529320695779278 motorbike=0.8822863768806136 pottedplant=0.0 boat=1.0000000000000002 car=0.7772610464571088 mAP=0.741247949291565 INFO:SSDEstimator:[Epoch 3] Current best map: 0.741248 vs previous 0.637956, saved to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/best_checkpoint.pkl INFO:SSDEstimator:Pickled to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/best_checkpoint.pkl INFO:SSDEstimator:[Epoch 4] Training cost: 9.059360, CrossEntropy=2.242657, SmoothL1=0.959251 INFO:SSDEstimator:[Epoch 4] Validation: dog=1.0000000000000002 chair=1.0000000000000002 cow=1.0000000000000002 bicycle=0.7030303030303031 bus=1.0000000000000002 person=0.8510013223518652 motorbike=0.8882810877374476 pottedplant=0.003896103896103896 boat=1.0000000000000002 car=0.8521074013092383 mAP=0.8298316218324959 INFO:SSDEstimator:[Epoch 4] Current best map: 0.829832 vs previous 0.741248, saved to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/best_checkpoint.pkl INFO:SSDEstimator:Pickled to /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225/.trial_0/best_checkpoint.pkl INFO:gluoncv.auto.tasks.object_detection:Finished, total runtime is 82.77 s INFO:gluoncv.auto.tasks.object_detection:{ 'best_config': { 'batch_size': 8, 'dist_ip_addrs': None, 'epochs': 5, 'final_fit': False, 'gpus': [0], 'log_dir': '/var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/docs/_build/eval/tutorials/object_detection/05c48225', 'lr': 0.001, 'ngpus_per_trial': 8, 'nthreads_per_trial': 128, 'num_trials': 1, 'num_workers': 8, 'search_strategy': 'random', 'seed': 518, 'time_limits': 1800, 'transfer': 'ssd_512_resnet50_v1_coco', 'wall_clock_tick': 1614127622.607981}, 'total_time': 69.39395928382874, 'train_map': 0.8298316218324959, 'valid_map': 0.8298316218324959} .. parsed-literal:: :class: output Note that ``num_trials=2`` above is only used to speed up the tutorial. In normal practice, it is common to only use ``time_limit`` and drop ``num_trials``. Also note that hyperparameter tuning defaults to random search. Model-based variants, such as ``search_strategy='bayesopt'`` or ``search_strategy='bayesopt_hyperband'`` can be a lot more sample-efficient. After fitting, AutoGluon automatically returns the best model among all models in the searching space. From the output, we know the best model is the one trained with the second learning rate. To see how well the returned model performed on test dataset, call detector.evaluate(). .. code:: python dataset_test = ObjectDetector.Dataset.from_voc(url, splits='test') test_map = detector.evaluate(dataset_test) print("mAP on test dataset: {}".format(test_map[1][-1])) .. parsed-literal:: :class: output tiny_motorbike/ ├── Annotations/ ├── ImageSets/ └── JPEGImages/ mAP on test dataset: 0.05792196933501283 Below, we randomly select an image from test dataset and show the predicted class, box and probability over the origin image, stored in ``predict_class``, ``predict_rois`` and ``predict_score`` columns, respectively. You can interpret ``predict_rois`` as a dict of (``xmin``, ``ymin``, ``xmax``, ``ymax``) proportional to original image size. .. code:: python image_path = dataset_test.iloc[0]['image'] result = detector.predict(image_path) print(result) .. parsed-literal:: :class: output INFO:numexpr.utils:NumExpr defaulting to 8 threads. .. parsed-literal:: :class: output predict_class predict_score \ 0 person 0.977397 1 motorbike 0.961676 2 car 0.557639 3 motorbike 0.483465 4 person 0.169022 .. ... ... 87 chair 0.029470 88 car 0.029415 89 person 0.029415 90 person 0.029240 91 person 0.029232 predict_rois 0 {'xmin': 0.3849811255931854, 'ymin': 0.3002845... 1 {'xmin': 0.3170837163925171, 'ymin': 0.4358768... 2 {'xmin': 0.004333138465881348, 'ymin': 0.63187... 3 {'xmin': 0.3680243492126465, 'ymin': 0.3390576... 4 {'xmin': 0.0605580173432827, 'ymin': 0.0173151... .. ... 87 {'xmin': 0.8222059011459351, 'ymin': 0.3000083... 88 {'xmin': 0.00212657917290926, 'ymin': 0.459672... 89 {'xmin': 0.05945102870464325, 'ymin': 0.075048... 90 {'xmin': 0.5079175233840942, 'ymin': 0.2999778... 91 {'xmin': 0.6806942224502563, 'ymin': 0.2688137... [92 rows x 3 columns] Prediction with multiple images is permitted: .. code:: python bulk_result = detector.predict(dataset_test) print(bulk_result) .. parsed-literal:: :class: output predict_class predict_score \ 0 person 0.977397 1 motorbike 0.961676 2 car 0.557639 3 motorbike 0.483465 4 person 0.169022 ... ... ... 4046 person 0.023806 4047 motorbike 0.023552 4048 person 0.023442 4049 person 0.023383 4050 motorbike 0.023344 predict_rois \ 0 {'xmin': 0.3849811255931854, 'ymin': 0.3002845... 1 {'xmin': 0.3170837163925171, 'ymin': 0.4358768... 2 {'xmin': 0.004333138465881348, 'ymin': 0.63187... 3 {'xmin': 0.3680243492126465, 'ymin': 0.3390576... 4 {'xmin': 0.0605580173432827, 'ymin': 0.0173151... ... ... 4046 {'xmin': 0.543327808380127, 'ymin': 0.61410915... 4047 {'xmin': 0.4615864157676697, 'ymin': 0.2539878... 4048 {'xmin': 0.49686524271965027, 'ymin': 0.190620... 4049 {'xmin': 0.3686455488204956, 'ymin': 0.1449364... 4050 {'xmin': 0.8133487701416016, 'ymin': 0.8839820... image 0 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... 1 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... 2 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... 3 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... 4 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... ... ... 4046 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... 4047 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... 4048 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... 4049 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... 4050 /var/lib/jenkins/.gluoncv/datasets/tiny_motorb... [4051 rows x 4 columns] We can also save the trained model, and use it later. .. code:: python savefile = 'detector.ag' detector.save(savefile) new_detector = ObjectDetector.load(savefile) .. parsed-literal:: :class: output /var/lib/jenkins/workspace/workspace/autogluon-tutorial-object-detection-v3/venv/lib/python3.8/site-packages/mxnet/gluon/block.py:1512: UserWarning: Cannot decide type for the following arguments. Consider providing them as input: data: None input_sym_arg_type = in_param.infer_type()[0]