forresti / SqueezeNet

SqueezeNet: AlexNet-level accuracy with 50x fewer parameters
BSD 2-Clause "Simplified" License
2.17k stars 723 forks source link

Reproduce AlexNet results. #11

Closed mrgloom closed 8 years ago

mrgloom commented 8 years ago

I'm trying to reproduce this example http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb using SqueezeNet, but for this picture https://github.com/BVLC/caffe/blob/master/examples/images/cat.jpg predicted class is 278 which is n02119789 kit fox, Vulpes macrotis from https://github.com/HoldenCaulfieldRye/caffe/blob/master/data/ilsvrc12/synset_words.txt

Is it normal? Or something is wrong?

Here is full code:

import numpy as np
import matplotlib.pyplot as plt

# The caffe module needs to be on the Python path;
import sys
caffe_root = '/home/myuser/Downloads/caffe'# Change this line !
sys.path.insert(0, caffe_root + 'python')

# If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.
import caffe

caffe.set_mode_cpu()

model_def = '/home/myuser/Desktop/GeneralDBCreator/python/SqueezeNet/SqueezeNet_v1.0/deploy.prototxt'
model_weights = '/home/myuser/Desktop/GeneralDBCreator/python/SqueezeNet/SqueezeNet_v1.0/squeezenet_v1.0.caffemodel'

net = caffe.Net(model_def,      # defines the structure of the model
                model_weights,  # contains the trained weights
                caffe.TEST)     # use test mode (e.g., don't perform dropout)

net.blobs['data'].reshape(1,        # batch size
                          3,         # 3-channel (BGR) images
                          227, 227)  # image size is 227x227

image_path= '/home/myuser/Desktop/GeneralDBCreator/python/cat.jpg' # Change this line !
image = caffe.io.load_image(image_path)

mu= np.array([104.0069879317889, 116.66876761696767, 122.6789143406786])
#mu= np.array([104, 117, 123])

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))  # move image channels to outermost dimension
transformer.set_mean('data', mu)           # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)      # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[...] = transformed_image

output = net.forward()

output_prob = output['prob'][0]  # the output probability vector for the first image in the batch

print 'predicted class is:', output_prob.argmax()

labels_file = '/home/myuser/Desktop/GeneralDBCreator/python/synset_words.txt'
labels = np.loadtxt(labels_file, str, delimiter='\t')
print 'output label:', labels[output_prob.argmax()]
forresti commented 8 years ago

Our claim is that, on the ImageNet test set of 50,000 images, SqueezeNet accuracy as at least as good as AlexNet accuracy. You have to admit that running one image isn't a good statistical measure of accuracy. :)

Even when training AlexNet multiple times with different random seeds, we've found that some training runs produce an AlexNet model that gets your cat image right, and some that get it wrong. But, zooming out to a larger statistically-significant test set, each training run leads to a model with similar overall accuracy. Same deal with SqueezeNet.

auzxb commented 7 years ago

I met the same problem. The output of my test demo is 278 n02119789 kit fox, Vulpes macrotis 151 n02085620 Chihuahua 263 n02113023 Pembroke, Pembroke Welsh corgi 277 n02119022 red fox, Vulpes vulpes 331 n02326432 hare

mrgloom commented 7 years ago

@auzxb as said before it's 'normal' behaviour, because accuracy calculated on larger set of images and in average it should be near the same as AlexNet accuracy.

You can look at my results, it's really near the same performance: https://github.com/mrgloom/kaggle-dogs-vs-cats-solution