参考文章链接:https://blog.csdn.net/qq_35200479/article/details/83752487、
1.打开jupyter notebook,导入包
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import gzip
import os
import matplotlib
2.准备数据集
下载数据集,并确定好数据集所在文件夹地址
# 下载中文支持字体。后面画图需要
zhfont = matplotlib.font_manager.FontProperties(fname='./SimHei.ttf')
def read_data():
path= './archive/files'
with open('./archive/files/train-labels-idx1-ubyte', 'rb') as lbpath:
y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with open('./archive/files/train-images-idx3-ubyte', 'rb') as imgpath:
x_train = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
with open('./archive/files/t10k-labels-idx1-ubyte', 'rb') as lbpath:
y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with open('./archive/files/t10k-images-idx3-ubyte', 'rb') as imgpath:
x_test = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
return (x_train, y_train), (x_test, y_test)
3.读取数据
数据集分类:训练、测试
(train_images, train_labels), (test_images, test_labels) = read_data()
类名与标签对应
例如:0-短袖圆领T恤 、1-裤子
class_names = ['短袖圆领T恤', '裤子', '套衫', '连衣裙', '外套',
'凉鞋', '衬衫', '运动鞋','包', '短靴']
4.创建网络模型
def build_model():
# 线性叠加
model = tf.keras.models.Sequential()
# 改变平缓输入
model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
# 第一层紧密连接128神经元
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
# 第二层分10 个类别
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
return model
5.编译模型及训练
确定好优化器、损失函数、指标等
model = build_model()
model.compile(optimizer='Adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
#训练
model.fit(train_images,train_labels,epochs=5)
6.评估模型
# 评估模型(主要是测试数据集)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('测试损失:%f 测试准确率: %f' % (test_loss, test_acc))
7.模型预测
predictions = model.predict(test_images)
# 提取20个数据集,进行预测判断是否正确
for i in range(25):
pre = class_names[np.argmax(predictions[i])]
tar = class_names[test_labels[i]]
print("预测:%s 实际:%s" % (pre, tar))
预测结果图片显示
# 保存画布的图形,宽度为 10 , 长度为10
plt.figure(figsize=(10,10))
# 预测 25 张图像是否准确,不准确为红色。准确为蓝色
for i in range(25):
# 创建分布 5 * 5 个图形
plt.subplot(5, 5, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
# 显示照片,以cm 为单位。
plt.imshow(test_images[i], cmap=plt.cm.binary)
# 预测的图片是否正确,黑色底表示预测正确,红色底表示预测失败
predicted_label = np.argmax(predictions[i])
true_label = test_labels[i]
if predicted_label == true_label:
color = 'black'
else:
color = 'red'
plt.xlabel("{} ({})".format(class_names[predicted_label],
class_names[true_label]),
color=color,
fontproperties = zhfont)
plt.show()
字体黑色正确,红色错误
模型框架:
model.summary()