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

来源:互联网 发布:淘宝海报怎么放 编辑:程序博客网 时间:2024/05/22 00:21

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")>>> carray([[ 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