>- **🍨 本文为[🔗365天深度学习训练营](https://mp.weixin.qq.com/s/Z9yL_wt7L8aPOr9Lqb1K3w) 中的学习记录博客** >- **🍖 原作者:[K同学啊](https://mtyjkh.blog.csdn.net/)**
📌 本周任务:
●1.请根据本文 TensorFlow 代码,编写出相应的 Pytorch 代码(建议使用上周的数据测试一下模型是否构建正确)
●2.了解ResNetV2与ResNetV的区别
●3.改进思路是否可以迁移到其他地方呢(自由探索)
一、ResNet50V2和ResNet50的对比
-
ResNet50:采用“后激活”结构。也就是说,卷积操作完成后才进行批归一化(Batch Normalization)和激活函数(ReLU)。在每个残差块中,输入数据直接跳跃到输出端,与卷积操作的结果相加,从而形成残差连接。这种设计有效缓解了深层网络中的梯度消失问题。
-
ResNet50V2:改进为“预激活”(Pre-Activation)结构。批归一化和激活函数都放在卷积操作之前。这种设计能够使得梯度在深层网络中的传播更加顺畅,进一步减轻梯度消失问题。此外,这种改进可以更好地保持信息流动,尤其是在更深的网络中表现得更为显著。
二 、模型复现
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
from torchvision import datasets
import torch.nn.functional as F
import torch.nn as nn
import torch
import os, PIL, random, pathlib, copy
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore") #忽略警告信息
# 设置gpu
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
# 构建模型
# 构建主要模块,残差块Residual Block
class Block2(nn.Module):
def __init__(self, in_channels, filters, kernel_size=3, strides=1, conv_shortcut=False):
super(Block2, self).__init__()
self.preact = nn.Sequential(
nn.BatchNorm2d(in_channels),
nn.ReLU(True)
)
# 修改跳跃连接,确保 identity 的大小匹配
if conv_shortcut or strides > 1:
self.shortcut = nn.Conv2d(in_channels, 4 * filters, 1, stride=strides, bias=False)
else:
self.shortcut = nn.Identity()
# 第一层卷积
self.conv1 = nn.Conv2d(in_channels, filters, 1, bias=False)
self.bn1 = nn.BatchNorm2d(filters)
self.relu1 = nn.ReLU(True)
# 第二层卷积
self.conv2 = nn.Conv2d(filters, filters, kernel_size, stride=strides, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(filters)
self.relu2 = nn.ReLU(True)
# 第三层卷积
self.conv3 = nn.Conv2d(filters, 4 * filters, 1, bias=False)
self.bn3 = nn.BatchNorm2d(4 * filters)
self.relu = nn.ReLU(True)
def forward(self, x):
identity = self.shortcut(x)
x = self.preact(x)
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
x = self.conv3(x)
x += identity # 确保形状一致
return self.relu(x)
# 堆叠Residual Block
class Stack2(nn.Module):
def __init__(self, in_channels, filters, blocks, stride1=2):
super(Stack2, self).__init__()
# 第一个块总是使用 conv_shortcut
self.block1 = Block2(in_channels, filters, conv_shortcut=True)
# 中间的块
self.middle_blocks = nn.ModuleList([
Block2(filters * 4, filters)
for i in range(2, blocks)
])
# 最后一个块可能会改变步长
self.last_block = Block2(filters * 4, filters, strides=stride1)
def forward(self, x):
x = self.block1(x)
for block in self.middle_blocks:
x = block(x)
x = self.last_block(x)
return x
# 使用现有的 Block2 和 Stack2 模块
# 将 TensorFlow 的 ResNet50V2 模型转换为 PyTorch 模型
class ResNet50V2(nn.Module):
def __init__(self, include_top=True, preact=True, use_bias=True, input_shape=(3, 224, 224), classes=1000,
classifier_activation='softmax'):
super(ResNet50V2, self).__init__()
self.include_top = include_top
self.preact = preact
self.classes = classes
# 第一个卷积层 + 批归一化 + ReLU
self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3, bias=use_bias)
if not self.preact:
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
# ResNet主体,使用已经定义的 Stack2 模块
self.stack2 = Stack2(64, 64, 3)
self.stack3 = Stack2(64 * 4, 128, 4)
self.stack4 = Stack2(128 * 4, 256, 6)
self.stack5 = Stack2(256 * 4, 512, 3)
if self.preact:
self.bn_post = nn.BatchNorm2d(512 * 4)
self.relu_post = nn.ReLU(inplace=True)
if self.include_top:
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * 4, classes)
def forward(self, x):
# 第一个卷积层
x = self.conv1(x)
if not self.preact:
x = self.bn1(x)
x = F.relu(x)
x = self.maxpool(x)
# 堆叠残差块
x = self.stack2(x)
x = self.stack3(x)
x = self.stack4(x)
x = self.stack5(x)
if self.preact:
x = self.bn_post(x)
x = self.relu_post(x)
if self.include_top:
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
if self.classes == 1:
x = torch.sigmoid(x)
else:
x = F.softmax(x, dim=1)
return x
模型结果:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 112, 112] 9,472
MaxPool2d-2 [-1, 64, 56, 56] 0
Conv2d-3 [-1, 256, 56, 56] 16,384
BatchNorm2d-4 [-1, 64, 56, 56] 128
ReLU-5 [-1, 64, 56, 56] 0
Conv2d-6 [-1, 64, 56, 56] 4,096
BatchNorm2d-7 [-1, 64, 56, 56] 128
ReLU-8 [-1, 64, 56, 56] 0
Conv2d-9 [-1, 64, 56, 56] 36,864
BatchNorm2d-10 [-1, 64, 56, 56] 128
ReLU-11 [-1, 64, 56, 56] 0
Conv2d-12 [-1, 256, 56, 56] 16,384
ReLU-13 [-1, 256, 56, 56] 0
Block2-14 [-1, 256, 56, 56] 0
Identity-15 [-1, 256, 56, 56] 0
BatchNorm2d-16 [-1, 256, 56, 56] 512
ReLU-17 [-1, 256, 56, 56] 0
Conv2d-18 [-1, 64, 56, 56] 16,384
BatchNorm2d-19 [-1, 64, 56, 56] 128
ReLU-20 [-1, 64, 56, 56] 0
Conv2d-21 [-1, 64, 56, 56] 36,864
BatchNorm2d-22 [-1, 64, 56, 56] 128
ReLU-23 [-1, 64, 56, 56] 0
Conv2d-24 [-1, 256, 56, 56] 16,384
ReLU-25 [-1, 256, 56, 56] 0
Block2-26 [-1, 256, 56, 56] 0
Conv2d-27 [-1, 256, 28, 28] 65,536
BatchNorm2d-28 [-1, 256, 56, 56] 512
ReLU-29 [-1, 256, 56, 56] 0
Conv2d-30 [-1, 64, 56, 56] 16,384
BatchNorm2d-31 [-1, 64, 56, 56] 128
ReLU-32 [-1, 64, 56, 56] 0
Conv2d-33 [-1, 64, 28, 28] 36,864
BatchNorm2d-34 [-1, 64, 28, 28] 128
ReLU-35 [-1, 64, 28, 28] 0
Conv2d-36 [-1, 256, 28, 28] 16,384
ReLU-37 [-1, 256, 28, 28] 0
Block2-38 [-1, 256, 28, 28] 0
Stack2-39 [-1, 256, 28, 28] 0
Conv2d-40 [-1, 512, 28, 28] 131,072
BatchNorm2d-41 [-1, 256, 28, 28] 512
ReLU-42 [-1, 256, 28, 28] 0
Conv2d-43 [-1, 128, 28, 28] 32,768
BatchNorm2d-44 [-1, 128, 28, 28] 256
ReLU-45 [-1, 128, 28, 28] 0
Conv2d-46 [-1, 128, 28, 28] 147,456
BatchNorm2d-47 [-1, 128, 28, 28] 256
ReLU-48 [-1, 128, 28, 28] 0
Conv2d-49 [-1, 512, 28, 28] 65,536
ReLU-50 [-1, 512, 28, 28] 0
Block2-51 [-1, 512, 28, 28] 0
Identity-52 [-1, 512, 28, 28] 0
BatchNorm2d-53 [-1, 512, 28, 28] 1,024
ReLU-54 [-1, 512, 28, 28] 0
Conv2d-55 [-1, 128, 28, 28] 65,536
BatchNorm2d-56 [-1, 128, 28, 28] 256
ReLU-57 [-1, 128, 28, 28] 0
Conv2d-58 [-1, 128, 28, 28] 147,456
BatchNorm2d-59 [-1, 128, 28, 28] 256
ReLU-60 [-1, 128, 28, 28] 0
Conv2d-61 [-1, 512, 28, 28] 65,536
ReLU-62 [-1, 512, 28, 28] 0
Block2-63 [-1, 512, 28, 28] 0
Identity-64 [-1, 512, 28, 28] 0
BatchNorm2d-65 [-1, 512, 28, 28] 1,024
ReLU-66 [-1, 512, 28, 28] 0
Conv2d-67 [-1, 128, 28, 28] 65,536
BatchNorm2d-68 [-1, 128, 28, 28] 256
ReLU-69 [-1, 128, 28, 28] 0
Conv2d-70 [-1, 128, 28, 28] 147,456
BatchNorm2d-71 [-1, 128, 28, 28] 256
ReLU-72 [-1, 128, 28, 28] 0
Conv2d-73 [-1, 512, 28, 28] 65,536
ReLU-74 [-1, 512, 28, 28] 0
Block2-75 [-1, 512, 28, 28] 0
Conv2d-76 [-1, 512, 14, 14] 262,144
BatchNorm2d-77 [-1, 512, 28, 28] 1,024
ReLU-78 [-1, 512, 28, 28] 0
Conv2d-79 [-1, 128, 28, 28] 65,536
BatchNorm2d-80 [-1, 128, 28, 28] 256
ReLU-81 [-1, 128, 28, 28] 0
Conv2d-82 [-1, 128, 14, 14] 147,456
BatchNorm2d-83 [-1, 128, 14, 14] 256
ReLU-84 [-1, 128, 14, 14] 0
Conv2d-85 [-1, 512, 14, 14] 65,536
ReLU-86 [-1, 512, 14, 14] 0
Block2-87 [-1, 512, 14, 14] 0
Stack2-88 [-1, 512, 14, 14] 0
Conv2d-89 [-1, 1024, 14, 14] 524,288
BatchNorm2d-90 [-1, 512, 14, 14] 1,024
ReLU-91 [-1, 512, 14, 14] 0
Conv2d-92 [-1, 256, 14, 14] 131,072
BatchNorm2d-93 [-1, 256, 14, 14] 512
ReLU-94 [-1, 256, 14, 14] 0
Conv2d-95 [-1, 256, 14, 14] 589,824
BatchNorm2d-96 [-1, 256, 14, 14] 512
ReLU-97 [-1, 256, 14, 14] 0
Conv2d-98 [-1, 1024, 14, 14] 262,144
ReLU-99 [-1, 1024, 14, 14] 0
Block2-100 [-1, 1024, 14, 14] 0
Identity-101 [-1, 1024, 14, 14] 0
BatchNorm2d-102 [-1, 1024, 14, 14] 2,048
ReLU-103 [-1, 1024, 14, 14] 0
Conv2d-104 [-1, 256, 14, 14] 262,144
BatchNorm2d-105 [-1, 256, 14, 14] 512
ReLU-106 [-1, 256, 14, 14] 0
Conv2d-107 [-1, 256, 14, 14] 589,824
BatchNorm2d-108 [-1, 256, 14, 14] 512
ReLU-109 [-1, 256, 14, 14] 0
Conv2d-110 [-1, 1024, 14, 14] 262,144
ReLU-111 [-1, 1024, 14, 14] 0
Block2-112 [-1, 1024, 14, 14] 0
Identity-113 [-1, 1024, 14, 14] 0
BatchNorm2d-114 [-1, 1024, 14, 14] 2,048
ReLU-115 [-1, 1024, 14, 14] 0
Conv2d-116 [-1, 256, 14, 14] 262,144
BatchNorm2d-117 [-1, 256, 14, 14] 512
ReLU-118 [-1, 256, 14, 14] 0
Conv2d-119 [-1, 256, 14, 14] 589,824
BatchNorm2d-120 [-1, 256, 14, 14] 512
ReLU-121 [-1, 256, 14, 14] 0
Conv2d-122 [-1, 1024, 14, 14] 262,144
ReLU-123 [-1, 1024, 14, 14] 0
Block2-124 [-1, 1024, 14, 14] 0
Identity-125 [-1, 1024, 14, 14] 0
BatchNorm2d-126 [-1, 1024, 14, 14] 2,048
ReLU-127 [-1, 1024, 14, 14] 0
Conv2d-128 [-1, 256, 14, 14] 262,144
BatchNorm2d-129 [-1, 256, 14, 14] 512
ReLU-130 [-1, 256, 14, 14] 0
Conv2d-131 [-1, 256, 14, 14] 589,824
BatchNorm2d-132 [-1, 256, 14, 14] 512
ReLU-133 [-1, 256, 14, 14] 0
Conv2d-134 [-1, 1024, 14, 14] 262,144
ReLU-135 [-1, 1024, 14, 14] 0
Block2-136 [-1, 1024, 14, 14] 0
Identity-137 [-1, 1024, 14, 14] 0
BatchNorm2d-138 [-1, 1024, 14, 14] 2,048
ReLU-139 [-1, 1024, 14, 14] 0
Conv2d-140 [-1, 256, 14, 14] 262,144
BatchNorm2d-141 [-1, 256, 14, 14] 512
ReLU-142 [-1, 256, 14, 14] 0
Conv2d-143 [-1, 256, 14, 14] 589,824
BatchNorm2d-144 [-1, 256, 14, 14] 512
ReLU-145 [-1, 256, 14, 14] 0
Conv2d-146 [-1, 1024, 14, 14] 262,144
ReLU-147 [-1, 1024, 14, 14] 0
Block2-148 [-1, 1024, 14, 14] 0
Conv2d-149 [-1, 1024, 7, 7] 1,048,576
BatchNorm2d-150 [-1, 1024, 14, 14] 2,048
ReLU-151 [-1, 1024, 14, 14] 0
Conv2d-152 [-1, 256, 14, 14] 262,144
BatchNorm2d-153 [-1, 256, 14, 14] 512
ReLU-154 [-1, 256, 14, 14] 0
Conv2d-155 [-1, 256, 7, 7] 589,824
BatchNorm2d-156 [-1, 256, 7, 7] 512
ReLU-157 [-1, 256, 7, 7] 0
Conv2d-158 [-1, 1024, 7, 7] 262,144
ReLU-159 [-1, 1024, 7, 7] 0
Block2-160 [-1, 1024, 7, 7] 0
Stack2-161 [-1, 1024, 7, 7] 0
Conv2d-162 [-1, 2048, 7, 7] 2,097,152
BatchNorm2d-163 [-1, 1024, 7, 7] 2,048
ReLU-164 [-1, 1024, 7, 7] 0
Conv2d-165 [-1, 512, 7, 7] 524,288
BatchNorm2d-166 [-1, 512, 7, 7] 1,024
ReLU-167 [-1, 512, 7, 7] 0
Conv2d-168 [-1, 512, 7, 7] 2,359,296
BatchNorm2d-169 [-1, 512, 7, 7] 1,024
ReLU-170 [-1, 512, 7, 7] 0
Conv2d-171 [-1, 2048, 7, 7] 1,048,576
ReLU-172 [-1, 2048, 7, 7] 0
Block2-173 [-1, 2048, 7, 7] 0
Identity-174 [-1, 2048, 7, 7] 0
BatchNorm2d-175 [-1, 2048, 7, 7] 4,096
ReLU-176 [-1, 2048, 7, 7] 0
Conv2d-177 [-1, 512, 7, 7] 1,048,576
BatchNorm2d-178 [-1, 512, 7, 7] 1,024
ReLU-179 [-1, 512, 7, 7] 0
Conv2d-180 [-1, 512, 7, 7] 2,359,296
BatchNorm2d-181 [-1, 512, 7, 7] 1,024
ReLU-182 [-1, 512, 7, 7] 0
Conv2d-183 [-1, 2048, 7, 7] 1,048,576
ReLU-184 [-1, 2048, 7, 7] 0
Block2-185 [-1, 2048, 7, 7] 0
Conv2d-186 [-1, 2048, 4, 4] 4,194,304
BatchNorm2d-187 [-1, 2048, 7, 7] 4,096
ReLU-188 [-1, 2048, 7, 7] 0
Conv2d-189 [-1, 512, 7, 7] 1,048,576
BatchNorm2d-190 [-1, 512, 7, 7] 1,024
ReLU-191 [-1, 512, 7, 7] 0
Conv2d-192 [-1, 512, 4, 4] 2,359,296
BatchNorm2d-193 [-1, 512, 4, 4] 1,024
ReLU-194 [-1, 512, 4, 4] 0
Conv2d-195 [-1, 2048, 4, 4] 1,048,576
ReLU-196 [-1, 2048, 4, 4] 0
Block2-197 [-1, 2048, 4, 4] 0
Stack2-198 [-1, 2048, 4, 4] 0
BatchNorm2d-199 [-1, 2048, 4, 4] 4,096
ReLU-200 [-1, 2048, 4, 4] 0
AdaptiveAvgPool2d-201 [-1, 2048, 1, 1] 0
Linear-202 [-1, 4] 8,196
================================================================
Total params: 29,079,172
Trainable params: 29,079,172
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 271.74
Params size (MB): 110.93
Estimated Total Size (MB): 383.24
----------------------------------------------------------------
ResNet50V2(
(conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))
(maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
(stack2): Stack2(
(block1): Block2(
(preact): Sequential(
(0): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
(conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
(middle_blocks): ModuleList(
(0): Block2(
(preact): Sequential(
(0): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Identity()
(conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
(last_block): Block2(
(preact): Sequential(
(0): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Conv2d(256, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
(conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
(stack3): Stack2(
(block1): Block2(
(preact): Sequential(
(0): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Conv2d(256, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
(middle_blocks): ModuleList(
(0-1): 2 x Block2(
(preact): Sequential(
(0): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Identity()
(conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
(last_block): Block2(
(preact): Sequential(
(0): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Conv2d(512, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
(conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
(stack4): Stack2(
(block1): Block2(
(preact): Sequential(
(0): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Conv2d(512, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
(conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
(middle_blocks): ModuleList(
(0-3): 4 x Block2(
(preact): Sequential(
(0): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Identity()
(conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
(last_block): Block2(
(preact): Sequential(
(0): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)
(conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
(stack5): Stack2(
(block1): Block2(
(preact): Sequential(
(0): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
(conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
(middle_blocks): ModuleList(
(0): Block2(
(preact): Sequential(
(0): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Identity()
(conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
(last_block): Block2(
(preact): Sequential(
(0): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(1): ReLU(inplace=True)
)
(shortcut): Conv2d(2048, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)
(conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
(bn_post): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu_post): ReLU(inplace=True)
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
(fc): Linear(in_features=2048, out_features=4, bias=True)
三、带入J1数据验证
四、总结
模型准确率较低的问题可能由多个因素引起,尤其在自定义网络相较于预训练的标准ResNet50时表现较差。可能是因为自定义的 ResNet50V2 模型在某些关键设计上可能未达到 ResNet50 的优化水平。例如,跳跃连接、Batch Normalization 和激活函数等处理顺序的细微调整,都会影响模型的性能。还需进一步的学习。