J2学习打卡

  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊

ResNet50V2

import torch
import torch.nn as nn
import torch.nn.functional as F

class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None, norm_layer=None):
        super(Bottleneck, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
        self.bn1 = norm_layer(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
                               padding=1, bias=False)
        self.bn2 = norm_layer(planes)
        self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
        self.bn3 = norm_layer(planes * self.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)

        return out

class ResNet50V2(nn.Module):
    def __init__(self, block, layers, num_classes=1000, norm_layer=None):
        super(ResNet50V2, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d

        self.inplanes = 64
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = norm_layer(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.layer1 = self._make_layer(block, 64, layers[0], norm_layer=norm_layer)
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2, norm_layer=norm_layer)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2, norm_layer=norm_layer)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=1, norm_layer=norm_layer)

        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)

    def _make_layer(self, block, planes, blocks, stride=1, norm_layer=None):
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        downsample = None

        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
                norm_layer(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample, norm_layer))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.inplanes, planes, norm_layer=norm_layer))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x

def resnet50v2(num_classes=1000):
    return ResNet50V2(Bottleneck, [3, 4, 6, 3], num_classes=num_classes)

# Example usage
model = resnet50v2()
print(model)
ResNet50V2(
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (relu): ReLU(inplace=True)
  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
  (layer1): Sequential(
    (0): Bottleneck(
      (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)
      (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)
      (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)
      (downsample): 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)
      )
    )
    (1): Bottleneck(
      (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)
      (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)
      (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)
    )
    (2): Bottleneck(
      (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)
      (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)
      (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)
    )
  )
  (layer2): Sequential(
    (0): Bottleneck(
      (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)
      (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)
      (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)
      (downsample): 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)
      )
    )
    (1): Bottleneck(
      (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)
      (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)
      (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)
    )
    (2): Bottleneck(
      (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)
      (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)
      (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)
    )
    (3): Bottleneck(
      (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)
      (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)
      (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)
    )
  )
  (layer3): Sequential(
    (0): Bottleneck(
      (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)
      (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)
      (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)
      (downsample): 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)
      )
    )
    (1): Bottleneck(
      (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)
      (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)
      (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)
    )
    (2): Bottleneck(
      (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)
      (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)
      (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)
    )
    (3): Bottleneck(
      (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)
      (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)
      (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)
    )
    (4): Bottleneck(
      (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)
      (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)
      (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)
    )
    (5): Bottleneck(
      (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)
      (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)
      (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)
    )
  )
  (layer4): Sequential(
    (0): Bottleneck(
      (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)
      (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)
      (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)
      (downsample): 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)
      )
    )
    (1): Bottleneck(
      (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)
      (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)
      (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)
    )
    (2): Bottleneck(
      (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)
      (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)
      (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)
    )
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (fc): Linear(in_features=2048, out_features=1000, bias=True)
)
# 数据预处理和加载
import torch
from torch import nn, optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms, models
import matplotlib.pyplot as plt
import numpy as np
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
# Example of initialization
model = ResNet50V2(Bottleneck, [3, 4, 6, 3],num_classes=4)
# model = models.resnet50(pretrained=True)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, len(class_names))
model = model.to(device)
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练模型
epochs = 100
train_losses, val_losses = [], []
train_acc, val_acc = [], []

best_val_loss = float('inf')
best_model_wts = None  # 用于保存最好的模型权重
for epoch in range(epochs):
    # Training
    model.train()
    running_loss, running_corrects = 0.0, 0

    for inputs, labels in train_loader:
        inputs, labels = inputs.to(device), labels.to(device)

        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item() * inputs.size(0)
        _, preds = torch.max(outputs, 1)
        running_corrects += torch.sum(preds == labels.data)

    epoch_loss = running_loss / train_size
    epoch_acc = running_corrects.double() / train_size

    train_losses.append(epoch_loss)
    train_acc.append(epoch_acc.item())

    # Validation
    model.eval()
    val_running_loss, val_running_corrects = 0.0, 0

    with torch.no_grad():
        for inputs, labels in val_loader:
            inputs, labels = inputs.to(device), labels.to(device)

            outputs = model(inputs)
            loss = criterion(outputs, labels)

            val_running_loss += loss.item() * inputs.size(0)
            _, preds = torch.max(outputs, 1)
            val_running_corrects += torch.sum(preds == labels.data)

    val_epoch_loss = val_running_loss / val_size
    val_epoch_acc = val_running_corrects.double() / val_size

    val_losses.append(val_epoch_loss)
    val_acc.append(val_epoch_acc.item())
    if val_epoch_loss < best_val_loss:
        best_val_loss = val_epoch_loss
        best_model_wts = model.state_dict()  # 记录当前模型的权重
    print(
        f'Epoch {epoch}/{epochs - 1}, Train Loss: {epoch_loss:.4f}, Train Acc: {epoch_acc:.4f}, Val Loss: {val_epoch_loss:.4f}, Val Acc: {val_epoch_acc:.4f}')

# 在训练结束后,加载最优的模型权重
model.load_state_dict(best_model_wts)
Epoch 0/99, Train Loss: 1.5081, Train Acc: 0.4049, Val Loss: 1.1780, Val Acc: 0.4867
Epoch 1/99, Train Loss: 1.1824, Train Acc: 0.5243, Val Loss: 1.1830, Val Acc: 0.5310
Epoch 2/99, Train Loss: 1.0909, Train Acc: 0.5708, Val Loss: 1.6719, Val Acc: 0.4425
Epoch 3/99, Train Loss: 1.0141, Train Acc: 0.6217, Val Loss: 1.3627, Val Acc: 0.4867
Epoch 4/99, Train Loss: 0.8892, Train Acc: 0.6527, Val Loss: 1.1815, Val Acc: 0.6372
Epoch 5/99, Train Loss: 0.8106, Train Acc: 0.6748, Val Loss: 0.9670, Val Acc: 0.6195
Epoch 6/99, Train Loss: 0.7749, Train Acc: 0.7168, Val Loss: 0.8506, Val Acc: 0.6283
Epoch 7/99, Train Loss: 0.7296, Train Acc: 0.6903, Val Loss: 1.3069, Val Acc: 0.4513
Epoch 8/99, Train Loss: 0.6890, Train Acc: 0.7389, Val Loss: 1.6562, Val Acc: 0.6195
Epoch 9/99, Train Loss: 0.6949, Train Acc: 0.7566, Val Loss: 0.7034, Val Acc: 0.7168
Epoch 10/99, Train Loss: 0.6817, Train Acc: 0.7500, Val Loss: 0.8593, Val Acc: 0.7257
Epoch 11/99, Train Loss: 0.6485, Train Acc: 0.7677, Val Loss: 0.8179, Val Acc: 0.6903
Epoch 12/99, Train Loss: 0.5581, Train Acc: 0.8097, Val Loss: 0.6367, Val Acc: 0.7434
Epoch 13/99, Train Loss: 0.4954, Train Acc: 0.8097, Val Loss: 0.6925, Val Acc: 0.7257
Epoch 14/99, Train Loss: 0.5071, Train Acc: 0.8164, Val Loss: 0.6167, Val Acc: 0.7699
Epoch 15/99, Train Loss: 0.5643, Train Acc: 0.8142, Val Loss: 0.5488, Val Acc: 0.8142
Epoch 16/99, Train Loss: 0.5602, Train Acc: 0.7942, Val Loss: 0.6716, Val Acc: 0.7611
Epoch 17/99, Train Loss: 0.4799, Train Acc: 0.8296, Val Loss: 0.5589, Val Acc: 0.7788
Epoch 18/99, Train Loss: 0.5476, Train Acc: 0.8164, Val Loss: 0.5755, Val Acc: 0.7434
Epoch 19/99, Train Loss: 0.4808, Train Acc: 0.8496, Val Loss: 0.5573, Val Acc: 0.7876
Epoch 20/99, Train Loss: 0.4338, Train Acc: 0.8296, Val Loss: 0.7486, Val Acc: 0.7788
Epoch 21/99, Train Loss: 0.4934, Train Acc: 0.7965, Val Loss: 0.5907, Val Acc: 0.7788
Epoch 22/99, Train Loss: 0.5047, Train Acc: 0.8164, Val Loss: 2.3444, Val Acc: 0.5929
Epoch 23/99, Train Loss: 0.4283, Train Acc: 0.8363, Val Loss: 0.5185, Val Acc: 0.8142
Epoch 24/99, Train Loss: 0.4079, Train Acc: 0.8518, Val Loss: 0.4883, Val Acc: 0.8407
Epoch 25/99, Train Loss: 0.4335, Train Acc: 0.8518, Val Loss: 0.8051, Val Acc: 0.7876
Epoch 26/99, Train Loss: 0.3173, Train Acc: 0.8827, Val Loss: 0.5862, Val Acc: 0.8142
Epoch 27/99, Train Loss: 0.3724, Train Acc: 0.8739, Val Loss: 0.7041, Val Acc: 0.7788
Epoch 28/99, Train Loss: 0.3117, Train Acc: 0.8850, Val Loss: 0.5650, Val Acc: 0.8053
Epoch 29/99, Train Loss: 0.3593, Train Acc: 0.8894, Val Loss: 0.5180, Val Acc: 0.8142
Epoch 30/99, Train Loss: 0.3348, Train Acc: 0.8850, Val Loss: 0.5450, Val Acc: 0.7788
Epoch 31/99, Train Loss: 0.2837, Train Acc: 0.9049, Val Loss: 0.9638, Val Acc: 0.8053
Epoch 32/99, Train Loss: 0.3975, Train Acc: 0.8584, Val Loss: 0.6834, Val Acc: 0.7876
Epoch 33/99, Train Loss: 0.4799, Train Acc: 0.8363, Val Loss: 1.0342, Val Acc: 0.6903
Epoch 34/99, Train Loss: 0.3318, Train Acc: 0.8827, Val Loss: 0.4223, Val Acc: 0.8673
Epoch 35/99, Train Loss: 0.2945, Train Acc: 0.8960, Val Loss: 0.6039, Val Acc: 0.7876
Epoch 36/99, Train Loss: 0.2797, Train Acc: 0.9004, Val Loss: 0.3947, Val Acc: 0.8407
Epoch 37/99, Train Loss: 0.3114, Train Acc: 0.8761, Val Loss: 0.3926, Val Acc: 0.8230
Epoch 38/99, Train Loss: 0.2819, Train Acc: 0.9049, Val Loss: 0.4662, Val Acc: 0.7876
Epoch 39/99, Train Loss: 0.2684, Train Acc: 0.9093, Val Loss: 0.6103, Val Acc: 0.8319
Epoch 40/99, Train Loss: 0.2565, Train Acc: 0.9159, Val Loss: 0.4893, Val Acc: 0.8230
Epoch 41/99, Train Loss: 0.2801, Train Acc: 0.9093, Val Loss: 0.3942, Val Acc: 0.8584
Epoch 42/99, Train Loss: 0.2272, Train Acc: 0.9137, Val Loss: 0.4284, Val Acc: 0.8496
Epoch 43/99, Train Loss: 0.2414, Train Acc: 0.9071, Val Loss: 0.4650, Val Acc: 0.8673
Epoch 44/99, Train Loss: 0.3233, Train Acc: 0.8960, Val Loss: 0.4346, Val Acc: 0.8053
Epoch 45/99, Train Loss: 0.3700, Train Acc: 0.8717, Val Loss: 0.5083, Val Acc: 0.8230
Epoch 46/99, Train Loss: 0.2476, Train Acc: 0.9071, Val Loss: 0.4358, Val Acc: 0.8938
Epoch 47/99, Train Loss: 0.2164, Train Acc: 0.9159, Val Loss: 0.5519, Val Acc: 0.8142
Epoch 48/99, Train Loss: 0.2091, Train Acc: 0.9159, Val Loss: 0.3499, Val Acc: 0.9027
Epoch 49/99, Train Loss: 0.2034, Train Acc: 0.9314, Val Loss: 0.3615, Val Acc: 0.8584
Epoch 50/99, Train Loss: 0.2402, Train Acc: 0.9336, Val Loss: 0.6434, Val Acc: 0.8053
Epoch 51/99, Train Loss: 0.1273, Train Acc: 0.9558, Val Loss: 0.2781, Val Acc: 0.8938
Epoch 52/99, Train Loss: 0.1549, Train Acc: 0.9580, Val Loss: 0.3524, Val Acc: 0.9115
Epoch 53/99, Train Loss: 0.2179, Train Acc: 0.9381, Val Loss: 0.3519, Val Acc: 0.8850
Epoch 54/99, Train Loss: 0.1922, Train Acc: 0.9381, Val Loss: 0.3164, Val Acc: 0.8673
Epoch 55/99, Train Loss: 0.1393, Train Acc: 0.9513, Val Loss: 0.6863, Val Acc: 0.8053
Epoch 56/99, Train Loss: 0.1997, Train Acc: 0.9469, Val Loss: 0.3548, Val Acc: 0.8584
Epoch 57/99, Train Loss: 0.1363, Train Acc: 0.9491, Val Loss: 0.5302, Val Acc: 0.8938
Epoch 58/99, Train Loss: 0.1810, Train Acc: 0.9513, Val Loss: 0.5357, Val Acc: 0.8584
Epoch 59/99, Train Loss: 0.1574, Train Acc: 0.9469, Val Loss: 0.5047, Val Acc: 0.8584
Epoch 60/99, Train Loss: 0.1661, Train Acc: 0.9403, Val Loss: 0.7250, Val Acc: 0.7345
Epoch 61/99, Train Loss: 0.1722, Train Acc: 0.9646, Val Loss: 0.4548, Val Acc: 0.8584
Epoch 62/99, Train Loss: 0.1271, Train Acc: 0.9558, Val Loss: 0.1897, Val Acc: 0.9292
Epoch 63/99, Train Loss: 0.1047, Train Acc: 0.9602, Val Loss: 0.2636, Val Acc: 0.9204
Epoch 64/99, Train Loss: 0.1882, Train Acc: 0.9403, Val Loss: 0.5540, Val Acc: 0.8584
Epoch 65/99, Train Loss: 0.1944, Train Acc: 0.9292, Val Loss: 0.3756, Val Acc: 0.8938
Epoch 66/99, Train Loss: 0.0680, Train Acc: 0.9779, Val Loss: 0.2917, Val Acc: 0.9027
Epoch 67/99, Train Loss: 0.0954, Train Acc: 0.9646, Val Loss: 0.4208, Val Acc: 0.8761
Epoch 68/99, Train Loss: 0.1136, Train Acc: 0.9469, Val Loss: 0.7063, Val Acc: 0.8407
Epoch 69/99, Train Loss: 0.0964, Train Acc: 0.9690, Val Loss: 0.3430, Val Acc: 0.8938
Epoch 70/99, Train Loss: 0.0654, Train Acc: 0.9779, Val Loss: 0.3915, Val Acc: 0.8938
Epoch 71/99, Train Loss: 0.1592, Train Acc: 0.9469, Val Loss: 0.4142, Val Acc: 0.8673
Epoch 72/99, Train Loss: 0.1646, Train Acc: 0.9447, Val Loss: 0.5199, Val Acc: 0.8407
Epoch 73/99, Train Loss: 0.2407, Train Acc: 0.9248, Val Loss: 0.5705, Val Acc: 0.8938
Epoch 74/99, Train Loss: 0.1716, Train Acc: 0.9425, Val Loss: 0.3962, Val Acc: 0.8938
Epoch 75/99, Train Loss: 0.0702, Train Acc: 0.9801, Val Loss: 0.3274, Val Acc: 0.8850
Epoch 76/99, Train Loss: 0.0804, Train Acc: 0.9757, Val Loss: 0.4674, Val Acc: 0.8761
Epoch 77/99, Train Loss: 0.0943, Train Acc: 0.9757, Val Loss: 0.4017, Val Acc: 0.8938
Epoch 78/99, Train Loss: 0.1096, Train Acc: 0.9735, Val Loss: 0.5062, Val Acc: 0.8938
Epoch 79/99, Train Loss: 0.0911, Train Acc: 0.9735, Val Loss: 0.6298, Val Acc: 0.8584
Epoch 80/99, Train Loss: 0.1131, Train Acc: 0.9558, Val Loss: 0.4371, Val Acc: 0.8584
Epoch 81/99, Train Loss: 0.1084, Train Acc: 0.9690, Val Loss: 0.2732, Val Acc: 0.9115
Epoch 82/99, Train Loss: 0.0616, Train Acc: 0.9779, Val Loss: 0.3224, Val Acc: 0.9027
Epoch 83/99, Train Loss: 0.1246, Train Acc: 0.9602, Val Loss: 0.3234, Val Acc: 0.8938
Epoch 84/99, Train Loss: 0.1272, Train Acc: 0.9580, Val Loss: 0.3273, Val Acc: 0.8673
Epoch 85/99, Train Loss: 0.0450, Train Acc: 0.9867, Val Loss: 0.1960, Val Acc: 0.9204
Epoch 86/99, Train Loss: 0.0637, Train Acc: 0.9779, Val Loss: 0.2931, Val Acc: 0.9027
Epoch 87/99, Train Loss: 0.1449, Train Acc: 0.9535, Val Loss: 1.2494, Val Acc: 0.7965
Epoch 88/99, Train Loss: 0.4223, Train Acc: 0.8650, Val Loss: 0.6742, Val Acc: 0.8230
Epoch 89/99, Train Loss: 0.1847, Train Acc: 0.9469, Val Loss: 0.2900, Val Acc: 0.9027
Epoch 90/99, Train Loss: 0.1394, Train Acc: 0.9513, Val Loss: 0.3145, Val Acc: 0.8850
Epoch 91/99, Train Loss: 0.1057, Train Acc: 0.9624, Val Loss: 0.3221, Val Acc: 0.8850
Epoch 92/99, Train Loss: 0.1004, Train Acc: 0.9757, Val Loss: 0.3705, Val Acc: 0.8850
Epoch 93/99, Train Loss: 0.0783, Train Acc: 0.9712, Val Loss: 0.3333, Val Acc: 0.9027
Epoch 94/99, Train Loss: 0.0424, Train Acc: 0.9867, Val Loss: 0.3462, Val Acc: 0.9027
Epoch 95/99, Train Loss: 0.0702, Train Acc: 0.9779, Val Loss: 0.2341, Val Acc: 0.9204
Epoch 96/99, Train Loss: 0.0424, Train Acc: 0.9867, Val Loss: 0.2882, Val Acc: 0.9204
Epoch 97/99, Train Loss: 0.0416, Train Acc: 0.9912, Val Loss: 0.2408, Val Acc: 0.9381
Epoch 98/99, Train Loss: 0.0397, Train Acc: 0.9823, Val Loss: 0.2606, Val Acc: 0.9027
Epoch 99/99, Train Loss: 0.1306, Train Acc: 0.9602, Val Loss: 0.4435, Val Acc: 0.8673





<All keys matched successfully>
# 预测模型
model.eval()
plt.figure(figsize=(10, 5))
plt.suptitle("bird")

for inputs, labels in val_loader:
    inputs, labels = inputs.to(device), labels.to(device)
    outputs = model(inputs)
    _, preds = torch.max(outputs, 1)

    for i in range(len(inputs)):
        ax = plt.subplot(2, 4, i + 1)

        img = inputs[i].cpu().numpy().transpose((1, 2, 0))
        plt.imshow(img)
        plt.title(class_names[preds[i]])

        plt.axis("off")
    break

在这里插入图片描述

在这里插入图片描述

个人总结

V2对比原V1区别:

  1. 预激活残差单元(Pre-activation Residual Units):
    ResNet(V1):在每个残差块中,卷积层在批归一化(Batch Normalization)和 ReLU 之前。
    ResNetV2:批归一化和 ReLU 激活在卷积操作之前。这被称为“预激活”结构,这样做有助于梯度流动,使得信息更容易传播,有助于更深的网络训练。

  2. 批归一化的位置(Batch Normalization Position):
    在 ResNetV2 中,所有的卷积操作前都有批归一化层,使得表现更稳定并协助训练深层模型。

  3. 最后的 ReLU 激活:
    ResNetV2 在残差块的最后使用了 ReLU 激活来处理块的输出(在添加 shortcut 之后),这可以让模型训练得更好。

  4. 无顶池化层(No top pooling layer):
    ResNetV2 版本去除了最后的全局平均池化前的顶池化操作(Top Pooling),直接在最后进行全局池化。这简化了网络设计,增强了其通用性。

这些修改使 ResNetV2 具有更好的梯度传递和稳定性(尤其是当网络变得非常深的时候),试验数据显示Val loss 得到了较大的改善

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/900189.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

软考(中级-软件设计师)计算机系统篇(1024)

#1024程序员节|正文# 六、树和二叉树 6.1 树的基本概念 描述结果结点的度子结点的个数树的度最大结点的度叶子结点没有子结点的结点内部结点除根结点和叶子结点外的结点父节点有子结点的结点子节点有父结点的结点兄弟节点有同一个父结点的结点层次4层 6.2 二叉树的基本概念…

AI时代LabVIEW程序员的未来出路

随着GPT等AI技术的迅速发展&#xff0c;AI已经能够自动完成大量的代码生成工作&#xff0c;这无疑给LabVIEW程序员带来了新的挑战和机遇。尽管AI能够替代部分编程工作&#xff0c;LabVIEW程序员依然可以通过以下几方面找到出路&#xff1a; 复杂系统集成&#xff1a; AI可以帮助…

【软考高级架构】关于分布式数据库缓存redis的知识要点汇总

一.分布式数据库的含义 分布式数据库缓存指的是在高并发的环境下&#xff0c;为了减轻数据库的压力和提高系统响应时间&#xff0c;在数据库系统和应用系统之间增加一个独立缓存系统。 二.常见的缓存技术 &#xff08;1&#xff09;MemCache: Memcache是一个高性能的分布式的内…

你对MySQL的having关键字了解多少?

在MySQL中&#xff0c;HAVING子句用于在数据分组并计算聚合函数之后&#xff0c;对结果进行进一步的过滤。它通常与GROUP BY子句一起使用&#xff0c;以根据指定的条件过滤分组。HAVING子句的作用类似于WHERE子句&#xff0c;但WHERE子句是在数据被聚合之前进行过滤&#xff0c…

闯关leetcode——205. Isomorphic Strings

大纲 题目地址内容 解题代码地址 题目 地址 https://leetcode.com/problems/isomorphic-strings/ 内容 Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occur…

2021亚洲机器学习会议:面向单阶段跨域检测的域自适应YOLO(ACML2021)

原文标题&#xff1a;Domain Adaptive YOLO for One-Stage Cross-Domain Detection 中文标题&#xff1a;面向单阶段跨域检测的域自适应YOLO 1、Abstract 域转移是目标检测器在实际应用中推广的主要挑战。两级检测器的域自适应新兴技术有助于解决这个问题。然而&#xff0c;两级…

现场总是发生急停,很可能是PLC和设置间网络中断

如果你的现场总是发生急停&#xff0c;很可能是PLC和设置间网络中断&#xff0c;本文用一个真实案例告诉你问题背后的原因和解决方法&#xff01; 这是一台生产汽车配件的机器&#xff0c;使用1500F的控制器连接机器人控制器&#xff0c;现场装置总会莫名其妙的发生急停故障。…

部署前后端分离若依项目--CentOS7Docker版

一、准备 centos7虚拟机或服务器一台 若依前后端分离项目&#xff1a;可在下面拉取 RuoYi-Vue: &#x1f389; 基于SpringBoot&#xff0c;Spring Security&#xff0c;JWT&#xff0c;Vue & Element 的前后端分离权限管理系统&#xff0c;同时提供了 Vue3 的版本 二、环…

JavaEE进阶----19.<Mybatis进阶(动态SQL)>

详解动态SQL <if>标签、<trim>标签、<where>标签、<set>标签、<foreach>标签、<include>标签 & <SQL>标签 MySQL&#xff08;进阶&#xff09; 一、动态SQL 也就是SQL语句中指定的属性&#xff0c;若我们不想输入进行查询&…

查缺补漏----分组交换所需时间计算

总结以及图片来源&#xff1a;b站湖科大真题计网讲解 对于报文交换&#xff0c;路由器完整接收整个报文后&#xff0c;才能对报文进行转发。对于分组交换&#xff0c;则是将报文划为更小的分组进行传送&#xff0c;路由器边接收分组边转发分组。 报文交换&#xff1a; 分组交换…

文件上传漏洞及安全

文件上传 文件上传安全指的是攻击者通过利用上传实现后门的写入连接后门进行权限控制的安全问题&#xff0c;对于如何确保这类安全问题&#xff0c;一般会从原生态功能中的文件内容&#xff0c;文件后缀&#xff0c;文件类型等方面判断&#xff0c;但是漏洞可能不仅在本身的代码…

Java的查找算法和排序算法

Java的查找算法和排序算法 一、查找算法1. 基本查找a. 示例 2. 二分查找a. 示例 3. 插值查找4. 斐波那契查找5. 分块查找a. 示例 二、排序算法1. 冒泡排序a. 示例 2. 选择排序a. 示例 3、插入排序a. 示例 4. 快速排序&#xff08;效率最高&#xff09;a. 示例 一、查找算法 1.…

期权懂|2024年期权最新止损策略有哪些?

本期让我懂 你就懂的期权懂带大家来了解&#xff0c;2024年期权最新止损策略有哪些&#xff1f;有兴趣的朋友可以看一下。期权小懂每日分享期权知识&#xff0c;帮助期权新手及时有效地掌握即市趋势与新资讯&#xff01; 2024年期权最新止损策略有哪些&#xff1f; 一、浮亏比例…

Pandas模块之垂直或水平交错条形图

目录 df.plot() 函数Pandas模块之垂直条形图Pandas模块之水平交错条形图 df.plot() 函数 df.plot() 是 Pandas 中的一个函数&#xff0c;用于绘制数据框中的数据。它是基于 Matplotlib 库构建的&#xff0c;可以轻松地创建各种类型的图表&#xff0c;包括折线图、柱状图、散点…

PCM5102A具有PLL和32位、384kHz PCM/I2S接口的2.1VRMS、112dB音频立体声DAC

PCM5102A外观和丝印 1 特性 1•超低带外噪声 •具有BCK基准的高性能集成音频锁相环(PLL)&#xff0c;可在内部生成SCK •直接线路电平2.1VRMS输出 •无需隔直电容 •线路电平输出支持低至1kΩ的负载 •智能静音系统&#xff1b;软斜升或斜降搭配模拟静音&#xff0c;实现120dB…

深度学习实战项目】基于OPenCV的人脸识别考勤系统软件开发【python源码+UI界面+功能源码详解】

背景及意义 人脸识别&#xff08;Face Recognition&#xff09;是基于人的脸部特征信息进行身份识别的一种生物识别技术&#xff0c;可以用来确认用户身份。本文详细介绍了人脸识别基本的实现原理&#xff0c;并且基于python与pyqt开发了人脸识别与信息管理软件&#xff0c;主要…

Go第三方框架--gorm框架(一)

前言 orm模型简介 orm模型全称是Object-Relational Mapping&#xff0c;即对象关系映射。其实就是在原生sql基础之上进行更高程度的封装。方便程序员采用面向对象的方式来操作数据库&#xff0c;将表映射成对象。 这种映射带来几个好处&#xff1a; 代码简洁&#xff1a;不用…

AVL树介绍与构建

目录 AVL树的概念 二叉树的构建 平衡因子的更新 旋转 左单旋 旋转过程 左单旋代码 右单旋 旋转过程 右单旋代码 左右双旋 发生情况 抽象图 具体图 平衡因子更新 左右双旋代码 右左双旋 右左双旋旋代码 验证测试AVL树 测试成员函数 测试代码 AVL树实现代码…

使用virtualenv导入ssl模块找不到指定的模块

最近在学习tensorflow&#xff0c;由于教程里面使用的是virtualenv&#xff0c;所以就按照教程开始安装了虚拟环境。但是在使用的时候&#xff0c;卡在了import ssl这一步&#xff0c;提示如下错误 >>> import ssl Traceback (most recent call last):File "<…

兼容Lodash的真正替代者

大家好&#xff0c;我是农村程序员&#xff0c;独立开发者&#xff0c;前端之虎陈随易。 这是我的个人网站&#xff1a;https://chensuiyi.me&#xff0c;欢迎一起交朋友~ 今天给大家分享一个前端工具库 Lodash 的替代品 es-toolkit。 仓库地址&#xff1a;https://github.com…