深度学习笔记31-ResNet算法实战与解析(Pytorch)

发布于:2025-08-01 ⋅ 阅读:(14) ⋅ 点赞:(0)

一、导入数据并处理

import torch
import torch.nn as nn
from torchvision import transforms, datasets
import os,PIL,pathlib,warnings
warnings .filterwarnings("ignore")
data_dir=r"D:\xky\Downloads\data"
train_transforms=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(data_dir, transform=train_transforms)
total_data

total_data.class_to_idx

 

train_size = int(len(total_data) * 0.8)
test_size = len(total_data)- train_size
train_dataset,test_dataset = torch.utils.data.random_split(total_data,[train_size,test_size])
train_dataset,test_dataset

batch_size = 4
train_dl = torch.utils.data.DataLoader(train_dataset,batch_size = batch_size,shuffle = True,num_workers = 1)
test_dl = torch.utils.data.DataLoader(test_dataset,batch_size = batch_size,shuffle = True,num_workers = 1)
for x,y in train_dl:
    print("Shape of x [N,C,H,W]:",x.shape)
    print("Shape of y:",y.shape,y.dtype)
    break

 

二、搭建网络模型

import torch
import torch.nn as nn
import torchvision.models as models

def identity_block(input_tensor, kernel_size, filters, stage, block):
    """
    构建残差网络的恒等映射块
    Args:
        input_tensor: 输入张量
        kernel_size: 卷积核大小
        filters: [f1, f2, f3] 形式的过滤器数量列表
        stage: 阶段编号
        block: 块编号
    """
    filters1, filters2, filters3 = filters
    name_base = f'{stage}{block}_identity_block_'
    
    # 第一个 1x1 卷积层
    x = nn.Conv2d(input_tensor.size(1), filters1, 1, bias=False)(input_tensor)
    x = nn.BatchNorm2d(filters1)(x)
    x = nn.ReLU(inplace=True)(x)
    
    # 3x3 卷积层
    x = nn.Conv2d(filters1, filters2, kernel_size, padding=kernel_size//2, bias=False)(x)
    x = nn.BatchNorm2d(filters2)(x)
    x = nn.ReLU(inplace=True)(x)
    
    # 第二个 1x1 卷积层
    x = nn.Conv2d(filters2, filters3, 1, bias=False)(x)
    x = nn.BatchNorm2d(filters3)(x)
    
    # 添加跳跃连接
    x = x + input_tensor
    x = nn.ReLU(inplace=True)(x)
    
    return x

def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2,2)):
    """
    构建残差网络的卷积块
    Args:
        input_tensor: 输入张量
        kernel_size: 卷积核大小
        filters: [f1, f2, f3] 形式的过滤器数量列表
        stage: 阶段编号
        block: 块编号
        strides: 步长元组
    """
    filters1, filters2, filters3 = filters
    name_base = f'{stage}{block}_conv_block_'
    
    # 主路径
    x = nn.Conv2d(input_tensor.size(1), filters1, 1, stride=strides, bias=False)(input_tensor)
    x = nn.BatchNorm2d(filters1)(x)
    x = nn.ReLU(inplace=True)(x)
    
    x = nn.Conv2d(filters1, filters2, kernel_size, padding=kernel_size//2, bias=False)(x)
    x = nn.BatchNorm2d(filters2)(x)
    x = nn.ReLU(inplace=True)(x)
    
    x = nn.Conv2d(filters2, filters3, 1, bias=False)(x)
    x = nn.BatchNorm2d(filters3)(x)
    
    # shortcut 路径
    shortcut = nn.Conv2d(input_tensor.size(1), filters3, 1, stride=strides, bias=False)(input_tensor)
    shortcut = nn.BatchNorm2d(filters3)(shortcut)
    
    # 添加跳跃连接
    x = x + shortcut
    x = nn.ReLU(inplace=True)(x)
    
    return x

def ResNet50(input_shape=[224,224,3], num_classes=1000):
    """
    构建 ResNet50 模型
    Args:
        input_shape: 输入图像的形状 [H, W, C]
        num_classes: 分类类别数
    """
    # 输入层
    inputs = torch.randn(1, input_shape[2], input_shape[0], input_shape[1])
    
    # 初始卷积块 - 修改 ZeroPadding2d 为 pad 操作
    x = nn.functional.pad(inputs, (3, 3, 3, 3))  # 替换 ZeroPadding2d
    x = nn.Conv2d(input_shape[2], 64, 7, stride=2, bias=False)(x)
    x = nn.BatchNorm2d(64)(x)
    x = nn.ReLU(inplace=True)(x)
    x = nn.MaxPool2d(3, stride=2, padding=1)(x)
    
    # Stage 2
    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1,1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
    
    # Stage 3
    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
    
    # Stage 4
    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    for block in ['b', 'c', 'd', 'e', 'f']:
        x = identity_block(x, 3, [256, 256, 1024], stage=4, block=block)
    
    # Stage 5
    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
    
    # 分类层
    x = nn.AdaptiveAvgPool2d((1, 1))(x)
    x = torch.flatten(x, 1)
    x = nn.Linear(2048, num_classes)(x)
    
    # 修改模型创建和前向传播的方式
    class ResNet(nn.Module):
        def __init__(self):
            super(ResNet, self).__init__()
            # 在这里定义所有层
            
        def forward(self, x):
            # 定义前向传播
            return x

    model = ResNet()
    # 移除 load_weights,改用 PyTorch 的加载方式
    model.load_state_dict(torch.load("resnet50_pretrained.pth"))
    return model

model = models.resnet50()
model

#统计模型参数量以及其他指标
import torchsummary as summary
summary.summary(model,(3,224,224))

 三、训练模型

def train(dataloader,model,optimizer,loss_fn):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    
    train_acc,train_loss = 0,0
    
    for X,y in dataloader:
        pred = model(X)
        loss = loss_fn(pred,y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        train_loss += loss.item()
        train_acc += (pred.argmax(1) == y).type(torch.float).sum().item()
    train_loss /= num_batches
    train_acc /= size
    
    return train_acc,train_loss

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:
            #计算loss
            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

import copy
optimizer= torch.optim.AdamW(model.parameters(), lr= 1e-4)
loss_fn= nn.CrossEntropyLoss()#创建损失函数
epochs=10
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,optimizer,loss_fn)
    model.eval()
    epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)
    #保存最佳模型到best_model
    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='./best_model.pth'#保存的参数文件名
torch.save(best_model.state_dict(), PATH)
print('Done')

四、模型评估

import matplotlib.pyplot as plt
from datetime import datetime
warnings.filterwarnings("ignore") #忽略警告信息
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
plt.rcParams['figure.dpi']= 100 #分辩率
current_time=datetime.now()#获取当前时间

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.xlabel(current_time)#打卡请带上时间戳,否则代码截图无效
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()

#将参数加载到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

总结

ResNet(Residual Network,残差网络)是一种深度卷积神经网络(CNN)架构,其核心创新是残差连接,成功解决了深度神经网络训练中的 “梯度消失” 和 “性能退化” 问题,使得训练超深网络(甚至超过 1000 层)成为可能。(ps:只要有合适的网络结构,更深的网络肯定会比较浅的网络效果要好。)

  1. 梯度消失:梯度消失是指在反向传播过程中,随着网络层数的增加,前面几层的梯度值变得非常小,接近于零。这会导致权重更新非常缓慢,从而无法有效训练深层网络。
  2. 梯度爆炸:梯度爆炸是指在反向传播过程中,随着网络层数的增加,梯度值变得非常大,最终导致网络权重的更新幅度过大,进而导致模型无法收敛,甚至出现数值溢出。

根据网络层数的不同,ResNet 有多个经典变体,层数对应残差块的数量:

模型名称

层数

残差块结构

应用场景

ResNet-18

18

基本块(Basic)

轻量级任务、实时性要求高的场景

ResNet-34

34

基本块(Basic)

平衡精度与计算量的场景

ResNet-50

50

瓶颈块(Bottleneck)

主流选择,兼顾精度和效率

ResNet-101

101

瓶颈块(Bottleneck)

高精度要求场景(如目标检测)

ResNet-152

152

瓶颈块(Bottleneck)

超高精度需求,计算量较大


网站公告

今日签到

点亮在社区的每一天
去签到