温馨提示:文末有 CSDN 平台官方提供的学长 QQ 名片 :)
1. 项目简介
本文详细探讨了一基于深度学习的交通标志图像识别系统。采用TensorFlow和Keras框架,利用卷积神经网络(CNN)进行模型训练和预测,并引入VGG16迁移学习模型,取得96%的高准确率。通过搭建Web系统,用户能上传交通标志图片,系统实现了自动实时的交通标志分类识别。该系统不仅展示了深度学习在交通领域的实际应用,同时为用户提供了一种高效、准确的交通标志识别服务。
2. 交通标志数据集读取
数据集里面的图像具有不同大小,光照条件,遮挡情况下的43种不同交通标志符号,图像的成像情况与你实际在真实环境中不同时间路边开车走路时看到的交通标志的情形非常相似。训练集包括大约39,000个图像,而测试集大约有12,000个图像。图像不能保证是固定 的尺寸,标志不一定在每个图像中都是居中。每个图像包含实际交通标志周围10%左右的边界。
folders = os.listdir(train_path)
train_number = []
class_num = []
for folder in folders:
train_files = os.listdir(train_path + '/' + folder)
train_number.append(len(train_files))
class_num.append(classes[int(folder)])
# 不同类别交通标志数量,并进行排序
zipped_lists = zip(train_number, class_num)
sorted_pairs = sorted(zipped_lists)
tuples = zip(*sorted_pairs)
train_number, class_num = [ list(t) for t in tuples]
# 绘制不同类别交通标志数量分布柱状图
plt.figure(figsize=(21,10))
plt.bar(class_num, train_number)
plt.xticks(class_num, rotation='vertical', fontsize=16)
plt.title('不同类别交通标志数量分布柱状图', fontsize=20)
plt.show()
划分训练集、验证集:
X_train, X_val, y_train, y_val = train_test_split(image_data, image_labels, test_size=0.3, random_state=42, shuffle=True)
X_train = X_train/255
X_val = X_val/255
print("X_train.shape", X_train.shape)
print("X_valid.shape", X_val.shape)
print("y_train.shape", y_train.shape)
print("y_valid.shape", y_val.shape)
类别标签进行 One-hot 编码:
y_train = keras.utils.to_categorical(y_train, NUM_CATEGORIES)
y_val = keras.utils.to_categorical(y_val, NUM_CATEGORIES)
print(y_train.shape)
print(y_val.shape)
3. 卷积神经网络模型构建
model = keras.models.Sequential([
keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu', input_shape=(IMG_HEIGHT,IMG_WIDTH,channels)),
keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),
# ......
keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),
# ......
keras.layers.Flatten(),
keras.layers.Dense(512, activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.Dropout(rate=0.5),
keras.layers.Dense(43, activation='softmax')
])
4. 模型训练与性能评估
设置模型训练参数:
epochs = 20
initial_learning_rate = 5e-5
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate, #设置初始学习率
decay_steps=64, #每隔多少个step衰减一次
decay_rate=0.98, #衰减系数
staircase=True)
# 将指数衰减学习率送入优化器
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=32, epochs=epochs, validation_data=(X_val, y_val))
加载测试集进行模型评估:
# 计算测试集准确率
pred = model.predict(X_test)
pred_labels = np.argmax(pred, 1)
print('测试集准确率: ',accuracy_score(labels, pred_labels)*100)
测试集准确率: 93.24623911322249
5. 基于迁移学习的交通标志识别
from tensorflow.keras.applications import VGG16
height = 32
width = 32
vgg_base_model = VGG16(weights='imagenet', include_top=False, input_shape=(height,width,3))
vgg_base_model.trainable=True
vgg_model = tf.keras.Sequential([
vgg_base_model,
keras.layers.BatchNormalization(),
keras.layers.Flatten(),
keras.layers.Dense(512, activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.Dropout(rate=0.5),
keras.layers.Dense(43, activation='softmax')])
vgg_model.summary()
Epoch 1/20 858/858 [==============================] - ETA: 0s - loss: 0.9774 - accuracy: 0.7366 Epoch 1: val_accuracy improved from -inf to 0.94806, saving model to best_model.h5 858/858 [==============================] - 334s 387ms/step - loss: 0.9774 - accuracy: 0.7366 - val_loss: 0.1651 - val_accuracy: 0.9481 Epoch 2/20 858/858 [==============================] - ETA: 0s - loss: 0.0737 - accuracy: 0.9804 Epoch 2: val_accuracy improved from 0.94806 to 0.97866, saving model to best_model.h5 858/858 [==============================] - 350s 408ms/step - loss: 0.0737 - accuracy: 0.9804 - val_loss: 0.0750 - val_accuracy: 0.9787 Epoch 3/20 858/858 [==============================] - ETA: 0s - loss: 0.0274 - accuracy: 0.9926 Epoch 3: val_accuracy improved from 0.97866 to 0.98266, saving model to best_model.h5 858/858 [==============================] - 351s 409ms/step - loss: 0.0274 - accuracy: 0.9926 - val_loss: 0.0681 - val_accuracy: 0.9827 Epoch 4/20 858/858 [==============================] - ETA: 0s - loss: 0.0197 - accuracy: 0.9946 Epoch 4: val_accuracy improved from 0.98266 to 0.99779, saving model to best_model.h5 858/858 [==============================] - 339s 395ms/step - loss: 0.0197 - accuracy: 0.9946 - val_loss: 0.0085 - val_accuracy: 0.9978 Epoch 5/20 858/858 [==============================] - ETA: 0s - loss: 0.0081 - accuracy: 0.9982 Epoch 5: val_accuracy improved from 0.99779 to 0.99830, saving model to best_model.h5 858/858 [==============================] - 364s 424ms/step - loss: 0.0081 - accuracy: 0.9982 - val_loss: 0.0067 - val_accuracy: 0.9983 Epoch 6/20 858/858 [==============================] - ETA: 0s - loss: 0.0025 - accuracy: 0.9995 Epoch 6: val_accuracy improved from 0.99830 to 0.99855, saving model to best_model.h5 858/858 [==============================] - 354s 413ms/step - loss: 0.0025 - accuracy: 0.9995 - val_loss: 0.0053 - val_accuracy: 0.9986 Epoch 7/20 858/858 [==============================] - ETA: 0s - loss: 0.0030 - accuracy: 0.9992 Epoch 7: val_accuracy did not improve from 0.99855 858/858 [==============================] - 333s 389ms/step - loss: 0.0030 - accuracy: 0.9992 - val_loss: 0.0126 - val_accuracy: 0.9969 Epoch 7: early stopping
模型评估:
# 计算测试集准确率
pred = vgg_model.predict(X_test)
pred_labels = np.argmax(pred, 1)
print('测试集准确率: ',accuracy_score(labels, pred_labels)*100)
测试集准确率: 96.02533650039588
6. 测试集预测结果可视化
plt.figure(figsize = (25, 25))
start_index = 0
for i in range(25):
plt.subplot(5, 5, i + 1)
plt.grid(False)
plt.xticks([])
plt.yticks([])
prediction = pred_labels[start_index + i]
actual = labels[start_index + i]
col = 'g'
if prediction != actual:
col = 'r'
plt.xlabel('实际类别:{}\n预测类别:{}'.format(classes[actual], classes[prediction]), color = col, fontsize=18)
plt.imshow(X_test[start_index + i])
plt.show()
7. 交通标志分类识别系统
7.1 首页
7.2 交通标志在线识别
8. 结论
本文详细探讨了一基于深度学习的交通标志图像识别系统。采用TensorFlow和Keras框架,利用卷积神经网络(CNN)进行模型训练和预测,并引入VGG16迁移学习模型,取得96%的高准确率。通过搭建Web系统,用户能上传交通标志图片,系统实现了自动实时的交通标志分类识别。该系统不仅展示了深度学习在交通领域的实际应用,同时为用户提供了一种高效、准确的交通标志识别服务。
欢迎大家点赞、收藏、关注、评论啦 ,由于篇幅有限,只展示了部分核心代码。技术交流、源码获取认准下方 CSDN 官方提供的学长 QQ 名片 :)
精彩专栏推荐订阅:
1. Python数据挖掘精品实战案例
2. 计算机视觉 CV 精品实战案例
3. 自然语言处理 NLP 精品实战案例