Caffe running mnist handwritten digit recognition

Old rules, first attach the official tutorial: http://caffe.berkeleyvision.org/gathered/examples/mnist.html

1. Necessary software

  Because Caffe uses shell scripts that can only be run by Linux, first install  wget (put wget into C:\windows\system32) and  Git  can run.

2. Then follow the official tutorial, first enter the root directory of the caffe path, and then open cmd and enter the command:

./data/mnist/get_mnist.sh

This command is to download the mnist data by opening the get_mnist.sh script in the /data/mnist directory. If there is an error in the cmd, you can directly enter and open the get_mnist.sh script. The effect is the same. After the operation is completed, the following 4 data files will appear:

Then continue to enter the following command, or enter the path to open the same

./examples/mnist/create_mnist.sh

If the file does not exist, you can create a create_mnist.sh by yourself. The specific code is as follows (Note: The ninth line of BUILD may have a different path to the old version, and you can modify it according to your own path):

#!/usr/bin/env sh
        
# This script converts the mnist data into lmdb/leveldb format,
# depending on the value assigned to $BACKEND.
set -e

EXAMPLE=.
DATA = .. / .. / data / mnist
BUILD=../../scripts/build/examples/mnist/Release

BACKEND="lmdb"

echo "Creating ${BACKEND}..."

rm -rf $EXAMPLE/mnist_train_${BACKEND}
rm -rf $EXAMPLE/mnist_test_${BACKEND}

$BUILD/convert_mnist_data.exe $DATA/train-images-idx3-ubyte \
  $DATA/train-labels-idx1-ubyte $EXAMPLE/mnist_train_${BACKEND} --backend=${BACKEND}
$BUILD/convert_mnist_data.exe $DATA/t10k-images-idx3-ubyte \
  $DATA/t10k-labels-idx1-ubyte $EXAMPLE/mnist_test_${BACKEND} --backend=${BACKEND}

echo "Done."

read -p "Enter to continue..."

  After the run is complete, two folders, mnist_test_lmdb and mnist_train_lmdb, will appear:

cmd shows:

 

3. Open lenet_solver.prototxt under the path /scripts/build/examples/mnist/Release (the paths of different versions of caffe are different, and the paths of some old versions of caffe are: /Build/x64/Release), according to your own situation Change parameters:

  The second line: If lenet_train_test.prototxt and lenet_solver.prototxt are not in the same path, you need to write the path where lenet_train_test.prototxt is located before it

  Line 23: snapshot_prefix: the generated model is the generated training model, you can modify the path according to yourself

  The last line selects whether the installed caffe is CPU or GPU. I installed the GPU version here.

Note: Do not copy the file path directly, because the path separator in it is / instead of \. If you use \ and run it later, the following error will occur (the same is true for the following steps, if you are not sure, just follow my writing ):

The result after modifying the parameters:

# The train/test net protocol buffer definition
net: "lenet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "E:/CaffeSource/caffe/data/mnist/model"
# solver mode: CPU or GPU
solver_mode: GPU

4. Open lenet_train_test.prototxt (the file in the second line above)

Then change the path in the figure above. These two files are the files downloaded when the ./data/mnist/get_mnist.sh command is executed, and their paths are added.

5. Create a new train_lenet.txt file in the directory \examples\mnist, add the following paragraph, and then change the suffix to .bat

..\..\Build\x64\Release\caffe.exe train --solver="lenet_solver.prototxt" --gpu 0
pause

  Or modify the train_lenet.sh file in this directory:

#!/usr/bin/env sh
set -e
BUILD=../../Build/x64/Release/
echo "Training lenet_solver.prototxt..."

$BUILD/caffe.exe train --solver=lenet_solver.prototxt $@
echo "Done."

read -p "Enter to continue..."

6. Run the file, and the result after running for a few minutes is as follows:

If no error is reported, the test is a success!

It can be seen that the accuracy is 99%, the trained model is saved in lenet_iter_10000.caffemodel, and the training state is saved in lenet_iter_10000.solverstate. The results are as follows:

 

 

 

 

 

 





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324684233&siteId=291194637