- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊
一、前期准备
1.设置GPU
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets
import warnings
warnings.filterwarnings('ignore') # 忽略警告信息
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device
2.导入数据
import os,PIL,pathlib
data_dir = "F:/host/Data/乳腺癌数据/"
data_dir = pathlib.Path(data_dir)
data_paths=list(data_dir.glob('*'))
classNames = []
for path in data_paths:
classNames.append(path.name)
classNames
import random
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(4, 8, figsize=(20, 10))
plt.subplots_adjust(hspace=0.4, wspace=0.1)
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
for i, class_name in enumerate(classNames):
class_dir = data_dir / class_name
class_paths = list(class_dir.glob('*.png'))
# 随机选择16张图片
random_images = random.sample(class_paths, 16)
for j, image_path in enumerate(random_images):
row = (i * 2) + (j // 8) # 计算图像所在行
col = j % 8 # 计算图像所在列
image = PIL.Image.open(image_path)
axs[row, col].imshow(np.array(image))
# axs[row, col].axis('off')
axs[row, col].set_title(class_name, fontsize=12)
plt.show()
train_transform = transforms.Compose([
transforms.Resize([224,224]), # 将图像大小调整为224*224
# transforms.RandomHorizontalFlip(), # 随机水平翻转
transforms.ToTensor(), # 将图像数据转换为Tensor,并将数值归一化到[0,1]
transforms.Normalize( # 对图像数据进行标准化处理
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
test_transform = transforms.Compose([
transforms.Resize([224,224]),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
total_data = datasets.ImageFolder(root=data_dir,transform=train_transform)
total_data
total_data.class_to_idx
3.划分数据集
train_size = int(0.8 * len(total_data)) # 80%的数据作为训练集
test_size = len(total_data) - train_size # 20%的数据作为测试集
# 随机划分数据集
train_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])
train_dataset, test_dataset
batch_size = 32 # 设置批量大小
# 创建数据加载器
train_dl = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size,
shuffle=True)
test_dl = torch.utils.data.DataLoader(test_dataset,
batch_size=batch_size,
shuffle=False)
for X, y in test_dl:
print('Shape of X [N, C, H, W]:', X.shape)
print('Shape of y:', y.shape, y.dtype)
break
二、搭建网络模型
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
1.DenseLayer模块
self.add_module()
函数用于向类中添加一个子模块。在这段代码中,self.add_module('name', module)
被用于将不同的模块添加到DenseLayer
类中。这些模块可以是任何继承自nn.Module
的子类,例如批标准化层(nn.BatchNorm2d
)、ReLu激活函数(nn.ReLU
)、卷积层(nn.Conv2d
)等。
该函数的参数包括一个字符串'name'
,用于给该模块命名,以及一个模块实例module
,用于表示要添加的模块对象。在代码中,每个模块都按顺序添加到DenseLayer
类中。
通过使用self.add module()
函数,这些子模块被存储在 DenseLayer
类的内部,成为该类的属性。这样,在类的其他方法中,可以通过引用这些属性来访问和操作这些模块,例如在 forward()
方法中使用 super().forward(x)
调用父类模块的前向传播方法。
总之self.add module()
函数的作用是将子模块添加到类中,并为这些模块提供属性名以便后续引用和操作。
class DenseLayer(nn.Sequential):
def __init__(self, in_channels, growth_rate, bn_size, drop_rate):
super(DenseLayer, self).__init__()
self.add_module('norm1', nn.BatchNorm2d(in_channels)),
self.add_module('relu1', nn.ReLU(inplace=True)),
self.add_module('conv1', nn.Conv2d(in_channels, bn_size * growth_rate,
kernel_size=1, stride=1, bias=False)),
self.add_module('norm2', nn.BatchNorm2d(bn_size * growth_rate)),
self.add_module('relu2', nn.ReLU(inplace=True)),
self.add_module('conv2', nn.Conv2d(bn_size * growth_rate, growth_rate,
kernel_size=3, stride=1, padding=1, bias=False)),
self.drop_rate = drop_rate
def forward(self, x):
new_features = super(DenseLayer, self).forward(x)
if self.drop_rate > 0:
new_features = F.dropout(new_features, p=self.drop_rate, training=self.training)
return torch.cat([x, new_features], 1)
2.DenseBlock模块
class DenseBlock(nn.Sequential):
def __init__(self, num_layers, in_channels, bn_size, growth_rate, drop_rate):
super(DenseBlock, self).__init__()
for i in range(num_layers):
layer = DenseLayer(in_channels + i * growth_rate, growth_rate, bn_size, drop_rate)
self.add_module('denselayer%d'%(i+1,), layer)
3.Transition模块
class Transition(nn.Sequential):
def __init__(self, in_channels, out_channels):
super(Transition, self).__init__()
self.add_module('norm', nn.BatchNorm2d(in_channels))
self.add_module('relu', nn.ReLU(inplace=True))
self.add_module('conv', nn.Conv2d(in_channels, out_channels,
kernel_size=1, stride=1, bias=False))
self.add_module('pool', nn.AvgPool2d(kernel_size=2, stride=2))
4.构建DenseNet
nn.Sequential
是PyTorch中的一个模型容器,它按照给定的顺序依次执行一系列的神经网络模块(layers)。在构建神经网络时,我们可以使用nn.Sequential
来简化代码。
OrderedDict
是Python中的一种有序字典数据结构,它保留了元素添加的顺序。在神经网络中,我门可以使用 OrderedDict
来指定模型的层次结构。
class DenseNet(nn.Module):
def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16), init_channel=64,
bn_size=4, compression_rate=0.5, drop_rate=0, num_classes=1000):
'''
:param growth_rate: 每个DenseLayer的输出通道数
:param block_config: DenseBlock的层数
:param in_channels: 输入通道数
:param bn_size: 批标准化层的缩放因子
:param compression_rate: Transition层的压缩率
:param drop_rate: Dropout层的dropout概率
:param num_classes: 待分类的类别数
'''
super(DenseNet, self).__init__()
# 初始化DenseNet的第一层
self.features =nn.Sequential(OrderedDict([
('conv0',nn.Conv2d(3, init_channel, kernel_size=7, stride=2, padding=3, bias=False)),
('norm0',nn.BatchNorm2d(init_channel)),
('relu0',nn.ReLU(inplace=True)),
('pool0',nn.MaxPool2d(3, stride=2, padding=1))
]))
# 初始化DenseBlock
num_features = init_channel
for i, num_layers in enumerate(block_config):
block = DenseBlock(num_layers, num_features, bn_size, growth_rate, drop_rate)
self.features.add_module('denseblock%d'%(i+1), block)
num_features += num_layers * growth_rate
if i != len(block_config)-1:
transition = Transition(num_features, int(num_features * compression_rate))
self.features.add_module('transition%d'%(i+1), transition)
num_features = int(num_features * compression_rate)
# 初始化DenseNet的最后一层
self.features.add_module('norm5',nn.BatchNorm2d(num_features))
self.features.add_module('relu5',nn.ReLU(inplace=True))
# 分类层
self.classifier = nn.Linear(num_features, num_classes)
# 初始化权重
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1)
elif isinstance(m, nn.Linear):
nn.init.constant_(m.bias, 0)
def forward(self, x):
x = self.features(x)
x = F.avg_pool2d(x, 7, stride=1).view(x.size(0),-1)
x = self.classifier(x)
return x
5.构建DenseNet121
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))
DenseNet121 = DenseNet(init_channel=64,
growth_rate=32,
block_config=(6, 12, 24, 16),
num_classes=len(classNames))
model = DenseNet121.to(device)
model
# 统计模型参数量以及其他指标
import torchsummary as summary
summary.summary(model, (3, 224, 224))
三、训练模型
1.编写训练函数
# 训练循环
def train(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset) # 训练集的大小
num_batches = len(dataloader) # 批次数量,(size/batch_size,向上取整)
train_loss, train_acc = 0, 0 # 初始化训练损失和准确率
for X, y in dataloader: # 遍历数据集
X, y = X.to(device), y.to(device) # 数据移动到GPU
# 计算预测误差
pred = model(X) # 模型预测
loss = loss_fn(pred, y) # 计算网络输出和真实值之间的差距,targets为真实值,计算二者差值即为损失
# 反向传播
optimizer.zero_grad() # 梯度清零
loss.backward() # 反向传播
optimizer.step() # 更新参数
# 记录训练损失和准确率
train_acc += (pred.argmax(1) == y).type(torch.float).sum().item()
train_loss += loss.item()
train_acc /= size
train_loss /= num_batches
return train_acc, train_loss
2.编写测试函数
def test (dataloader, model, loss_fn):
size = len(dataloader.dataset) # 测试集的大小
num_batches = len(dataloader) # 批次数量,(size/batch_size,向上取整)
test_loss, test_acc = 0, 0 # 初始化测试损失和准确率
# 当不进行训练时,停止梯度更新,节省计算内存消耗
with torch.no_grad():
for imgs, target in dataloader:
imgs, target = imgs.to(device), target.to(device)
# 计算预测误差
target_pred = model(imgs)
loss = loss_fn(target_pred, target)
# 记录测试损失和准确率
test_loss += loss.item()
test_acc += (target_pred.argmax(1) == target).type(torch.float).sum().item()
# 计算平均损失和准确率
test_acc /= size
test_loss /= num_batches
return test_acc, test_loss
3.正式训练
import copy
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) # 定义优化器
loss_fn = nn.CrossEntropyLoss() # 定义损失函数
epochs = 20 # 定义训练轮数
train_loss = []
train_acc = []
test_loss = []
test_acc = []
best_acc = 0 # 设置一个最佳准确率,作为最佳模型的判别指标
for epoch in range(epochs):
model.train() # 设置模型为训练模式
epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, optimizer) # 训练模型
model.eval() # 设置模型为测试模式
epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn) # 测试模型
# 保存最佳模型
if epoch_test_acc > best_acc:
best_acc = epoch_test_acc # 更新最佳准确率
best_model = copy.deepcopy(model) # 保存最佳模型
train_acc.append(epoch_train_acc)
train_loss.append(epoch_train_loss)
test_acc.append(epoch_test_acc)
test_loss.append(epoch_test_loss)
# 获取当前的学习率
lr = optimizer.state_dict()['param_groups'][0]['lr']
# 打印训练信息
template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E}')
print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss,epoch_test_acc*100, epoch_test_loss, lr))
# 保存最佳模型到文件中
PATH = './model/3.densenet_best_model.pth' # 保存的参数文件名
torch.save(best_model.state_dict(), PATH) # 保存模型参数
print('最佳准确率:', best_acc)
print('Done')
四、结果可视化
1.Loss与Accuracy曲线
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore") # 忽略警告
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
plt.rcParams['figure.dpi'] = 100 # 分辨率
epochs_range = range(epochs)
plt.figure(figsize=(12, 3))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, train_acc, label='Training Accuracy')
plt.plot(epochs_range, test_acc, label='Test Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, train_loss, label='Training Loss')
plt.plot(epochs_range, test_loss, label='Test Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
2.模型评估
# 将参数加载到model当中
best_model.load_state_dict(torch.load(PATH, map_location=device))
epoch_test_acc, epoch_test_loss = test(test_dl, best_model, loss_fn)
epoch_test_acc, epoch_test_loss
五、个人总结
个人总结
通过此次乳腺癌图像分类的实验,我从数据准备、模型搭建、训练与测试等方面系统地学习了如何利用深度学习技术构建一个有效的图像分类模型。使用了DenseNet架构,其密集连接特性有助于梯度的流动,缓解了梯度消失问题,同时提升了模型的特征提取能力。最终,我通过调整超参数、优化训练策略和评估模型性能,成功实现了对乳腺癌图像的高效分类。通过可视化训练过程中的损失和准确率变化曲线,能够直观地观察到模型的训练效果与收敛情况。