caffe中 cifar10案例(二)使用模型

1. 准备素材

a、一张猫图片cat-test.jpeg

b、创建标签文件

$ gedit /opt/caffe-master/data/cifar10/cifar10_words.txt

加入:
0 airplane
1 automobile
2 bird
3 cat
4 deer
5 dog
6 frog
7 horse
8 ship
9 truck

c、准备classifer.py文件
caffe本身带了一个classify.py文件,但出来的结果存在foo.npy文件中,看不懂,只好自己在网上找一个。由于那个版本较老,与新版的库不兼容。作者在原版的基础上稍作修改。
具体文件,请在这里下载。

2. 操作

$  cd  $CAFFE_HOME

$ python python/classify.py --print_results --model_def examples/cifar10/cifar10_quick.prototxt --pretrained_model examples/cifar10/cifar10_quick_iter_5000.caffemodel.h5 --labels_file data/cifar10/cifar10_words.txt  --center_only  examples/images/ cat-test.jpeg foo

这里写图片描述

3. 问题
上述命令产生的结果是foo.npy。这是一个numpy格式的矩阵文件,读出来内容如下:

>>> import numpy as np
>>> c = np.load("foo.npy")
>>> c
array([[ 0.0259649 ,  0.00272303,  0.02379645,  0.3592225 ,  0.03843106,
         0.39542419,  0.04642963,  0.03978103,  0.04933688,  0.01889039]], dtype=float32)

问题来了:这个矩阵是啥意思涅?

4. 调试
报错: Mean shape incompatible with input shape

原因是‘mean’与‘input’的尺寸不同造成的。修改 ./python/caffe/io.py 文件。

找到:
if ms != self.inputs[in_][1:]:
raise ValueError(‘Mean shape incompatible with input shape.’)

修改为:
if ms != self.inputs[in_][1:]:
print(self.inputs[in_])
in_shape = self.inputs
m_min, m_max = mean.min(), mean.max()
normal_mean = (mean - m_min) / (m_max - m_min)
mean = resize_image(normal_mean.transpose((1,2,0)),
in_shape[1:]).transpose((2,0,1)) * \
(m_max - m_min) + m_min
#raise ValueError(‘Mean shape incompatible with input shape.’)

5. 参考
以上方法,只是其中一种,下面引用的是一个大牛的博客,请参考:
http://www.cnblogs.com/denny402/p/5111018.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值