TensorFlow2.0 Convolutional Neural Network Handwritten Digit Recognition Simple Actual Combat


Compared with fully connected neural networks, convolutional neural networks have better performance on classification tasks, higher recognition accuracy, and more prominent performance on large data sets. This article takes the MNIST data set as an example, and the construction contains two layers The convolutional neural network of the convolutional layer, the following is the specific code:

1. Configure library files

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, optimizers, datasets

2. Import the database

# 下面一行是在线加载方式
# mnist = tf.keras.datasets.mnist
# 下面两行是加载本地的数据集
datapath  = r'E:\Pycharm\project\project_TF\.idea\data\mnist.npz'
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(datapath)

3. Data preprocessing

Since the imported data is 60000* 28* 28, the fully connected network should be reshaped to 60000* 784 (that is, Flatten()), and the convolutional network is 60000* 28 * 28*1 (black and white single channel) , So data preprocessing is required.

# 数据预处理
x_train = x_train.reshape(x_train.shape[0],28,28,1).astype('float32')
x_test = x_test.reshape(x_test.shape[0],28,28,1).astype('float32')
# 归一化
x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)   #归一化

4. Establish a convolutional neural network structure model

Build a convolutional neural network with two convolutional layers and two pooling layers. Among them, the depth (number) of the first layer of convolution kernel is 16, and the size is 5 * 5. The same filling means that the output image size is consistent with the input (this is a bit easier than PyTorch); the second layer of convolution kernel depth is 36 , In order to extract higher dimensional information, the size is 5 * 5. Generally speaking, the maximum pooling layer performs better in classification than the average pooling layer.

model = keras.models.Sequential([
    layers.Conv2D(filters=16, kernel_size=(5,5), padding='same',
                 input_shape=(28,28,1),  activation='relu'),
    layers.MaxPooling2D(pool_size=(2, 2)),
    layers.Conv2D(filters=36, kernel_size=(5,5), padding='same',
    			 activation='relu'),
    layers.MaxPooling2D(pool_size=(2, 2)),
    layers.Dropout(0.25),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(10,activation='softmax')
])
#打印模型
print(model.summary())

The network structure model is printed as follows:

5. Model compilation and training

The loss function uses the cross-entropy loss function, and the optimizer uses adam; training 10 epochs and 128 batch_size.

#训练配置
model.compile(loss='sparse_categorical_crossentropy',
              optimizer='adam', metrics=['accuracy'])
#开始训练
model.fit(x=x_train, y=y_train, validation_split=0.2,	##validation_split是指将一部分作为验证集使用,0.2代表80%的数据作为训练集,20%作为验证集
                        epochs=10, batch_size=128, verbose=1)   #verbose=1代表显示训练过程

6. Test set verification

val_loss, val_acc = model.evaluate(x_test, y_test) # model.evaluate输出计算的损失和精确度
print('Test Loss:{:.6f}'.format(val_loss))
print('Test Acc:{:.6f}'.format(val_acc))

The whole procedure is now over.

7. Test results

In the cpu environment, it takes about 70s to complete each epoch. The average accuracy of the final training is 98.62%, and the average Loss is 0.0439. In the test set, the recognition accuracy reached 99.07%, and the Loss was 0.0277. Compared with the 97.87% of the fully connected neural network in the previous article, the accuracy rate has increased.

Guess you like

Origin blog.csdn.net/weixin_45371989/article/details/104691919