fashion MNIST识别(Tensorflow + Keras + NN)

Fashion MNIST

https://www.kaggle.com/zalando-research/fashionmnist

Fashion-MNIST is a dataset of Zalando‘s article images—consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes. Zalando intends Fashion-MNIST to serve as a direct drop-in replacement for the original MNIST dataset for benchmarking machine learning algorithms. It shares the same image size and structure of training and testing splits.

The original MNIST dataset contains a lot of handwritten digits. Members of the AI/ML/Data Science community love this dataset and use it as a benchmark to validate their algorithms. In fact, MNIST is often the first dataset researchers try. "If it doesn‘t work on MNIST, it won‘t work at all", they said. "Well, if it does work on MNIST, it may still fail on others."

Zalando seeks to replace the original MNIST dataset

Code

https://github.com/fanqingsong/code-snippet/blob/master/machine_learning/FMNIST/code.py

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np

print(tf.__version__)

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‘]

train_images = train_images / 255.0

test_images = test_images / 255.0

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer=tf.train.AdamOptimizer(),
              loss=‘sparse_categorical_crossentropy‘,
              metrics=[‘accuracy‘])

model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)

print(‘Test accuracy:‘, test_acc)

predictions = model.predict(test_images)

print(test_labels[0])

print(np.argmax(predictions[0]))

run

[email protected]:~/mine/code-snippet/machine_learning/FMNIST#
[email protected]:~/mine/code-snippet/machine_learning/FMNIST# python code.py
1.14.0
WARNING: Logging before flag parsing goes to stderr.
W0816 23:26:49.741352 140630311962432 deprecation.py:506] From /usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling __init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
W0816 23:26:49.977197 140630311962432 deprecation_wrapper.py:119] From code.py:33: The name tf.train.AdamOptimizer is deprecated. Please use tf.compat.v1.train.AdamOptimizer instead.

2019-08-16 23:26:50.289949: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-08-16 23:26:50.684455: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 1992000000 Hz
2019-08-16 23:26:50.686887: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fffe64d99e0 executing computations on platform Host. Devices:
2019-08-16 23:26:50.686967: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
2019-08-16 23:26:50.958569: W tensorflow/compiler/jit/mark_for_compilation_pass.cc:1412] (One-time warning): Not using XLA:CPU for cluster because envvar TF_XLA_FLAGS=--tf_xla_cpu_global_jit was not set.  If you want XLA:CPU, either set that envvar, or use experimental_jit_scope to enable XLA:CPU.  To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a proper command-line flag, not via TF_XLA_FLAGS) or set the envvar XLA_FLAGS=--xla_hlo_profile.
Epoch 1/5
60000/60000 [==============================] - 3s 50us/sample - loss: 0.4992 - acc: 0.8240
Epoch 2/5
60000/60000 [==============================] - 2s 40us/sample - loss: 0.3758 - acc: 0.8650
Epoch 3/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.3382 - acc: 0.8770
Epoch 4/5
60000/60000 [==============================] - 2s 41us/sample - loss: 0.3135 - acc: 0.8854
Epoch 5/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.2953 - acc: 0.8922
10000/10000 [==============================] - 0s 25us/sample - loss: 0.3533 - acc: 0.8715
(‘Test accuracy:‘, 0.8715)
9
9
[email protected]:~/mine/code-snippet/machine_learning/FMNIST#

Reference

https://github.com/MachineIntellect/DeepLearner/blob/master/basic_classification.ipynb

https://tensorflow.google.cn/beta/guide/data

原文地址:https://www.cnblogs.com/lightsong/p/11366934.html

时间: 2024-07-30 14:04:11

fashion MNIST识别(Tensorflow + Keras + NN)的相关文章

手写数字识别——利用keras高层API快速搭建并优化网络模型

在<手写数字识别——手动搭建全连接层>一文中,我们通过机器学习的基本公式构建出了一个网络模型,其实现过程毫无疑问是过于复杂了——不得不考虑诸如数据类型匹配.梯度计算.准确度的统计等问题,但是这样的实践对机器学习的理解是大有裨益的.在大多数情况下,我们还是希望能多简单就多简单地去搭建网络模型,这同时也算对得起TensorFlow这个强大的工具了.本节,还是以手写数据集MNIST为例,利用TensorFlow2.0的keras高层API重现之前的网络. 一.数据的导入与预处理 关于这个过程,与上节

用标准3层神经网络实现MNIST识别

一.MINIST数据集下载 1.https://pjreddie.com/projects/mnist-in-csv/      此网站提供了mnist_train.csv和mnist_test.csv,其中mnist_train.csv有60000个训练数据,mnist_test.csv有10000个测试数据 2.还有两个较小数据集,可供测试. https://raw.githubusercontent.com/makeyourownneuralnetwork/makeyourownneura

python用K近邻(KNN)算法分类MNIST数据集和Fashion MNIST数据集

一.KNN算法的介绍 K最近邻(k-Nearest Neighbor,KNN)分类算法是最简单的机器学习算法之一,理论上比较成熟.KNN算法首先将待分类样本表达成和训练样本一致的特征向量:然后根据距离计算待测试样本和每个训练样本的距离,选择距离最小的K个样本作为近邻样本:最后根据K个近邻样本判断待分类样本的类别.KNN算法的正确选取是分类正确的关键因素之一,而近邻样本是通过计算测试样本与每个训练集样本的距离来选定的,故定义合适的距离是KNN正确分类的前提. 本文中在上述研究的基础上,将特征属性值

CS224d 单隐层全连接网络处理英文命名实体识别tensorflow

什么是NER? 命名实体识别(NER)是指识别文本中具有特定意义的实体,主要包括人名.地名.机构名.专有名词等.命名实体识别是信息提取.问答系统.句法分析.机器翻译等应用领域的重要基础工具,作为结构化信息提取的重要步骤. NER具体任务 1.确定实体位置 2.确定实体类别 给一个单词,我们需要根据上下文判断,它属于下面四类的哪一个,如果都不属于,则类别为0,即不是实体,所以这是一个需要分成 5 类的问题: ? Person (PER) ? Organization (ORG) ? Locatio

TensorFlow 从入门到精通(八):TensorFlow tf.nn.conv2d 一路追查

读者可能还记得本系列博客(二)和(六)中 tf.nn 模块,其中最关心的是 conv2d 这个函数. 首先将博客(二) MNIST 例程中 convolutional.py 关键源码列出: def model(data, train=False): """The Model definition.""" # 2D convolution, with 'SAME' padding (i.e. the output feature map has #

基于多层感知机的手写数字识别(Tensorflow实现)

import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import os mnist = input_data.read_data_sets('MNIST_data', one_hot=True) class MNISTModel(object): def __init__(self, lr, batch_size, iter_num): self

手写数字识别-Tensorflow框架

#MNIST数据集 # coding: utf-8 # In[2]: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # In[3]: #载入数据集 mnist = input_data.read_data_sets("MNIST_data",one_hot=True) #每个批次的大小 batch_size = 50 #计算一共有多少个批次 n_batch = mni

TensorFlow keras 迁移学习

数据的读取 import tensorflow as tf from tensorflow.python import keras from tensorflow.python.keras.preprocessing.image import ImageDataGenerator class TransferModel(object): def __init__(self): #标准化和数据增强 self.train_generator = ImageDataGenerator(rescale=

Mac OS安装TensorFlow+Keras

因为显示不支持GPU加速,所以本文没有相关配置. 1. 安装 Homebrew,macOS 不可或缺的套件管理器.     /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 2. 安装python     2.1 检查是否已经装了python.            python -V            如果已经安装了2.7 或者 3.