tensorflow——FashionMnist

  • 数据集下载

下载说明看https://github.com/zalandoresearch/fashion-mnist/blob/master/README.zh-CN.md

和mnist数据集类似。

数据集读取问题请详看tensorflow——FashionMnist数据集读取问题

  •  数据表现形式用matlibplot

与mnist数据不同的是,此次数据集是非单一热点模式

 

 奉上代码实现:

import tensorflow as tf
import numpy as np
import tensorflow.keras as keras
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist
import  matplotlib.pyplot as plt
import read

train_image_filename='data/train-images-idx3-ubyte.gz'
train_label_filename='data/train-labels-idx1-ubyte.gz'
test_image_filename='data/t10k-images-idx3-ubyte.gz'
test_label_filename='data/t10k-labels-idx1-ubyte.gz'

train_buffer_size=60000
test_buffer_size=10000

x_train = read.get_images(train_image_filename).reshape(-1, 28, 28)/255.
y_train = read.get_labels(train_label_filename)
x_test =  read.get_images(test_image_filename ).reshape(-1, 28, 28)/255.
y_test =  read.get_labels(test_label_filename )
plt.figure()
plt.imshow(x_train[0])#显示第一张图片


model = keras.models.load_model('model.h5')#导入训练好的模型
predictions = model.predict(x_test)
print(predictions[0])
print(np.argmax(predictions[0]))
print(y_test[0])
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

def score_bar(i, prediction_array, xticks, num_classes=10):
    plt.figure()
    plt.grid(False)
    x = np.arange(num_classes)
    if i >= 0:
        plt.bar(range(num_classes), prediction_array[i], color='#777777')
        for index,data in zip(x,list(prediction_array[i])):
            plt.text(index, data, '%.3f' %data, ha='center', va='bottom', fontsize=7)
    else:
        plt.bar(range(num_classes), prediction_array, color='#777777')
        for index,data in zip(x,list(prediction_array)):
            plt.text(index, data, '%.3f' %data, ha='center', va='bottom', fontsize=7)
            
    plt.xticks(x, list(xticks), size='small', rotation=30)
    plt.ylim([0,1])
    plt.title('Score')
    plt.xlabel('class_names')
    plt.ylabel('accuracy-rate')
    plt.show()

score_bar(0, predictions, class_names, 10)#显示第一张图片预测结果

def all_kind_score_bar(test_label, prediction_array, x_ticks, num_classes=10):
    test_each_kind_num = np.zeros([10])
    test_each_kind_acc_num = np.zeros([10])
    dst_prediction_rate = np.zeros([10]).astype('float32')
    for i in range(len(y_test)):
        test_each_kind_num[y_test[i]] += 1
    for i in range(len(y_test)):
        if(np.argmax(predictions[i]) == test_label[i]):
            test_each_kind_acc_num[y_test[i]] += 1
    test_each_kind_num.astype('float32')
    test_each_kind_acc_num.astype('float32')
    for i in range(num_classes):
        dst_prediction_rate[i] = test_each_kind_acc_num[i] / test_each_kind_num[i]
    score_bar(-1, dst_prediction_rate, x_ticks, num_classes)
    
all_kind_score_bar(y_test, predictions, class_names, num_classes=10)#显示10种样本预测正确性


'''#训练模型
inputs = tf.keras.Input(shape=[28,28], name='img')
x =  layers.Flatten()(inputs)
h1 = layers.Dense(128, activation='relu')(x)
pred = layers.Dense(10, activation='softmax')(h1)

model = tf.keras.Model(inputs=inputs, outputs=pred)
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.5),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=100, epochs=5)

test_score = model.evaluate(x_test, y_test)


print('test loss: %0.3f' %test_score[0])
print('test acc: %0.3f' %test_score[1])
model.save('model.h5')#保存模型
'''

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值