TensorFlow2开发基本步骤

TensorFlow2开发基本步骤

HCIA-AI 华为认证人工智能工程师在线课程
在这里插入图片描述

本单元课程主要讲解了如何使用tf.keras模块来完成MNIST手写体数字识别。

详细内容要点:

1、数据预处理

2、全连接和卷积神经网络建模

3、网络测试与结果可视化

学完本单元后,你能掌握tf.keras模块来完成基于全连接与卷积神经网络的MNIST手写体数字识别。

代码

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 18 15:06:45 2021
《HCIA-AI 华为认证人工智能工程师在线课程》
TensorFlow2开发基本步骤
MNIST手写体数字识别-全连接和卷积神经网络
@author: FIEforever
"""

import os
import tensorflow as tf
from tensorflow.keras import layers,optimizers,datasets
from matplotlib import pyplot as plt
import numpy as np
import tensorflow.keras as keras
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

(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_train_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])
print(np.shape(y_train))

plt.figure()
for i in range(9):
    plt.subplot(3,3, i+1)
    plt.imshow(x_train_raw[i])
    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个全连接层和两个RELU激活函数
model = keras.Sequential([
    layers.Dense(512, activation='relu',input_dim=784),
    layers.Dense(256, activation='relu'),
    layers.Dense(124, activation='relu'),
    layers.Dense(num_classes, activation='softmax'),
    ])
model.summary()

#定义优化器
Optimizer = optimizers.Adam(0.001)
#编译模型
model.compile(loss=keras.losses.categorical_crossentropy, 
              optimizer=Optimizer,
              metrics=['accuracy'])
#训练模型
model.fit(x_train,y_train,batch_size=128,epochs=10,verbose=1)
#测试模型
score = model.evaluate(x_test,y_test,verbose=0)
print('Test loss:',score[0])
print('Test accuracy:',score[1])
#保存模型
model.save('./model/final_DNN_model.h5')

#卷积神经网络网络构建
#创建网络序列
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))

#将数据扩充维度,以适应CNN模型
X_train = x_train.reshape(60000,28,28,1)
X_test = x_test.reshape(10000,28,28,1)
model.compile(optimizer=Optimizer,
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x=X_train,y=y_train,epochs=5,batch_size=128)
#CNN模型验证
test_loss,test_acc = model.evaluate(x=X_test,y=y_test)
print("Test Accuracy %.2f"%test_acc)
#保存模型
model.save('./model/final_CNN_model.h5')
#加载保存的模型
new_model = keras.models.load_model('./model/final_CNN_model.h5')
new_model.summary()

#测试集输出结果可视化
#%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")
    print('测试集前{}张图片为:'.format((n)))
res_Visual(20)

运行输出:

5
(60000, 28, 28) (60000,)
(10000, 28, 28) (60000,)
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
(60000, 10)

在这里插入图片描述

Model: "sequential_8"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_18 (Dense)             (None, 512)               401920    
_________________________________________________________________
dense_19 (Dense)             (None, 256)               131328    
_________________________________________________________________
dense_20 (Dense)             (None, 124)               31868     
_________________________________________________________________
dense_21 (Dense)             (None, 10)                1250      
=================================================================
Total params: 566,366
Trainable params: 566,366
Non-trainable params: 0
_________________________________________________________________
Epoch 1/10
469/469 [==============================] - 3s 7ms/step - loss: 0.2333 - accuracy: 0.9320
Epoch 2/10
469/469 [==============================] - 4s 8ms/step - loss: 0.0815 - accuracy: 0.9750
Epoch 3/10
469/469 [==============================] - 4s 7ms/step - loss: 0.0560 - accuracy: 0.9822
Epoch 4/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0384 - accuracy: 0.9879
Epoch 5/10
469/469 [==============================] - 4s 7ms/step - loss: 0.0304 - accuracy: 0.9904
Epoch 6/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0261 - accuracy: 0.9913
Epoch 7/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0221 - accuracy: 0.9926
Epoch 8/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0159 - accuracy: 0.9950
Epoch 9/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0194 - accuracy: 0.9936
Epoch 10/10
469/469 [==============================] - 3s 7ms/step - loss: 0.0165 - accuracy: 0.9946
Test loss: 0.09183511883020401
Test accuracy: 0.9789000153541565
Epoch 1/5
469/469 [==============================] - 43s 91ms/step - loss: 0.1833 - accuracy: 0.9438
Epoch 2/5
469/469 [==============================] - 43s 91ms/step - loss: 0.0746 - accuracy: 0.9778
Epoch 3/5
469/469 [==============================] - 43s 92ms/step - loss: 0.0576 - accuracy: 0.9834
Epoch 4/5
469/469 [==============================] - 43s 91ms/step - loss: 0.0485 - accuracy: 0.9860
Epoch 5/5
469/469 [==============================] - 43s 92ms/step - loss: 0.0427 - accuracy: 0.9868
313/313 [==============================] - 2s 7ms/step - loss: 0.0234 - accuracy: 0.9916
Test Accuracy 0.99
WARNING:tensorflow:Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
Model: "sequential_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_14 (Conv2D)           (None, 28, 28, 32)        832       
_________________________________________________________________
max_pooling2d_14 (MaxPooling (None, 14, 14, 32)        0         
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 14, 14, 64)        18496     
_________________________________________________________________
max_pooling2d_15 (MaxPooling (None, 7, 7, 64)          0         
_________________________________________________________________
dropout_14 (Dropout)         (None, 7, 7, 64)          0         
_________________________________________________________________
flatten_7 (Flatten)          (None, 3136)              0         
_________________________________________________________________
dense_22 (Dense)             (None, 128)               401536    
_________________________________________________________________
dropout_15 (Dropout)         (None, 128)               0         
_________________________________________________________________
dense_23 (Dense)             (None, 10)                1290      
=================================================================
Total params: 422,154
Trainable params: 422,154
Non-trainable params: 0
_________________________________________________________________
WARNING:tensorflow:6 out of the last 6 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001FF26E82040> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for  more details.20张图片预测结果为:
7,2,1,0,4,	
1,4,9,5,9,	
0,6,9,0,1,	
5,9,7,3,4,	
测试集前20张图片为:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值