使用TensorFlow构建卷积神经网络(CNN)进行图像分类
·
下面是一个完整的示例,展示如何使用TensorFlow/Keras构建CNN模型进行图像分类任务。
1. 安装必要库
bash
pip install tensorflow matplotlib numpy
2. 数据准备与预处理
python
import tensorflow as tf from tensorflow.keras import datasets, layers, models import matplotlib.pyplot as plt import numpy as np # 加载CIFAR-10数据集(内置数据集,包含10类图像) (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data() # 类别名称 class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] # 数据归一化(将像素值缩放到0-1之间) train_images, test_images = train_images / 255.0, test_images / 255.0 # 查看数据形状 print("训练集图像形状:", train_images.shape) print("训练集标签形状:", train_labels.shape) print("测试集图像形状:", test_images.shape) print("测试集标签形状:", test_labels.shape) # 可视化一些图像 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) plt.xlabel(class_names[train_labels[i][0]]) plt.show()
3. 构建CNN模型
python
model = models.Sequential([
# 卷积层和池化层
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
# 全连接层
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10) # 输出层,10个类别
])
# 编译模型
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# 查看模型结构
model.summary()
4. 训练模型
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
5. 评估模型
python
# 绘制训练过程中的准确率和损失曲线
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(loc='upper right')
plt.show()
# 在测试集上评估模型
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f"测试准确率: {test_acc}")
6. 使用模型进行预测
python
# 添加softmax层使输出为概率
probability_model = tf.keras.Sequential([model, layers.Softmax()])
# 对测试集图像进行预测
predictions = probability_model.predict(test_images)
# 查看第一个测试图像的预测结果
def plot_image(i, predictions_array, true_label, img):
true_label, img = true_label[i][0], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(
class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
def plot_value_array(i, predictions_array, true_label):
true_label = true_label[i][0]
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
# 可视化单个预测
i = 0
plt.figure(figsize=(6, 3))
plt.subplot(1, 2, 1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1, 2, 2)
plot_value_array(i, predictions[i], test_labels)
plt.show()
# 可视化多个预测
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()
7. 模型改进建议
-
数据增强:添加数据增强层以减少过拟合
python
data_augmentation = tf.keras.Sequential([ layers.RandomFlip("horizontal"), layers.RandomRotation(0.1), layers.RandomZoom(0.1), ]) -
添加Dropout层:防止过拟合
python
model.add(layers.Dropout(0.2))
-
使用预训练模型:如ResNet, VGG等
python
base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False)
-
调整超参数:如学习率、批次大小、epoch数等
-
更复杂的架构:尝试更深的网络或不同的卷积核大小
8. 保存和加载模型
python
# 保存模型
model.save('my_cnn_model.h5')
# 加载模型
loaded_model = tf.keras.models.load_model('my_cnn_model.h5')
# 使用加载的模型进行预测
loaded_predictions = loaded_model.predict(test_images)
这个示例展示了如何使用TensorFlow构建一个基本的CNN模型进行图像分类。你可以根据具体任务调整模型架构和参数。
更多推荐
所有评论(0)