Article directory
Using CNN to complete MNIST handwriting recognition (Keras)
Convolutional Neural Network (CNN) is a deep learning algorithm and a powerful tool for processing data with a grid-like structure, such as images and sounds. CNN is mainly used in image recognition, speech recognition, natural language processing and other fields. It is currently one of the most effective algorithms in the field of computer vision.
The main features of convolutional neural networks are local connections, weight sharing and pooling. Local connection means that each neuron is connected to only a small part of the input data; weight sharing means that all neurons use the same weight matrix for convolution calculation; pooling is to perform the output after the convolution calculation. Downsampling. These properties make CNN very suitable for processing image data.
The structure of CNN usually consists of multiple convolutional layers, pooling layers and fully connected layers. The convolutional layer and pooling layer are used to extract the features of the image, and the fully connected layer is used to map these features to the output result. During the training process, CNN updates the weight matrix through the back propagation algorithm, so that the network can automatically learn the most suitable feature representation for the task. During the test process, CNN passes the input data into the network through the forward propagation algorithm and obtains the output results.
CNN has a wide range of applications, such as face recognition, object recognition, image classification, image segmentation, target detection, etc. In object recognition and image classification tasks, CNNs are often trained using the ImageNet dataset, which contains millions of images and thousands of categories and is one of the largest datasets in the field of computer vision. In target detection tasks, CNN usually uses network structures such as Faster R-CNN, YOLO, and SSD to predict the location and category of objects at the same time.
In general, convolutional neural network is a very powerful deep learning algorithm with excellent image processing capabilities, but there are also some problems in practical application, such as long training time and the need for more computing resources and data. Set etc. With the continuous advancement and development of technology, I believe that CNN will be more widely used in the future.
1. Import the Keras library
# 导入相关库
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Flatten, Dropout, Dense
from keras.losses import categorical_crossentropy
from keras.optimizers import Adadelta
Using TensorFlow backend.
2. Dataset
# 导入数据集
train_X, train_y = mnist.load_data()[0]
train_X, train_y
(array([[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]],
[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]],
[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]],
...,
[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]],
[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]],
[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]]], dtype=uint8),
array([5, 0, 4, ..., 5, 6, 8], dtype=uint8))
# 训练集
train_X = train_X.reshape(-1, 28 ,28, 1)
train_X = train_X.astype('float32')
train_X /= 255
train_y = to_categorical(train_y, 10)
3. Construct a neural network
# 构造神经网络
model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=[28, 28, 1]))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss=categorical_crossentropy, optimizer=Adadelta(), metrics=['accuracy'])
4. Training model
# 开始训练
batch_size = 100
epochs = 1
model.fit(train_X, train_y, batch_size=batch_size, epochs=epochs)
WARNING:tensorflow:From /home/nlp/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.
Epoch 1/1
60000/60000 [==============================] - 190s 3ms/step - loss: 0.2228 - accuracy: 0.9316
<keras.callbacks.callbacks.History at 0x7f7835e74940>
5. Test the model
# 测试准确率
test_X, test_y = mnist.load_data()[1]
test_X = test_X.reshape(-1, 28, 28, 1)
test_X = test_X.astype('float32')
test_X /= 255
test_y = to_categorical(test_y, 10)
loss, accuracy = model.evaluate(test_X, test_y, verbose=1)
print('loss:%.4f accuracy:%.4f' %(loss, accuracy))
10000/10000 [==============================] - 9s 919us/step
loss:0.0467 accuracy:0.9844