当前位置: 首页 > news >正文

Python训练营打卡 Day43

复习日

作业:

kaggle找到一个图像数据集,用cnn网络进行训练并且用grad-cam做可视化

进阶:并拆分成多个文件

1. 数据集准备

在 Kaggle 上搜索并下载 “Cats VS. Dogs” 数据集

2. 项目结构

CNN_CatDog/
├── data/                  # 数据集目录
│   ├── train/             # 训练集
│   └── test/              # 测试集
├── models/                # 模型保存目录
├── utils/                 # 工具文件夹
│   ├── data_loader.py     # 数据加载工具
│   ├── model.py           # 模型定义
│   └── grad_cam.py        # Grad-CAM 实现
├── train.py               # 训练脚本
├── test.py                # 测试脚本
└── visualize.py           # 可视化脚本

3. 数据预处理(utils/data_loader.py)

import os
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGeneratordef load_data(train_dir, test_dir, img_height, img_width, batch_size):# 数据增强和加载train_datagen = ImageDataGenerator(rescale=1./255,rotation_range=20,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,validation_split=0.2  # 划分验证集)test_datagen = ImageDataGenerator(rescale=1./255)train_generator = train_datagen.flow_from_directory(train_dir,target_size=(img_height, img_width),batch_size=batch_size,class_mode='binary',subset='training')validation_generator = train_datagen.flow_from_directory(train_dir,target_size=(img_height, img_width),batch_size=batch_size,class_mode='binary',subset='validation')test_generator = test_datagen.flow_from_directory(test_dir,target_size=(img_height, img_width),batch_size=batch_size,class_mode='binary',shuffle=False)return train_generator, validation_generator, test_generator

4. 模型定义(utils/model.py)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalizationdef create_cnn_model(img_height, img_width):model = Sequential([Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)),BatchNormalization(),MaxPooling2D((2, 2)),Conv2D(64, (3, 3), activation='relu'),BatchNormalization(),MaxPooling2D((2, 2)),Conv2D(128, (3, 3), activation='relu'),BatchNormalization(),MaxPooling2D((2, 2)),Flatten(),Dense(512, activation='relu'),Dropout(0.5),Dense(1, activation='sigmoid')])model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])return model

5. Grad-CAM 实现(utils/grad_cam.py)

import numpy as np
import tensorflow as tf
import cv2def get_last_conv_layer(model):# 获取模型中最后一个卷积层for layer in reversed(model.layers):if isinstance(layer, tf.keras.layers.Conv2D):return layerreturn Nonedef compute_grad_cam(model, img_array, pred_class, last_conv_layer):# 构建梯度模型grad_model = tf.keras.models.Model([model.inputs],[last_conv_layer.output, model.output])# 计算梯度with tf.GradientTape() as tape:last_conv_layer_output, preds = grad_model(img_array)class_channel = preds[:, pred_class]grads = tape.gradient(class_channel, last_conv_layer_output)pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))# 应用权重到卷积层输出并加和last_conv_layer_output = last_conv_layer_output[0]heatmap = tf.reduce_mean(tf.multiply(last_conv_layer_output, pooled_grads), axis=-1)# ReLUheatmap = np.maximum(heatmap, 0)max_heat = np.max(heatmap)if max_heat == 0:max_heat = 1e-10heatmap /= max_heat# 热图大小调整与应用heatmap = cv2.resize(heatmap.numpy(), (img_array.shape[2], img_array.shape[1]))return heatmap

6. 训练脚本(train.py)

import os
import numpy as np
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from utils.data_loader import load_data
from utils.model import create_cnn_modelif __name__ == "__main__":# 参数设置img_height = 150img_width = 150batch_size = 32epochs = 50# 加载数据train_generator, validation_generator, _ = load_data('data/train','data/test',img_height,img_width,batch_size)# 创建模型model = create_cnn_model(img_height, img_width)# 回调函数checkpoint = ModelCheckpoint('models/best_model.h5', monitor='val_accuracy', save_best_only=True, mode='max')early_stop = EarlyStopping(monitor='val_loss', patience=5, mode='min')# 训练模型history = model.fit(train_generator,steps_per_epoch=train_generator.samples // batch_size,epochs=epochs,validation_data=validation_generator,validation_steps=validation_generator.samples // batch_size,callbacks=[checkpoint, early_stop])

7. 测试脚本(test.py)

import os
import numpy as np
from tensorflow.keras.models import load_model
from utils.data_loader import load_dataif __name__ == "__main__":# 参数设置img_height = 150img_width = 150batch_size = 32# 加载数据_, _, test_generator = load_data('data/train','data/test',img_height,img_width,batch_size)# 加载最佳模型model = load_model('models/best_model.h5')# 测试模型test_loss, test_acc = model.evaluate(test_generator,steps=test_generator.samples // batch_size,verbose=2)print(f'\nTest accuracy: {test_acc:.4f}')print(f'Test loss: {test_loss:.4f}')

8. 可视化脚本(visualize.py)

import os
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model
from utils.grad_cam import get_last_conv_layer, compute_grad_camif __name__ == "__main__":# 参数设置img_height = 150img_width = 150# 加载模型和最后一层卷积层model = load_model('models/best_model.h5')last_conv_layer = get_last_conv_layer(model)# 预测并可视化img_path = 'data/test/dog/10002.jpg'  # 替换为测试图像路径img = image.load_img(img_path, target_size=(img_height, img_width))img_array = image.img_to_array(img)img_array = np.expand_dims(img_array, axis=0) / 255.0# 预测类别pred = model.predict(img_array)pred_class = 0 if pred[0] < 0.5 else 1  # 假设二分类任务# 计算 Grad-CAM 热图heatmap = compute_grad_cam(model, img_array, pred_class, last_conv_layer)# 可视化热图plt.matshow(heatmap)plt.title('Grad-CAM Heatmap')plt.show()# 将热图叠加到原图上img = cv2.imread(img_path)img = cv2.resize(img, (img_height, img_width))heatmap = cv2.resize(heatmap, (img_height, img_width))heatmap = np.uint8(255 * heatmap)heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)superimposed_img = heatmap * 0.4 + imgcv2.imwrite('grad_cam_result.jpg', superimposed_img)plt.imshow(cv2.cvtColor(superimposed_img, cv2.COLOR_BGR2RGB))plt.title('Grad-CAM Result')plt.show()

总结

以上代码实现了使用 CNN 网络对猫狗图像数据集进行训练,并通过 Grad-CAM 对模型进行可视化。代码被拆分成多个文件,每个文件负责不同的功能模块,便于管理和维护。你可以根据实际需求调整网络结构、超参数和数据集路径等。

通过训练脚本 train.py 进行模型训练,并通过回调函数保存最佳模型。使用 test.py 进行模型测试,评估模型性能。最后通过 visualize.py 使用 Grad-CAM 对模型的预测结果进行可视化,生成热图并叠加到原图上,帮助理解模型的决策依据。

@浙大疏锦行

http://www.lqws.cn/news/83845.html

相关文章:

  • 2023年12月6级第一套第一篇
  • mybatisplus的总结
  • Linux配置DockerHub镜像源配置
  • 代码随想录算法训练营第六天| 242.有效的字母异位词 、 349. 两个数组的交集 、 202. 快乐数 、1. 两数之和
  • 【看到哪里写到哪里】C的指针-3(函数指针)
  • TC3xx学习笔记-启动过程详解(一)
  • Arch安装botw-save-state
  • deep forest安装及使用教程
  • 一步一步配置 Ubuntu Server 的 NodeJS 服务器详细实录——4. 配置服务器终端环境 zsh , oh my zsh, vim
  • 基于爬取的典籍数据重新设计前端界面
  • 前端八股之CSS
  • 推荐一款使用html开发桌面应用的工具——mixone
  • 力扣HOT100之多维动态规划:62. 不同路径
  • 力扣HOT100之多维动态规划:64. 最小路径和
  • 量子物理:深入学习量子物理的基本概念与应用
  • Python_day43
  • Linux运维笔记:服务器感染 netools 病毒案例
  • mysql专题上
  • Vue 项目创建教程 (开发前的准备工作保姆级辅助文档)
  • 专注成就技术传奇:一路向前的力量
  • 数学建模期末速成 最短路径
  • Ubuntu22.04 安装 ROS2 Humble
  • Spark-TTS: AI语音合成的“变声大师“
  • ubuntu 添加应用到启动菜单
  • P5684 [CSP-J2019 江西] 非回文串 题解
  • Webpack依赖
  • Android高级开发第四篇 - JNI性能优化技巧和高级调试方法
  • 网络攻防技术三:网络脆弱性分析
  • 高阶数据结构——并查集
  • C语言基础(10)【二维数组 字符数组 字符串相关操作】