Python使用Keras深度学习框架构建卷积神经网络模型识别MNIST手写字体实战例子

1、MNIST数据集介绍

MNIST数据集是一个非常著名的手写数字数据集,是机器学习领域中最受欢迎的数据集之一。由该数据集构成的图片都是28x28的灰度图像。它总共有70000张图片,它由60000个训练图像和10000个测试图像组成。MNIST数据集已经经过预处理,可以直接拿来训练模型。
数据集中的图片显示了从0到9的手写数字,标签标识了图片显示的数字是多少。

2.获取训练集和测试

Python使用Keras深度学习框架基于卷积神经网络模型识别MNIST手写字体,模型在测试集上的预测准确率是99%

## 60000张 训练图片,每张图片28*28
## 10000张 测试图片,每张图片28*28
def prepareData(self):
    (xTrain, yTrain), (xTest, yTest) = keras.datasets.mnist.load_data()
    xTrain = xTrain / 255.0 # 缩放  numpy.ndarray,(60000, 28, 28)
    xTest = xTest / 255.0   # 缩放, 范围[0,1]

    xTrain = np.expand_dims(xTrain, -1)  # (60000, 28, 28) -> (60000, 28, 28, 1)
    xTest = np.expand_dims(xTest, -1)    # (10000, 28, 28) -> (10000, 28, 28, 1)
    print("xTrain shape", xTrain.shape)
    yTrain = keras.utils.to_categorical(yTrain, num_classes=10) # 将整型的类别标签转为onehot编码
    yTest = keras.utils.to_categorical(yTest, num_classes=10)   # 10个数字,num_classes为10
    print(type(yTrain))
    return xTrain, yTrain, xTest, yTest

返回训练集X,y ; 测试X,y

3、创建卷积神经网络模型

def createModel(self):
    inputShape = (28, 28, 1)    #  (60000, 28, 28, 1),60000个样本
    # 激活函数:sigmoid,relu,tanh
    # 卷积层可以处理二维数据,矩阵
    # 全连接层只能处理一维数据,向量
    model = keras.Sequential(
        [
            keras.Input(shape=inputShape),
            layers.Conv2D(32, kernel_size=(3, 3), activation='relu'),  # 卷积层:卷积函数:32 过滤器个数
            layers.MaxPooling2D(pool_size=(2, 2)),   # 最大池化层
            layers.Conv2D(32, kernel_size=(3, 3), activation='relu'),  # 卷积层:卷积核尺寸,一般是3*3,或者5*5
            layers.MaxPooling2D(pool_size=(2, 2)),   # 最大池化层
            layers.Flatten(),     # 将输入层的数据压成一维数据,
            layers.Dropout(0.5),  # 深度学习网络训练过程中,按照一定的概率丢弃神经网络单元,防止过拟合,默认0.5,丢弃50%的神经元
            # Softmax 函数能够将一个K维实值向量归一化,所以它主要被用于多分类任务
            # Sigmoid 能够将一个实数归一化,因此它一般用于二分类任务。
            # 当 Softmax 的维数 K=2 时,Softmax 会退化为 Sigmoid 函数
            layers.Dense(10, activation="softmax")  # Dense代表全连接网络,输出维度10,激活函数softmax
        ]
    )

    model.summary()  # 输出模型各层的参数状况
    return model

4、训练模型

def trainModel(self, model:keras.Sequential, xTrain:numpy.array, yTrain:numpy.array):
    # 指定损失函数
    model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
    model.fit(xTrain,
              yTrain,
              batch_size=128,         # 每次梯度更新的样本数,默认32
              epochs=15,              # 训练模型迭代次数
              validation_split=0.1)   # 验证集的比例
    return model

5、测试模型

def testModel(self, model:keras.Sequential, xTest, yTest):
    score = model.evaluate(xTest, yTest,
                           verbose=0) # 日志展示,0-不输出;1-显示进度条;2-每个epochs输出1行记录
    print("Loss:", score[0])        # 损失
    print("Accuracy:", score[1])    # 准确率

6、完整代码

# -*- coding: utf-8 -*-
import numpy
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers

# Keras Mnist 模型
class KrMnistModel(object):

    ## 60000张 训练图片,每张图片28*28
    ## 10000张 测试图片,每张图片28*28
    def prepareData(self):
        (xTrain, yTrain), (xTest, yTest) = keras.datasets.mnist.load_data()
        xTrain = xTrain / 255.0 # 缩放  numpy.ndarray,(60000, 28, 28)
        xTest = xTest / 255.0   # 缩放, 范围[0,1]

        xTrain = np.expand_dims(xTrain, -1)  # (60000, 28, 28) -> (60000, 28, 28, 1)
        xTest = np.expand_dims(xTest, -1)    # (10000, 28, 28) -> (10000, 28, 28, 1)
        print("xTrain shape", xTrain.shape)
        yTrain = keras.utils.to_categorical(yTrain, num_classes=10) # 将整型的类别标签转为onehot编码
        yTest = keras.utils.to_categorical(yTest, num_classes=10)   # 10个数字,num_classes为10
        print(type(yTrain))
        return xTrain, yTrain, xTest, yTest

    def createModel(self):
        inputShape = (28, 28, 1)    #  (60000, 28, 28, 1),60000个样本
        # 激活函数:sigmoid,relu,tanh
        # 卷积层可以处理二维数据,矩阵
        # 全连接层只能处理一维数据,向量
        model = keras.Sequential(
            [
                keras.Input(shape=inputShape),
                layers.Conv2D(32, kernel_size=(3, 3), activation='relu'),  # 卷积层:卷积函数:32 过滤器个数
                layers.MaxPooling2D(pool_size=(2, 2)),   # 最大池化层
                layers.Conv2D(32, kernel_size=(3, 3), activation='relu'),  # 卷积层:卷积核尺寸,一般是3*3,或者5*5
                layers.MaxPooling2D(pool_size=(2, 2)),   # 最大池化层
                layers.Flatten(),     # 将输入层的数据压成一维数据,
                layers.Dropout(0.5),  # 深度学习网络训练过程中,按照一定的概率丢弃神经网络单元,防止过拟合,默认0.5,丢弃50%的神经元
                # Softmax 函数能够将一个K维实值向量归一化,所以它主要被用于多分类任务
                # Sigmoid 能够将一个实数归一化,因此它一般用于二分类任务。
                # 当 Softmax 的维数 K=2 时,Softmax 会退化为 Sigmoid 函数
                layers.Dense(10, activation="softmax")  # Dense代表全连接网络,输出维度10,激活函数softmax
            ]
        )

        model.summary()  # 输出模型各层的参数状况
        return model


    def trainModel(self, model:keras.Sequential, xTrain:numpy.array, yTrain:numpy.array):
        # 指定损失函数
        model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
        model.fit(xTrain,
                  yTrain,
                  batch_size=128,         # 每次梯度更新的样本数,默认32
                  epochs=15,              # 训练模型迭代次数
                  validation_split=0.1)   # 验证集的比例
        return model

    def testModel(self, model:keras.Sequential, xTest, yTest):
        score = model.evaluate(xTest, yTest,
                               verbose=0) # 日志展示,0-不输出;1-显示进度条;2-每个epochs输出1行记录
        print("Loss:", score[0])        # 损失
        print("Accuracy:", score[1])    # 准确率


# main function
if __name__ == '__main__':
    krObj = KrMnistModel()
    print("准备数据……")
    xTrain, yTrain, xTest, yTest = krObj.prepareData()
    print("创建模型……")
    model = krObj.createModel()
    print("训练模型……")
    model = krObj.trainModel(model, xTrain, yTrain)
    print("测试模型……")
    krObj.testModel(model, xTest, yTest)

7、运行结果

"C:\Program Files\Python37\python.exe" D:/MyProj/TfDLProj/krModel/KrMnistModel.py
2023-04-09 20:22:06.754283: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
准备数据……
xTrain shape (60000, 28, 28, 1)
<class 'numpy.ndarray'>
创建模型……
2023-04-09 20:22:13.968864: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2023-04-09 20:22:13.978588: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library nvcuda.dll
2023-04-09 20:22:14.046314: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1650 Ti computeCapability: 7.5
coreClock: 1.485GHz coreCount: 16 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 178.84GiB/s
2023-04-09 20:22:14.047261: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2023-04-09 20:22:14.097692: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2023-04-09 20:22:14.097992: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2023-04-09 20:22:14.122078: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2023-04-09 20:22:14.129733: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2023-04-09 20:22:14.183585: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2023-04-09 20:22:14.199841: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2023-04-09 20:22:14.204166: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2023-04-09 20:22:14.204704: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
2023-04-09 20:22:14.206580: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-04-09 20:22:14.208133: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1650 Ti computeCapability: 7.5
coreClock: 1.485GHz coreCount: 16 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 178.84GiB/s
2023-04-09 20:22:14.208710: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2023-04-09 20:22:14.208996: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2023-04-09 20:22:14.209275: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2023-04-09 20:22:14.209560: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2023-04-09 20:22:14.209838: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2023-04-09 20:22:14.210123: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2023-04-09 20:22:14.210397: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2023-04-09 20:22:14.210673: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2023-04-09 20:22:14.210985: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
2023-04-09 20:22:15.198078: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1261] Device interconnect StreamExecutor with strength 1 edge matrix:
2023-04-09 20:22:15.198413: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1267]      0 
2023-04-09 20:22:15.198590: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 0:   N 
2023-04-09 20:22:15.199449: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1406] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2903 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1650 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)
2023-04-09 20:22:15.201051: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
Model: "sequential"

_________________________________________________________________

Layer (type)                 Output Shape              Param #   
=================================================================

conv2d (Conv2D)              (None, 26, 26, 32)        320       

_________________________________________________________________

max_pooling2d (MaxPooling2D) (None, 13, 13, 32)        0         

_________________________________________________________________

conv2d_1 (Conv2D)            (None, 11, 11, 32)        9248      

_________________________________________________________________

max_pooling2d_1 (MaxPooling2 (None, 5, 5, 32)          0         

_________________________________________________________________

flatten (Flatten)            (None, 800)               0         

_________________________________________________________________

dropout (Dropout)            (None, 800)               0         

_________________________________________________________________

dense (Dense)                (None, 10)                8010      
=================================================================

Total params: 17,578
Trainable params: 17,578
Non-trainable params: 0

_________________________________________________________________

训练模型……
2023-04-09 20:22:15.442757: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
Epoch 1/15
2023-04-09 20:22:15.784489: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2023-04-09 20:22:16.640072: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2023-04-09 20:22:16.653720: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2023-04-09 20:22:18.352832: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 0

2023-04-09 20:22:18.470222: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 0

422/422 [==============================] - 7s 8ms/step - loss: 0.9618 - accuracy: 0.6814 - val_loss: 0.0942 - val_accuracy: 0.9747
Epoch 2/15
422/422 [==============================] - 3s 6ms/step - loss: 0.1555 - accuracy: 0.9518 - val_loss: 0.0662 - val_accuracy: 0.9832
Epoch 3/15
422/422 [==============================] - 3s 6ms/step - loss: 0.1119 - accuracy: 0.9658 - val_loss: 0.0522 - val_accuracy: 0.9853
Epoch 4/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0925 - accuracy: 0.9707 - val_loss: 0.0461 - val_accuracy: 0.9878
Epoch 5/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0804 - accuracy: 0.9739 - val_loss: 0.0444 - val_accuracy: 0.9878
Epoch 6/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0752 - accuracy: 0.9766 - val_loss: 0.0426 - val_accuracy: 0.9873
Epoch 7/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0723 - accuracy: 0.9783 - val_loss: 0.0379 - val_accuracy: 0.9890
Epoch 8/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0677 - accuracy: 0.9791 - val_loss: 0.0375 - val_accuracy: 0.9887
Epoch 9/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0633 - accuracy: 0.9798 - val_loss: 0.0333 - val_accuracy: 0.9907
Epoch 10/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0606 - accuracy: 0.9804 - val_loss: 0.0323 - val_accuracy: 0.9902
Epoch 11/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0571 - accuracy: 0.9826 - val_loss: 0.0348 - val_accuracy: 0.9905
Epoch 12/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0555 - accuracy: 0.9825 - val_loss: 0.0327 - val_accuracy: 0.9903
Epoch 13/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0520 - accuracy: 0.9841 - val_loss: 0.0326 - val_accuracy: 0.9905
Epoch 14/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0506 - accuracy: 0.9836 - val_loss: 0.0328 - val_accuracy: 0.9910
Epoch 15/15
422/422 [==============================] - 3s 6ms/step - loss: 0.0533 - accuracy: 0.9834 - val_loss: 0.0316 - val_accuracy: 0.9910
测试模型……
Loss: 0.029247775673866272
Accuracy: 0.9900000095367432

Process finished with exit code 0

Accuracy: 0.9900000095367432
正确率:99%

你可能感兴趣的:(Python,AI人工智能,深度学习,深度学习,python,keras,卷积神经网络,人工智能)