[PYTHON] Learn to recognize handwritten numbers (MNIST) with Caffe

What is Caffe

It is an open source framework of Deep Learning, which has been a hot topic recently. The official homepage can be used with here, C ++, Python, and MATLAB, so you can choose the one you are good at. You can see a demo of image classification from here, so if you are interested, please try it.

The introduction method of Caffe is summarized in this article, so please refer to it if you like.

What is MNIST (Mixed National Institute of Standards and Technology)?

MNIST is a handwritten digit image database consisting of 60,000 learning samples and 10000 test samples of 28x28px. It is widely used in the fields of machine learning and deep learning as a benchmark for neural networks.

Caffe has a script that makes learning MNIST easy, so I tried it!

Learn MNIST with Caffe

First, change to the root directory of Caffe. After moving, execute the script prepared in Caffe in advance to download the MNIST data set, but wget and gunzip are required for this execution, so if you do not have it, please put it in homebrew.

cd /path/to/caffe
./data/mnist/get_mnist.sh

Since Caffe needs to be read in LevelDB or LMDB format when performing image learning, the data downloaded above is converted to LMDB with the following script.

./examples/mnist/create_mnist.sh

Then you should have created ./example/mnist/mnist_train_lmdb and ./example/mnist/mnist_test_lmdb. The train is used for learning and the test is used for learning evaluation.

If you are running Caffe on CPU_ONLY before training, rewrite solver_mode: GPU in ./examples/mnist/lenet_solver.prototxt to solver_mode: CPU. If you don't do this, you'll get a bug.

Finally, we will learn based on the prepared data, which also has a script, so just execute it.

./examples/mnist/train_lenet.sh

Then, lenet_train_test.prototxt and lent_solver.prototxt will be read and layer network construction and learning will start.

You should see loss and accuracy during learning. loss is a value that decreases as the number of matches between the prediction and the correct answer increases, and accuracy means the accuracy rate. So, if you look at the transition of these values, you can see how learning is progressing.

I0704 solver.cpp:343]     Test net output #0: accuracy = 0.0912
I0704 solver.cpp:343]     Test net output #1: loss = 2.44682 (* 1 = 2.44682 loss)

At the end of the training, the test runs and the final evaluation of the network is displayed, and .examples/mnist/lenet_iter_10000.caffemodel is output as the trained model. By reading this trained model, the network in the state after MNIST recognition learning can be reproduced at any time, so the trained network can be incorporated into actual products.

This is the end of MNIST recognition learning. This time, I read the originally prepared prototxt and learned it, so I don't think it makes much sense to tune the parameters, but repeat the test while changing the value of the contents of lenet_solver.prototxt and compare each data. It's fun, so please try it.

If you have any idea how to use the model output by learning and how to display the learning progress in a graph, I will summarize it.

If you have any mistakes or opinions, please let us know.

Recommended Posts

Learn to recognize handwritten numbers (MNIST) with Caffe
Playing handwritten numbers with python Part 1
Learn to colorize monochrome images with Chainer
Play handwritten numbers with python Part 2 (identify)
[Python] Introduction to CNN with Pytorch MNIST
"How to pass PATH" to learn with homebrew
Preparing to learn technical indicators with TFlearn
[Computer vision] Epipolar geometry to learn with cats
OpenAI Gym to learn with PD-controlled Cart Pole
I tried to learn logical operations with TF Learn
I tried to move GAN (mnist) with keras
Script to tweet with multiples of 3 and numbers with 3 !!
AWS Step Functions to learn with a sample
How to Learn Kaldi with the JUST Corpus
MNIST (handwritten digit) image classification with multi-layer perceptron
How to create random numbers with NumPy's random module
I tried to learn the sin function with chainer
Procedure to load MNIST with python and output to png
I tried to implement and learn DCGAN with PyTorch
Classify mnist numbers by unsupervised learning with keras [Autoencoder]
The first algorithm to learn with Python: FizzBuzz problem
How to install caffe on OS X with macports
Site summary to learn machine learning with English video
How to learn structured SVM of ChainCRF with PyStruct
Converts numbers with commas and triangles to numeric types.
[How to!] Learn and play Super Mario with Tensorflow !!
MVC --Model edition to learn from 0 with prejudice only
I learned MNIST with Caffe and tried to draw it (MAC OS X El Capitan)