Caffe Python Interface Learning (4) Mnist instance handwritten digit recognition

Source: Internet
Author: User

The following is mainly excerpt Denny blog content, more content everyone to see the original author it

A data preparation

Prepare a list of training set and test set pictures;

Two import Caffe Library, set file path

  

#-*-Coding:utf-8-*-ImportCaffeFrom CaffeImportLayers as l,params as P,proto,to_proto#Save path to set file root=‘/home/xxx/‘#Root directory train_list=root+‘Mnist/train/train.txt‘# training picture List Test_list=root+mnist/test/test.txt "#< Span style= "COLOR: #008000" > test picture list Train_proto=root+ '  Mnist/train.prototxt "# Training profile Test_proto=root+ "mnist/test.prototxt ' # test configuration file Solver_proto=root+ ' mnist/solver.prototxt "# parameter file        

where Train.txt and test.txt files already have, the other three files that we need to write ourselves.

Note here: The General Caffe program is the first to convert the picture into a Lmdb file, but this is a bit cumbersome. So I do not convert, I directly with the original image to operate, the difference is directly with the picture operation, the mean value is difficult to calculate, so you can not reduce the mean.

Three build configuration Files

  

The configuration file is actually some txt document, but the suffix name is prototxt, we can write it directly into the editor, or it can be generated in code. Here, I use Python to build.

#Write a function that generates a configuration file Prototxtdef Lenet (img_list,batch_size,include_acc=False):#The first layer, the data entry layer, enters the 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: Convolution layer conv1=l.convolution (data, kernel_size=5, stride=1,num_output=20, Pad=0,weight_filler=dict (type=‘Xavier‘))#Pool Layer pool1=l.pooling (CONV1, Pool=p.pooling.max, kernel_size=2, stride=2)#Convolution layer conv2=l.convolution (pool1, kernel_size=5, stride=1,num_output=50, Pad=0,weight_filler=dict (type=‘Xavier‘))#Pool Layer pool2=l.pooling (Conv2, Pool=p.pooling.max, kernel_size=2, stride=2)#Fully connected layer fc3=l.innerproduct (pool2, Num_output=500,weight_filler=dict (type=‘Xavier‘))#Activate 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 a accuracy layer ACC =L.accuracy (FC4, label)Return To_proto (loss, ACC) else: return To_proto (loss) def  Write_net (): # Write Train.prototxt with open (Train_proto,  ' w" ) as F:f.write (str (Lenet (train_list,batch_size=64# write Test.prototxt with open (Test_proto, Span style= "COLOR: #800000" > ' w " ) as F:f.write (str (Lenet (test_list,batch_size=100, Include_acc=true)))    

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 doesn't make much difference.

Quad Build Solver file

  

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

#Write a function to generate the parameter fileDefGen_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: Once all the pictures are trained, one Test s.test_iter.append (100)#10000/100 test iterations, need to iterate 100 times to complete the test of all data once s.max_iter = 9380#epochs, 938*10, maximum training times 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 Caffemodel interval s.snapshot_prefix =root+ ' mnist/lenet< Span style= "COLOR: #800000" > ' #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))    

  

Five-start training model

  

During the training process, the tests are also constantly being tested.

# Start training def Training (Solver_proto):    caffe.set_device (0)    Caffe.set_mode_gpu ()    solver =  Caffe. Sgdsolver (Solver_proto)    solver.solve ()   

Finally, you can call the above function.

'__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 you can use the following code to execute

sudo python mnist/mnist.py

During the training process, some Caffemodel are saved. How often to save, save how many times, can be set in the Solver parameter file.

I set the training to epoch,9000 multiple times, the test accuracy can reach 99%

Caffe Python Interface Learning (4) Mnist instance handwritten digit recognition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.