Table Of Contents
Table Of Contents

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.


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:





439 MB



430 MB



1.9 GB



1.4 GB


The easiest way to download and unpack these files is to download helper script and run the following command:


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:

python --download-dir ~/VOCdevkit

Read with GluonCV

Loading images and labels is straight-forward with

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))

Now let’s visualize one example.

train_image, train_label = train_dataset[5]
print('Image size (height, width, RGB):', train_image.shape)

Take bounding boxes by slice columns from 0 to 4

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',

take class ids by slice the 5th column

class_ids = train_label[:, 4:5]
print('Class IDs (num_boxes, ):\n', class_ids)

Visualize image, bounding boxes

utils.viz.plot_bbox(train_image.asnumpy(), bounding_boxes, scores=None,
                    labels=class_ids, class_names=train_dataset.classes)

Finally, to use both train_dataset and val_dataset for training, we can pass them through data transformations and load with, see for more information.

Total running time of the script: ( 0 minutes 0.000 seconds)

Gallery generated by Sphinx-Gallery