欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > J6学习打卡

J6学习打卡

2024/11/30 20:11:35 来源:https://blog.csdn.net/Inface0443/article/details/144122479  浏览:    关键词:J6学习打卡
  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊

ResNeXt-50实战

# 定义transforms
import torchvision.transforms as transformstrain_transforms = transforms.Compose([transforms.Resize([224, 224]),  # 统一图片尺寸transforms.RandomHorizontalFlip(p=0.5),  # 随机水平翻转transforms.RandomRotation(degrees=15),  # 小角度随机旋转transforms.RandomResizedCrop(size=224, scale=(0.8, 1.2)),  # 随机缩放裁剪transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.1),  # 光照变化transforms.ToTensor(),  # 转换为 Tensor 格式transforms.Normalize(  # 标准化mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
])
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import warningswarnings.filterwarnings('ignore')  # 忽略警告信息plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.rcParams['figure.dpi'] = 100  # 分辨率
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
data_dir = r"C:\Users\11054\Desktop\kLearning\J1_learning\bird_photos"transform = transforms.Compose([transforms.Resize((224, 224)),transforms.ToTensor(),
])train_dataset = datasets.ImageFolder(data_dir, transform=transform)
train_size = int(0.8 * len(train_dataset))
val_size = len(train_dataset) - train_size
train_dataset, val_dataset = torch.utils.data.random_split(train_dataset, [train_size, val_size])train_loader = DataLoader(train_dataset, batch_size=8, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=8, shuffle=False)class_names = train_dataset.dataset.classes
class_names
['Bananaquit', 'Black Skimmer', 'Black Throated Bushtiti', 'Cockatoo']

train_transforms = transforms.Compose([transforms.Resize([224, 224]),  # 统一图片尺寸transforms.RandomHorizontalFlip(p=0.5),  # 随机水平翻转transforms.RandomRotation(degrees=15),  # 小角度随机旋转transforms.RandomResizedCrop(size=224, scale=(0.8, 1.2)),  # 随机缩放裁剪transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.1),  # 光照变化transforms.ToTensor(),  # 转换为 Tensor 格式transforms.Normalize(  # 标准化mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
])test_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
Dataset ImageFolderNumber of datapoints: 565Root location: C:\Users\11054\Desktop\kLearning\J1_learning\bird_photosStandardTransform
Transform: Compose(Resize(size=[224, 224], interpolation=bilinear, max_size=None, antialias=True)RandomHorizontalFlip(p=0.5)RandomRotation(degrees=[-15.0, 15.0], interpolation=nearest, expand=False, fill=0)RandomResizedCrop(size=(224, 224), scale=(0.8, 1.2), ratio=(0.75, 1.3333), interpolation=bilinear, antialias=True)ColorJitter(brightness=(0.8, 1.2), contrast=(0.8, 1.2), saturation=(0.9, 1.1), hue=None)ToTensor()Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]))
# 设置随机种子
torch.manual_seed(42)# 划分数据集
train_size = int(len(total_data) * 0.8)
test_size = len(total_data) - train_sizetrain_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])
train_dataset, test_dataset
(<torch.utils.data.dataset.Subset at 0x184276162b0>,<torch.utils.data.dataset.Subset at 0x184276163a0>)
# 定义DataLoader用于数据集的加载batch_size = 8train_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 test_dl:print("Shape of X [N,C,H,W]: ", X.shape)print("Shape of y: ", y.shape, y.dtype)break
Shape of X [N,C,H,W]:  torch.Size([8, 3, 224, 224])
Shape of y:  torch.Size([8]) torch.int64
import torch
import torch.nn as nn# 定义分组卷积模块
class GroupedConvBlock(nn.Module):def __init__(self, in_channels, groups, g_channels, stride):super(GroupedConvBlock, self).__init__()self.groups = groupsself.group_conv = nn.ModuleList([nn.Conv2d(g_channels, g_channels, kernel_size=3, stride=stride, padding=1, bias=False)for _ in range(groups)])self.bn = nn.BatchNorm2d(in_channels)self.relu = nn.ReLU(inplace=True)def forward(self, x):# 分组数据split_x = torch.split(x, x.size(1) // self.groups, dim=1)group_out = [conv(g) for g, conv in zip(split_x, self.group_conv)]# 合并数据x = torch.cat(group_out, dim=1)x = self.bn(x)x = self.relu(x)return x# 定义残差模块
class ResNeXtBlock(nn.Module):def __init__(self, in_channels, filters, groups=32, stride=1, conv_shortcut=True):super(ResNeXtBlock, self).__init__()self.conv_shortcut = conv_shortcutself.groups = groupsself.g_channels = filters // groups# Shortcut分支if conv_shortcut:self.shortcut = nn.Sequential(nn.Conv2d(in_channels, filters * 2, kernel_size=1, stride=stride, bias=False),nn.BatchNorm2d(filters * 2),)else:self.shortcut = nn.Identity()# 主分支self.conv1 = nn.Sequential(nn.Conv2d(in_channels, filters, kernel_size=1, stride=1, bias=False),nn.BatchNorm2d(filters),nn.ReLU(inplace=True))self.grouped_conv = GroupedConvBlock(filters, groups, self.g_channels, stride)self.conv3 = nn.Sequential(nn.Conv2d(filters, filters * 2, kernel_size=1, stride=1, bias=False),nn.BatchNorm2d(filters * 2),)self.relu = nn.ReLU(inplace=True)def forward(self, x):shortcut = self.shortcut(x)x = self.conv1(x)x = self.grouped_conv(x)x = self.conv3(x)x += shortcutx = self.relu(x)return x# 定义 ResNeXt-50 模型
class ResNeXt50(nn.Module):def __init__(self, num_classes=1000):super(ResNeXt50, self).__init__()self.stem = nn.Sequential(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),nn.BatchNorm2d(64),nn.ReLU(inplace=True),nn.MaxPool2d(kernel_size=3, stride=2, padding=1))# 堆叠ResNeXt模块self.layer1 = self.make_layer(64, 128, 3, stride=1)self.layer2 = self.make_layer(256, 256, 4, stride=2)self.layer3 = self.make_layer(512, 512, 6, stride=2)self.layer4 = self.make_layer(1024, 1024, 3, stride=2)# 全局平均池化和分类层self.global_avg_pool = nn.AdaptiveAvgPool2d(1)self.fc = nn.Linear(2048, num_classes)@staticmethoddef make_layer(in_channels, filters, blocks, stride):layers = [ResNeXtBlock(in_channels, filters, stride=stride)]for _ in range(1, blocks):layers.append(ResNeXtBlock(filters * 2, filters, stride=1))return nn.Sequential(*layers)def forward(self, x):x = self.stem(x)x = self.layer1(x)x = self.layer2(x)x = self.layer3(x)x = self.layer4(x)x = self.global_avg_pool(x)x = torch.flatten(x, 1)x = self.fc(x)return x
model = ResNeXt50(num_classes=4).to(device)
model
ResNeXt50((stem): Sequential((0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False))(layer1): Sequential((0): ResNeXtBlock((shortcut): Sequential((0): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(64, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(4, 4, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(128, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(1): ResNeXtBlock((shortcut): Sequential((0): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(4, 4, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(128, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(2): ResNeXtBlock((shortcut): Sequential((0): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(4, 4, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(128, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True)))(layer2): Sequential((0): ResNeXtBlock((shortcut): Sequential((0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(8, 8, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False))(bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(256, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(1): ResNeXtBlock((shortcut): Sequential((0): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(8, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(256, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(2): ResNeXtBlock((shortcut): Sequential((0): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(8, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(256, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(3): ResNeXtBlock((shortcut): Sequential((0): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(8, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(256, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True)))(layer3): Sequential((0): ResNeXtBlock((shortcut): Sequential((0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(16, 16, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False))(bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(1): ResNeXtBlock((shortcut): Sequential((0): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(2): ResNeXtBlock((shortcut): Sequential((0): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(3): ResNeXtBlock((shortcut): Sequential((0): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(4): ResNeXtBlock((shortcut): Sequential((0): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(5): ResNeXtBlock((shortcut): Sequential((0): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True)))(layer4): Sequential((0): ResNeXtBlock((shortcut): Sequential((0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)(1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(32, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False))(bn): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(1): ResNeXtBlock((shortcut): Sequential((0): Conv2d(2048, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(2048, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True))(2): ResNeXtBlock((shortcut): Sequential((0): Conv2d(2048, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(conv1): Sequential((0): Conv2d(2048, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(grouped_conv): GroupedConvBlock((group_convs): ModuleList((0-31): 32 x Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(bn): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(conv3): Sequential((0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)(1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))(relu): ReLU(inplace=True)))(global_avg_pool): AdaptiveAvgPool2d(output_size=1)(fc): Linear(in_features=2048, out_features=4, bias=True)
)
# 查看模型详情
import torchsummary as summarysummary.summary(model, (3, 224, 224))
----------------------------------------------------------------Layer (type)               Output Shape         Param #
================================================================Conv2d-1         [-1, 64, 112, 112]           9,408BatchNorm2d-2         [-1, 64, 112, 112]             128ReLU-3         [-1, 64, 112, 112]               0MaxPool2d-4           [-1, 64, 56, 56]               0Conv2d-5          [-1, 256, 56, 56]          16,384BatchNorm2d-6          [-1, 256, 56, 56]             512Conv2d-7          [-1, 128, 56, 56]           8,192BatchNorm2d-8          [-1, 128, 56, 56]             256ReLU-9          [-1, 128, 56, 56]               0Conv2d-10            [-1, 4, 56, 56]             144Conv2d-11            [-1, 4, 56, 56]             144Conv2d-12            [-1, 4, 56, 56]             144Conv2d-13            [-1, 4, 56, 56]             144Conv2d-14            [-1, 4, 56, 56]             144Conv2d-15            [-1, 4, 56, 56]             144Conv2d-16            [-1, 4, 56, 56]             144Conv2d-17            [-1, 4, 56, 56]             144Conv2d-18            [-1, 4, 56, 56]             144Conv2d-19            [-1, 4, 56, 56]             144Conv2d-20            [-1, 4, 56, 56]             144Conv2d-21            [-1, 4, 56, 56]             144Conv2d-22            [-1, 4, 56, 56]             144Conv2d-23            [-1, 4, 56, 56]             144Conv2d-24            [-1, 4, 56, 56]             144Conv2d-25            [-1, 4, 56, 56]             144Conv2d-26            [-1, 4, 56, 56]             144Conv2d-27            [-1, 4, 56, 56]             144Conv2d-28            [-1, 4, 56, 56]             144Conv2d-29            [-1, 4, 56, 56]             144Conv2d-30            [-1, 4, 56, 56]             144Conv2d-31            [-1, 4, 56, 56]             144Conv2d-32            [-1, 4, 56, 56]             144Conv2d-33            [-1, 4, 56, 56]             144Conv2d-34            [-1, 4, 56, 56]             144Conv2d-35            [-1, 4, 56, 56]             144Conv2d-36            [-1, 4, 56, 56]             144Conv2d-37            [-1, 4, 56, 56]             144Conv2d-38            [-1, 4, 56, 56]             144Conv2d-39            [-1, 4, 56, 56]             144Conv2d-40            [-1, 4, 56, 56]             144Conv2d-41            [-1, 4, 56, 56]             144BatchNorm2d-42          [-1, 128, 56, 56]             256ReLU-43          [-1, 128, 56, 56]               0GroupedConvBlock-44          [-1, 128, 56, 56]               0Conv2d-45          [-1, 256, 56, 56]          32,768BatchNorm2d-46          [-1, 256, 56, 56]             512ReLU-47          [-1, 256, 56, 56]               0ResNeXtBlock-48          [-1, 256, 56, 56]               0Conv2d-49          [-1, 256, 56, 56]          65,536BatchNorm2d-50          [-1, 256, 56, 56]             512Conv2d-51          [-1, 128, 56, 56]          32,768BatchNorm2d-52          [-1, 128, 56, 56]             256ReLU-53          [-1, 128, 56, 56]               0Conv2d-54            [-1, 4, 56, 56]             144Conv2d-55            [-1, 4, 56, 56]             144Conv2d-56            [-1, 4, 56, 56]             144Conv2d-57            [-1, 4, 56, 56]             144Conv2d-58            [-1, 4, 56, 56]             144Conv2d-59            [-1, 4, 56, 56]             144Conv2d-60            [-1, 4, 56, 56]             144Conv2d-61            [-1, 4, 56, 56]             144Conv2d-62            [-1, 4, 56, 56]             144Conv2d-63            [-1, 4, 56, 56]             144Conv2d-64            [-1, 4, 56, 56]             144Conv2d-65            [-1, 4, 56, 56]             144Conv2d-66            [-1, 4, 56, 56]             144Conv2d-67            [-1, 4, 56, 56]             144Conv2d-68            [-1, 4, 56, 56]             144Conv2d-69            [-1, 4, 56, 56]             144Conv2d-70            [-1, 4, 56, 56]             144Conv2d-71            [-1, 4, 56, 56]             144Conv2d-72            [-1, 4, 56, 56]             144Conv2d-73            [-1, 4, 56, 56]             144Conv2d-74            [-1, 4, 56, 56]             144Conv2d-75            [-1, 4, 56, 56]             144Conv2d-76            [-1, 4, 56, 56]             144Conv2d-77            [-1, 4, 56, 56]             144Conv2d-78            [-1, 4, 56, 56]             144Conv2d-79            [-1, 4, 56, 56]             144Conv2d-80            [-1, 4, 56, 56]             144Conv2d-81            [-1, 4, 56, 56]             144Conv2d-82            [-1, 4, 56, 56]             144Conv2d-83            [-1, 4, 56, 56]             144Conv2d-84            [-1, 4, 56, 56]             144Conv2d-85            [-1, 4, 56, 56]             144BatchNorm2d-86          [-1, 128, 56, 56]             256ReLU-87          [-1, 128, 56, 56]               0GroupedConvBlock-88          [-1, 128, 56, 56]               0Conv2d-89          [-1, 256, 56, 56]          32,768BatchNorm2d-90          [-1, 256, 56, 56]             512ReLU-91          [-1, 256, 56, 56]               0ResNeXtBlock-92          [-1, 256, 56, 56]               0Conv2d-93          [-1, 256, 56, 56]          65,536BatchNorm2d-94          [-1, 256, 56, 56]             512Conv2d-95          [-1, 128, 56, 56]          32,768BatchNorm2d-96          [-1, 128, 56, 56]             256ReLU-97          [-1, 128, 56, 56]               0Conv2d-98            [-1, 4, 56, 56]             144Conv2d-99            [-1, 4, 56, 56]             144Conv2d-100            [-1, 4, 56, 56]             144Conv2d-101            [-1, 4, 56, 56]             144Conv2d-102            [-1, 4, 56, 56]             144Conv2d-103            [-1, 4, 56, 56]             144Conv2d-104            [-1, 4, 56, 56]             144Conv2d-105            [-1, 4, 56, 56]             144Conv2d-106            [-1, 4, 56, 56]             144Conv2d-107            [-1, 4, 56, 56]             144Conv2d-108            [-1, 4, 56, 56]             144Conv2d-109            [-1, 4, 56, 56]             144Conv2d-110            [-1, 4, 56, 56]             144Conv2d-111            [-1, 4, 56, 56]             144Conv2d-112            [-1, 4, 56, 56]             144Conv2d-113            [-1, 4, 56, 56]             144Conv2d-114            [-1, 4, 56, 56]             144Conv2d-115            [-1, 4, 56, 56]             144Conv2d-116            [-1, 4, 56, 56]             144Conv2d-117            [-1, 4, 56, 56]             144Conv2d-118            [-1, 4, 56, 56]             144Conv2d-119            [-1, 4, 56, 56]             144Conv2d-120            [-1, 4, 56, 56]             144Conv2d-121            [-1, 4, 56, 56]             144Conv2d-122            [-1, 4, 56, 56]             144Conv2d-123            [-1, 4, 56, 56]             144Conv2d-124            [-1, 4, 56, 56]             144Conv2d-125            [-1, 4, 56, 56]             144Conv2d-126            [-1, 4, 56, 56]             144Conv2d-127            [-1, 4, 56, 56]             144Conv2d-128            [-1, 4, 56, 56]             144Conv2d-129            [-1, 4, 56, 56]             144BatchNorm2d-130          [-1, 128, 56, 56]             256ReLU-131          [-1, 128, 56, 56]               0
GroupedConvBlock-132          [-1, 128, 56, 56]               0Conv2d-133          [-1, 256, 56, 56]          32,768BatchNorm2d-134          [-1, 256, 56, 56]             512ReLU-135          [-1, 256, 56, 56]               0ResNeXtBlock-136          [-1, 256, 56, 56]               0Conv2d-137          [-1, 512, 28, 28]         131,072BatchNorm2d-138          [-1, 512, 28, 28]           1,024Conv2d-139          [-1, 256, 56, 56]          65,536BatchNorm2d-140          [-1, 256, 56, 56]             512ReLU-141          [-1, 256, 56, 56]               0Conv2d-142            [-1, 8, 28, 28]             576Conv2d-143            [-1, 8, 28, 28]             576Conv2d-144            [-1, 8, 28, 28]             576Conv2d-145            [-1, 8, 28, 28]             576Conv2d-146            [-1, 8, 28, 28]             576Conv2d-147            [-1, 8, 28, 28]             576Conv2d-148            [-1, 8, 28, 28]             576Conv2d-149            [-1, 8, 28, 28]             576Conv2d-150            [-1, 8, 28, 28]             576Conv2d-151            [-1, 8, 28, 28]             576Conv2d-152            [-1, 8, 28, 28]             576Conv2d-153            [-1, 8, 28, 28]             576Conv2d-154            [-1, 8, 28, 28]             576Conv2d-155            [-1, 8, 28, 28]             576Conv2d-156            [-1, 8, 28, 28]             576Conv2d-157            [-1, 8, 28, 28]             576Conv2d-158            [-1, 8, 28, 28]             576Conv2d-159            [-1, 8, 28, 28]             576Conv2d-160            [-1, 8, 28, 28]             576Conv2d-161            [-1, 8, 28, 28]             576Conv2d-162            [-1, 8, 28, 28]             576Conv2d-163            [-1, 8, 28, 28]             576Conv2d-164            [-1, 8, 28, 28]             576Conv2d-165            [-1, 8, 28, 28]             576Conv2d-166            [-1, 8, 28, 28]             576Conv2d-167            [-1, 8, 28, 28]             576Conv2d-168            [-1, 8, 28, 28]             576Conv2d-169            [-1, 8, 28, 28]             576Conv2d-170            [-1, 8, 28, 28]             576Conv2d-171            [-1, 8, 28, 28]             576Conv2d-172            [-1, 8, 28, 28]             576Conv2d-173            [-1, 8, 28, 28]             576BatchNorm2d-174          [-1, 256, 28, 28]             512ReLU-175          [-1, 256, 28, 28]               0
GroupedConvBlock-176          [-1, 256, 28, 28]               0Conv2d-177          [-1, 512, 28, 28]         131,072BatchNorm2d-178          [-1, 512, 28, 28]           1,024ReLU-179          [-1, 512, 28, 28]               0ResNeXtBlock-180          [-1, 512, 28, 28]               0Conv2d-181          [-1, 512, 28, 28]         262,144BatchNorm2d-182          [-1, 512, 28, 28]           1,024Conv2d-183          [-1, 256, 28, 28]         131,072BatchNorm2d-184          [-1, 256, 28, 28]             512ReLU-185          [-1, 256, 28, 28]               0Conv2d-186            [-1, 8, 28, 28]             576Conv2d-187            [-1, 8, 28, 28]             576Conv2d-188            [-1, 8, 28, 28]             576Conv2d-189            [-1, 8, 28, 28]             576Conv2d-190            [-1, 8, 28, 28]             576Conv2d-191            [-1, 8, 28, 28]             576Conv2d-192            [-1, 8, 28, 28]             576Conv2d-193            [-1, 8, 28, 28]             576Conv2d-194            [-1, 8, 28, 28]             576Conv2d-195            [-1, 8, 28, 28]             576Conv2d-196            [-1, 8, 28, 28]             576Conv2d-197            [-1, 8, 28, 28]             576Conv2d-198            [-1, 8, 28, 28]             576Conv2d-199            [-1, 8, 28, 28]             576Conv2d-200            [-1, 8, 28, 28]             576Conv2d-201            [-1, 8, 28, 28]             576Conv2d-202            [-1, 8, 28, 28]             576Conv2d-203            [-1, 8, 28, 28]             576Conv2d-204            [-1, 8, 28, 28]             576Conv2d-205            [-1, 8, 28, 28]             576Conv2d-206            [-1, 8, 28, 28]             576Conv2d-207            [-1, 8, 28, 28]             576Conv2d-208            [-1, 8, 28, 28]             576Conv2d-209            [-1, 8, 28, 28]             576Conv2d-210            [-1, 8, 28, 28]             576Conv2d-211            [-1, 8, 28, 28]             576Conv2d-212            [-1, 8, 28, 28]             576Conv2d-213            [-1, 8, 28, 28]             576Conv2d-214            [-1, 8, 28, 28]             576Conv2d-215            [-1, 8, 28, 28]             576Conv2d-216            [-1, 8, 28, 28]             576Conv2d-217            [-1, 8, 28, 28]             576BatchNorm2d-218          [-1, 256, 28, 28]             512ReLU-219          [-1, 256, 28, 28]               0
GroupedConvBlock-220          [-1, 256, 28, 28]               0Conv2d-221          [-1, 512, 28, 28]         131,072BatchNorm2d-222          [-1, 512, 28, 28]           1,024ReLU-223          [-1, 512, 28, 28]               0ResNeXtBlock-224          [-1, 512, 28, 28]               0Conv2d-225          [-1, 512, 28, 28]         262,144BatchNorm2d-226          [-1, 512, 28, 28]           1,024Conv2d-227          [-1, 256, 28, 28]         131,072BatchNorm2d-228          [-1, 256, 28, 28]             512ReLU-229          [-1, 256, 28, 28]               0Conv2d-230            [-1, 8, 28, 28]             576Conv2d-231            [-1, 8, 28, 28]             576Conv2d-232            [-1, 8, 28, 28]             576Conv2d-233            [-1, 8, 28, 28]             576Conv2d-234            [-1, 8, 28, 28]             576Conv2d-235            [-1, 8, 28, 28]             576Conv2d-236            [-1, 8, 28, 28]             576Conv2d-237            [-1, 8, 28, 28]             576Conv2d-238            [-1, 8, 28, 28]             576Conv2d-239            [-1, 8, 28, 28]             576Conv2d-240            [-1, 8, 28, 28]             576Conv2d-241            [-1, 8, 28, 28]             576Conv2d-242            [-1, 8, 28, 28]             576Conv2d-243            [-1, 8, 28, 28]             576Conv2d-244            [-1, 8, 28, 28]             576Conv2d-245            [-1, 8, 28, 28]             576Conv2d-246            [-1, 8, 28, 28]             576Conv2d-247            [-1, 8, 28, 28]             576Conv2d-248            [-1, 8, 28, 28]             576Conv2d-249            [-1, 8, 28, 28]             576Conv2d-250            [-1, 8, 28, 28]             576Conv2d-251            [-1, 8, 28, 28]             576Conv2d-252            [-1, 8, 28, 28]             576Conv2d-253            [-1, 8, 28, 28]             576Conv2d-254            [-1, 8, 28, 28]             576Conv2d-255            [-1, 8, 28, 28]             576Conv2d-256            [-1, 8, 28, 28]             576Conv2d-257            [-1, 8, 28, 28]             576Conv2d-258            [-1, 8, 28, 28]             576Conv2d-259            [-1, 8, 28, 28]             576Conv2d-260            [-1, 8, 28, 28]             576Conv2d-261            [-1, 8, 28, 28]             576BatchNorm2d-262          [-1, 256, 28, 28]             512ReLU-263          [-1, 256, 28, 28]               0
GroupedConvBlock-264          [-1, 256, 28, 28]               0Conv2d-265          [-1, 512, 28, 28]         131,072BatchNorm2d-266          [-1, 512, 28, 28]           1,024ReLU-267          [-1, 512, 28, 28]               0ResNeXtBlock-268          [-1, 512, 28, 28]               0Conv2d-269          [-1, 512, 28, 28]         262,144BatchNorm2d-270          [-1, 512, 28, 28]           1,024Conv2d-271          [-1, 256, 28, 28]         131,072BatchNorm2d-272          [-1, 256, 28, 28]             512ReLU-273          [-1, 256, 28, 28]               0Conv2d-274            [-1, 8, 28, 28]             576Conv2d-275            [-1, 8, 28, 28]             576Conv2d-276            [-1, 8, 28, 28]             576Conv2d-277            [-1, 8, 28, 28]             576Conv2d-278            [-1, 8, 28, 28]             576Conv2d-279            [-1, 8, 28, 28]             576Conv2d-280            [-1, 8, 28, 28]             576Conv2d-281            [-1, 8, 28, 28]             576Conv2d-282            [-1, 8, 28, 28]             576Conv2d-283            [-1, 8, 28, 28]             576Conv2d-284            [-1, 8, 28, 28]             576Conv2d-285            [-1, 8, 28, 28]             576Conv2d-286            [-1, 8, 28, 28]             576Conv2d-287            [-1, 8, 28, 28]             576Conv2d-288            [-1, 8, 28, 28]             576Conv2d-289            [-1, 8, 28, 28]             576Conv2d-290            [-1, 8, 28, 28]             576Conv2d-291            [-1, 8, 28, 28]             576Conv2d-292            [-1, 8, 28, 28]             576Conv2d-293            [-1, 8, 28, 28]             576Conv2d-294            [-1, 8, 28, 28]             576Conv2d-295            [-1, 8, 28, 28]             576Conv2d-296            [-1, 8, 28, 28]             576Conv2d-297            [-1, 8, 28, 28]             576Conv2d-298            [-1, 8, 28, 28]             576Conv2d-299            [-1, 8, 28, 28]             576Conv2d-300            [-1, 8, 28, 28]             576Conv2d-301            [-1, 8, 28, 28]             576Conv2d-302            [-1, 8, 28, 28]             576Conv2d-303            [-1, 8, 28, 28]             576Conv2d-304            [-1, 8, 28, 28]             576Conv2d-305            [-1, 8, 28, 28]             576BatchNorm2d-306          [-1, 256, 28, 28]             512ReLU-307          [-1, 256, 28, 28]               0
GroupedConvBlock-308          [-1, 256, 28, 28]               0Conv2d-309          [-1, 512, 28, 28]         131,072BatchNorm2d-310          [-1, 512, 28, 28]           1,024ReLU-311          [-1, 512, 28, 28]               0ResNeXtBlock-312          [-1, 512, 28, 28]               0Conv2d-313         [-1, 1024, 14, 14]         524,288BatchNorm2d-314         [-1, 1024, 14, 14]           2,048Conv2d-315          [-1, 512, 28, 28]         262,144BatchNorm2d-316          [-1, 512, 28, 28]           1,024ReLU-317          [-1, 512, 28, 28]               0Conv2d-318           [-1, 16, 14, 14]           2,304Conv2d-319           [-1, 16, 14, 14]           2,304Conv2d-320           [-1, 16, 14, 14]           2,304Conv2d-321           [-1, 16, 14, 14]           2,304Conv2d-322           [-1, 16, 14, 14]           2,304Conv2d-323           [-1, 16, 14, 14]           2,304Conv2d-324           [-1, 16, 14, 14]           2,304Conv2d-325           [-1, 16, 14, 14]           2,304Conv2d-326           [-1, 16, 14, 14]           2,304Conv2d-327           [-1, 16, 14, 14]           2,304Conv2d-328           [-1, 16, 14, 14]           2,304Conv2d-329           [-1, 16, 14, 14]           2,304Conv2d-330           [-1, 16, 14, 14]           2,304Conv2d-331           [-1, 16, 14, 14]           2,304Conv2d-332           [-1, 16, 14, 14]           2,304Conv2d-333           [-1, 16, 14, 14]           2,304Conv2d-334           [-1, 16, 14, 14]           2,304Conv2d-335           [-1, 16, 14, 14]           2,304Conv2d-336           [-1, 16, 14, 14]           2,304Conv2d-337           [-1, 16, 14, 14]           2,304Conv2d-338           [-1, 16, 14, 14]           2,304Conv2d-339           [-1, 16, 14, 14]           2,304Conv2d-340           [-1, 16, 14, 14]           2,304Conv2d-341           [-1, 16, 14, 14]           2,304Conv2d-342           [-1, 16, 14, 14]           2,304Conv2d-343           [-1, 16, 14, 14]           2,304Conv2d-344           [-1, 16, 14, 14]           2,304Conv2d-345           [-1, 16, 14, 14]           2,304Conv2d-346           [-1, 16, 14, 14]           2,304Conv2d-347           [-1, 16, 14, 14]           2,304Conv2d-348           [-1, 16, 14, 14]           2,304Conv2d-349           [-1, 16, 14, 14]           2,304BatchNorm2d-350          [-1, 512, 14, 14]           1,024ReLU-351          [-1, 512, 14, 14]               0
GroupedConvBlock-352          [-1, 512, 14, 14]               0Conv2d-353         [-1, 1024, 14, 14]         524,288BatchNorm2d-354         [-1, 1024, 14, 14]           2,048ReLU-355         [-1, 1024, 14, 14]               0ResNeXtBlock-356         [-1, 1024, 14, 14]               0Conv2d-357         [-1, 1024, 14, 14]       1,048,576BatchNorm2d-358         [-1, 1024, 14, 14]           2,048Conv2d-359          [-1, 512, 14, 14]         524,288BatchNorm2d-360          [-1, 512, 14, 14]           1,024ReLU-361          [-1, 512, 14, 14]               0Conv2d-362           [-1, 16, 14, 14]           2,304Conv2d-363           [-1, 16, 14, 14]           2,304Conv2d-364           [-1, 16, 14, 14]           2,304Conv2d-365           [-1, 16, 14, 14]           2,304Conv2d-366           [-1, 16, 14, 14]           2,304Conv2d-367           [-1, 16, 14, 14]           2,304Conv2d-368           [-1, 16, 14, 14]           2,304Conv2d-369           [-1, 16, 14, 14]           2,304Conv2d-370           [-1, 16, 14, 14]           2,304Conv2d-371           [-1, 16, 14, 14]           2,304Conv2d-372           [-1, 16, 14, 14]           2,304Conv2d-373           [-1, 16, 14, 14]           2,304Conv2d-374           [-1, 16, 14, 14]           2,304Conv2d-375           [-1, 16, 14, 14]           2,304Conv2d-376           [-1, 16, 14, 14]           2,304Conv2d-377           [-1, 16, 14, 14]           2,304Conv2d-378           [-1, 16, 14, 14]           2,304Conv2d-379           [-1, 16, 14, 14]           2,304Conv2d-380           [-1, 16, 14, 14]           2,304Conv2d-381           [-1, 16, 14, 14]           2,304Conv2d-382           [-1, 16, 14, 14]           2,304Conv2d-383           [-1, 16, 14, 14]           2,304Conv2d-384           [-1, 16, 14, 14]           2,304Conv2d-385           [-1, 16, 14, 14]           2,304Conv2d-386           [-1, 16, 14, 14]           2,304Conv2d-387           [-1, 16, 14, 14]           2,304Conv2d-388           [-1, 16, 14, 14]           2,304Conv2d-389           [-1, 16, 14, 14]           2,304Conv2d-390           [-1, 16, 14, 14]           2,304Conv2d-391           [-1, 16, 14, 14]           2,304Conv2d-392           [-1, 16, 14, 14]           2,304Conv2d-393           [-1, 16, 14, 14]           2,304BatchNorm2d-394          [-1, 512, 14, 14]           1,024ReLU-395          [-1, 512, 14, 14]               0
GroupedConvBlock-396          [-1, 512, 14, 14]               0Conv2d-397         [-1, 1024, 14, 14]         524,288BatchNorm2d-398         [-1, 1024, 14, 14]           2,048ReLU-399         [-1, 1024, 14, 14]               0ResNeXtBlock-400         [-1, 1024, 14, 14]               0Conv2d-401         [-1, 1024, 14, 14]       1,048,576BatchNorm2d-402         [-1, 1024, 14, 14]           2,048Conv2d-403          [-1, 512, 14, 14]         524,288BatchNorm2d-404          [-1, 512, 14, 14]           1,024ReLU-405          [-1, 512, 14, 14]               0Conv2d-406           [-1, 16, 14, 14]           2,304Conv2d-407           [-1, 16, 14, 14]           2,304Conv2d-408           [-1, 16, 14, 14]           2,304Conv2d-409           [-1, 16, 14, 14]           2,304Conv2d-410           [-1, 16, 14, 14]           2,304Conv2d-411           [-1, 16, 14, 14]           2,304Conv2d-412           [-1, 16, 14, 14]           2,304Conv2d-413           [-1, 16, 14, 14]           2,304Conv2d-414           [-1, 16, 14, 14]           2,304Conv2d-415           [-1, 16, 14, 14]           2,304Conv2d-416           [-1, 16, 14, 14]           2,304Conv2d-417           [-1, 16, 14, 14]           2,304Conv2d-418           [-1, 16, 14, 14]           2,304Conv2d-419           [-1, 16, 14, 14]           2,304Conv2d-420           [-1, 16, 14, 14]           2,304Conv2d-421           [-1, 16, 14, 14]           2,304Conv2d-422           [-1, 16, 14, 14]           2,304Conv2d-423           [-1, 16, 14, 14]           2,304Conv2d-424           [-1, 16, 14, 14]           2,304Conv2d-425           [-1, 16, 14, 14]           2,304Conv2d-426           [-1, 16, 14, 14]           2,304Conv2d-427           [-1, 16, 14, 14]           2,304Conv2d-428           [-1, 16, 14, 14]           2,304Conv2d-429           [-1, 16, 14, 14]           2,304Conv2d-430           [-1, 16, 14, 14]           2,304Conv2d-431           [-1, 16, 14, 14]           2,304Conv2d-432           [-1, 16, 14, 14]           2,304Conv2d-433           [-1, 16, 14, 14]           2,304Conv2d-434           [-1, 16, 14, 14]           2,304Conv2d-435           [-1, 16, 14, 14]           2,304Conv2d-436           [-1, 16, 14, 14]           2,304Conv2d-437           [-1, 16, 14, 14]           2,304BatchNorm2d-438          [-1, 512, 14, 14]           1,024ReLU-439          [-1, 512, 14, 14]               0
GroupedConvBlock-440          [-1, 512, 14, 14]               0Conv2d-441         [-1, 1024, 14, 14]         524,288BatchNorm2d-442         [-1, 1024, 14, 14]           2,048ReLU-443         [-1, 1024, 14, 14]               0ResNeXtBlock-444         [-1, 1024, 14, 14]               0Conv2d-445         [-1, 1024, 14, 14]       1,048,576BatchNorm2d-446         [-1, 1024, 14, 14]           2,048Conv2d-447          [-1, 512, 14, 14]         524,288BatchNorm2d-448          [-1, 512, 14, 14]           1,024ReLU-449          [-1, 512, 14, 14]               0Conv2d-450           [-1, 16, 14, 14]           2,304Conv2d-451           [-1, 16, 14, 14]           2,304Conv2d-452           [-1, 16, 14, 14]           2,304Conv2d-453           [-1, 16, 14, 14]           2,304Conv2d-454           [-1, 16, 14, 14]           2,304Conv2d-455           [-1, 16, 14, 14]           2,304Conv2d-456           [-1, 16, 14, 14]           2,304Conv2d-457           [-1, 16, 14, 14]           2,304Conv2d-458           [-1, 16, 14, 14]           2,304Conv2d-459           [-1, 16, 14, 14]           2,304Conv2d-460           [-1, 16, 14, 14]           2,304Conv2d-461           [-1, 16, 14, 14]           2,304Conv2d-462           [-1, 16, 14, 14]           2,304Conv2d-463           [-1, 16, 14, 14]           2,304Conv2d-464           [-1, 16, 14, 14]           2,304Conv2d-465           [-1, 16, 14, 14]           2,304Conv2d-466           [-1, 16, 14, 14]           2,304Conv2d-467           [-1, 16, 14, 14]           2,304Conv2d-468           [-1, 16, 14, 14]           2,304Conv2d-469           [-1, 16, 14, 14]           2,304Conv2d-470           [-1, 16, 14, 14]           2,304Conv2d-471           [-1, 16, 14, 14]           2,304Conv2d-472           [-1, 16, 14, 14]           2,304Conv2d-473           [-1, 16, 14, 14]           2,304Conv2d-474           [-1, 16, 14, 14]           2,304Conv2d-475           [-1, 16, 14, 14]           2,304Conv2d-476           [-1, 16, 14, 14]           2,304Conv2d-477           [-1, 16, 14, 14]           2,304Conv2d-478           [-1, 16, 14, 14]           2,304Conv2d-479           [-1, 16, 14, 14]           2,304Conv2d-480           [-1, 16, 14, 14]           2,304Conv2d-481           [-1, 16, 14, 14]           2,304BatchNorm2d-482          [-1, 512, 14, 14]           1,024ReLU-483          [-1, 512, 14, 14]               0
GroupedConvBlock-484          [-1, 512, 14, 14]               0Conv2d-485         [-1, 1024, 14, 14]         524,288BatchNorm2d-486         [-1, 1024, 14, 14]           2,048ReLU-487         [-1, 1024, 14, 14]               0ResNeXtBlock-488         [-1, 1024, 14, 14]               0Conv2d-489         [-1, 1024, 14, 14]       1,048,576BatchNorm2d-490         [-1, 1024, 14, 14]           2,048Conv2d-491          [-1, 512, 14, 14]         524,288BatchNorm2d-492          [-1, 512, 14, 14]           1,024ReLU-493          [-1, 512, 14, 14]               0Conv2d-494           [-1, 16, 14, 14]           2,304Conv2d-495           [-1, 16, 14, 14]           2,304Conv2d-496           [-1, 16, 14, 14]           2,304Conv2d-497           [-1, 16, 14, 14]           2,304Conv2d-498           [-1, 16, 14, 14]           2,304Conv2d-499           [-1, 16, 14, 14]           2,304Conv2d-500           [-1, 16, 14, 14]           2,304Conv2d-501           [-1, 16, 14, 14]           2,304Conv2d-502           [-1, 16, 14, 14]           2,304Conv2d-503           [-1, 16, 14, 14]           2,304Conv2d-504           [-1, 16, 14, 14]           2,304Conv2d-505           [-1, 16, 14, 14]           2,304Conv2d-506           [-1, 16, 14, 14]           2,304Conv2d-507           [-1, 16, 14, 14]           2,304Conv2d-508           [-1, 16, 14, 14]           2,304Conv2d-509           [-1, 16, 14, 14]           2,304Conv2d-510           [-1, 16, 14, 14]           2,304Conv2d-511           [-1, 16, 14, 14]           2,304Conv2d-512           [-1, 16, 14, 14]           2,304Conv2d-513           [-1, 16, 14, 14]           2,304Conv2d-514           [-1, 16, 14, 14]           2,304Conv2d-515           [-1, 16, 14, 14]           2,304Conv2d-516           [-1, 16, 14, 14]           2,304Conv2d-517           [-1, 16, 14, 14]           2,304Conv2d-518           [-1, 16, 14, 14]           2,304Conv2d-519           [-1, 16, 14, 14]           2,304Conv2d-520           [-1, 16, 14, 14]           2,304Conv2d-521           [-1, 16, 14, 14]           2,304Conv2d-522           [-1, 16, 14, 14]           2,304Conv2d-523           [-1, 16, 14, 14]           2,304Conv2d-524           [-1, 16, 14, 14]           2,304Conv2d-525           [-1, 16, 14, 14]           2,304BatchNorm2d-526          [-1, 512, 14, 14]           1,024ReLU-527          [-1, 512, 14, 14]               0
GroupedConvBlock-528          [-1, 512, 14, 14]               0Conv2d-529         [-1, 1024, 14, 14]         524,288BatchNorm2d-530         [-1, 1024, 14, 14]           2,048ReLU-531         [-1, 1024, 14, 14]               0ResNeXtBlock-532         [-1, 1024, 14, 14]               0Conv2d-533         [-1, 1024, 14, 14]       1,048,576BatchNorm2d-534         [-1, 1024, 14, 14]           2,048Conv2d-535          [-1, 512, 14, 14]         524,288BatchNorm2d-536          [-1, 512, 14, 14]           1,024ReLU-537          [-1, 512, 14, 14]               0Conv2d-538           [-1, 16, 14, 14]           2,304Conv2d-539           [-1, 16, 14, 14]           2,304Conv2d-540           [-1, 16, 14, 14]           2,304Conv2d-541           [-1, 16, 14, 14]           2,304Conv2d-542           [-1, 16, 14, 14]           2,304Conv2d-543           [-1, 16, 14, 14]           2,304Conv2d-544           [-1, 16, 14, 14]           2,304Conv2d-545           [-1, 16, 14, 14]           2,304Conv2d-546           [-1, 16, 14, 14]           2,304Conv2d-547           [-1, 16, 14, 14]           2,304Conv2d-548           [-1, 16, 14, 14]           2,304Conv2d-549           [-1, 16, 14, 14]           2,304Conv2d-550           [-1, 16, 14, 14]           2,304Conv2d-551           [-1, 16, 14, 14]           2,304Conv2d-552           [-1, 16, 14, 14]           2,304Conv2d-553           [-1, 16, 14, 14]           2,304Conv2d-554           [-1, 16, 14, 14]           2,304Conv2d-555           [-1, 16, 14, 14]           2,304Conv2d-556           [-1, 16, 14, 14]           2,304Conv2d-557           [-1, 16, 14, 14]           2,304Conv2d-558           [-1, 16, 14, 14]           2,304Conv2d-559           [-1, 16, 14, 14]           2,304Conv2d-560           [-1, 16, 14, 14]           2,304Conv2d-561           [-1, 16, 14, 14]           2,304Conv2d-562           [-1, 16, 14, 14]           2,304Conv2d-563           [-1, 16, 14, 14]           2,304Conv2d-564           [-1, 16, 14, 14]           2,304Conv2d-565           [-1, 16, 14, 14]           2,304Conv2d-566           [-1, 16, 14, 14]           2,304Conv2d-567           [-1, 16, 14, 14]           2,304Conv2d-568           [-1, 16, 14, 14]           2,304Conv2d-569           [-1, 16, 14, 14]           2,304BatchNorm2d-570          [-1, 512, 14, 14]           1,024ReLU-571          [-1, 512, 14, 14]               0
GroupedConvBlock-572          [-1, 512, 14, 14]               0Conv2d-573         [-1, 1024, 14, 14]         524,288BatchNorm2d-574         [-1, 1024, 14, 14]           2,048ReLU-575         [-1, 1024, 14, 14]               0ResNeXtBlock-576         [-1, 1024, 14, 14]               0Conv2d-577           [-1, 2048, 7, 7]       2,097,152BatchNorm2d-578           [-1, 2048, 7, 7]           4,096Conv2d-579         [-1, 1024, 14, 14]       1,048,576BatchNorm2d-580         [-1, 1024, 14, 14]           2,048ReLU-581         [-1, 1024, 14, 14]               0Conv2d-582             [-1, 32, 7, 7]           9,216Conv2d-583             [-1, 32, 7, 7]           9,216Conv2d-584             [-1, 32, 7, 7]           9,216Conv2d-585             [-1, 32, 7, 7]           9,216Conv2d-586             [-1, 32, 7, 7]           9,216Conv2d-587             [-1, 32, 7, 7]           9,216Conv2d-588             [-1, 32, 7, 7]           9,216Conv2d-589             [-1, 32, 7, 7]           9,216Conv2d-590             [-1, 32, 7, 7]           9,216Conv2d-591             [-1, 32, 7, 7]           9,216Conv2d-592             [-1, 32, 7, 7]           9,216Conv2d-593             [-1, 32, 7, 7]           9,216Conv2d-594             [-1, 32, 7, 7]           9,216Conv2d-595             [-1, 32, 7, 7]           9,216Conv2d-596             [-1, 32, 7, 7]           9,216Conv2d-597             [-1, 32, 7, 7]           9,216Conv2d-598             [-1, 32, 7, 7]           9,216Conv2d-599             [-1, 32, 7, 7]           9,216Conv2d-600             [-1, 32, 7, 7]           9,216Conv2d-601             [-1, 32, 7, 7]           9,216Conv2d-602             [-1, 32, 7, 7]           9,216Conv2d-603             [-1, 32, 7, 7]           9,216Conv2d-604             [-1, 32, 7, 7]           9,216Conv2d-605             [-1, 32, 7, 7]           9,216Conv2d-606             [-1, 32, 7, 7]           9,216Conv2d-607             [-1, 32, 7, 7]           9,216Conv2d-608             [-1, 32, 7, 7]           9,216Conv2d-609             [-1, 32, 7, 7]           9,216Conv2d-610             [-1, 32, 7, 7]           9,216Conv2d-611             [-1, 32, 7, 7]           9,216Conv2d-612             [-1, 32, 7, 7]           9,216Conv2d-613             [-1, 32, 7, 7]           9,216BatchNorm2d-614           [-1, 1024, 7, 7]           2,048ReLU-615           [-1, 1024, 7, 7]               0
GroupedConvBlock-616           [-1, 1024, 7, 7]               0Conv2d-617           [-1, 2048, 7, 7]       2,097,152BatchNorm2d-618           [-1, 2048, 7, 7]           4,096ReLU-619           [-1, 2048, 7, 7]               0ResNeXtBlock-620           [-1, 2048, 7, 7]               0Conv2d-621           [-1, 2048, 7, 7]       4,194,304BatchNorm2d-622           [-1, 2048, 7, 7]           4,096Conv2d-623           [-1, 1024, 7, 7]       2,097,152BatchNorm2d-624           [-1, 1024, 7, 7]           2,048ReLU-625           [-1, 1024, 7, 7]               0Conv2d-626             [-1, 32, 7, 7]           9,216Conv2d-627             [-1, 32, 7, 7]           9,216Conv2d-628             [-1, 32, 7, 7]           9,216Conv2d-629             [-1, 32, 7, 7]           9,216Conv2d-630             [-1, 32, 7, 7]           9,216Conv2d-631             [-1, 32, 7, 7]           9,216Conv2d-632             [-1, 32, 7, 7]           9,216Conv2d-633             [-1, 32, 7, 7]           9,216Conv2d-634             [-1, 32, 7, 7]           9,216Conv2d-635             [-1, 32, 7, 7]           9,216Conv2d-636             [-1, 32, 7, 7]           9,216Conv2d-637             [-1, 32, 7, 7]           9,216Conv2d-638             [-1, 32, 7, 7]           9,216Conv2d-639             [-1, 32, 7, 7]           9,216Conv2d-640             [-1, 32, 7, 7]           9,216Conv2d-641             [-1, 32, 7, 7]           9,216Conv2d-642             [-1, 32, 7, 7]           9,216Conv2d-643             [-1, 32, 7, 7]           9,216Conv2d-644             [-1, 32, 7, 7]           9,216Conv2d-645             [-1, 32, 7, 7]           9,216Conv2d-646             [-1, 32, 7, 7]           9,216Conv2d-647             [-1, 32, 7, 7]           9,216Conv2d-648             [-1, 32, 7, 7]           9,216Conv2d-649             [-1, 32, 7, 7]           9,216Conv2d-650             [-1, 32, 7, 7]           9,216Conv2d-651             [-1, 32, 7, 7]           9,216Conv2d-652             [-1, 32, 7, 7]           9,216Conv2d-653             [-1, 32, 7, 7]           9,216Conv2d-654             [-1, 32, 7, 7]           9,216Conv2d-655             [-1, 32, 7, 7]           9,216Conv2d-656             [-1, 32, 7, 7]           9,216Conv2d-657             [-1, 32, 7, 7]           9,216BatchNorm2d-658           [-1, 1024, 7, 7]           2,048ReLU-659           [-1, 1024, 7, 7]               0
GroupedConvBlock-660           [-1, 1024, 7, 7]               0Conv2d-661           [-1, 2048, 7, 7]       2,097,152BatchNorm2d-662           [-1, 2048, 7, 7]           4,096ReLU-663           [-1, 2048, 7, 7]               0ResNeXtBlock-664           [-1, 2048, 7, 7]               0Conv2d-665           [-1, 2048, 7, 7]       4,194,304BatchNorm2d-666           [-1, 2048, 7, 7]           4,096Conv2d-667           [-1, 1024, 7, 7]       2,097,152BatchNorm2d-668           [-1, 1024, 7, 7]           2,048ReLU-669           [-1, 1024, 7, 7]               0Conv2d-670             [-1, 32, 7, 7]           9,216Conv2d-671             [-1, 32, 7, 7]           9,216Conv2d-672             [-1, 32, 7, 7]           9,216Conv2d-673             [-1, 32, 7, 7]           9,216Conv2d-674             [-1, 32, 7, 7]           9,216Conv2d-675             [-1, 32, 7, 7]           9,216Conv2d-676             [-1, 32, 7, 7]           9,216Conv2d-677             [-1, 32, 7, 7]           9,216Conv2d-678             [-1, 32, 7, 7]           9,216Conv2d-679             [-1, 32, 7, 7]           9,216Conv2d-680             [-1, 32, 7, 7]           9,216Conv2d-681             [-1, 32, 7, 7]           9,216Conv2d-682             [-1, 32, 7, 7]           9,216Conv2d-683             [-1, 32, 7, 7]           9,216Conv2d-684             [-1, 32, 7, 7]           9,216Conv2d-685             [-1, 32, 7, 7]           9,216Conv2d-686             [-1, 32, 7, 7]           9,216Conv2d-687             [-1, 32, 7, 7]           9,216Conv2d-688             [-1, 32, 7, 7]           9,216Conv2d-689             [-1, 32, 7, 7]           9,216Conv2d-690             [-1, 32, 7, 7]           9,216Conv2d-691             [-1, 32, 7, 7]           9,216Conv2d-692             [-1, 32, 7, 7]           9,216Conv2d-693             [-1, 32, 7, 7]           9,216Conv2d-694             [-1, 32, 7, 7]           9,216Conv2d-695             [-1, 32, 7, 7]           9,216Conv2d-696             [-1, 32, 7, 7]           9,216Conv2d-697             [-1, 32, 7, 7]           9,216Conv2d-698             [-1, 32, 7, 7]           9,216Conv2d-699             [-1, 32, 7, 7]           9,216Conv2d-700             [-1, 32, 7, 7]           9,216Conv2d-701             [-1, 32, 7, 7]           9,216BatchNorm2d-702           [-1, 1024, 7, 7]           2,048ReLU-703           [-1, 1024, 7, 7]               0
GroupedConvBlock-704           [-1, 1024, 7, 7]               0Conv2d-705           [-1, 2048, 7, 7]       2,097,152BatchNorm2d-706           [-1, 2048, 7, 7]           4,096ReLU-707           [-1, 2048, 7, 7]               0ResNeXtBlock-708           [-1, 2048, 7, 7]               0
AdaptiveAvgPool2d-709           [-1, 2048, 1, 1]               0Linear-710                    [-1, 4]           8,196
================================================================
Total params: 37,559,620
Trainable params: 37,559,620
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 444.08
Params size (MB): 143.28
Estimated Total Size (MB): 587.93
----------------------------------------------------------------
# 定义损失函数和优化器
loss_fn = nn.CrossEntropyLoss()learn_rate = 5e-4
lambda1 = lambda epc: (0.92 ** (epc // 2))optimizer = torch.optim.Adam(model.parameters(), lr=learn_rate)
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda1)  # 选定调整方法# 训练函数
def train(dataloader,model,loss_fn,optimizer):size = len(dataloader.dataset) # 训练集大小num_batches = len(dataloader) # 批次数目train_loss,train_acc = 0,0for X,y in dataloader:X,y = X.to(device),y.to(device)# 计算预测误差pred = model(X)loss = loss_fn(pred,y)# 反向传播optimizer.zero_grad()loss.backward()optimizer.step()# 记录acc与losstrain_acc += (pred.argmax(1)==y).type(torch.float).sum().item()train_loss += loss.item()train_acc /= sizetrain_loss /= num_batchesreturn train_acc,train_loss# 测试函数
def test(dataloader,model,loss_fn):size = len(dataloader.dataset)num_batches = len(dataloader)test_acc,test_loss = 0,0with torch.no_grad():for X,y in dataloader:X,y = X.to(device),y.to(device)# 计算losspred = model(X)loss = loss_fn(pred,y)test_acc += (pred.argmax(1)==y).type(torch.float).sum().item()test_loss += loss.item()test_acc /= sizetest_loss /= num_batchesreturn test_acc,test_loss
import copyepochs = 20train_acc = []
train_loss = []
test_acc = []
test_loss = []best_acc = 0.0# 检查 GPU 可用性并打印设备信息
if torch.cuda.is_available():for i in range(torch.cuda.device_count()):print(f"GPU {i}: {torch.cuda.get_device_name(i)}")print(f"Initial Memory Allocated: {torch.cuda.memory_allocated(i)/1024**2:.2f} MB")print(f"Initial Memory Cached: {torch.cuda.memory_reserved(i)/1024**2:.2f} MB")
else:print("No GPU available. Using CPU.")# 多显卡设置 当前使用的是使用 PyTorch 自带的 DataParallel,后续如有需要可以设置为DistributedDataParallel,这是更加高效的方式
# 且多卡不一定比单卡效果就好,需要调整优化
# if torch.cuda.device_count() > 1:
#     print(f"Using {torch.cuda.device_count()} GPUs")
#     model = nn.DataParallel(model)
# model = model.to('cuda')for epoch in range(epochs):# 更新学习率——使用自定义学习率时使用# adjust_learning_rate(optimizer,epoch,learn_rate)model.train()epoch_train_acc,epoch_train_loss = train(train_dl,model,loss_fn,optimizer)scheduler.step() # 更新学习率——调用官方动态学习率时使用model.eval()epoch_test_acc,epoch_test_loss = test(test_dl,model,loss_fn)# 保存最佳模型到 best_modelif epoch_test_acc > best_acc:best_acc = epoch_test_accbest_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))# 实时监控 GPU 状态if torch.cuda.is_available():for i in range(torch.cuda.device_count()):print(f"GPU {i} Usage:")print(f"  Memory Allocated: {torch.cuda.memory_allocated(i)/1024**2:.2f} MB")print(f"  Memory Cached: {torch.cuda.memory_reserved(i)/1024**2:.2f} MB")print(f"  Max Memory Allocated: {torch.cuda.max_memory_allocated(i)/1024**2:.2f} MB")print(f"  Max Memory Cached: {torch.cuda.max_memory_reserved(i)/1024**2:.2f} MB")print('Done','best_acc: ',best_acc)
GPU 0: NVIDIA GeForce RTX 2070
Initial Memory Allocated: 151.86 MB
Initial Memory Cached: 606.00 MB
Epoch: 1,Train_acc:36.5%,Train_loss:1.495,Test_acc:48.7%,Test_loss:1.233,Lr:5.00E-04
GPU 0 Usage:Memory Allocated: 743.27 MBMemory Cached: 2194.00 MBMax Memory Allocated: 1733.31 MBMax Memory Cached: 2194.00 MB
Epoch: 2,Train_acc:50.4%,Train_loss:1.141,Test_acc:58.4%,Test_loss:0.928,Lr:4.60E-04
GPU 0 Usage:Memory Allocated: 738.20 MBMemory Cached: 2208.00 MBMax Memory Allocated: 1882.58 MBMax Memory Cached: 2208.00 MB
Epoch: 3,Train_acc:56.4%,Train_loss:1.019,Test_acc:61.9%,Test_loss:0.943,Lr:4.60E-04
GPU 0 Usage:Memory Allocated: 737.86 MBMemory Cached: 2208.00 MBMax Memory Allocated: 1883.39 MBMax Memory Cached: 2208.00 MB
Epoch: 4,Train_acc:65.9%,Train_loss:0.851,Test_acc:63.7%,Test_loss:0.890,Lr:4.23E-04
GPU 0 Usage:Memory Allocated: 737.74 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1883.39 MBMax Memory Cached: 2210.00 MB
Epoch: 5,Train_acc:72.3%,Train_loss:0.746,Test_acc:77.9%,Test_loss:0.636,Lr:4.23E-04
GPU 0 Usage:Memory Allocated: 738.58 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1884.70 MBMax Memory Cached: 2210.00 MB
Epoch: 6,Train_acc:71.9%,Train_loss:0.770,Test_acc:79.6%,Test_loss:0.512,Lr:3.89E-04
GPU 0 Usage:Memory Allocated: 737.86 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1884.70 MBMax Memory Cached: 2210.00 MB
Epoch: 7,Train_acc:73.2%,Train_loss:0.677,Test_acc:80.5%,Test_loss:0.643,Lr:3.89E-04
GPU 0 Usage:Memory Allocated: 737.42 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1884.70 MBMax Memory Cached: 2210.00 MB
Epoch: 8,Train_acc:71.9%,Train_loss:0.709,Test_acc:86.7%,Test_loss:0.476,Lr:3.58E-04
GPU 0 Usage:Memory Allocated: 737.61 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1884.70 MBMax Memory Cached: 2210.00 MB
Epoch: 9,Train_acc:73.7%,Train_loss:0.658,Test_acc:70.8%,Test_loss:0.712,Lr:3.58E-04
GPU 0 Usage:Memory Allocated: 737.95 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1884.70 MBMax Memory Cached: 2210.00 MB
Epoch:10,Train_acc:77.4%,Train_loss:0.645,Test_acc:85.0%,Test_loss:0.414,Lr:3.30E-04
GPU 0 Usage:Memory Allocated: 737.95 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1884.89 MBMax Memory Cached: 2210.00 MB
Epoch:11,Train_acc:77.7%,Train_loss:0.654,Test_acc:83.2%,Test_loss:0.527,Lr:3.30E-04
GPU 0 Usage:Memory Allocated: 737.27 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1884.89 MBMax Memory Cached: 2210.00 MB
Epoch:12,Train_acc:81.0%,Train_loss:0.561,Test_acc:87.6%,Test_loss:0.403,Lr:3.03E-04
GPU 0 Usage:Memory Allocated: 736.58 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Epoch:13,Train_acc:81.9%,Train_loss:0.482,Test_acc:81.4%,Test_loss:0.472,Lr:3.03E-04
GPU 0 Usage:Memory Allocated: 736.74 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Epoch:14,Train_acc:81.9%,Train_loss:0.539,Test_acc:85.0%,Test_loss:0.380,Lr:2.79E-04
GPU 0 Usage:Memory Allocated: 737.14 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Epoch:15,Train_acc:83.0%,Train_loss:0.496,Test_acc:86.7%,Test_loss:0.369,Lr:2.79E-04
GPU 0 Usage:Memory Allocated: 741.05 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Epoch:16,Train_acc:80.8%,Train_loss:0.488,Test_acc:86.7%,Test_loss:0.449,Lr:2.57E-04
GPU 0 Usage:Memory Allocated: 741.00 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Epoch:17,Train_acc:82.5%,Train_loss:0.477,Test_acc:83.2%,Test_loss:0.471,Lr:2.57E-04
GPU 0 Usage:Memory Allocated: 741.89 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Epoch:18,Train_acc:86.5%,Train_loss:0.422,Test_acc:84.1%,Test_loss:0.513,Lr:2.36E-04
GPU 0 Usage:Memory Allocated: 741.00 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Epoch:19,Train_acc:81.6%,Train_loss:0.494,Test_acc:82.3%,Test_loss:0.414,Lr:2.36E-04
GPU 0 Usage:Memory Allocated: 741.89 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Epoch:20,Train_acc:83.8%,Train_loss:0.423,Test_acc:91.2%,Test_loss:0.276,Lr:2.17E-04
GPU 0 Usage:Memory Allocated: 741.30 MBMemory Cached: 2210.00 MBMax Memory Allocated: 1886.42 MBMax Memory Cached: 2210.00 MB
Done best_acc:  0.911504424778761
# 结果可视化
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 = 'Test Accuracy')
plt.plot(epochs_range,test_loss,label = 'Test Loss')
plt.legend(loc = 'lower right')
plt.title('Training and validation Loss')
plt.show()# 自定义模型保存
# 状态字典保存
torch.save(model.state_dict(),r'C:\Users\11054\Desktop\kLearning\J6_learning\J6_ResNeXt50.pth') # 仅保存状态字典# 定义模型用来加载参数
best_model = ResNeXt50(num_classes=len(class_names)).to(device)best_model.load_state_dict(torch.load(r'C:\Users\11054\Desktop\kLearning\J6_learning\J6_ResNeXt50.pth')) # 加载状态字典到模型

在这里插入图片描述

<All keys matched successfully>
# 预测指定模型
from PIL import Image
import torchvision.transforms as transformsclasses = list(total_data.class_to_idx) # classes = list(total_data.class_to_idx)def predict_one_image(image_path,model,transform,classes):test_img = Image.open(image_path).convert('RGB')test_img = transform(test_img)img = test_img.to(device).unsqueeze(0)model.eval()output = model(img)print(output) # 观察模型预测结果的输出数据_,pred = torch.max(output,1)pred_class = classes[pred]print(f'预测结果是:{pred_class}')# 预测训练集中的某张照片
predict_one_image(image_path=r'C:\Users\11054\Desktop\kLearning\J1_learning\bird_photos\Bananaquit\001.jpg',model = model,transform = test_transforms,classes = classes)
tensor([[ 1.1780, -1.3850,  1.1631, -1.2493]], device='cuda:0',grad_fn=<AddmmBackward0>)
预测结果是:Bananaquit

个人总结

ResNeXt-50特点

  1. 分组卷积(Grouped Convolutions):

ResNeXt 引入了分组卷积的概念,将输入特征图分成多个组(group),每个组独立进行卷积操作,然后将结果拼接起来。
2. 残差连接(Residual Connections):

与 ResNet 类似,ResNeXt 也使用了残差连接,允许信息在网络中直接跳过某些层,从而缓解梯度消失问题,并使得训练更深的网络变得可行。
ResNeXt-50 中的每个残差块(residual block)由多个分组卷积层组成,每个分组卷积层都包含 1x1、3x3、1x1 三个卷积核。
3. 网络结构:

ResNeXt-50 的网络结构主要由几个阶段组成,每个阶段包含多个残差块。典型的结构如下:
阶段 1:7x7 卷积层 + 3x3 最大池化层
阶段 2:包含 3 个残差块,每个残差块的输出通道数为 256
阶段 3:包含 4 个残差块,每个残差块的输出通道数为 512
阶段 4:包含 6 个残差块,每个残差块的输出通道数为 1024
阶段 5:包含 3 个残差块,每个残差块的输出通道数为 2048
最后是全局平均池化层和全连接层,用于分类任务。
4. 性能提升:

ResNeXt-50 通过增加基数(cardinality)而不显著增加模型参数的数量,实现了比 ResNet-50 更好的性能。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com