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 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> HCIA-AI_深度学习_利用TensorFlow进行手写数字识别 -> 正文阅读

[人工智能]HCIA-AI_深度学习_利用TensorFlow进行手写数字识别

3 利用TensorFlow进行手写数字识别

3.1 实验介绍

3.2 实验目的

  • 掌握TensorFlow开发的基本流程
  • 熟悉神经网络模型开发的基本要素:数据集、网络模型构建、模型训练、模型评估

3.3 实验步骤

3.3.1 项目描述和数据集获取

3.3.1.1 项目描述

3.3.1.2 数据获取以及数据处理

import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers,optimizers,datasets
from matplotlib import pyplot as plt
import numpy as np


(x_train_raw, y_train_raw), (x_test_raw, y_test_raw) = datasets.mnist.load_data()
print(y_train_raw[0])
print(x_train_raw.shape, y_train_raw.shape)
print(x_test_raw.shape, y_test_raw.shape)

# 将分类标签变为onehot编码
num_classes = 10
y_train = keras.utils.to_categorical(y_train_raw, num_classes)
y_test = keras.utils.to_categorical(y_test_raw, num_classes)
print(y_train[0], y_train.shape)
5
(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] (60000, 10)

3.3.2 数据集预处理及可视化

plt.figure()
for i in range(9):
    x_item = x_train_raw[i]
    y_item = y_train[i]
    plt.subplot(3, 3, i+1)
    plt.imshow(x_item)
    plt.axis('off')
plt.show()

在这里插入图片描述

# 将28*28的图像展开成784*1的向量
x_train = x_train_raw.reshape(60000, 784)
x_test = x_test_raw.reshape(10000, 784)
# 将图像像素值归一化
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255

3.3.3 DNN模型

3.3.3.1 DNN模型构建

model = keras.Sequential()
model.add(layers.Dense(512, activation='relu', input_dim=784))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(num_classes, activation='softmax'))
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               401920    
_________________________________________________________________
dense_2 (Dense)              (None, 256)               131328    
_________________________________________________________________
dense_3 (Dense)              (None, 128)               32896     
_________________________________________________________________
dense_4 (Dense)              (None, 10)                1290      
=================================================================
Total params: 567,434
Trainable params: 567,434
Non-trainable params: 0
_________________________________________________________________

3.3.3.2 DNN模型编译

model.compile(optimizer=optimizers.Adam(0.001),
              loss=keras.losses.categorical_crossentropy,
              metrics=['accuracy'])

3.3.3.3 DNN模型训练

model.fit(x_train, y_train, batch_size=128,
         epochs=10, verbose=1)
Epoch 1/10
469/469 [==============================] - 4s 8ms/step - loss: 0.2243 - accuracy: 0.9330
Epoch 2/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0836 - accuracy: 0.9740
Epoch 3/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0558 - accuracy: 0.9821
Epoch 4/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0403 - accuracy: 0.9870
Epoch 5/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0304 - accuracy: 0.9900
Epoch 6/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0261 - accuracy: 0.9916
Epoch 7/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0193 - accuracy: 0.9938
Epoch 8/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0200 - accuracy: 0.9934
Epoch 9/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0211 - accuracy: 0.9930
Epoch 10/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0122 - accuracy: 0.9961





<tensorflow.python.keras.callbacks.History at 0x13143ba5cd0>

3.3.3.4 DNN模型评估

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Test loss: 0.0889819785952568
Test accuracy: 0.9783999919891357

3.3.3.5 保存模型

logdir = './mnist_model'
if not os.path.exists(logdir):
    os.mkdir(logdir)
model.save(logdir + '/final_DNN_model.h5')

3.3.4 CNN模型

3.3.4.1 CNN模型构建

import tensorflow as tf
from tensorflow import keras
import numpy as np

model = keras.Sequential()
# 添加第一层卷积层和池化层
model.add(keras.layers.Conv2D(filters=32, kernel_size=5, strides=(1,1),
                             padding='same', activation=tf.nn.relu,
                              input_shape=(28,28,1)))
model.add(keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2), padding='valid'))

# 添加第二层卷积层和池化层
model.add(keras.layers.Conv2D(filters=64, kernel_size=3, strides=(1,1),
                             padding='same', activation=tf.nn.relu))
model.add(keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2), padding='valid'))
# 添加dropout层以减少过拟合
model.add(keras.layers.Dropout(0.25))
model.add(keras.layers.Flatten())
# 添加两层全连接层
model.add(keras.layers.Dense(units=128, activation=tf.nn.relu))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(units=10, activation=tf.nn.softmax))

model.summary()

3.3.4.2 CNN模型编译和训练

# 将数据扩充维度,以适应CNN模型
X_train = x_train.reshape(60000, 28, 28, 1)
X_test = x_test.reshape(10000, 28, 28, 1)

model.compile(optimizer='adam',
             loss='categorical_crossentropy',
             metrics=['accuracy'])

model.fit(X_train, y_train, epochs=5, batch_size=128)
Epoch 1/5
469/469 [==============================] - 34s 72ms/step - loss: 0.2759 - accuracy: 0.9143
Epoch 2/5
469/469 [==============================] - 33s 71ms/step - loss: 0.0907 - accuracy: 0.9738
Epoch 3/5
469/469 [==============================] - 33s 71ms/step - loss: 0.0671 - accuracy: 0.9794
Epoch 4/5
469/469 [==============================] - 33s 71ms/step - loss: 0.0567 - accuracy: 0.9833
Epoch 5/5
469/469 [==============================] - 33s 71ms/step - loss: 0.0472 - accuracy: 0.9858


<tensorflow.python.keras.callbacks.History at 0x1314fffe250>

3.3.4.3 CNN模型验证

test_loss, test_acc = model.evaluate(X_test, y_test)
print('Test Accuracy %.4f' %test_acc)
313/313 [==============================] - 3s 10ms/step - loss: 0.0219 - accuracy: 0.9923
Test Accuracy 0.9923

3.3.4.4 CNN模型保存

logdir = './mnist_model'
if not os.path.exists(logdir):
    os.mkdir(logdir)
model.save(logdir + '/final_CNN_model.h5')

3.3.5 预测结果可视化

from tensorflow.keras.models import load_model

new_model = load_model(logdir + '/final_CNN_model.h5')
new_model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 28, 28, 32)        832       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 14, 14, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 64)          0         
_________________________________________________________________
dropout (Dropout)            (None, 7, 7, 64)          0         
_________________________________________________________________
flatten (Flatten)            (None, 3136)              0         
_________________________________________________________________
dense_5 (Dense)              (None, 128)               401536    
_________________________________________________________________
dropout_1 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_6 (Dense)              (None, 10)                1290      
=================================================================
Total params: 422,154
Trainable params: 422,154
Non-trainable params: 0
_________________________________________________________________
# 测试集输出结果可视化

import matplotlib.pyplot as plt

%matplotlib inline
def res_Visual(n):
    final_opt_a = new_model.predict_classes(X_test[0:n])
    fig, ax = plt.subplots(nrows=int(n/5), ncols=5)
    ax = ax.flatten()
    print('前{}张图片预测结果为:'.format(n))
    for i in range(n):
        print(final_opt_a[i], end=',')
        if int((i+1)%5) == 0:
            print('\t')
        img = X_test[i].reshape(28, 28)
        plt.axis('off')
        ax[i].imshow(img, cmap='Greys', interpolation='nearest')
        ax[i].axis('off')
res_Visual(20)
WARNING:tensorflow:From <ipython-input-36-eb12790dfd7d>:7: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.
Instructions for updating:
Please use instead:* `np.argmax(model.predict(x), axis=-1)`,   if your model does multi-class classification   (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,   if your model does binary classification   (e.g. if it uses a `sigmoid` last-layer activation).
前20张图片预测结果为:
7,2,1,0,4,	
1,4,9,5,9,	
0,6,9,0,1,	
5,9,7,3,4,	

在这里插入图片描述

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-04-09 18:22:35  更:2022-04-09 18:26:55 
 
开发: 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年11日历 -2024/11/26 10:36:41-

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