caffe生成lenet-5的deploy.prototxt文件

接前面博客:http://blog.csdn.net/lanxuecc/article/details/52329708 我学会了用caffe训练自己的测试自己的图片,但是这里测试的是测试数据集,那么如何用训练好的caffemodel测试自己的单张图片呢。下面记录下我用训练好的lenet_iter_10000.caffemodelg来测试mnist图片的整个摸索过程::::

生成deploy.prototxt文件:

用训练好的caffemodel来测试单张图片需要一个deploy.prototxt文件来指定网络的模型构造。
事实上deploy.prototxt文件与lenet_train_test.prototxt文件类似,只是首尾有些差别。仿照博客http://www.cnblogs.com/denny402/p/5685818.html 中的教程用deploy.py文件来生成deploy.prototxt文件。

# -*- coding: utf-8 -*-
caffe_root = '/home/schao/sc_tmp/caffe/caffe-master/'  
import sys  
sys.path.insert(0, caffe_root + 'python')  
from caffe  import layers as L,params as P,to_proto
root='/home/schao/sc_tmp/caffe/caffe-master/'
deploy='/home/schao/sc_tmp/caffe/caffe-master/examples/mnist/deploy.prototxt'    #文件保存路径

def create_deploy():
    #少了第一层,data层
    conv1=L.Convolution(name='conv1',bottom='data', kernel_size=5, stride=1,num_output=20, pad=0,weight_filler=dict(type='xavier'))
    pool1=L.Pooling(conv1,name='pool1',pool=P.Pooling.MAX, kernel_size=2, stride=2)
    conv2=L.Convolution(pool1, name='conv2',kernel_size=5, stride=1,num_output=50, pad=0,weight_filler=dict(type='xavier'))
    pool2=L.Pooling(conv2, name='pool2',top='pool2', pool=P.Pooling.MAX, kernel_size=2, stride=2)
    fc3=L.InnerProduct(pool2, name='ip1',num_output=500,weight_filler=dict(type='xavier'))
    relu3=L.ReLU(fc3, name='relu1',in_place=True)
    fc4 = L.InnerProduct(relu3, name='ip2',num_output=10,weight_filler=dict(type='xavier'))
    #最后没有accuracy层,但有一个Softmax层
    prob=L.Softmax(fc4, name='prob')
    return to_proto(prob)
def write_deploy(): 
    with open(deploy, 'w') as f:
        f.write('name:"LeNet"\n')
        f.write('layer {\n')
        f.write('name:"data"\n')
        f.write('type:"Input"\n')
        f.write('input_param { shape : {')
        f.write('dim:1 ')
        f.write('dim:3 ')
        f.write('dim:28 ')
        f.write('dim:28 ')
        f.write('} }\n\n')
        f.write(str(create_deploy()))
if __name__ == '__main__':
    write_deploy()

生成的deploy.prototxt文件如下

name: "LeNet"
/*原来训练与测试两层数据层*/
/*layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_train_lmdb"
    batch_size: 64
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}*/

/*被替换成如下*/

layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 1 dim: 28 dim: 28 } }
}

/*卷积层与全连接层中的权值学习率,偏移值学习率,偏移值初始化方式,因为这些值在caffemodel文件中已经提供*/
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
  }
}

/*删除了原有的测试模块的测试精度层*/

/*输出层的类型由SoftmaxWithLoss变成Softmax,训练是输出时是loss,应用时是prob。*/
layer {
  name: "prob"
  type: "Softmax"
  bottom: "ip2"
  top: "prob"
}

总得来说,deploy.prototxt就是在lenet_train_test.prototxt的基础上稍作改动,input_param { shape: { dim: 1 dim: 1 dim: 28 dim: 28 } } 这四个dim参数分别是
第一个:对待识别样本图片进行数据增广的数量,一个图片会变成10个,之后输入到网络进行识别。如果不进行数据增广,可以设置成1。
第二个:图片的通道数,一般灰度图片为单通道,则值为1,如果为非灰度图3通道图片则为3。
第三个:图片的高度,单位像素。
第四个:图片的宽度,单位像素。

对deploy.prototxt的理解来源于:
http://blog.csdn.net/ddqqfree123/article/details/52389337
http://www.cnblogs.com/daihengchen/p/5761304.html
http://caffecn.cn/?/question/431
http://blog.csdn.net/sunshine_in_moon/article/details/49472901
http://stackoverflow.com/questions/36002387/channel-swap-needs-to-have-the-same-number-of-dimensions-as-the-input-channels-e/36075282
http://blog.csdn.net/u010417185/article/details/52137825
http://www.cnblogs.com/denny402/p/5685818.html

执行

python deploy.py

生成deploy.prototxt。在生成过程中我遇到以下几个报错

报错: ImportError: No module named caffe
解决
这种情况一般是没有把caffe中的和python相关的内容的路径添加到python的编译路径中。所以在deploy.py文件开始部位加入

caffe_root = '/home/schao/sc_tmp/caffe/caffe-master/'  
import sys  
sys.path.insert(0, caffe_root + 'python')  

指定caffe源码所在路径。

报错:ImportError: liblapack.so.3 cannot open shared object file:No such file or directory
解决
找不到这个库,但是我在系统中能找到这个库,说明已安装但找不到库位置,那么指定该库所有位置到LD_LIBRARY_PATH共享目录去,ubuntu系统在/etc/ld.so.conf.d/目录中添加lapack.conf,指定这个库目录。caffe生成lenet-5的deploy.prototxt文件_第1张图片

报错: import numpy as npImportError: No module named numpy
解决
用下列命令安装

 apt-get install pyton-numpy

报错:libblas.so.3: cannot open shared object file: No such file or directory
解决
因为我已经安装守OpenBlas库,所以建立一个软链接指向这个库

update-alternatives --install /usr/lib/libblas.so.3 libblas.so.3 /usr/local/OpenBlas/lib/libopenblas.so 37

报错: import skimage.ioImportError: No module named skimage.io
解决
参考这个网页中方案解决:
http://www.linuxdiyf.com/linux/15537.html

你可能感兴趣的:(深度学习)