Ubuntu + Caffe + ImageNet






import numpy as np

import matplotlib.pyplot as plt

import sys

caffe_root = '../'

sys.path.insert(0, caffe_root + 'python')

import caffe




# Set the pathes of model definition, trained model and image file to be predicted

MODEL_FILE = '../models/bvlc_reference_caffenet/deploy.prototxt'

PRETRAINED = '../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'

IMAGE_FILE = 'images/irish_terrier.jpg'




# load classifier

net = caffe.Classifier(MODEL_FILE, PRETRAINED,

mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1),

channel_swap=(2,1,0),

raw_scale=255,

image_dims=(256, 256))




# net.set_phase_test()

# net.set_mode_cpu()




# load image file to be predicted

input_image = caffe.io.load_image(IMAGE_FILE)




# predict

prediction = net.predict([input_image])

sorted_predict = sorted(range(len(prediction[0])),key=lambda x:prediction[0][x],reverse=True)




# print top5 result

for i in sorted_predict[0:5]:

print 'class=',i,', score=',prediction[0][i]




# plot image and result

plt.subplot(2,1,1)

plt.imshow(input_image)

plt.subplot(2,1,2)

plt.plot(prediction[0])

plt.show()

댓글

이 블로그의 인기 게시물

파이썬으로 Homomorphic Filtering 하기

파이썬으로 2D FFT/iFFT 하기: numpy 버전