Caffe mnist训练、参数打印、识别示例及可能错误记录

前提假设你的caffe已经配置好了,并且编译了pycaffe接口。

一、mnist训练

1、得到数据
cd ~/caffe

sudo sh data/mnist/get_mnist.sh

2、制作符合caffe要求的数据

sudo sh examples/mnist/create_mnist.sh

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
此段主要是当时运行程序有些问题及处理方法
当时不方便打汉字,采用了中式英文注解~~
bad luck,terminal appear these:
Creating lmdb...
examples/mnist/create_mnist.sh: 17: examples/mnist/create_mnist.sh: build/examples/mnist/convert_mnist_data.bin: not found
solve method see following website:
https://blog.csdn.net/wangxing233/article/details/51734080
but it not solve this

following website :
https://blog.csdn.net/huaxie3355/article/details/71055704
can solve the problem

cd caffe
make all -j4

so,do this ,problem is solved
following website good
https://blog.csdn.net/dcxhun3/article/details/51859740#commentBox
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

3、修改lenet_solver.prototxt和train_lenet.prototxt 网络配置
cd ~/caffe
subl ./examples/mnist/lenet_solver.prototxt
设置 solver_mode: CPU ,使用GPU就不用改了
train_lenet.prototxt 可以不用修改

4、训练网络得到caffemodel
cd ~/caffe
sudo time sh examples/mnist/train_lenet.sh

二、参数打印

mnist的参数打印在此只做两个方面,画出网络图和打印网络 .caffemodel中的部分数据

1、画出训练网络的结构图

cd ~/caffe/python
python draw_net.py ../examples/mnist/lenet_train_test.prototxt lenet.png

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
画网络过程可能会出现如下报错:
Traceback (most recent call last):
  File "draw_net.py", line 94, in <module>
    main()
  File "draw_net.py", line 90, in main
    phase)
  File "/home/pikachu/caffe/intel/caffe/python/caffe/draw.py", line 280, in draw_net_to_file
    fid.write(draw_net(caffe_net, rankdir, ext, phase))
  File "/home/pikachu/caffe/intel/caffe/python/caffe/draw.py", line 259, in draw_net
    return get_pydot_graph(caffe_net, rankdir, phase=phase).create(format=ext)
  File "/home/pikachu/.local/lib/python2.7/site-packages/pydot.py", line 1883, in create
    prog=prog))
Exception: "dot" not found in path.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

这是因为在编译pycaffe 之前安装的一些依赖库不全造成的,缺啥就安装啥吧,这里缺少dot,因此执行:

sudo pip install pydot
执行这条命令后,再此执行:
cd ~/caffe/python

画图lenet_train_test.prototxt网络结构如下:

python draw_net.py ../examples/mnist/lenet_train_test.prototxt lenet_train_test.png

这里还是贴出一张图吧



画图lenet.prototxt网络结构如下:

python draw_net.py ../examples/mnist/lenet.prototxt lenet.png


有时安装pydot后,还可能需要安装graphviz:

sudo apt install graphviz

2、打印训练好的caffemodel中一些参数

这个可以参考这个:

https://blog.csdn.net/huobanjishijian/article/details/78478957

点击打开链接

需要注意的是:

root = '/caffe/examples/mnist/' 此处caffe是在根目录下,不是home

这个路径一定要是绝对路径,一般我们会把caffe放在home,因此上述路径一定要改成对应的就可以了,不然会报错,我刚开始就是把程序拿来一看觉得好像对着,直接跑后就报错了,找了好一会儿才发现路径不对。

三、识别示例

1、准备28x28黑底白字的图片

因为训练使用的是28x28黑底白字的图片,因此识别也要是这样(具体原有除了尺寸统一方面的原因外其他不清楚为啥这样做),你说突然来个28x28的黑底白字且含有数字的图片,一时还真不好弄,最好的还是采用原作者的那一对图片吧,终于在网上找到了这些图片,我把它放在百度网盘上去,需要的可以去这里免费下载:

链接: https://pan.baidu.com/s/14hNlWjbZUkYoqrVh4PLIhw 密码: hbk6  

2、编写识别过程代码

在sublime下编写识别代码,如下:

import caffe
import os 
import sys
import numpy as np 
from PIL import Image
import matplotlib.pyplot as plt 

CaffePath='/home/dq/caffe/'
sys.path.insert(0,CaffePath+'python')
ModelFile=CaffePath+'examples/mnist/lenet.prototxt'
CaffeModel=CaffePath+'examples/mnist/lenet_iter_10000.caffemodel'
ImPath=CaffePath+'examples/images/MNIST/testimage/pic2/0/1.bmp'
InputIm=caffe.io.load_image(ImPath,color=False)
net=caffe.Classifier(ModelFile,CaffeModel)
Prediction=net.predict([InputIm],oversample=False)
caffe.set_mode_cpu()
print 'predict class is',Prediction[0].argmax()
###########下面这段代码在我电脑上运行不了,一时没找到原因先记录###############
“”“”“
img = Image.open(ImPath)
plt.figure("Image") # 图像窗口名称
plt.imshow(img)
plt.axis('on') # 关掉坐标轴为 off
plt.title('image') # 图像题目
plt.show()
”“”“”

这里以识别0为例子。

ctrl+b 运行程序

输出:

predict class is 0

ubuntu不方便截图,就不上图了。

至此mnist训练识别以及.caffemodel数据打印都大体走了一遍

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值