caffe利用lenet_5模型跑MNIST数据

一:MNIST数据集的获取

1.获取MNIST数据集

cd data/mnist/
./get_mnist.sh
tree
caffe利用lenet_5模型跑MNIST数据_第1张图片

2.此时可以查看一下caffe下get_mnist.sh脚本的内容
#!/usr/bin/env sh
# This scripts downloads the mnist data and unzips it.

DIR="$( cd "$(dirname "$0")" ; pwd -P )"
cd "$DIR"

echo "Downloading..."

for fname in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte
do
    if [ ! -e $fname ]; then
        wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz
        gunzip ${fname}.gz
    fi
done

3.目前下载下来的是压缩形式的bin二进制文件,需要转换为LEVELDB或者LMDB形式才能被caffe所识别

执行:

./examples/mnist/create_mnist.sh
查看所生成的目录文件:

ls -l examples/mnist/mnist_train_lmdb/
ls -l examples/mnist/mnist_test_lmdb/
当然具体大家可以使用vi或者gedit去查看该文件的具体内容以及将MNIST数据集转换为llmdb格式的源文件:

examples/mnist/convert_mnist_data.cpp


二:跑lenet-5模型

1.查看lenet-5模型描述文件

gedit examples/mnist/lenet_train_val.prototxt


2.执行训练模型的脚本

gedit examples/mnist/lenet_solver.prototxt
修改为CPU,我自己使用CPU跑

caffe利用lenet_5模型跑MNIST数据_第2张图片
执行:

examples/mnist/train_lenet.sh
开始盖楼:

caffe利用lenet_5模型跑MNIST数据_第3张图片
caffe利用lenet_5模型跑MNIST数据_第4张图片
模型描述

name: "LeNet"
state {
  phase: TEST
}
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: "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: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}
开始训练

caffe利用lenet_5模型跑MNIST数据_第5张图片
最终训练模型结果是保存在examples/mnist/lenet_iter_10000.caffemodel,训练状态保存在
examples/mnist/lenet_iter_10000.solverstate中,均是protobuffer二进制格式文件。


3.测试模型

./build/tools/caffe.bin test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -iterations 100
caffe利用lenet_5模型跑MNIST数据_第6张图片

你可能感兴趣的:(caffe)