记录下使用caffe mnist数据集,训练+识别图片过程中遇到的问题。 很不详细。
按部就班,获取mnist数据集、创建lmdb、使用example文件训练,得到caffemodel,找了图片预测,错误率很高 原因: mnist原始图片是黑底白字,找的图片是白底黑字。。。自己来重做lmdb吧。—注意灰度图的格式 2.网络上找来mnist手写图片,白底黑字,使用脚本生成lmdb cp create_imagenet.sh create_imagenet_my_mnist.sh cat create_imagenet_my_mnist.sh #!/usr/bin/env sh # Create the imagenet lmdb inputs # N.B. set the path to the imagenet train + val data dirs EXAMPLE=examples/mnist DATA=data/mnist/mnist TOOLS=build/tools TRAIN_DATA_ROOT=/root/caffe-0.16.3/data/mnist/mnist/train/ VAL_DATA_ROOT=/root/caffe-0.16.3/data/mnist/mnist/test/ # Set RESIZE=true to resize the images to 256x256. Leave as false if images have # already been resized using another tool. RESIZE=true if $RESIZE; then RESIZE_HEIGHT=28 RESIZE_WIDTH=28 else RESIZE_HEIGHT=0 RESIZE_WIDTH=0 fi if [ ! -d "$TRAIN_DATA_ROOT" ]; then echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT" echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \ "where the ImageNet training data is stored." exit 1 fi if [ ! -d "$VAL_DATA_ROOT" ]; then echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT" echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \ "where the ImageNet validation data is stored." exit 1 fi echo "Creating train lmdb..." GLOG_logtostderr=1 $TOOLS/convert_imageset \ --resize_height=$RESIZE_HEIGHT \ --resize_width=$RESIZE_WIDTH \ --shuffle \ --gray \ $TRAIN_DATA_ROOT \ $DATA/train.txt \ ##注意如果图片就在$DATA下面,train.txt和val.txt里的路径$DATA下的路径就可以 $EXAMPLE/my_mnist_train_lmdb echo "Creating val lmdb..." GLOG_logtostderr=1 $TOOLS/convert_imageset \ --resize_height=$RESIZE_HEIGHT \ --resize_width=$RESIZE_WIDTH \ --shuffle \ --gray \ ###默认是RGB,需要加个强制灰度,否则预测依然不准 $VAL_DATA_ROOT \ $DATA/val.txt \ $EXAMPLE/my_mnist_val_lmdb echo "Done." ###开始识别 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" param { lr_mult: 1 } param { lr_mult: 2 } convolution_param { num_output: 20 kernel_size: 5 stride: 1 weight_filler { type: "xavier" } bias_filler { type: "constant" } } } 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" param { lr_mult: 1 } param { lr_mult: 2 } convolution_param { num_output: 50 kernel_size: 5 stride: 1 weight_filler { type: "xavier" } bias_filler { type: "constant" } } } 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" param { lr_mult: 1 } param { lr_mult: 2 } inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } } } layer { name: "relu1" type: "ReLU" bottom: "ip1" top: "ip1" } layer { name: "ip2" type: "InnerProduct" bottom: "ip1" top: "ip2" param { lr_mult: 1 } param { lr_mult: 2 } inner_product_param { num_output: 10 weight_filler { type: "xavier" } bias_filler { type: "constant" } } } layer { name: "prob" type: "Softmax" bottom: "ip2" top: "prob" } [root@7ed2726e0c5a caffe-0.16.3]# ./build/examples/cpp_classification/classification.bin examples/mnist/lenet_deploy.prototxt examples/mnist/lenet_iter_10000.caffemodel examples/mnist/mean.binaryproto data/mnist/mnist/test/labels.txt /root/8.png ---------- Prediction for /root/8.png ---------- 1.0000 - "8" 0.0000 - "5" 0.0000 - "3" 0.0000 - "1" 0.0000 - "4" ##导出镜像,有空再继续遇到的几个小坑: 1. 可能是第一次弄不太熟悉,需要保证训练集-模型-测试集,图片格式的一致 2. 注意均值文件,训练用了,预测的时候不能忘用 3. 创建lmdb时 灰度图不是默认选项,需要加参数