TensorFlow2.0学习笔记1一Fashion MNIST实战

一,Fashion MNIST数据集

Fashion MNIST旨在替代经典的MNIST数据集,通常用作计算机视觉机器学习计划的“Hello,World”。该数据集包含10个类别中的70,000个灰度图像。 图像显示了低分辨率(28 x 28像素)的单件服装,如下所示:

我们将使用60,000张图像来训练网络和10,000张图像,以评估网络学习图像分类的准确程度。 

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

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

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

让我们在训练模型之前探索数据集的格式。 以下显示训练集中有60,000个图像,每个图像表示为28 x 28像素:

print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)

结果:

(60000, 28, 28)
(60000,)
(10000, 28, 28)
(10000,)

显示图片:

plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

 

train_images = train_images / 255.0

test_images = test_images / 255.0
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()

 

二, 代码解析

1)构造网络:

model = keras.Sequential(
[
    layers.Flatten(input_shape=[28, 28]),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
             loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])

2) 训练与验证:

 训练:

model.fit(train_images, train_labels, epochs=5)
Use tf.where in 2.0, which has the same broadcast rule as np.where
1875/1875 [==============================] - 38s 20ms/step - loss: 0.4696 - accuracy: 0.8299
Epoch 2/5
1875/1875 [==============================] - 27s 14ms/step - loss: 0.3543 - accuracy: 0.8688
Epoch 3/5
1875/1875 [==============================] - 27s 14ms/step - loss: 0.3189 - accuracy: 0.8813
Epoch 4/5
1875/1875 [==============================] - 27s 14ms/step - loss: 0.3016 - accuracy: 0.8886
Epoch 5/5
1875/1875 [==============================] - 27s 14ms/step - loss: 0.2812 - accuracy: 0.8953
313/313 [==============================] - 3s 10ms/step - loss: 0.3675 - accuracy: 0.8734
Accuracy on test dataset: 0.8734

 验证:

model.evaluate(test_images, test_labels)
[0.3623474566936493, 0.8737]

 上面第一个数是loss,第二个数是准确率。

3)预测 

predictions = model.predict(test_images)
print(predictions[0])
print(np.argmax(predictions[0]))
print(test_labels[0])
[2.1831402e-05 1.0357383e-06 1.0550731e-06 1.3231372e-06 8.0873624e-06
 2.6805745e-02 1.2466960e-05 1.6174167e-01 1.4259206e-04 8.1126428e-01]
9
9

 第一行是预测的值数组

 第二行是经过argmax处理后的预测值

 第三行是图像的标签

预测结果图形显示效果

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([])
  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 = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
plt.show()

# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
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.show()

 

img = test_images[0]

img = (np.expand_dims(img,0))

print(img.shape)
predictions_single = model.predict(img)

print(predictions_single)
plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)

 

(1, 28, 28)
[[2.1831380e-05 1.0357381e-06 1.0550700e-06 1.3231397e-06 8.0873460e-06
  2.6805779e-02 1.2466959e-05 1.6174166e-01 1.4259205e-04 8.1126422e-01]]

三,完整代码 

import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
import math
import tqdm
import tqdm.auto
tqdm.tqdm = tqdm.auto.tqdm
print(tf.__version__)
#导入库

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("训练样本个数: {}".format(num_train_examples))
print("测试样本个数:{}".format(num_test_examples))

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)
plt.show()

#显示前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.grid(False)
    plt.imshow(image, cmap=plt.cm.binary)
    plt.xlabel(class_names[label])
    i += 1
plt.show()

#建立模型
model = tf.keras.Sequential([
        tf.keras.layers.Flatten(input_shape=(28,28,1)), #输入层
        tf.keras.layers.Dense(256,activation=tf.nn.relu),#隐藏层1
        tf.keras.layers.Dense(128,activation=tf.nn.relu),#隐藏层2
        tf.keras.layers.Dense(10,activation=tf.nn.softmax)#输出层
    ])

#定义优化器和损失函数
model.compile(optimizer='adam',
              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=5, steps_per_epoch=math.ceil(num_train_examples/BATCH_SIZE))

test_loss, test_accuracy = model.evaluate(test_dataset, steps=math.ceil(num_test_examples/32))
print('Accuracy on test dataset:', test_accuracy)

for test_images, test_labels in test_dataset.take(1):
  test_images = test_images.numpy()
  test_labels = test_labels.numpy()
  predictions = model.predict(test_images)

predictions.shape

predictions[0]

np.argmax(predictions[0])

test_labels[0]

def plot_image(i, predictions_array, true_labels, images):
  predictions_array, true_label, img = predictions_array[i], true_labels[i], images[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  
  plt.imshow(img[...,0], 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([])
  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 = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)

i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)

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)

img = test_images[0]

print(img.shape)

img = np.array([img])

print(img.shape)

predictions_single = model.predict(img)

print(predictions_single)

plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)

np.argmax(predictions_single[0])

四,运行效果(上面分解是参照别人的,下面的是我自己跑的截图):

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值