caffe training (7) generating deploy file

caffe training (7)


If you want good training model used to test a new picture, it must have to be a deploy.prototxt file, the file is actually test.prototxt files and almost, but not identical but also head and tail.

In the classification, deploy file has no first tier data input layer, there is no final Accuracy layer, but in the end more than a probability Softmax layer.

Here we use the code to automatically generate the file to mnist example.

deploy.py Code

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

from caffe import layers as L,params as P,to_proto
root='D:/caffe-master/zzhld/'
deploy=root+'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()

After running the file, the input file in the directory, generate a deploy.prototxt file.

Here Insert Picture Description
This file is not recommended for use to generate the code, but trouble. After becoming acquainted with the test.prototxt we can copy, modify the appropriate place can be, and more convenient.

Published 61 original articles · won praise 15 · views 960

Guess you like

Origin blog.csdn.net/weixin_42535423/article/details/103833174