caffe环境下训练mnist数据集并测试

mnist手写数字识别是学习深度学习的入门,本人通过多次尝试和阅读多篇博客得出以下经验,如有遗漏,欢迎交流指正。
我的系统环境为Ubuntu16.04+caffe。如果还没有配置好环境,请移步到我的博客
[https://blog.csdn.net/xue_csdn/article/details/90898400],有详细的caffe安装步骤。

1、下载数据集,在caffe根目录下执行

cd $CAFFE_ROOT
./data/mnist/get_mnist.sh

2、转换格式

./examples/mnist/create_mnist.sh

一共生成4个文件,在examples/mnist下有mnist_train_lmdb和mnist_test_lmdb两个文件夹,每个文件夹里都有data.lmdb和lock.lmdb两个文件。
mnist使用的是lenet网络模型,其定义在mnist的lenet_train_test.prototxt文件中,只需要注意source参数文件路径,一般默认在mnist/mnist_train_lmdb,不需要修改。
我的网络模型位置:

$CAFFE_ROOT/examples/mnist/lenet_train_test.prototxt

3、训练,生成模型

./examples/mnist/train_lenet.sh

4、测试模型的参数

$./build/tools/caffe.bin test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -iteration=100

得到准确率及损失函数的值
命令行解释
./build/tools/caffe.bin test,表示只做预测(forward前向传播计算),不进行参数更新
-model examples/mnist/lenet_train_test.prototxt,指定模型
-weights examples/mnist/lenet_iter_10000.caffemodel,指定模型预先训练好的权值文件
-iterations 100,指定测试迭代次数。
5、使用自己的图片测试
处理图片:256位黑白图,黑底白字,像素28*28,数字尽量处于图片正中间,存放在examples/images目录下,名称为test.jpg。
以下设置均在examples/mnist目录下,测试在caffe根目录下。
(1)生成deploy.prototxt文件:与lenet_train_test.prototxt文件类似,直接复制,改名,稍改内容。
我的deploy.prototxt文件内容:

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

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"
    }
  }
}
layer {
  name: "prob"
  type: "Softmax"
  bottom: "ip2"
  top: "prob"
}

(2)生成labels.txt文件
在这里插入图片描述
(3)生成均值文件mean.binaryproto:
将文件examples/imagenet/make_imagenet_mean.sh拷贝到examples/mnist文件夹下,改名(采用imagenet其中的内容,需修改名称),改变路径(改为绝对路径),保存之后运行该文件,生成mean.binaryproto文件于examples/mnist目录下。
(4)开始测试:
在caffe根目录下执行:

./build/examples/cpp_classification/classification.bin examples/mnist/deploy.prototxt examples/mnist/lenet_iter_10000.caffemodel examples/mnist/mean.binaryproto examples/mnist/labels.txt examples/images/test.jpg

确保所有路径正确。
即可得出测试出的手写。

猜你喜欢

转载自blog.csdn.net/xue_csdn/article/details/90700809
今日推荐