.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "build/examples_datasets/pascal_voc.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_build_examples_datasets_pascal_voc.py: Prepare PASCAL VOC datasets ============================== `Pascal VOC `_ is a collection of datasets for object detection. The most commonly combination for benchmarking is using *2007 trainval* and *2012 trainval* for training and *2007 test* for validation. This tutorial will walk through the steps of preparing this dataset for GluonCV. .. image:: http://host.robots.ox.ac.uk/pascal/VOC/pascal2.png .. hint:: You need 8.4 GB disk space to download and extract this dataset. SSD is preferred over HDD because of its better performance. The total time to prepare the dataset depends on your Internet speed and disk performance. For example, it often takes 10 min on AWS EC2 with EBS. Prepare the dataset ------------------- We need the following four files from Pascal VOC: +------------------------------------------------------------------------------------------------------------------------+--------+------------------------------------------+ | Filename | Size | SHA-1 | +========================================================================================================================+========+==========================================+ | `VOCtrainval_06-Nov-2007.tar `_ | 439 MB | 34ed68851bce2a36e2a223fa52c661d592c66b3c | +------------------------------------------------------------------------------------------------------------------------+--------+------------------------------------------+ | `VOCtest_06-Nov-2007.tar `_ | 430 MB | 41a8d6e12baa5ab18ee7f8f8029b9e11805b4ef1 | +------------------------------------------------------------------------------------------------------------------------+--------+------------------------------------------+ | `VOCtrainval_11-May-2012.tar `_ | 1.9 GB | 4e443f8a2eca6b1dac8a6c57641b67dd40621a49 | +------------------------------------------------------------------------------------------------------------------------+--------+------------------------------------------+ | `benchmark.tgz `_ | 1.4 GB | 7129e0a480c2d6afb02b517bb18ac54283bfaa35 | +------------------------------------------------------------------------------------------------------------------------+--------+------------------------------------------+ The easiest way to download and unpack these files is to download helper script :download:`pascal_voc.py<../../../scripts/datasets/pascal_voc.py>` and run the following command: .. code-block:: bash python pascal_voc.py which will automatically download and extract the data into ``~/.mxnet/datasets/voc``. If you already have the above files sitting on your disk, you can set ``--download-dir`` to point to them. For example, assuming the files are saved in ``~/VOCdevkit/``, you can run: .. code-block:: bash python pascal_voc.py --download-dir ~/VOCdevkit .. GENERATED FROM PYTHON SOURCE LINES 58-63 Read with GluonCV ----------------- Loading images and labels is straight-forward with :py:class:`gluoncv.data.VOCDetection`. .. GENERATED FROM PYTHON SOURCE LINES 63-74 .. code-block:: default from gluoncv import data, utils from matplotlib import pyplot as plt train_dataset = data.VOCDetection(splits=[(2007, 'trainval'), (2012, 'trainval')]) val_dataset = data.VOCDetection(splits=[(2007, 'test')]) print('Num of training images:', len(train_dataset)) print('Num of validation images:', len(val_dataset)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Num of training images: 16551 Num of validation images: 4952 .. GENERATED FROM PYTHON SOURCE LINES 75-76 Now let's visualize one example. .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: default train_image, train_label = train_dataset[5] print('Image size (height, width, RGB):', train_image.shape) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Image size (height, width, RGB): (364, 480, 3) .. GENERATED FROM PYTHON SOURCE LINES 81-82 Take bounding boxes by slice columns from 0 to 4 .. GENERATED FROM PYTHON SOURCE LINES 82-87 .. code-block:: default bounding_boxes = train_label[:, :4] print('Num of objects:', bounding_boxes.shape[0]) print('Bounding boxes (num_boxes, x_min, y_min, x_max, y_max):\n', bounding_boxes) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Num of objects: 2 Bounding boxes (num_boxes, x_min, y_min, x_max, y_max): [[184. 61. 278. 198.] [ 89. 77. 402. 335.]] .. GENERATED FROM PYTHON SOURCE LINES 88-89 take class ids by slice the 5th column .. GENERATED FROM PYTHON SOURCE LINES 89-92 .. code-block:: default class_ids = train_label[:, 4:5] print('Class IDs (num_boxes, ):\n', class_ids) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Class IDs (num_boxes, ): [[14.] [12.]] .. GENERATED FROM PYTHON SOURCE LINES 93-94 Visualize image, bounding boxes .. GENERATED FROM PYTHON SOURCE LINES 94-99 .. code-block:: default utils.viz.plot_bbox(train_image.asnumpy(), bounding_boxes, scores=None, labels=class_ids, class_names=train_dataset.classes) plt.show() .. image-sg:: /build/examples_datasets/images/sphx_glr_pascal_voc_001.png :alt: pascal voc :srcset: /build/examples_datasets/images/sphx_glr_pascal_voc_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-104 Finally, to use both ``train_dataset`` and ``val_dataset`` for training, we can pass them through data transformations and load with :py:class:`mxnet.gluon.data.DataLoader`, see :download:`train_ssd.py <../../../scripts/detection/ssd/train_ssd.py>` for more information. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 1 minutes 52.250 seconds) .. _sphx_glr_download_build_examples_datasets_pascal_voc.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pascal_voc.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pascal_voc.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_