caffe初体验:通过mnist例子对caffe训练模型及使用模型进行了解
说明
记录一下小白的我使用caffe对mnist例子进行的一系列操作。参考了很多博文,算是对这些博文进行一个详细的整合,方便以后自己回顾。
参考博文及问题解决
1. win10安装虚拟机和ubantu来配置caffe环境
参考博文:
https://www.cnblogs.com/acgoto/p/11570188.html
按照上面的博文一步步安装的,中间有出现问题,最后都通过百度解决掉了。(当时没想着会记录,遇到的问题记不清是啥了…)
caffe配置好就可以进行下一步啦!
2. 训练caffe自带的mnist数据集
参考博文:
https://www.cnblogs.com/ranxf/p/11510810.html
caffe/example/mnist下 有一个readme.md 文件可参考。
问题及解决:
当时在下载数据集就遇到了问题,如果运行脚本下载不可,可去官网下载数据集解压放到相应文件夹。
3. 使用mnist数据集训练好的模型进行测试
参考博文:
[1]https://blog.csdn.net/xunan003/article/details/73126425
[2]https://www.cnblogs.com/denny402/p/5685909.html#!comments
[3]https://blog.csdn.net/wangkun1340378/article/details/78360606
需要准备的文件如下:
- 3.jpeg、labels.txt参考博文[1];
- deploy.prototxt其实就是examples/mnist/lenet.prototxt,复制一份重命名即可;
- lenet_iter_10000.caffemodel就是上一步训练好的模型;
- test.py参考博文[2],根据这篇博文下的评论,对代码里出现的小错误进行了修正。代码如下:
#coding=utf-8
import caffe
import numpy as np
root='/home/ubantu/caffe/' #根目录
deploy=root + 'test/deploy.prototxt' #deploy文件
caffe_model=root + 'test/lenet_iter_10000.caffemodel' #训练好的 caffemodel
img=root+'test/3.jpeg' #随机找的一张待测图片
labels_filename = root + 'test/labels.txt' #类别名称文件,将数字标签转换回类别名称
net = caffe.Net(deploy,caffe_model,caffe.TEST) #加载model和network
#图片预处理设置
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) #设定图片的shape格式(1,3,28,28)
transformer.set_transpose('data', (2,0,1)) #改变维度的顺序,由原始图片(28,28,3)变为(3,28,28)
#transformer.set_mean('data', np.load(mean_file).mean(1).mean(1)) #减去均值,前面训练模型时没有减均值,这儿就不用
transformer.set_raw_scale('data', 255) # 缩放到【0,255】之间
#transformer.set_channel_swap('data', (2,1,0)) #交换通道,将图片由RGB变为BGR
#im=caffe.io.load_image(img) #加载图片
im=caffe.io.load_image(img,False)
net.blobs['data'].data[...] = transformer.preprocess('data',im) #执行上面设置的图片预处理操作,并将图片载入到blob中
#执行测试
out = net.forward()
labels = np.loadtxt(labels_filename, str, delimiter='\t') #读取类别名称文件
prob= net.blobs['prob'].data[0].flatten() #取出最后一层(Softmax)属于某个类别的概率值,并打印
print prob
order=prob.argsort()[-1] #将概率值排序,取出最大值所在的序号
print 'the class is:',labels[order] #将该序号转换成对应的类别名称,并打印
以上文件准备好后,在终端cd到caffe下输入以下命令:
python ./test/test.py
运行得到结果:
/usr/local/lib/python2.7/dist-packages/skimage/io/_io.py:49: UserWarning: `as_grey` has been deprecated in favor of `as_gray`
warn('`as_grey` has been deprecated in favor of `as_gray`')
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
the class is: 3
测试成功!
问题及解决:
1.第一次运行脚本出现以下错误:
[libprotobuf ERROR google/protobuf/text_format.cc:274] Error parsing text-format caffe.NetParameter: 1:15: Interpreting non ascii codepoint 194.
[libprotobuf ERROR google/protobuf/text_format.cc:274] Error parsing text-format caffe.NetParameter: 1:15: Expected identifier.
F0417 17:35:12.922912 5427 upgrade_proto.cpp:90] Check failed: ReadProtoFromTextFile(param_file, param) Failed to parse NetParameter file: /home/ubantu/caffe/test/deploy.prototxt
第一次生成的deploy.prototxt是直接从博文[1]复制粘贴的,参考博文[3]解决了这个问题。
估计当时复制粘贴的时候格式出了问题。(太懒了,自己把头尾改掉不行吗?懒就要付出代价…)
不过后来我发现这个文件就是mnist例子的lenet.prototxt,直接重命名就ok啦。
2.第二次运行脚本测试结果出了问题:
/usr/local/lib/python2.7/dist-packages/skimage/io/_io.py:49: UserWarning: `as_grey` has been deprecated in favor of `as_gray`
warn('`as_grey` has been deprecated in favor of `as_gray`')
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
the class is: 5
我第一次脑抽用了5000的模型,不过改成10000的模型就ok啦。
超级感谢大家的分享与帮助啊!