当前位置:   article > 正文

keras深度学习框架通过卷积神经网络cnn实现手写数字识别_keras手写数字识别

keras手写数字识别

    昨天通过keras构建简单神经网络实现手写数字识别,结果在最后进行我们自己的手写数字识别的时候,准确率堪忧,只有60%。今天通过卷积神经网络来实现手写数字识别。

    构建卷积神经网络和简单神经网络思路类似,只不过这里加入了卷积、池化等概念,网络结构复杂了一些,但是整体的思路没有变化,加载数据集,数据集修改,搭建网络模型,编译模型,训练模型,保存模型,利用模型预测。

    这里还是给出两个例子,一个是构建网络,最后保存训练好的网络模型,一个是通过加载保存的网络模型预测我们自己的手写数字图片。

  1. import keras
  2. import numpy as np
  3. import tensorflow as tf
  4. from keras.models import Sequential
  5. from keras.layers import Dense, Activation, Dropout, Conv2D, Flatten, MaxPool2D
  6. from tensorflow.keras import datasets, utils
  7. # 数据处理
  8. (x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()
  9. x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[1], 1)
  10. x_train = x_train.astype('float32') / 255
  11. x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[1], 1)
  12. x_test = x_test.astype('float32') / 255
  13. y_train = utils.to_categorical(y_train, num_classes=10)
  14. y_test = utils.to_categorical(y_test, num_classes=10)
  15. # 构建模型
  16. model = Sequential()
  17. model.add(Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation="relu", input_shape=(28, 28, 1)))
  18. model.add(MaxPool2D(pool_size=(2, 2)))
  19. model.add(Conv2D(filters=36, kernel_size=(3, 3), padding='same', activation="relu"))
  20. model.add(MaxPool2D(pool_size=(2, 2)))
  21. model.add(Dropout(0.2))
  22. model.add(Flatten())
  23. model.add(Dense(128, activation="relu"))
  24. model.add(Dropout(0.25))
  25. model.add(Dense(10, activation="softmax"))
  26. # 编译
  27. model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
  28. model.summary()
  29. # 训练
  30. model.fit(x_train, y_train, epochs=5, batch_size=128, validation_data=(x_test, y_test))
  31. # 保存模型
  32. model.save("mnist.h5")

     训练模型,打印信息如下:

  1. Model: "sequential"
  2. _________________________________________________________________
  3. Layer (type) Output Shape Param #
  4. =================================================================
  5. conv2d (Conv2D) (None, 28, 28, 16) 160
  6. max_pooling2d (MaxPooling2D (None, 14, 14, 16) 0
  7. )
  8. conv2d_1 (Conv2D) (None, 14, 14, 36) 5220
  9. max_pooling2d_1 (MaxPooling (None, 7, 7, 36) 0
  10. 2D)
  11. dropout (Dropout) (None, 7, 7, 36) 0
  12. flatten (Flatten) (None, 1764) 0
  13. dense (Dense) (None, 128) 225920
  14. dropout_1 (Dropout) (None, 128) 0
  15. dense_1 (Dense) (None, 10) 1290
  16. =================================================================
  17. Total params: 232,590
  18. Trainable params: 232,590
  19. Non-trainable params: 0
  20. _________________________________________________________________
  21. Epoch 1/5
  22. 2023-08-28 16:03:54.677314: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8800
  23. 469/469 [==============================] - 10s 17ms/step - loss: 0.2842 - accuracy: 0.9123 - val_loss: 0.0628 - val_accuracy: 0.9798
  24. Epoch 2/5
  25. 469/469 [==============================] - 7s 16ms/step - loss: 0.0836 - accuracy: 0.9743 - val_loss: 0.0473 - val_accuracy: 0.9841
  26. Epoch 3/5
  27. 469/469 [==============================] - 7s 16ms/step - loss: 0.0627 - accuracy: 0.9801 - val_loss: 0.0325 - val_accuracy: 0.9886
  28. Epoch 4/5
  29. 469/469 [==============================] - 7s 15ms/step - loss: 0.0497 - accuracy: 0.9844 - val_loss: 0.0346 - val_accuracy: 0.9882
  30. Epoch 5/5
  31. 469/469 [==============================] - 7s 15ms/step - loss: 0.0422 - accuracy: 0.9867 - val_loss: 0.0298 - val_accuracy: 0.9898

    准确率最后,到达了98.5%以上。

    用模型预测

  1. import keras
  2. import numpy as np
  3. import cv2
  4. from keras.models import load_model
  5. model = load_model("mnist.h5")
  6. def predict(img_path):
  7. img = cv2.imread(img_path, 0)
  8. img = img.reshape(28, 28).astype("float32") / 255 # 0 1
  9. img = img.reshape(1, 28, 28, 1) # 28 * 28 -> (1,28,28,1)
  10. label = model.predict(img)
  11. label = np.argmax(label, axis=1)
  12. print('{} -> {}'.format(img_path, label[0]))
  13. if __name__ == '__main__':
  14. for _ in range(10):
  15. predict("number_images/b_{}.png".format(_))

    数字图片如下: 

    图片放在项目目录number_images中。

    预测结果打印:

 

    感觉就是不一样,准确率从60%提升到了90%。虽然没有达到100%,但是已经很好了。 

    对比之前的代码,改动很小,主要是网络输入的时候,数据形状发生了改变,简单神经网络需要的是(784,*)结构,卷积神经网络需要的是(1,28,28,1)的结构, 在数据处理上做了调整,另一个不一样的地方就是网络模型在添加的时候,之前就是简单的两层网络,卷积神经网络复杂了很多。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/751805
推荐阅读
相关标签
  

闽ICP备14008679号