caffe's python interface learning (4) mnist example handwritten digit recognition

The following are mainly excerpts from denny's blog post. For more content, please go to the original author.  

a data preparation

  Prepare a list of training set and test set images;

  Second, import the caffe library and set the file path

  

# -*- coding: utf-8 -*-

import caffe from caffe import layers as L,params as P,proto,to_proto #Set the file save path root= ' /home/xxx/ ' #root directory train_list=root+ ' mnist/train/train.txt ' #training picture List test_list=root+ ' mnist/test/test.txt ' #Test image list train_proto=root+ ' mnist/train.prototxt ' #Training configuration file test_proto=root+ ' mnist/test.prototxt ' #Testing configuration file solver_proto=root+ ' mnist /solver.prototxt ' #parameter file
copy code

The train.txt and test.txt files already exist, and the other three files need to be written by ourselves.

Note here: Generally, the caffe program converts the image into an lmdb file first, but it is a bit troublesome to do so. Therefore, I will not convert it. I will operate directly with the original image. The difference is that I will operate directly with the image. The mean value is difficult to calculate, so the mean value can not be subtracted.

  Three generated configuration files

  

The configuration files are actually some txt documents, but the suffix name is prototxt, we can write them directly in the editor, or generate them with code. Here, I use python to generate.

copy code
#Write a function to generate a configuration file prototxt
 def Lenet(img_list,batch_size,include_acc= False): #The first layer, data input layer, input data in ImageData format, label = L.ImageData(source=img_list, batch_size=batch_size, ntop=2 ,root_folder=root, transform_param=dict(scale=0.00390625 )) #Second layer: convolutional layer conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=20, pad=0,weight_filler =dict(type= ' xavier ' )) #pooling layer pool1=L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=2, stride=2 ) #convolutional layer conv2=L.Convolution(pool1, kernel_size=5, stride=1,num_output=50, pad=0,weight_filler=dict(type= ' xavier ' )) #Pooling layer pool2=L.Pooling(conv2, pool=P.Pooling.MAX, kernel_size=2, stride=2 ) #Full connection layer fc3=L.InnerProduct(pool2, num_output=500,weight_filler=dict(type= ' xavier ' )) #Activation function layer relu3=L.ReLU(fc3, in_place= True) #Full connection layer fc4 = L.InnerProduct(relu3, num_output=10,weight_filler=dict(type= ' xavier ' )) #softmax layer loss = L.SoftmaxWithLoss(fc4, label) if include_acc: # The test phase requires an accuracy layer acc = L.Accuracy(fc4, label) return to_proto(loss, acc) else : return to_proto(loss) def write_net(): #写入train.prototxt with 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)))
copy code

What is stored in the configuration file is what we call the network. The network I generated here may not be the same as the original Lenet, but it has little effect.

  Four generate solver files

  

Similarly, you can write directly in the editor, or you can use code generation.

copy code
#Write a function to generate a parameter file
 def gen_solver(solver_file,train_net,test_net): s=proto.caffe_pb2.SolverParameter() s.train_net = train_net s.test_net.append(test_net) s.test_interval = 938 # 60000/64, test interval parameter: after training all images once, perform a test s.test_iter.append(100) #1 0000/100 The number of test iterations, it takes 100 iterations to complete a test of all data s.max_iter = 9380 # 10 epochs , 938*10, the maximum number of training s.base_lr = 0.01 #basic learning rate s.momentum = 0.9 #momentum s . weight_decay = 5e-4 #weight decay term s.lr_policy = ' step ' #learning rate change rule s.stepsize=3000 #learning rate change frequency s.gamma = 0.1 #learning rate change index s.display = 20 #screen display Interval s.snapshot = 938 #Save the interval of caffemodel s.snapshot_prefix =root+ 'mnist/ lenet '# caffemodel prefix s.type = ' SGD ' #optimization algorithm s.solver_mode = proto.caffe_pb2.SolverParameter.GPU #acceleration #write solver.prototxt with open(solver_file, ' w ' ) as f: f.write(str( s))

copy code  

  Five to start training the model

  

During the training process, it is also constantly tested.

#Start training
 def training(solver_proto): caffe.set_device(0) caffe.set_mode_gpu() solver = caffe.SGDSolver(solver_proto) solver.solve()

Finally, just call the above function.

if __name__ == '__main__': write_net() gen_solver(solver_proto,train_proto,test_proto) training(solver_proto)

  Six completed python files

  

mnist.py

View Code

 

I put this file under the mnist folder in the root directory, so it can be executed with the following code

sudo python mnist/mnist.py

During training, some caffemodels are saved. How often to save and how many times to save can be set in the solver parameter file.

I set it to train for 10 epochs, more than 9000 times, and the test accuracy can reach 99%

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325220815&siteId=291194637