承接上文的MNIST的練手,本文再來練習卷積神經網絡實現cifar10的分類,環境還是在colab是安裝的tensorflow。不會用colab的可以看我之前博客https://blog.csdn.net/hesongzefairy/article/details/105411219
Step1:國際慣例加載tensorflow
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
Step2:加載數據集(已經集成到tf中,直接調用方法加載)
CIFAR10數據集包含60000張彩色圖像,共10類,平均每類6000張圖,圖像大小是(32, 32)另外加載數據集已經做好了切分,訓練集50000張,測試集10000張。
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# 歸一化處理
train_images, test_images = train_images / 255.0, test_images / 255.0
Step3:查看數據集內容
把前10張圖打印出來瞧瞧
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure(figsize=(10,10))
for i in range(10):
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][0]])
plt.show()
Step4:搭建模型並設置訓練流程
卷積神經網絡CNN接收數據的shape爲(height,width,channel),不用考慮batch_size ,對於channel來說,RGB彩圖就channel=3,mnist的灰度圖像channel=1。
這裏使用了三層2D卷積層,隨着網絡的深入,特徵圖尺寸變小可以添加更多的卷積層通道數,32增加到64。
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'))
# 添加分類器
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
顯示一下網絡結構
model.summary()
Step5:啓動訓練並評估模型
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
Step6:測試模型並繪製loss圖(history的使用)
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')
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(test_acc)