mnist识别优化——使用新的fashion mnist进行模型训练
今天通过论坛偶然知道,在mnist之后,还出现了一个旨在代替经典mnist数据集的Fashion MNIST,同mnist一样,它也是被用作深度学习程序的“hello world”,而且也是由70k张28*28的图片组成的,它们也被分为10类,有60k被用作训练,10k被用作测试。唯一的区别就是,fashion mnist的十种类别由手写数字换成了服装。这十种类别如下:
'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'
设计流程如下:
· 首先获取数据集,tensorflow获取fashion mnist的方法和mnist类似,使用keras.datasets.fashion_mnist.load_data()即可
· 将数据集划分为训练集和测试集
· 由于图片像素值范围是0-255,将数据集进行预处理,把像素值缩放到0到1的范围(即除以255)
· 搭建网络模型 (784→128(relu)→10(softmax)),全连接
· 编译模型,设计损失函数(对数损失)、优化器(adam)以及训练指标(accuracy)
· 训练模型
· 评估准确性(测试数据使用matplotlib进行可视化)
关于Adam优化器的来源和特点请参考:https://www.jianshu.com/p/aebcaf8af76e
关于matplotlib数据可视化请参考:https://blog.csdn.net/xHibiki/article/details/84866887
训练集部分数据可视化如下:

一共做了50轮训练,训练开始时的损失和精度如下:

训练完成时的损失和精度如下:

模型在测试集上的表现如下:

选择测试集某张图片的预测可视化结果如下:

程序代码如下:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt # 导入fashion mnist数据集
fashion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data() # 衣服类别
class_names = ['T-shirt/top','Trouser','Pullover','Dress','Coat','Sandal',
'Shirt','Sneaker','Bag','Ankle boot']
print(train_images.shape,len(train_labels))
print(test_images.shape,len(test_labels)) # 查看图片
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show() # 预处理数据,将像素值除以255,使其缩放到0到1的范围
train_images = train_images / 255.0
test_images = test_images / 255.0 # 验证数据格式的正确性,显示训练集前25张图像并注明类别
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i],cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show() # 搭建网络结构
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28)),
keras.layers.Dense(128,activation='relu'),
keras.layers.Dense(10,activation='softmax')
]) # 设置损失函数、优化器及训练指标
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
) # 训练模型
model.fit(train_images,train_labels,epochs=50) # 模型评估
test_loss,test_acc=model.evaluate(test_images,test_labels,verbose=2)
print('/nTest accuracy:',test_acc) # 选择测试集中的图像进行预测
predictions=model.predict(test_images) # 查看第一个预测
print("预测结果:",np.argmax(predictions[0]))
# 将正确标签打印出来和预测结果对比
print("真实结果:",test_labels[0]) # 以图形方式查看完整的十个类的预测
def plot_image(i,predictions_array,true_label,img):
predictions_array,true_label,img=predictions_array,true_label[i],img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([]) plt.imshow(img,cmap=plt.cm.binary) predicted_label=np.argmax(predictions_array)
if predicted_label==true_label:
color='blue'
else:
color='red' plt.xlabel("{}{:2.0f}%({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color) def plot_value_array(i,predictions_array,true_label):
predictions_array,true_label=predictions_array,true_label[i]
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot=plt.bar(range(10),predictions_array,color="#777777")
plt.ylim([0,1])
predicted_label=np.argmax(predictions_array) thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue') i=10
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i,predictions[i],test_labels,test_images)
plt.subplot(1,2,2)
plot_value_array(i,predictions[i],test_labels)
plt.show()
mnist识别优化——使用新的fashion mnist进行模型训练的更多相关文章
- 人脸检测及识别python实现系列(3)——为模型训练准备人脸数据
人脸检测及识别python实现系列(3)——为模型训练准备人脸数据 机器学习最本质的地方就是基于海量数据统计的学习,说白了,机器学习其实就是在模拟人类儿童的学习行为.举一个简单的例子,成年人并没有主动 ...
- 用标准3层神经网络实现MNIST识别
一.MINIST数据集下载 1.https://pjreddie.com/projects/mnist-in-csv/ 此网站提供了mnist_train.csv和mnist_test.cs ...
- fashion MNIST识别(Tensorflow + Keras + NN)
Fashion MNIST https://www.kaggle.com/zalando-research/fashionmnist Fashion-MNIST is a dataset of Zal ...
- 深度学习常用数据集 API(包括 Fashion MNIST)
基准数据集 深度学习中经常会使用一些基准数据集进行一些测试.其中 MNIST, Cifar 10, cifar100, Fashion-MNIST 数据集常常被人们拿来当作练手的数据集.为了方便,诸如 ...
- NO.3:自学tensorflow之路------MNIST识别,神经网络拓展
引言 最近自学GRU神经网络,感觉真的不简单.为了能够快速跑完程序,给我的渣渣笔记本(GT650M)也安装了一个GPU版的tensorflow.顺便也更新了版本到了tensorflow-gpu 1.7 ...
- 莫烦大大keras学习Mnist识别(4)-----RNN
一.步骤: 导入包以及读取数据 设置参数 数据预处理 构建模型 编译模型 训练以及测试模型 二.代码: 1.导入包以及读取数据 #导入包 import numpy as np np.random.se ...
- TensorFlow+实战Google深度学习框架学习笔记(12)------Mnist识别和卷积神经网络LeNet
一.卷积神经网络的简述 卷积神经网络将一个图像变窄变长.原本[长和宽较大,高较小]变成[长和宽较小,高增加] 卷积过程需要用到卷积核[二维的滑动窗口][过滤器],每个卷积核由n*m(长*宽)个小格组成 ...
- Windows下mnist数据集caffemodel分类模型训练及测试
1. MNIST数据集介绍 MNIST是一个手写数字数据库,样本收集的是美国中学生手写样本,比较符合实际情况,大体上样本是这样的: MNIST数据库有以下特性: 包含了60000个训练样本集和1000 ...
- 吴裕雄 python 神经网络——TensorFlow实现回归模型训练预测MNIST手写数据集
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...
随机推荐
- POJ_1050_最大子矩阵
http://poj.org/problem?id=1050 这道题是最大子串的扩展,遍历过每一个子矩阵就好了,期间用了最大子串的方法. #include<iostream> #inclu ...
- Java1变量数据类型和运算符
day02_变量数据类型和运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class jh_01_数据类型说明 { public ...
- MySQL中遍历查询结果的常用API(c)
本中所使用的table: MySQL中的错误处理函数 unsigned int mysql_errno(MYSQL *mysql) const char *mysql_error(MYSQL *mys ...
- C++括号匹配检测(用栈)
输入一串括号,包括圆括号和方括号,()[],判断是否匹配,即([]())或[([][])]为匹配的正确的格式,[(])或([())为不匹配的格式. #include<iostream> # ...
- GO语言slice详解(结合源码)
一.GO语言中slice的定义 slice 是一种结构体类型,在源码中的定义为: src/runtime/slice.go type slice struct { array unsafe.Point ...
- pytoch之 encoder,decoder
import torch import torch.nn as nn import torch.utils.data as Data import torchvision import matplot ...
- Zookeeper 应用实例
配置管理 程序总是需要配置的,如果程序分散部署在多台机器上,要逐个改变配置就变得困难.好吧,现在把这些配置全部放到zookeeper上去,保存在 Zookeeper 的某个目录节点中,然后所有相关应用 ...
- 为什么Linux 实例执行 df 和 du 查看磁盘时结果不一致
问题现象 执行 df -h 查看 ECS Linux 实例文件系统使用率,可以看到 /dev/xvdb1 磁盘占用了约27G,挂载目录为 /opt . 进入到 /opt 目录执行 du -sh ,显示 ...
- mac chrome
command + < 可以直接跳转到谷歌设置的页面去.
- docker安装db2数据库
查询可安装的db2镜像 # docker search db2 [root@docker-servers ~]# docker search db2 INDEX NAME DESCRIPTION ST ...