本文主要是介绍caffe训练自己的手写数字,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转自:http://blog.csdn.net/houwenbin1986/article/details/52956101
搭建好caffe Python环境后,我们都需要跑通mnist和imagenet示例,感谢博主:http://www.cnblogs.com/denny402/p/5684431.html
官网提供的mnist数据并不是图片,但我们以后做的实际项目可能是图片。因此有些人并不知道该怎么办。在此我将mnist数据进行了转化,变成了一张张的图片,我们练习就从图片开始。mnist图片数据我放在了百度云盘。
mnist图片数据下载:http://pan.baidu.com/s/1pLMV4Kz
数据分成了训练集(60000张共10类)和测试集(共10000张10类),每个类别放在一个单独的文件夹里。并且将所有的图片,都生成了txt列表清单(train.txt和test.txt)。大家下载下来后,直接解压到当前用户根目录下就可以了。由于我是在windows下压缩的,因此是winrar文件。如果大家要在linux下解压缩,需要安装rar的linux版本,也是十分简单
记录一下自己的实验步骤:
(root) [root@localhost lenet5]# python verify.py
WARNING: Logging before InitGoogleLogging() is written to STDERR
I1028 14:14:45.532413 3195 net.cpp:49] Initializing net from parameters:
name: "Lenet"
input: "data"
input_dim: 1
input_dim: 3
input_dim: 28
input_dim: 28
state {phase: TEST
}
layer {name: "Convolution1"type: "Convolution"bottom: "data"top: "Convolution1"convolution_param {num_output: 20pad: 0kernel_size: 5stride: 1weight_filler {type: "xavier"}}
}
layer {name: "Pooling1"type: "Pooling"bottom: "Convolution1"top: "Pooling1"pooling_param {pool: MAXkernel_size: 2stride: 2}
}
layer {name: "Convolution2"type: "Convolution"bottom: "Pooling1"top: "Convolution2"convolution_param {num_output: 50pad: 0kernel_size: 5stride: 1weight_filler {type: "xavier"}}
}
layer {name: "Pooling2"type: "Pooling"bottom: "Convolution2"top: "Pooling2"pooling_param {pool: MAXkernel_size: 2stride: 2}
}
layer {name: "InnerProduct1"type: "InnerProduct"bottom: "Pooling2"top: "InnerProduct1"inner_product_param {num_output: 500weight_filler {type: "xavier"}}
}
layer {name: "ReLU1"type: "ReLU"bottom: "InnerProduct1"top: "InnerProduct1"
}
layer {name: "InnerProduct2"type: "InnerProduct"bottom: "InnerProduct1"top: "InnerProduct2"inner_product_param {num_output: 10weight_filler {type: "xavier"}}
}
layer {name: "Softmax1"type: "Softmax"bottom: "InnerProduct2"top: "Softmax1"
}
I1028 14:14:45.532510 3195 net.cpp:413] Input 0 -> data
I1028 14:14:45.532536 3195 layer_factory.hpp:77] Creating layer Convolution1
I1028 14:14:45.532551 3195 net.cpp:106] Creating Layer Convolution1
I1028 14:14:45.532555 3195 net.cpp:454] Convolution1 <- data
I1028 14:14:45.532562 3195 net.cpp:411] Convolution1 -> Convolution1
I1028 14:14:45.533006 3195 net.cpp:150] Setting up Convolution1
I1028 14:14:45.533018 3195 net.cpp:157] Top shape: 1 20 24 24 (11520)
I1028 14:14:45.533022 3195 net.cpp:165] Memory required for data: 46080
I1028 14:14:45.533035 3195 layer_factory.hpp:77] Creating layer Pooling1
I1028 14:14:45.533044 3195 net.cpp:106] Creating Layer Pooling1
I1028 14:14:45.533048 3195 net.cpp:454] Pooling1 <- Convolution1
I1028 14:14:45.533054 3195 net.cpp:411] Pooling1 -> Pooling1
I1028 14:14:45.533067 3195 net.cpp:150] Setting up Pooling1
I1028 14:14:45.533073 3195 net.cpp:157] Top shape: 1 20 12 12 (2880)
I1028 14:14:45.533077 3195 net.cpp:165] Memory required for data: 57600
I1028 14:14:45.533082 3195 layer_factory.hpp:77] Creating layer Convolution2
I1028 14:14:45.533088 3195 net.cpp:106] Creating Layer Convolution2
I1028 14:14:45.533093 3195 net.cpp:454] Convolution2 <- Pooling1
I1028 14:14:45.533099 3195 net.cpp:411] Convolution2 -> Convolution2
I1028 14:14:45.533278 3195 net.cpp:150] Setting up Convolution2
I1028 14:14:45.533285 3195 net.cpp:157] Top shape: 1 50 8 8 (3200)
I1028 14:14:45.533290 3195 net.cpp:165] Memory required for data: 70400
I1028 14:14:45.533298 3195 layer_factory.hpp:77] Creating layer Pooling2
I1028 14:14:45.533304 3195 net.cpp:106] Creating Layer Pooling2
I1028 14:14:45.533308 3195 net.cpp:454] Pooling2 <- Convolution2
I1028 14:14:45.533314 3195 net.cpp:411] Pooling2 -> Pooling2
I1028 14:14:45.533323 3195 net.cpp:150] Setting up Pooling2
I1028 14:14:45.533329 3195 net.cpp:157] Top shape: 1 50 4 4 (800)
I1028 14:14:45.533332 3195 net.cpp:165] Memory required for data: 73600
I1028 14:14:45.533336 3195 layer_factory.hpp:77] Creating layer InnerProduct1
I1028 14:14:45.533345 3195 net.cpp:106] Creating Layer InnerProduct1
I1028 14:14:45.533349 3195 net.cpp:454] InnerProduct1 <- Pooling2
I1028 14:14:45.533355 3195 net.cpp:411] InnerProduct1 -> InnerProduct1
I1028 14:14:45.536108 3195 net.cpp:150] Setting up InnerProduct1
I1028 14:14:45.536118 3195 net.cpp:157] Top shape: 1 500 (500)
I1028 14:14:45.536121 3195 net.cpp:165] Memory required for data: 75600
I1028 14:14:45.536130 3195 layer_factory.hpp:77] Creating layer ReLU1
I1028 14:14:45.536136 3195 net.cpp:106] Creating Layer ReLU1
I1028 14:14:45.536140 3195 net.cpp:454] ReLU1 <- InnerProduct1
I1028 14:14:45.536146 3195 net.cpp:397] ReLU1 -> InnerProduct1 (in-place)
I1028 14:14:45.536154 3195 net.cpp:150] Setting up ReLU1
I1028 14:14:45.536159 3195 net.cpp:157] Top shape: 1 500 (500)
I1028 14:14:45.536161 3195 net.cpp:165] Memory required for data: 77600
I1028 14:14:45.536165 3195 layer_factory.hpp:77] Creating layer InnerProduct2
I1028 14:14:45.536171 3195 net.cpp:106] Creating Layer InnerProduct2
I1028 14:14:45.536175 3195 net.cpp:454] InnerProduct2 <- InnerProduct1
I1028 14:14:45.536181 3195 net.cpp:411] InnerProduct2 -> InnerProduct2
I1028 14:14:45.536227 3195 net.cpp:150] Setting up InnerProduct2
I1028 14:14:45.536233 3195 net.cpp:157] Top shape: 1 10 (10)
I1028 14:14:45.536237 3195 net.cpp:165] Memory required for data: 77640
I1028 14:14:45.536243 3195 layer_factory.hpp:77] Creating layer Softmax1
I1028 14:14:45.536250 3195 net.cpp:106] Creating Layer Softmax1
I1028 14:14:45.536254 3195 net.cpp:454] Softmax1 <- InnerProduct2
I1028 14:14:45.536259 3195 net.cpp:411] Softmax1 -> Softmax1
I1028 14:14:45.536270 3195 net.cpp:150] Setting up Softmax1
I1028 14:14:45.536275 3195 net.cpp:157] Top shape: 1 10 (10)
I1028 14:14:45.536279 3195 net.cpp:165] Memory required for data: 77680
I1028 14:14:45.536283 3195 net.cpp:228] Softmax1 does not need backward computation.
I1028 14:14:45.536288 3195 net.cpp:228] InnerProduct2 does not need backward computation.
I1028 14:14:45.536293 3195 net.cpp:228] ReLU1 does not need backward computation.
I1028 14:14:45.536296 3195 net.cpp:228] InnerProduct1 does not need backward computation.
I1028 14:14:45.536300 3195 net.cpp:228] Pooling2 does not need backward computation.
I1028 14:14:45.536304 3195 net.cpp:228] Convolution2 does not need backward computation.
I1028 14:14:45.536309 3195 net.cpp:228] Pooling1 does not need backward computation.
I1028 14:14:45.536314 3195 net.cpp:228] Convolution1 does not need backward computation.
I1028 14:14:45.536317 3195 net.cpp:270] This network produces output Softmax1
I1028 14:14:45.536325 3195 net.cpp:283] Network initialization done.
I1028 14:14:45.539165 3195 net.cpp:816] Ignoring source layer ImageData1
I1028 14:14:45.539448 3195 net.cpp:816] Ignoring source layer SoftmaxWithLoss1
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
the class is: 9
(root) [root@localhost lenet5]#
# -*- coding: utf-8 -*-import caffe
from caffe import layers as L,params as P,proto,to_proto#设定文件的保存路径
root = '/root/AI/lenet5/' #根目录
train_list = root+'mnist/train/train.txt' #训练图片列表
test_list = root+'mnist/test/test.txt' #测试图片列表
train_proto = root+'mnist/train.prototxt' #训练配置文件
test_proto = root+'mnist/test.prototxt' #测试配置文件
solver_proto = root+'mnist/solver.prototxt' #参数文件#编写一个函数,生成配置文件prototxt
def Lenet(img_list,batch_size,include_acc=False):#第一层,数据输入层,以ImageData格式输入data, label = L.ImageData(source=img_list, batch_size=batch_size, ntop=2,root_folder=root,transform_param=dict(scale= 0.00390625))#第二层:卷积层conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=20, pad=0,weight_filler=dict(type='xavier'))#池化层pool1=L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=2, stride=2)#卷积层conv2=L.Convolution(pool1, kernel_size=5, stride=1,num_output=50, pad=0,weight_filler=dict(type='xavier'))#池化层pool2=L.Pooling(conv2, pool=P.Pooling.MAX, kernel_size=2, stride=2)#全连接层fc3=L.InnerProduct(pool2, num_output=500,weight_filler=dict(type='xavier'))#激活函数层relu3=L.ReLU(fc3, in_place=True)#全连接层fc4 = L.InnerProduct(relu3, num_output=10,weight_filler=dict(type='xavier'))#softmax层loss = L.SoftmaxWithLoss(fc4, label)if include_acc: # test阶段需要有accuracy层acc = L.Accuracy(fc4, label)return to_proto(loss, acc)else:return to_proto(loss)def write_net():#写入train.prototxtwith open(train_proto, 'w') as f:f.write(str(Lenet(train_list,batch_size=64)))#写入test.prototxt with open(test_proto, 'w') as f:f.write(str(Lenet(test_list,batch_size=100, include_acc=True)))#编写一个函数,生成参数文件
def gen_solver(solver_file,train_net,test_net):s = proto.caffe_pb2.SolverParameter()s.train_net =train_nets.test_net.append(test_net)s.test_interval = 938 #60000/64,测试间隔参数:训练完一次所有的图片,进行一次测试 s.test_iter.append(100) #10000/100 测试迭代次数,需要迭代100次,才完成一次所有数据的测试s.max_iter = 9380 #10 epochs , 938*10,最大训练次数s.base_lr = 0.01 #基础学习率s.momentum = 0.9 #动量s.weight_decay = 5e-4 #权值衰减项s.lr_policy = 'step' #学习率变化规则s.stepsize=3000 #学习率变化频率s.gamma = 0.1 #学习率变化指数s.display = 20 #屏幕显示间隔s.snapshot = 938 #保存caffemodel的间隔s.snapshot_prefix = root + 'mnist/lenet' #caffemodel前缀s.type ='SGD' #优化算法s.solver_mode = proto.caffe_pb2.SolverParameter.CPU #加速#写入solver.prototxtwith open(solver_file, 'w') as f:f.write(str(s))#开始训练
def training(solver_proto):#caffe.set_device(0)#caffe.set_mode_gpu()caffe.set_mode_cpu()solver = caffe.SGDSolver(solver_proto)solver.solve()if __name__ == '__main__':write_net()gen_solver(solver_proto,train_proto,test_proto) training(solver_proto)
2、训练好模型后,生成识别用的网络模型,脚本 2.mkdeploy.py
# -*- coding: utf-8 -*-import caffe
from caffe import layers as L,params as P,to_protoroot = 'D:/MyWorks/caffe-windows-master/examples/lenet5/'
deploy = root+'mnist/deploy.prototxt' #文件保存路径def create_deploy():#少了第一层,data层conv1 = L.Convolution(bottom='data', kernel_size=5, stride=1,num_output=20, pad=0,weight_filler=dict(type='xavier'))pool1 = L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=2, stride=2)conv2 = L.Convolution(pool1, kernel_size=5, stride=1,num_output=50, pad=0,weight_filler=dict(type='xavier'))pool2 = L.Pooling(conv2, pool=P.Pooling.MAX, kernel_size=2, stride=2)fc3 = L.InnerProduct(pool2, num_output=500,weight_filler=dict(type='xavier'))relu3 = L.ReLU(fc3, in_place=True)fc4 = L.InnerProduct(relu3, num_output=10,weight_filler=dict(type='xavier'))#最后没有accuracy层,但有一个Softmax层prob = L.Softmax(fc4)return to_proto(prob)def write_deploy(): with open(deploy, 'w') as f:f.write('name:"Lenet"\n')f.write('input:"data"\n')f.write('input_dim:1\n')f.write('input_dim:3\n')f.write('input_dim:28\n')f.write('input_dim:28\n')f.write(str(create_deploy()))if __name__ == '__main__':write_deploy()
3、最后是读取待识别的图片进行数字识别,脚本 3.verify.py
#coding=utf-8import caffe
import numpy as nproot = 'D:/MyWorks/caffe-windows-master/examples/lenet5/' #根目录
deploy = root + 'mnist/deploy.prototxt' #deploy文件
caffe_model = root + 'mnist/lenet_iter_9380.caffemodel' #训练好的 caffemodel
img = root + 'mnist/test/9/00479.png' #随机找的一张待测图片
labels_filename = root + 'mnist/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变为BGRim = caffe.io.load_image(img) #加载图片
net.blobs['data'].data[...] = transformer.preprocess('data',im) #执行上面设置的图片预处理操作,并将图片载入到blob中#执行测试
out = net.forward()labels = np.loadtxt(labels_filename, str, delimiter='\t') #读取类别名称文件
prob= net.blobs['Softmax1'].data[0].flatten() #取出最后一层(Softmax)属于某个类别的概率值,并打印
print prob
order=prob.argsort()[-1] #将概率值排序,取出最大值所在的序号
print 'the class is:',labels[order] #将该序号转换成对应的类别名称,并打印
这篇关于caffe训练自己的手写数字的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!