Caffe-python Interface Learning | Network training, deployment, testing

Source: Internet
Author: User
Tags jupyter notebook

Continue the learning of the Python interface. There are also solver, deploy file generation and model testing.

Network Training Solver file generation

In fact, I think that using Python to generate solver is not as straightforward as writing a configuration file, it's not as much of a recurring thing as a net configuration.
For the Solver configuration file:

BASE_LR:0.001Display782Gamma0.1Lr_policy: "Step" Max_iter:78200 #训练样本迭代次数 =max_iter/782 (iterative algebra of all samples after training)Momentum0.9Snapshot7820Snapshot_prefix:"Snapshot"Solver_mode:GPUsolver_type:SGDstepsize:26067Test_interval:782 #test_interval = number of training samples (50000)/batch_size (train:64)Test_iter:313     #test_iter = Test specimen number (10000)/batch_size (test:32)Test_net:"/home/xxx/data/val.prototxt"Train_net:"/home/xxx/data/proto/train.prototxt"Weight_decay:0.0005

The build can be implemented in the following ways:

 fromCaffe.protoImportCaffe_pb2s = Caffe_pb2. Solverparameter () path='/home/xxx/data/'solver_file=path+' Solver1.prototxt 'S.train_net = path+' Train.prototxt 'S.test_net.append (path+' Val.prototxt ') S.test_interval =782S.test_iter.append (313)#这里用的是append, the code wind is not quite the sameS.max_iter =78200S.BASE_LR =0.001S.momentum =0.9S.weight_decay =5e-4S.lr_policy =' Step 'S.stepsize=26067S.gamma =0.1S.display =782S.snapshot =7820S.snapshot_prefix =' Shapshot 'S.type = "SGD" S.solver_mode = caffe_pb2. Solverparameter.gpu withOpen (Solver_file,' W ') asF:f.write (str (s))

is not much simpler.


It is important to note that some of the parameters need to be calculated:

    • Test_interval:
      Let's say we have 50,000 training samples. Batch_size is 64. That is, the processing of 64 samples per batch requires an iterative 50000/64=782 to process all samples. We have finished processing all the samples, called Generations, epoch. So. The test_interval here is set to 782, after all the training data has been processed. Before going to the test.

      Let's say we want to train 100 generations. You need to set Max_iter to 78200.

    • Test_iter:
      Similarly, assuming that there are 10,000 test samples, Batch_size set to 32, then the need to iterate 10000/32=313 times to complete the test once. So set Test_iter to 313.
    • Lr_rate:
      The rule of learning rate change we set it down as the number of iterations is added. The total iteration 78,200 times, we will change lr_rate three times. So stepsize is set to 78200/3=26067. That is, 26,067 times per iteration, we reduce the one-time learning rate.

Model Training

Complete the training as defined by the network and solver, just like the command line:

solver = caffe.SGDSolver(‘/home/xxx/solver.prototxt‘)solver.solve()

You can just get a little more granular, for example, to load the model first:

solver = caffe.get_solver(‘models/bvlc_reference_caffenet/solver.prototxt‘)

It's used here .get_solver . The default is solved according to the SGD method.


Propagate the network forward. That is, from the input layer to the loss layer, compute net.blobs[k].data .

solver.net.forward()  # train net

The reverse propagation of a network, that is, from the loss layer to the input layer, calculation net.blobs[k].diff and net.params[k][j].diff .

solver.net.backward()

Assume that a complete calculation is required, forward, reverse, and update weights ( net.params[k][j].data ). be able to use

solver.step(1)

Change the number for multiple calculations.

Network deployment

Deployment generates a deploy file for the following model tests.

You can also change the net file directly using Python.

 fromCaffeImportLayers asL,params asp,to_protoroot='/home/xxx/'deploy=root+' Mnist/deploy.prototxt '    #文件保存路径 def create_deploy():    #少了第一层. Data LayerConv1=l.convolution (bottom=' Data ', kernel_size=5, stride=1, num_output= -, 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= -, 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= -, Weight_filler=dict (type=' Xavier ')) Relu3=l.relu (FC3, in_place=True) FC4 = L.innerproduct (RELU3, num_output=Ten, Weight_filler=dict (type=' Xavier '))#最后没有accuracy层, but there is a softmax layerProb=l.softmax (FC4)returnTo_proto (Prob) def write_deploy():      withOpen (Deploy,' W ') asF: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 ()

Suppose you change net. Need to change data entry:

layer {  "data"  "Input"  "data"  dim1dim3dim100dim100 } }}

and add a Softmax. For the original Softmaxwithloss can be directly replaced.

Network test

Get the model after training well. The actual use is the need to use a model to predict.

You need to use the deploy file and Caffemodel.

#coding =utf-8ImportCaffeImportNumPy asnproot='/home/xxx/'   #根文件夹Deploy=root +' Mnist/deploy.prototxt '    #deploy文件Caffe_model=root +' Mnist/lenet_iter_9380.caffemodel '   #训练好的 Caffemodelimg=root+' Mnist/test/5/00008.png '    #随机找的一张待測图片Labels_filename = root +' Mnist/test/labels.txt '  #类别名称文件, convert a numeric label back to the category nameNET = 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))#改变维度的顺序, changed from original picture (28,28,3) to (3,28,28)#transformer. Set_mean (' Data ', Np.load (mean_file). Mean (1). Mean (1)) #减去均值. There's no mean reduction in the previous training model.Transformer.set_raw_scale (' Data ',255)# Zoom to "0. 255 "BetweenTransformer.set_channel_swap (' Data ', (2,1,0))#交换通道, change the image from RGB to BGRIm=caffe.io.load_image (IMG)#载入图片net.blobs[' Data '].data[...] = transformer.preprocess (' Data ', IM)#运行上面设置的图片预处理操作 and load the picture into the blob#运行測试out = Net.forward () labels = np.loadtxt (labels_filename, str, delimiter=' \ t ')#读取类别名称文件prob= net.blobs[' Softmax1 '].data[0].flatten ()#取出最后一层 (Softmax) belongs to a category of probability values and printsPrintProborder=prob.argsort () [-1]#将概率值排序, remove the number of the maximum valuePrint ' The class is: ', Labels[order]#将该序号转换成相应的类别名称, and print
Summarize

Using the Python interface, you can have a more comprehensive understanding and understanding of the network's detailed parameters. There are just a few points to note:

    1. Conversion of data formats
      The Caffe Data blob shape is n*c*h*w. Number of channels in front. While the python image is processed, shape is h*w*c. Number of channels in the rear.

      So we need to switch.

    2. Picture Display and save
      Because there is no graphical interface, very convenient jupyter notebook can not be used, just good to save the picture view.

Caffe Python Interface Learning (2): Generating a Solver file
Caffe Python Interface Learning (5): Generating a deploy file
Caffe Python Interface Learning (6): Use a well-trained model (Caffemodel) to classify new images
Deep Learning tutorial on Caffe technology:basic commands, Python and C + + code.
Multilabel classification on PASCAL using Python data-layers

Caffe-python Interface Learning | Network training, deployment, testing

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.