有T恤、短靴等10个类别图像,每张图片为28*28像素的灰阶图像
共70000张图像,60K当作训练集,10K当作测试集
# !pip install -U tensorflow_datasets
# !pip install -U -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.12.0 # 2.0.0a0
import tensorflow as tf
# 设置日志输出等级
tf.logging.set_verbosity(tf.logging.ERROR)
print(tf.__version__)
# tf.contrib 将被弃用,tf2.0 Eager Execution 变为默认执行模式
# import tensorflow.contrib.eager as tfe
# 开启Eager贪婪模式。一旦开启不能撤销!只执行一次。
tf.enable_eager_execution()
import tensorflow_datasets as tfds
import math
import numpy as np
import matplotlib.pyplot as plt
import tqdm
import tqdm.auto
tqdm.tqdm = tqdm.auto.tqdm
1.12.0
dataset, metadata = tfds.load('fashion_mnist', as_supervised=True, with_info=True)
train_dataset, test_dataset = dataset['train'], dataset['test']
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
num_train_examples = metadata.splits['train'].num_examples
num_test_examples = metadata.splits['test'].num_examples
print("Number of training examples: {}".format(num_train_examples))
print("Number of test examples: {}".format(num_test_examples))
Number of training examples: 60000
Number of test examples: 10000
# 标准化
def normalize(images, labels):
images = tf.cast(images, tf.float32)
images /= 255
return images, labels
train_dataset = train_dataset.map(normalize)
test_dataset = test_dataset.map(normalize)
# 查看单个样本图像
for image, label in test_dataset.take(1):
break
image = image.numpy().reshape((28, 28))
plt.figure()
plt.imshow(image, cmap=plt.cm.binary)
# 添加颜色标注
plt.colorbar()
# 去掉网格,默认也是去掉
plt.grid(False)
# 查看25个样本图像
plt.figure(figsize=(10, 10))
i = 0
for image, label in test_dataset.take(25):
image = image.numpy().reshape((28, 28))
plt.subplot(5, 5, i+1)
plt.xticks([])
plt.yticks([])
plt.imshow(image, cmap=plt.cm.binary)
plt.xlabel(class_names[label])
i += 1
# 建立模型
model = tf.keras.Sequential([
# 32个卷积输出,原来的一张图像变为32张卷积图像
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu,
input_shape=(28, 28, 1)),
# strides=2 步长为2
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Conv2D(64, (3,3), padding='same', activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
#model = tf.keras.Sequential([
# # 扁平化Flatten 将二维图像转换为一维向量的过程
# tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
# tf.keras.layers.Dense(128, activation=tf.nn.relu),
# tf.keras.layers.Dense(10, activation=tf.nn.softmax)
#])
# 编译模型
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
# 分类使用此损失函数
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 训练模型
BATCH_SIZE = 32
train_dataset = train_dataset.repeat().shuffle(num_train_examples).batch(BATCH_SIZE)
test_dataset = test_dataset.batch(BATCH_SIZE)
model.fit(train_dataset, epochs=10, steps_per_epoch=math.ceil(num_train_examples/BATCH_SIZE), verbose=1)
Epoch 1/10
1875/1875 [==============================] - 35s 19ms/step - loss: 0.4032 - acc: 0.8542
Epoch 2/10
1875/1875 [==============================] - 29s 16ms/step - loss: 0.2568 - acc: 0.9071
Epoch 3/10
1875/1875 [==============================] - 29s 15ms/step - loss: 0.2163 - acc: 0.9201
Epoch 4/10
1875/1875 [==============================] - 30s 16ms/step - loss: 0.1832 - acc: 0.9327
Epoch 5/10
1875/1875 [==============================] - 30s 16ms/step - loss: 0.1557 - acc: 0.9437
Epoch 6/10
1875/1875 [==============================] - 29s 16ms/step - loss: 0.1357 - acc: 0.9501
Epoch 7/10
1875/1875 [==============================] - 29s 16ms/step - loss: 0.1162 - acc: 0.9570
Epoch 8/10
1875/1875 [==============================] - 29s 16ms/step - loss: 0.0949 - acc: 0.9651
Epoch 9/10
1875/1875 [==============================] - 29s 16ms/step - loss: 0.0803 - acc: 0.9705
Epoch 10/10
1875/1875 [==============================] - 28s 15ms/step - loss: 0.0696 - acc: 0.9749
# 评估模型在测试集上的效果
test_loss, test_acc = model.evaluate(test_dataset, steps=math.ceil(num_test_examples/BATCH_SIZE))
print("Accuracy on test dataset:", test_acc)
313/313 [==============================] - 2s 7ms/step
Accuracy on test dataset: 0.9222
!jupyter nbconvert --to markdown "Udacity优达学城 TensorFlow笔记2-对服饰图像分类.ipynb"