Created
October 25, 2016 12:52
-
-
Save peryaudo/800c954d272f3751e9eecb2d49c6b7a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -- coding: utf-8 -- | |
import numpy,os,caffe | |
import scipy.misc | |
import numpy as np | |
MODEL_FILE="models/bvlc_alexnet/deploy.prototxt" | |
PRETRAINED_MODEL_FILE="models/bvlc_alexnet/bvlc_alexnet.caffemodel" | |
CATEGORY_FILE="data/ilsvrc12/synset_words.txt" | |
MEAN_FILE="python/caffe/imagenet/ilsvrc_2012_mean.npy" | |
CLASSIFIED_FILE="../101_ObjectCategories/airplanes/image_0001.jpg" | |
mean = numpy.load(MEAN_FILE) | |
caffe.set_mode_gpu() | |
classifier = caffe.Classifier(MODEL_FILE, PRETRAINED_MODEL_FILE, | |
image_dims=(227, 227), mean=np.swapaxes(np.swapaxes(scipy.misc.imresize(mean, (227, 227)), 1, 2), 0, 1), | |
raw_scale=255, | |
channel_swap=(2, 1, 0)) | |
inputs = [caffe.io.load_image(os.path.expanduser(CLASSIFIED_FILE))] | |
scores = classifier.predict(inputs) | |
prediction = zip(scores[0].tolist(), numpy.loadtxt(CATEGORY_FILE, str, delimiter="\t")) | |
prediction.sort(cmp=lambda x, y: cmp(x[0], y[0]), reverse=True) | |
#Show result | |
for rank, (score, name) in enumerate(prediction[:3], start=1): | |
print('#%d | %s | %4.1f%%' % (rank, name, score * 100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment