您的位置:首页 > 编程语言 > Python开发

caffe 使用python测试ImageNet classification

2014-12-27 10:32 330 查看
环境:ubutun 14.04 caffe

1.首先要确保caffe已经编译成功,如果没有可以参考:
http://blog.csdn.net/huangshanchun/article/details/41276901
首先安装pip和python-dev (系统默认有python环境的, 不过我们需要的使python-dev)

sudo apt-get install python-dev python-pip


然后执行如下命令安装编译caffe python wrapper 所需要的额外包

sudo pip install -r /path/to/caffe/python/requirements.txt


在执行上述命令时, 会报错导致不能完全安装所有需要的包(我这里就安装出错了)。 可以按照官方建议安装anaconda包。 在anaconda官网下载.sh文件,执行,最后添加bin目录到环境变量即可。

2.
执行命令:ipython qtconsole --pylab=inline


如果有问题参考:http://blog.csdn.net/huangshanchun/article/details/42083985

3. 下载model后放到相应的位置,但是你要知道具体路径(编程时候要用)

bvlc_reference_caffenet.caffemodel

下载地址:
http://dl.caffe.berkeleyvision.org/
4.执行这个命令后
ipython qtconsole --pylab=inline





测试代码:
import matplotlib.pyplot as plt
%matplotlib inline
caffe_root = '/home/hsc/caffe-master/' #caffe主目录

sys.path.append(caffe_root+'python')
import caffe

model_file = caffe_root+'/models/bvlc_reference_caffenet/deploy.prototxt'
pretrained = caffe_root+'/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
image_file = caffe_root+'examples/images/cat.jpg'
npload = caffe_root+ 'python/caffe/imagenet/ilsvrc_2012_mean.npy'
import caffe

net = caffe.Classifier(model_file, pretrained,maaaaean=np.load(npload),channel_swap=(2,1,0),raw_scale=255,image_dims=(256, 256))
net.set_phase_test()
net.set_mode_gpu()#如果没有gpu net.set_mode_cpu()
input_image = caffe.io.load_image(image_file)
plt.imshow(input_image)
<span style="color:#FF0000;">#下面程序在出现In[2]再执行</span>
prediction = net.predict([input_image])  # predict takes any number of images, and formats them for the Caffe net automatically
print 'prediction shape:', prediction[0].shape
plt.plot(prediction[0])
print 'predicted class:', prediction[0].argmax()

prediction = net.predict([input_image], oversample=False)
print 'prediction shape:', prediction[0].shape
plt.plot(prediction[0])
print 'predicted class:', prediction[0].argmax()
程序中所给的文件路径都都需要给自己不同做出修改。

参考文献:
http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/classification.ipynb
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: