Prepare the ImageNet dataset

The ImageNet project contains millions of images and thousands of objects for image classification. It is widely used in the research community for benchmarking state-of-the-art models.


The dataset has multiple versions. The one commonly used for image classification is ILSVRC 2012. This tutorial will go through the steps of preparing this dataset for GluonCV.


You need at least 300 GB disk space to download and extract the dataset. SSD (Solid-state disks) is preferred over HDD because of faster speed.


First, go to the download page (you may need to register an account), and find the page for ILSVRC2012. Next, find and download the following two files:




138 GB


6.3 GB


First, please download the helper script validation image info imagenet_val_maps.pklz. Make sure to put them in the same directory.

Assuming the tar files are saved in folder ~/ILSVRC2012. We can use the following command to prepare the dataset automatically.

python --download-dir ~/ILSVRC2012


Extracting the images may take a while. For example, it takes about 30min on an AWS EC2 instance with EBS.

By default will extract the images into ~/.mxnet/datasets/imagenet. You can specify a different target folder by setting --target-dir.

Read with GluonCV

The prepared dataset can be loaded with utility class directly. Here is an example that randomly reads 128 images each time and performs randomized resizing and cropping.

from import ImageNet
from import DataLoader
from import transforms

train_trans = transforms.Compose([

# You need to specify ``root`` for ImageNet if you extracted the images into
# a different folder
train_data = DataLoader(
    batch_size=128, shuffle=True)
for x, y in train_data:
    print(x.shape, y.shape)


(128, 3, 224, 224) (128,)

Plot some validation images

from gluoncv.utils import viz
val_dataset = ImageNet(train=False)
viz.plot_image(val_dataset[1234][0])  # index 0 is image, 1 is label
  • imagenet
  • imagenet

Total running time of the script: ( 2 minutes 37.729 seconds)

Gallery generated by Sphinx-Gallery