【雷克萨】数据科学家书房就是我的全世界

TensorFlow 2.0 Tutorial: 2 - 识别

2019-03-29  本文已影响11人  不会停的蜗牛

本文要学习如何用 TensorFlow 2.0 识别 Fashion MNIST。

MNIST 可以说是深度学习里面的 Hello World 了,几乎每个 AI/ML/Data Science 教程里面都用 MNIST 手写识别数字来开启,
不过呢,现在 MNIST 已经显得很简单了,CNN 可以达到 99.7% ,
用的也太多了,而且已经不能代表当前的计算机视觉的主流任务了。

Fashion-MNIST:
https://github.com/zalandoresearch/fashion-mnist

是来自 Zalando 文章的数据集,是时尚版的 MNIST,
包括 60,000 个训练集数据,10,000 个测试集数据, 每个数据为 28x28 灰度图像,一共有 10 类:

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


1. 安装所需包

!pip install tensorflow==2.0.0-alpha0 
!pip install -U tensorflow_datasets


from __future__ import absolute_import, division, print_function

# Import TensorFlow and TensorFlow Datasets
import tensorflow as tf
import tensorflow_datasets as tfds
tf.logging.set_verbosity(tf.logging.ERROR)

# Helper libraries
import math
import numpy as np
import matplotlib.pyplot as plt

# Improve progress bar display
import tqdm
import tqdm.auto
tqdm.tqdm = tqdm.auto.tqdm


print(tf.__version__)

2. 下载数据,分为训练集/测试集

这部分的代码都变得很简单,class_names 为 10 个类别:

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

3. 数据预处理

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


4. 标准化

数据中每个像素的值在 [0,255],
首先,构造一个函数,将这些值标准化为[0,1]之间,再应用于测试集和训练集中的每个图像上。

def normalize(images, labels):
  images = tf.cast(images, tf.float32)
  images /= 255
  return images, labels

# The map function applies the normalize function to each element in the train
# and test datasets
train_dataset =  train_dataset.map(normalize)
test_dataset  =  test_dataset.map(normalize)

5. 数据可视化

看一下标准化后的数据的前 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()

6. 定义模型

这个模型有三层,

input: tf.keras.layers.Flatten,没有参数,只是转换数据,将 28 × 28 转换为 1 × 784 .
hidden: tf.keras.layers.Dense,
output: tf.keras.layers.Dense,一个 10 节点的 softmax 层,代表属于每个类的概率

model = tf.keras.Sequential([
    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)
])

7. 模型编译

model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

8. 训练模型

dataset.repeat()是要一直重复,用 epochs 参数来控制训练的时间,
dataset.shuffle(60000)将训练集顺序打乱,
dataset.batch(32)告诉 model.fit 每批用 32 个图像和标签数据。

BATCH_SIZE = 32
train_dataset = train_dataset.repeat().shuffle(num_train_examples).batch(BATCH_SIZE)
test_dataset = test_dataset.batch(BATCH_SIZE)

这里只迭代 5 次,看看效果:

model.fit(train_dataset, epochs=5, steps_per_epoch=math.ceil(num_train_examples/BATCH_SIZE))

9. 模型评估

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

313/313 [==============================] - 3s 8ms/step - loss: 0.3669 - acc: 0.8670
Accuracy on test dataset: 0.867


10. 模型预测

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)

11. 结果可视化

plot_image
如果预测标签和实际标签是一样的,则字是蓝色的,否则是红色的
预测标签就是预测的向量所代表的 10 个概率值中最大的那个

plot_value_array:画出每个图片的 10 个预测概率的柱状图

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')

看一下结果:

# 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)

学习资料:
Udacity: TensorFlow course
https://classroom.udacity.com/courses/ud187


大家好!
我是 不会停的蜗牛 Alice,
喜欢人工智能,没事儿写写机器学习干货,
欢迎关注我!

上一篇下一篇

猜你喜欢

热点阅读