IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> MNIST手写数字识别_CNN05 -> 正文阅读

[人工智能]MNIST手写数字识别_CNN05

MNIST手写数字识别_CNN

加载数据集得到训练集和测试集:

mnist = input_data.read_data_sets('D:\pythonProject1\MNIST\MNIST_data',one_hot=True) # 热编码
train_X = mnist.train.images
train_Y = mnist.train.labels
test_X = mnist.test.images
test_Y = mnist.test.labels

数据预处理:

train_X = train_X.reshape(train_X.shape[0],28,28,1)
test_X = test_X.reshape(test_X.shape[0],28,28,1)
print(train_X.shape, type(train_X)) # (55000,  28, 28,1 ) <class 'numpy.ndarray'>
print(test_X.shape, type(test_X)) # (10000,  28, 28, 1 ) <class 'numpy.ndarray'>

将数据的格式处理为(55000,28,28,1)的形式。彩色图片一般会有Width,Hight,Channel信息。图像的维度信息分为两种channels_first和channels_last。其中channels_first为(Channel, Width, Hight),channels_last为(Width, Hight,Channel).系统默认为channels_last.

#数据归一化 min-max 标准化
train_X /= 255
test_X /= 255

构建模型加编译模型:

def create_model():
    model = Sequential()
    model.add(Conv2D(filters=32, kernel_size=(5, 5), activation='relu',
                     input_shape=(28, 28, 1)))
    # 最大池化层,池化窗口 2x2
    model.add(MaxPooling2D(pool_size=(2, 2)))
    # Dropout 25% 的输入神经元
    model.add(Dropout(0.2))
    # 将 Pooled feature map 摊平后输入全连接网络
    model.add(Flatten())
    # 全联接层
    model.add(Dense(128, activation='relu'))
    # 使用 softmax 激活函数做多分类,输出各数字的概率
    model.add(Dense(10, activation='softmax'))
    # 编译模型
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

    return model

model = create_model()

查看模型:

# 查看模型
model.summary()
for layer in model.layers:
    print(layer.get_output_at(0).get_shape().as_list())

在这里插入图片描述

训练模型:

# 训练模型,保存到history中
history =model.fit(train_X,train_Y,epochs=10,batch_size=200,verbose=2,validation_data=(test_X, test_Y))
# verbose=2  仅输出每个epoch的最终结果

绘制折线图:

fig = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='lower right')

plt.subplot(2, 1, 2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper right')
plt.tight_layout()

plt.show()

保存模型:

# 保存模型
gfile = tf.io.gfile
save_dir = "./MNIST_model/"
if gfile.exists(save_dir):
    gfile.rmtree(save_dir)
gfile.mkdir(save_dir)

model_name = 'keras_mnist.h5'
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)

加载模型并检测正确率:

# 加载模型
mnist_model = tf.keras.models.load_model(model_path)

# 统计模型在测试上的分类结果
loss_and_metrics = mnist_model.evaluate(test_X, test_Y, verbose=2)

print("Test Loss: {}".format(loss_and_metrics[0]))
print("Test Accuracy: {}".format(loss_and_metrics[1] * 100))

在这里插入图片描述

模型训练结果为95.81%,结果还没有MLP好,没事,我们还有更复杂的卷积神经网络。

完整代码:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Activation
from keras.layers import Conv2D
from keras.layers import Flatten
from keras.layers import MaxPooling2D
from keras.layers import Dropout
import os


mnist = input_data.read_data_sets('D:\pythonProject1\MNIST\MNIST_data',one_hot=True) # 热编码
train_X = mnist.train.images
train_Y = mnist.train.labels
test_X = mnist.test.images
test_Y = mnist.test.labels

print(train_X.shape, type(train_X))
print(test_X.shape, type(test_X))


train_X = train_X.reshape(train_X.shape[0],28,28,1)
test_X = test_X.reshape(test_X.shape[0],28,28,1)
print(train_X.shape, type(train_X)) # (55000,  28, 28,1 ) <class 'numpy.ndarray'>
print(test_X.shape, type(test_X)) # (10000,  28, 28, 1 ) <class 'numpy.ndarray'>

#数据归一化 min-max 标准化
train_X /= 255
test_X /= 255

def create_model():
    model = Sequential()
    model.add(Conv2D(filters=32, kernel_size=(5, 5), activation='relu',
                     input_shape=(28, 28, 1)))
    # 最大池化层,池化窗口 2x2
    model.add(MaxPooling2D(pool_size=(2, 2)))
    # Dropout 25% 的输入神经元
    model.add(Dropout(0.2))
    # 将 Pooled feature map 摊平后输入全连接网络
    model.add(Flatten())
    # 全联接层
    model.add(Dense(128, activation='relu'))
    # 使用 softmax 激活函数做多分类,输出各数字的概率
    model.add(Dense(10, activation='softmax'))
    # 编译模型
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

    return model

model = create_model()

# 查看模型
model.summary()
for layer in model.layers:
    print(layer.get_output_at(0).get_shape().as_list())

# 训练模型,保存到history中
history =model.fit(train_X,train_Y,epochs=10,batch_size=200,verbose=2,validation_data=(test_X, test_Y))
# verbose=2  仅输出每个epoch的最终结果

# 可视化数据
fig = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='lower right')

plt.subplot(2, 1, 2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper right')
plt.tight_layout()

plt.show()

# 保存模型
gfile = tf.io.gfile
save_dir = "./MNIST_model/"
if gfile.exists(save_dir):
    gfile.rmtree(save_dir)
gfile.mkdir(save_dir)

model_name = 'keras_mnist.h5'
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)


# 加载模型
mnist_model = tf.keras.models.load_model(model_path)

# 统计模型在测试上的分类结果
loss_and_metrics = mnist_model.evaluate(test_X, test_Y, verbose=2)

print("Test Loss: {}".format(loss_and_metrics[0]))
print("Test Accuracy: {}".format(loss_and_metrics[1] * 100))
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-07-16 11:17:35  更:2021-07-16 11:18:51 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年3日历 -2024/3/29 3:36:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码