tensorflow2.0学习-Fashion MNIST识别

参考文章链接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()

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值