ImageNet Classification

You can use Darknet to classify images for the 1000-class ImageNet challenge. If you haven't installed Darknet yet, you should do that first.

Classifying With Pre-Trained Models

Here are the commands to install Darknet, download a classification weights file, and run a classifier on an image:

git clone https://github.com/pjreddie/darknet.git
cd darknet
make
wget https://pjreddie.com/media/files/extraction.weights
./darknet classifier predict cfg/imagenet1k.data cfg/extraction.cfg extraction.weights data/dog.jpg

This example uses the Extraction model, you can read more about it below. After running this command you should see the following output:

0: Convolutional Layer: 224 x 224 x 3 image, 64 filters -> 112 x 112 x 64 image
1: Maxpool Layer: 112 x 112 x 64 image, 2 size, 2 stride
...
23: Convolutional Layer: 7 x 7 x 512 image, 1024 filters -> 7 x 7 x 1024 image
24: Convolutional Layer: 7 x 7 x 1024 image, 1000 filters -> 7 x 7 x 1000 image
25: Avgpool Layer: 7 x 7 x 1000 image
26: Softmax Layer: 1000 inputs
27: Cost Layer: 1000 inputs
Loading weights from extraction.weights...Done!
298 224
data/dog.jpg: Predicted in 3.756339 seconds.
malamute: 0.194782
Eskimo dog: 0.155007
Siberian husky: 0.143937
dogsled: 0.020943
miniature schnauzer: 0.020566

Darknet displays information as it loads the config file and weights, then it classifies the image and prints the top-10 classes for the image. Kelp is a mixed breed dog but she has a lot of malamute in her so we'll consider this a success!

You can also try with other images, like the bald eagle image:

./darknet classifier predict cfg/imagenet1k.data cfg/extraction.cfg extraction.weights data/eagle.jpg

Which produces:

...
data/eagle.jpg: Predicted in 4.036698 seconds.
bald eagle: 0.797689
kite: 0.185116
vulture: 0.006402
prairie chicken: 0.001041
hen: 0.000888

Pretty good!

If you don't specify an image file you will be prompted at run-time for an image. This way you can classify multiple in a row without reloading the whole model. Use the command:

./darknet classifier predict cfg/imagenet1k.data cfg/extraction.cfg extraction.weights

Then you will get a prompt that looks like:

....
27: Softmax Layer: 1000 inputs
28: Cost Layer: 1000 inputs
Loading weights from extraction.weights...Done!
Enter Image Path:

Whenever you get bored of classifying images you can use Ctrl-C to exit the program.

Validating On ImageNet

You see these validation set numbers thrown around everywhere. Maybe you want to double check for yourself how well these models actually work. Let's do it!

First you need to download the validation images, and the cls-loc annotations. You can get them here but you'll have to make an account! Once you download everything you should have a directory with ILSVRC2012_bbox_val_v3.tgz, and ILSVRC2012_img_val.tar. First we unpack them:

tar -xzf ILSVRC2012_bbox_val_v3.tgz
mkdir -p imgs && tar xf ILSVRC2012_img_val.tar -C imgs

Now we have the images and the annotations but we need to label the images so Darknet can evaluate its predictions. We do that using this bash script. It's already in your scripts/ subdirectory. We can just get it again though and run it:

wget https://pjreddie.com/media/files/imagenet_label.sh
bash imagenet_label.sh

This will generate two things: a directory called labelled/ which contains renamed symbolic links to the images, and a file called inet.val.list which contains a list of the paths of the labelled images. We need to move this file to the data/ subdirectory in Darknet:

mv inet.val.list <path-to>/darknet/data

Now you are finally ready to validate your model! First re-make Darknet. Then run the validation routine like so:

./darknet classifier valid cfg/imagenet1k.data cfg/extraction.cfg extraction.weights

Note: if you don't compile Darknet with OpenCV then you won't be able to load all of the ImageNet images since some of them are weird formats not supported by stb_image.h.

If you don't compile with CUDA you can still validate on ImageNet but it will take like a reallllllly long time. Not recommended.

Pre-Trained Models

Here are a variety of pre-trained models for ImageNet classification. Accuracy is measured as single-crop validation accuracy on ImageNet. GPU timing is measured on a Titan X, CPU timing on an Intel i7-4790K (4 GHz).

Model Top-1 Top-5 Ops GPU CPU Cfg Weights
AlexNet 57.0 80.3 2.27 Bn 1.5 ms 0.3 s cfg 285 MB
Darknet Reference 61.1 83.0 0.81 Bn 1.5 ms 0.16 s cfg 28 MB
VGG-16 70.5 90.0 30.94 Bn 10.7 ms 4.9 s cfg 528 MB
Extraction 72.5 90.8 8.52 Bn 6.4 ms 0.95 s cfg 90 MB
Darknet19 72.9 91.2 5.58 Bn 6.0 ms 0.66 s cfg 80 MB
Darknet19 448x448 76.4 93.5 22.33 Bn 11.0 ms 2.8 s cfg 80 MB
Resnet 50 75.8 92.9 10 Bn 7.0 ms ?? s cfg 87 MB
Resnet 152 77.6 93.8 29.4 Bn ?? ms ?? s cfg 220 MB
Densenet 201 77.0 93.7 10.9 Bn ?? ms ?? s cfg 66 MB

AlexNet

The model that started a revolution! The original model was crazy with the split GPU thing so this is the model from some follow-up work.

Darknet Reference Model

This model is designed to be small but powerful. It attains the same top-1 and top-5 performance as AlexNet but with 1/10th the parameters. It uses mostly convolutional layers without the large fully connected layers at the end. It is about twice as fast as AlexNet on CPU making it more suitable for some vision applications.

VGG-16

The Visual Geometry Group at Oxford developed the VGG-16 model for the ILSVRC-2014 competition. It is highly accurate and widely used for classification and detection. I adapted this version from the Caffe pre-trained model. It was trained for an additional 6 epochs to adjust to Darknet-specific image preprocessing (instead of mean subtraction Darknet adjusts images to fall between -1 and 1).

Extraction

I developed this model as an offshoot of the GoogleNet model. It doesn't use the "inception" modules, only 1x1 and 3x3 convolutional layers.

Darknet19

I modified the Extraction network to be faster and more accurate. This network was sort of a merging of ideas from the Darknet Reference network and Extraction as well as numerous publications like Network In Network, Inception, and Batch Normalization.

Darknet19 448x448

I trained Darknet19 for 10 more epochs with a larger input image size, 448x448. This model performs significantly better but is slower since the whole image is larger.

Resnet 50

For some reason people love these networks even though they are so sloooooow. Whatever. Paper

Resnet 152

For some reason people love these networks even though they are so sloooooow. Whatever. Paper

Densenet 201

I love DenseNets! They are just so deep and so crazy and work so well. Like Resnet, still slow since they are sooooo many layers but at least they work really well! Paper