人工智能学习:NMIST数据分类识别-CNN网络(3)

这里采用CNN模型(卷积神经网络)来进行MNIST数据集的分类识别

1 导入模块
首先,导入需要的模块

import numpy as np

import tensorflow as tf
from tensorflow import keras
from keras import models, layers

import matplotlib.pyplot as plt

2 载入MNIST数据集
调用keras集成的mnist的load_data函数载入数据集

# load MNIST dataset
(train_images, train_labels), (test_images, test_labels) = keras.datasets.mnist.load_data()

# train_images: 60000*28*28, train_labels: 60000*1
# test_images: 10000*28*28, test_labels: 10000*1

# pre-process data, change data shape & type
train_input = train_images.reshape(60000,28,28,1)
train_input = train_input.astype('float32')/255
test_input = test_images.reshape(10000,28,28,1)
test_input = test_input.astype('float32')/255
train_output = keras.utils.to_categorical(train_labels)
test_output = keras.utils.to_categorical(test_labels)

3 构建模型
构建一个卷积神经网络,定义构建函数如下

def build_model():
    model = models.Sequential()
    model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)))
    model.add(layers.MaxPooling2D((2,2)))
    model.add(layers.Dropout(0.25))
    model.add(layers.Conv2D(64, (3,3), activation='relu'))
    model.add(layers.MaxPooling2D((2,2)))
    model.add(layers.Flatten())
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(10, activation='softmax'))
    
    model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
    
    return model

构建模型

# build model
network = build_model()

# show network summary
network.summary()

显示结果如下

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 32)        0         
_________________________________________________________________
dropout (Dropout)            (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0         
_________________________________________________________________
flatten (Flatten)            (None, 1600)              0         
_________________________________________________________________
dense (Dense)                (None, 64)                102464    
_________________________________________________________________
dropout_1 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 10)                650       
=================================================================
Total params: 121,930
Trainable params: 121,930
Non-trainable params: 0
_________________________________________________________________

显示是一个多层的卷积神经网络

4 训练模型
调用如下函数

# train, with batch size and epochs
network.fit(train_input, train_output, epochs=10, batch_size=128)

显示训练结果如下

Epoch 1/10
469/469 [==============================] - 16s 4ms/step - loss: 0.4858 - accuracy: 0.8445
Epoch 2/10
469/469 [==============================] - 2s 4ms/step - loss: 0.1779 - accuracy: 0.9471
Epoch 3/10
469/469 [==============================] - 2s 4ms/step - loss: 0.1290 - accuracy: 0.9641
Epoch 4/10
469/469 [==============================] - 2s 4ms/step - loss: 0.1045 - accuracy: 0.9695
Epoch 5/10
469/469 [==============================] - 2s 4ms/step - loss: 0.0883 - accuracy: 0.9744
Epoch 6/10
469/469 [==============================] - 2s 4ms/step - loss: 0.0795 - accuracy: 0.9770
Epoch 7/10
469/469 [==============================] - 2s 4ms/step - loss: 0.0719 - accuracy: 0.9797
Epoch 8/10
469/469 [==============================] - 2s 4ms/step - loss: 0.0689 - accuracy: 0.9803
Epoch 9/10
469/469 [==============================] - 2s 4ms/step - loss: 0.0669 - accuracy: 0.9811
Epoch 10/10
469/469 [==============================] - 2s 4ms/step - loss: 0.0636 - accuracy: 0.9821

显示每次训练的损失函数和准确率,准确率最后为0.9821。

5 评估训练后的模型
调用评估函数

# evaluate performance using test data
network.evaluate(test_input, test_output)

显示结果如下

313/313 [==============================] - 1s 2ms/step - loss: 0.0347 - accuracy: 0.9896
[0.034671902656555176, 0.9896000027656555]

准确率为0.9896。

这里显示测试集前100张的分类识别结果

# perform prediction on test data
predict_output = network.predict(test_input)
# lines and columns of subplots

m = 10
n = 10
num = m*n

# figure size
plt.figure(figsize=(11,11))

# plot first 100 pictures of test images and results
for i in range(num):
    plt.subplot(m,n,i+1)
    
    if np.argmax(predict_output[i]) == np.argmax(test_output[i]):
        opt = 'gray_r'
    else:
        opt = 'gray'
                 
    plt.imshow(test_images[i], cmap=opt)
    #plt.axis('off')
    plt.xticks([])
    plt.yticks([])

plt.show()

结果如下
人工智能学习:NMIST数据分类识别-CNN网络(3)_第1张图片
前100张图片全部识别正确。从结果来看,CNN神经网络训练后的识别准确率没有前面全连接层神经网络的准确率高,但是它的泛化能力比前面的网络好,体现为它在测试集上的识别准确率高于前面全连接层组成的模型。

参考链接:https://blog.csdn.net/weixin_45825073/article/details/121753882

你可能感兴趣的:(人工智能,人工智能,CNN,MNIST)