caffe 使用python测试ImageNet classification

环境: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


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值