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

Source: Internet
Author: User
Tags diff 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 does not have as many duplicates as the 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 = number of test samples (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 parameters need to be calculated:

    • Test_interval:
      Assuming we have 50,000 training samples, Batch_size is 64, which is 64 samples per batch, we need to iterate 50000/64=782 times to process all the samples. We have finished processing all the samples, called Generations, epoch. So, the test_interval here is set to 782, which means that once all the training data has been processed, the test is done. If we want to train 100 generations, we need to set Max_iter to 78200.
    • Test_iter:
      Similarly, if you have 10,000 test samples, Batch_size is set to 32, then you need to iterate 10000/32=313 times to complete the test once, so set Test_iter to 313.
    • Lr_rate:
      The change of learning rate we set it down slowly as the number of iterations increases. A total of 78,200 iterations, we will change lr_rate three times, so Stepsize set to 78200/3=26067, that is, 26,067 times per iteration, we will reduce the 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()

But you can also get a little more granular, such as loading the model first:

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

Here, the default is solved by the .get_solver SGD method.
Propagates the network forward, that is, from the input layer to the loss layer, computing 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()

If you need a complete calculation, forward, reverse, and update weights ( net.params[k][j].data ), you can use the

solver.step(1)

Change the number for multiple calculations.

Network deployment

Deployment generates a deploy file for the following model tests. You can either use Python or modify the net file directly.

 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 ()

If you modify NET, you need to modify the data entry:

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

and add a Softmax, for the original Softmaxwithloss directly replaced on the line.

Network test

After training to get the model, the actual use is to use the model to predict. The deploy file and Caffemodel are required.

#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 is no reduction in the mean value of the previous training model.Transformer.set_raw_scale (' Data ',255)# Zoom to "0,255"Transformer.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 Python interface, the specific parameters of the network can be more comprehensive understanding and understanding. However, there are several points to note:

    1. Conversion of data formats
      The Caffe Data blob shape is n*c*h*w, with the number of channels in front. While the python image is processed, shape is h*w*c, and the number of channels is behind. So you need to switch.
    2. Picture Display and save
      Because there is no graphical interface, very convenient jupyter notebook can not be used, had to save pictures to 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

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.