- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊
- 我的环境
- 操作系统:CentOS7
- 显卡:RTX3090
- 显卡驱动:550.78
- CUDA版本: 12.4
- 语言环境:Python3.8.19
- 编译器:Jupyter Lab
- 深度学习环境:
- TensorFlow2
一、前期工作
1. 设置GPU
import tensorflow as tf
gpus = tf.config.list_physical_devices("GPU")
if gpus:
gpu0 = gpus[0] #如果有多个GPU,仅使用第0个GPU
tf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用
tf.config.set_visible_devices([gpu0],"GPU")
2024-09-13 20:59:05.444849: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-09-13 20:59:05.708639: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2024-09-13 20:59:06.395345: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2024-09-13 20:59:06.441801: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-09-13 20:59:12.144829: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
2024-09-13 20:59:18.925951: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1960] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2. 导入数据
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()
3. 归一化
# 将像素的值标准化至0到1的区间内。
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))
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),均值池化对背景保留更好,最大池化对纹理提取更好)。同卷积计算,池化层计算窗口内的平均值或者最大值。
- 模型立体结构图
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), #卷积层1,卷积核3*3
layers.MaxPooling2D((2, 2)), #池化层1,2*2采样
layers.Conv2D(64, (3, 3), activation='relu'), #卷积层2,卷积核3*3
layers.MaxPooling2D((2, 2)), #池化层2,2*2采样
layers.Conv2D(64, (3, 3), activation='relu'), #卷积层3,卷积核3*3
layers.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)
_________________________________________________________________
三、编译
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/10
1563/1563 [==============================] - 17s 10ms/step - loss: 1.5061 - accuracy: 0.4523 - val_loss: 1.1889 - val_accuracy: 0.5736
Epoch 2/10
1563/1563 [==============================] - 15s 9ms/step - loss: 1.1359 - accuracy: 0.5994 - val_loss: 1.1567 - val_accuracy: 0.6031
Epoch 3/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.9863 - accuracy: 0.6544 - val_loss: 0.9507 - val_accuracy: 0.6697
Epoch 4/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.8832 - accuracy: 0.6891 - val_loss: 0.9570 - val_accuracy: 0.6649
Epoch 5/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.8080 - accuracy: 0.7170 - val_loss: 0.8681 - val_accuracy: 0.6987
Epoch 6/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.7426 - accuracy: 0.7374 - val_loss: 0.8579 - val_accuracy: 0.7076
Epoch 7/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.6921 - accuracy: 0.7558 - val_loss: 0.8654 - val_accuracy: 0.7111
Epoch 8/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.6496 - accuracy: 0.7722 - val_loss: 0.8323 - val_accuracy: 0.7174
Epoch 9/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.6052 - accuracy: 0.7870 - val_loss: 0.8762 - val_accuracy: 0.7103
Epoch 10/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.5607 - accuracy: 0.8023 - val_loss: 0.8747 - val_accuracy: 0.7041
五、预测
plt.imshow(test_images[0])
<matplotlib.image.AxesImage at 0x7f8e2c670fd0>
import numpy as np
pre = model.predict(test_images)
print(class_names[np.argmax(pre[0])])
313/313 [==============================] - 1s 3ms/step
cat
六、模型评估
import matplotlib.pyplot as plt
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)
313/313 - 1s - loss: 0.8747 - accuracy: 0.7041 - 1s/epoch - 3ms/step
print(test_acc)
0.7041000127792358
七、总结
- 学习了如何使用Tesorflow写CNN模型,个人觉得Terosflow模型比PyTorch更简洁一些。