1
votes

I have been using TensorFlow but I am new to Caffe. I wanted to try out a reliable implementation of AlexNet trained on ImageNet and I found one included in the official Caffe repository.

I was able to link the weights packaged in bvlc_alexnet.caffemodel file and the model specified in deploy.prototxt in a very short Caffe code and obtain an output vector of 1000 probabilities corresponding to the 1000 categories the network classifies the images into.

import numpy as np
import matplotlib.pyplot as plt
import sys
import caffe
import operator

MODEL_FILE = 'D:\\Desktop\\caffe\\models\\bvlc_alexnet\\deploy.prototxt'
PRETRAINED = 'D:\\AlexNet_Caffe\\bvlc_alexnet.caffemodel'

net = caffe.Classifier(MODEL_FILE, PRETRAINED)


IMAGE_FILE = 'D:\\Desktop\\caffe\\python\\testIm1.jpg'
input_image1 = caffe.io.load_image(IMAGE_FILE)

IMAGE_FILE = 'D:\\Desktop\\caffe\\python\\testIm2.jpg'
input_image2 = caffe.io.load_image(IMAGE_FILE)


pred = net.predict([input_image1, input_image2])
print pred # prints the array of 1000 probabilities


index, value = max(enumerate(pred[0]), key=operator.itemgetter(1))

print index # prints the index of max probability
print value # prints the max probability


index, value = max(enumerate(pred[1]), key=operator.itemgetter(1))

print index # prints the index of max probability
print value # prints the max probability

In my case, I am able to just specify the model and weights and obtain an output, however, the output seems to be the same (669) regardless of whatever image I input.

In the Caffe repository, there is a script for fetching ImageNet dataset. The tarball it downloads and extracts contains the following files:

$ ls -al
total 80395
drwxr-xr-x 1 root 197121        0 Aug  8 14:09 ./
drwxr-xr-x 1 root 197121        0 Aug  7 16:27 ../
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._imagenet_mean.binaryproto
-rw-r--r-- 1 root 197121      187 Apr  8  2014 ._synset_words.txt
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._synsets.txt
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._test.txt
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._train.txt
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._val.txt
-rw-r--r-- 1 root 197121 17858008 Aug  8 14:09 caffe_ilsvrc12.tar.gz
-rw-r--r-- 1 root 197121     3787 Jun  8  2014 det_synset_words.txt
-rwxr-xr-x 1 root 197121      610 Aug  8 13:41 get_ilsvrc_aux.sh*
-rw-r--r-- 1 root 197121 14931117 Jul 11  2014 imagenet.bet.pickle
-rw-r--r-- 1 root 197121   786446 Feb 25  2014 imagenet_mean.binaryproto
-rw-r--r-- 1 root 197121    31675 Apr  8  2014 synset_words.txt
-rw-r--r-- 1 root 197121    10000 Feb 25  2014 synsets.txt
-rw-r--r-- 1 root 197121  3200000 Feb 25  2014 test.txt
-rw-r--r-- 1 root 197121 43829433 Feb 25  2014 train.txt
-rw-r--r-- 1 root 197121  1644500 Feb 25  2014 val.txt

I am not sure if I also need to use the files imagenet_mean.binaryproto and imagenet.bet.pickle

Could someone please clarify?

1

1 Answers

2
votes

You need to subtract image mean.

When deep models are trained the inputs are usually normalized to have roughly mean=0. In case of Caffe's AlexNet, the image mean is saved in imagenet_mean.binaryproto.

See this example on how to use a pre-trained model to get classification results.