tensorflow2.0入门系列(二)

tensorflow2.0入门系列(二)

导入相关包

1
2
3
4
5
import tensorflow as tf
from tensorflow import keras

import numpy as np
import matplotlib.pyplot as plt

Fashion MNIST数据集,包含10个类别中的70,000个灰度图像。 图像显示了(28 x 28)的单个服装物品.
60,000张图像来训练网络和10,000张图像来评估网络学习图像分类的准确程度
图像为28x28 NumPy数组,像素值范围为0到255.标签是一个整数数组,范围从0到9.这些对应于图像所代表的服装类别:

Label Class
0 T-shirt/top
1 Trouser
2 Pullover
3 Dress
4 Coat
5 Sandal
6 Shirt
7 Sneaker
8 Bag
9 Ankle boot

导入时装MNIST数据集,作为MNIST手写时装数据集的扩展

1
2
3
fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

每个图像都映射到一个标签。 由于类名不包含在数据集中,因此将它们存储在此处以便在后面绘制图像时使用

1
2
#新建列表用于存儲每個标签所对应的类型
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

数据扩展

(ps下方展示的代码仅可用于了解一些基本的数据操作扩展方法,并非核心代码)

1
2
3
4
5
6
7
#获取数据集的Shape
print(train_images)
#输出(60000, 28, 28)

#获取数据集的数量
print(len(train_images))
#输出60000

数据预处理

在训练网络之前必须对数据进行预处理。
一是方便我们的神经网络能够快速的识别里面的特征。
二是能够有效的帮助我们训练出一个成功的模型。
我们可以借助matplotlib这个绘图工具包帮助我们直观的感受数据集,以及训练过程。当然TensorFlow也提供了tensorboard用于可视化,暂时我们先不做讲解
我们可以先试着借助matplotlib画出我们数据集中的某张图片:

1
2
3
4
5
6
7
8
9
10
11
plt.figure()
#显示图像
plt.imshow(train_images[0])
#颜色比例彩条
plt.colorbar()
#不显示网格线
plt.grid(False)
#保存图像
plt.savefig('some_one.png')
#显示图像
plt.show()

输出结果
在将它们送到神经网络模型之前,我们为了将这些值缩放到0到1的范围,我们将值除以255.训练集和测试集以相同的方式进行预处理

1
2
3
train_images = train_images / 255.0

test_images = test_images / 255.0

为了验证数据格式是否正确以及我们是否已准备好构建和训练网络,让我们显示训练集中的前25个图像并在每个图像下方显示类名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#创建图
plt.figure(figsize=(10,10))
for i in range(25):
#创建子图,5行5列,第i+1个
plt.subplot(5,5,i+1)
#X轴坐标刻度设置
plt.xticks([])
#Y轴坐标刻度设置
plt.yticks([])
plt.grid(False)
#cmap: 颜色图谱(colormap), 默认绘制为RGB(A)颜色空间,这里使用的是二值图
plt.imshow(train_images[i])
plt.colorbar()
#设置X轴标签
plt.xlabel(class_names[train_labels[i]])
plt.show()

可以通过右边的彩色比例条可以明显看出图像值已经缩放到0-1的范围

构建模型

设置神经网络层

这里还是采用Keras提供的层叠模型

1
2
3
4
5
6
7
8
model = keras.Sequential([
# 输入层
keras.layers.Flatten(input_shape=(28, 28)),
# 隐藏层一,全连接
keras.layers.Dense(128, activation='relu'),
# 输出层,全连接
keras.layers.Dense(10, activation='softmax')
])

配置模型

1
2
3
4
#采用Adam优化器,loss计算方法为sparse_categorical_crossentropy,评价函数
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

训练模型

1
2
#训练总轮数为5轮
model.fit(train_images, train_labels, epochs=10)

评估模型

看准确度

1
2
3
test_loss, test_acc = model.evaluate(test_images, test_labels)

print('\nTest accuracy:', test_acc)

进行预测

1
predictions = model.predict(test_images)

现在我们去其中第一个结果看看预测情况:

1
print(predictions[0])

输出:[2.4625590e-08 6.4621108e-10 6.6212579e-08 1.8379983e-11 4.6871547e-09
4.0910607e-05 3.9370971e-07 9.7850722e-04 2.8181713e-09 9.9898010e-01]
预测是10个数字的数组。 它们代表模型的“信心”,即图像对应于10种不同服装中的每一种。 我们可以看到哪个标签具有最高的置信度值
我们可以使用np.argmax()来直接计算出置信度最高的所对应的标签

1
2
print(np.argmax(predictions[0]))
#输出 9

我们再对比一下真正的标签

1
2
print(test_labels[0])
# 输出 9

我们同样可以借助matplotlib直观的表现出来
首先我们可以构建画小图的函数和画预测值直方图的函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array[i], 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[i], true_label[i]
plt.grid(False)
plt.xticks(range(10), class_names, rotation=90)
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')

我们可以看看15张图片的表现情况

1
2
3
4
5
6
7
8
9
10
11
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions, test_labels)
plt.tight_layout()
plt.show()

预测情况