CIFAR10 数据集包含 10 类,共 60000 张彩色图片,每类图片有 6000 张。此数据集中 50000 个样例被作为训练集,剩余 10000 个样例作为测试集。类之间相互度立,不存在重叠的部分。因此,cifar10分类就是一个图像多分类任务。Keras另一个好处在于已经集成了很多常见的数据集和模型,在接口里可以直接调用。
%matplotlib inline #如果不是juptorbook,请删除此行
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
#CIFAR10 数据集包含 10 类,共 60000 张彩色图片,每类图片有 6000 张。此数据集中 50000 个样例被作为训练集,剩余 10000 个样例作为测试集。类之间相互度立,不存在重叠的部分。
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# 将像素的值标准化至0到1的区间内。
train_images, test_images = train_images / 255.0, test_images / 255.0
#我们将测试集的前 25 张图片和类名打印出来,来确保数据集被正确加载。
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
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)
# 由于 CIFAR 的标签是 array,
# 因此您需要额外的索引(index)。
plt.xlabel(class_names[train_labels[i][0]])
plt.show()
#下方展示的 6 行代码声明了了一个常见卷积神经网络,由几个 Conv2D 和 MaxPooling2D 层组成。
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
#在模型的最后,您将把卷积后的输出张量(本例中形状为 (4, 4, 64))传给一个或多个 Dense 层来完成分类。
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.summary()
#编译并训练模型
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
#评估模型
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
plt.show()
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(test_acc)
output1:
<pre style="box-sizing: border-box; overflow: auto; font-family: monospace; font-size: inherit; display: block; padding: 1px 0px; margin: 0px; line-height: inherit; color: black; word-break: break-all; overflow-wrap: break-word; background-color: transparent; border: 0px; border-radius: 0px; white-space: pre-wrap; vertical-align: baseline;">A local file was found, but it seems to be incomplete or outdated because the auto file hash does not match the original value of 6d958be074577803d12ecdefd02955f39262c83c16fe9348329d7fe0b5c001ce so we will re-download the data.
Downloading data from [https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz](https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz)
170500096/170498071 [==============================] - 17522s 103us/step
<pre style="box-sizing: border-box; overflow: auto; font-family: monospace; font-size: inherit; display: block; padding: 1px 0px; margin: 0px; line-height: inherit; color: black; word-break: break-all; overflow-wrap: break-word; background-color: transparent; border: 0px; border-radius: 0px; white-space: pre-wrap; vertical-align: baseline;">Model: "sequential_12"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 30, 30, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 13, 13, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 4, 4, 64) 36928
_________________________________________________________________
flatten_1 (Flatten) (None, 1024) 0
_________________________________________________________________
dense_14 (Dense) (None, 64) 65600
_________________________________________________________________
dense_15 (Dense) (None, 10) 650
=================================================================
Total params: 122,570
Trainable params: 122,570
Non-trainable params: 0
_________________________________________________________________
Epoch 1/10
1563/1563 [==============================] - 89s 57ms/step - loss: 1.5250 - accuracy: 0.4467 - val_loss: 1.2551 - val_accuracy: 0.5520
Epoch 2/10
1563/1563 [==============================] - 78s 50ms/step - loss: 1.1560 - accuracy: 0.5923 - val_loss: 1.1427 - val_accuracy: 0.5997
Epoch 3/10
1563/1563 [==============================] - 71s 45ms/step - loss: 1.0224 - accuracy: 0.6403 - val_loss: 1.0141 - val_accuracy: 0.6483
Epoch 4/10
1563/1563 [==============================] - 61s 39ms/step - loss: 0.9337 - accuracy: 0.6744 - val_loss: 0.9356 - val_accuracy: 0.6767
Epoch 5/10
1563/1563 [==============================] - 59s 38ms/step - loss: 0.8591 - accuracy: 0.6997 - val_loss: 1.0341 - val_accuracy: 0.6487
Epoch 6/10
1563/1563 [==============================] - 58s 37ms/step - loss: 0.7981 - accuracy: 0.7228 - val_loss: 0.8725 - val_accuracy: 0.6994
Epoch 7/10
1563/1563 [==============================] - 57s 37ms/step - loss: 0.7518 - accuracy: 0.7376 - val_loss: 0.8625 - val_accuracy: 0.7005
Epoch 8/10
1563/1563 [==============================] - 62s 40ms/step - loss: 0.7073 - accuracy: 0.7527 - val_loss: 0.9021 - val_accuracy: 0.6954
Epoch 9/10
1563/1563 [==============================] - 66s 43ms/step - loss: 0.6669 - accuracy: 0.7657 - val_loss: 0.8610 - val_accuracy: 0.7083
Epoch 10/10
1563/1563 [==============================] - 58s 37ms/step - loss: 0.6353 - accuracy: 0.7771 - val_loss: 0.9132 - val_accuracy: 0.6999