- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊
👉 要求:
- 学习如何编写一个完整的深度学习程序
- 了解分类彩色图片会灰度图片有什么区别
- 测试集accuracy到达72%
🦾我的环境:
- 语言环境:Python3.8
- 编译器:Jupyter Lab
- 深度学习环境:
- TensorFlow2
一、 前期准备
1.1. 设置GPU
- 如果设备上支持GPU就使用GPU,否则使用CPU
- Mac上的GPU使用mps
import tensorflow as tfgpus = tf.config.list_physical_devices("GPU")if gpus:gpu0 = gpus[0] #如果有多个GPU,仅使用第0个GPUtf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用tf.config.set_visible_devices([gpu0],"GPU")gpu0
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')
1.2. 导入数据
使用dataset下载MNIST数据集,并划分好训练集与测试集
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
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
170498071/170498071 [==============================] - 8500s 50us/step
1.3. 归一化
数据归一化作用
● 使不同量纲的特征处于同一数值量级,减少方差大的特征的影响,使模型更准确。
● 加快学习算法的收敛速度。
更详解的介绍请参考文章:🔗归一化与标准化
# 将像素的值标准化至0到1的区间内。(对于灰度图片来说,每个像素最大值是255,每个像素最小值是0,也就是直接除以255就可以完成归一化。)
train_images, test_images = train_images / 255.0, test_images / 255.0# 查看数据维数信息
train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
((50000, 32, 32, 3), (10000, 32, 32, 3), (50000, 1), (10000, 1))
1.4. 可视化图片
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer','dog', 'frog', 'horse', 'ship', 'truck']plt.figure(figsize=(20,10))
for i in range(20):plt.subplot(5,10,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()
二、构建简单的CNN网络
⭐池化层
池化层对提取到的特征信息进行降维,一方面使特征图变小,简化网络计算复杂度;另一方面进行特征压缩,提取主要特征,增加平移不变性,减少过拟合风险。但其实池化更多程度上是一种计算性能的一个妥协,强硬地压缩特征的同时也损失了一部分信息,所以现在的网络比较少用池化层或者使用优化后的如SoftPool。
池化层包括最大池化层(MaxPooling)和平均池化层(AveragePooling),均值池化对背景保留更好,最大池化对纹理提取更好)。同卷积计算,池化层计算窗口内的平均值或者最大值。例如通过一个 2*2 的最大池化层,其计算方式如下:
我们即将构建模型的结构图,我以分别二维和三维的形式展示出来方便大家理解。
-
平面结构图
-
立体结构图
model = models.Sequential([layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), #卷积层1,卷积核3*3layers.MaxPooling2D((2, 2)), #池化层1,2*2采样layers.Conv2D(64, (3, 3), activation='relu'), #卷积层2,卷积核3*3layers.MaxPooling2D((2, 2)), #池化层2,2*2采样layers.Conv2D(64, (3, 3), activation='relu'), #卷积层3,卷积核3*3layers.Flatten(), #Flatten层,连接卷积层与全连接层layers.Dense(64, activation='relu'), #全连接层,特征进一步提取layers.Dense(10) #输出层,输出预期结果
])model.summary() # 打印网络结构
Model: "sequential"
_________________________________________________________________Layer (type) Output Shape Param #
=================================================================conv2d (Conv2D) (None, 30, 30, 32) 896 max_pooling2d (MaxPooling2 (None, 15, 15, 32) 0 D) conv2d_1 (Conv2D) (None, 13, 13, 64) 18496 max_pooling2d_1 (MaxPoolin (None, 6, 6, 64) 0 g2D) conv2d_2 (Conv2D) (None, 4, 4, 64) 36928 flatten (Flatten) (None, 1024) 0 dense (Dense) (None, 64) 65600 dense_1 (Dense) (None, 10) 650 =================================================================
Total params: 122570 (478.79 KB)
Trainable params: 122570 (478.79 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________2024-06-23 22:16:01.054779: I metal_plugin/src/device/metal_device.cc:1154] Metal device set to: Apple M2
2024-06-23 22:16:01.054802: I metal_plugin/src/device/metal_device.cc:296] systemMemory: 16.00 GB
2024-06-23 22:16:01.054811: I metal_plugin/src/device/metal_device.cc:313] maxCacheSize: 5.33 GB
2024-06-23 22:16:01.054984: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:303] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2024-06-23 22:16:01.055316: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:269] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
三、编译模型
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))
Epoch 1/102024-06-23 22:16:41.825293: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.1563/1563 [==============================] - ETA: 0s - loss: 1.5781 - accuracy: 0.42422024-06-23 22:16:54.304550: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.1563/1563 [==============================] - 13s 8ms/step - loss: 1.5781 - accuracy: 0.4242 - val_loss: 1.3528 - val_accuracy: 0.5133
Epoch 2/10
1563/1563 [==============================] - 12s 8ms/step - loss: 1.2892 - accuracy: 0.5464 - val_loss: 1.2880 - val_accuracy: 0.5617
Epoch 3/10
1563/1563 [==============================] - 12s 8ms/step - loss: 1.3585 - accuracy: 0.5521 - val_loss: 1.6484 - val_accuracy: 0.5155
Epoch 4/10
1563/1563 [==============================] - 12s 8ms/step - loss: 2.0448 - accuracy: 0.5044 - val_loss: 3.0545 - val_accuracy: 0.4380
Epoch 5/10
1563/1563 [==============================] - 12s 8ms/step - loss: 5.7139 - accuracy: 0.4563 - val_loss: 20.7035 - val_accuracy: 0.2908
Epoch 6/10
1563/1563 [==============================] - 12s 8ms/step - loss: 45.9029 - accuracy: 0.3672 - val_loss: 109.2576 - val_accuracy: 0.3624
Epoch 7/10
1563/1563 [==============================] - 12s 8ms/step - loss: 504.0281 - accuracy: 0.2838 - val_loss: 1375.9681 - val_accuracy: 0.2399
Epoch 8/10
1563/1563 [==============================] - 12s 8ms/step - loss: 3719.2263 - accuracy: 0.2359 - val_loss: 6212.4688 - val_accuracy: 0.2268
Epoch 9/10
1563/1563 [==============================] - 12s 8ms/step - loss: 11472.0957 - accuracy: 0.2238 - val_loss: 20005.8828 - val_accuracy: 0.1773
Epoch 10/10
1563/1563 [==============================] - 12s 8ms/step - loss: 25618.4004 - accuracy: 0.2182 - val_loss: 31095.4336 - val_accuracy: 0.2160
五、预测
通过模型进行预测得到的是每一个类别的概率,数字越大该图片为该类别的可能性越大
plt.imshow(test_images[1])
输出测试集中第一张图片的预测结果
import numpy as nppre = model.predict(test_images)
print(class_names[np.argmax(pre[1])])
75/313 [======>.......................] - ETA: 0s2024-06-23 22:20:12.257425: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.313/313 [==============================] - 1s 3ms/step
ship
六、模型评估
import matplotlib.pyplot as pltplt.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)
0.6845156432345124