深度学习笔记33_ResNeXt-50实战解析

一、我的环境

1.语言环境:Python 3.9

2.编译器:Pycharm

3.深度学习环境:pytorch

二、GPU设置

       若使用的是cpu则可忽略

# 设置GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

、数据导入

import pathlib
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms, datasets

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

print(device)

data_dir = './data/'
data_dir = pathlib.Path(data_dir)

data_paths = list(data_dir.glob('*'))
classeNames = [str(path).split("\\")[1] for path in data_paths]
print(classeNames)

image_count = len(list(data_dir.glob('*/*')))
print("图片总数为:", image_count)
['Monkeypox', 'Others']
图片总数为: 2142

、加载数据

batch_size = 32
train_dl = torch.utils.data.DataLoader(train_dataset,
                                       batch_size=batch_size,
                                       shuffle=True,
                                       num_workers=0)
test_dl = torch.utils.data.DataLoader(test_dataset,
                                      batch_size=batch_size,
                                      shuffle=True,
                                      num_workers=0)

五、数据处理

train_transforms = transforms.Compose([
    transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸
    # transforms.RandomHorizontalFlip(), # 随机水平翻转
    transforms.ToTensor(),  # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
    transforms.Normalize(  # 标准化处理-->转换为标准正太分布(高斯分布),使模型更容易收敛
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225])  # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
])

test_transform = transforms.Compose([
    transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸
    transforms.ToTensor(),  # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
    transforms.Normalize(  # 标准化处理-->转换为标准正太分布(高斯分布),使模型更容易收敛
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225])  # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
])
#划分数据集
train_size = int(0.8 * len(total_data))  # train_size表示训练集大小,通过将总体数据长度的80%转换为整数得到;
test_size = len(total_data) - train_size  # test_size表示测试集大小,是总体数据长度减去训练集大小。
# 使用torch.utils.data.random_split()方法进行数据集划分。该方法将总体数据total_data按照指定的大小比例([train_size, test_size])随机划分为训练集和测试集,
# 并将划分结果分别赋值给train_dataset和test_dataset两个变量。
train_ds, test_ds = random_split(total_data, [train_size, test_size])

 数据可视化

plt.figure(figsize=(16, 10))
# plt.title("数据集")
for i in range(20):
    plt.subplot(4, 5, i + 1)
    plt.axis("off")
    image = random.choice(img_list)
    label_name = image.parts[-2]
    plt.title(label_name)
    plt.imshow(Image.open(str(image)))

plt.show()

再次检查数据

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

六、构建模型

class BN_Conv2d(nn.Module):
    """
    BN_CONV_RELU
    """

    def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation=1, groups=1, bias=False):
        super(BN_Conv2d, self).__init__()
        self.seq = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride,
                      padding=padding, dilation=dilation, groups=groups, bias=bias),
            nn.BatchNorm2d(out_channels)
        )

    def forward(self, x):
        return F.relu(self.seq(x))


class ResNeXt_Block(nn.Module):
    """
    ResNeXt block with group convolutions
    """

    def __init__(self, in_chnls, cardinality, group_depth, stride):
        super(ResNeXt_Block, self).__init__()
        self.group_chnls = cardinality * group_depth
        self.conv1 = BN_Conv2d(in_chnls, self.group_chnls, 1, stride=1, padding=0)
        self.conv2 = BN_Conv2d(self.group_chnls, self.group_chnls, 3, stride=stride, padding=1, groups=cardinality)
        self.conv3 = nn.Conv2d(self.group_chnls, self.group_chnls * 2, 1, stride=1, padding=0)
        self.bn = nn.BatchNorm2d(self.group_chnls * 2)
        self.short_cut = nn.Sequential(
            nn.Conv2d(in_chnls, self.group_chnls * 2, 1, stride, 0, bias=False),
            nn.BatchNorm2d(self.group_chnls * 2)
        )

    def forward(self, x):
        out = self.conv1(x)
        out = self.conv2(out)
        out = self.bn(self.conv3(out))
        out += self.short_cut(x)
        return F.relu(out)


class ResNeXt(nn.Module):
    """
    ResNeXt builder
    """

    def __init__(self, layers: object, cardinality, group_depth, num_classes) -> object:
        super(ResNeXt, self).__init__()
        self.cardinality = cardinality
        self.channels = 64
        self.conv1 = BN_Conv2d(3, self.channels, 7, stride=2, padding=3)
        d1 = group_depth
        self.conv2 = self.___make_layers(d1, layers[0], stride=1)
        d2 = d1 * 2
        self.conv3 = self.___make_layers(d2, layers[1], stride=2)
        d3 = d2 * 2
        self.conv4 = self.___make_layers(d3, layers[2], stride=2)
        d4 = d3 * 2
        self.conv5 = self.___make_layers(d4, layers[3], stride=2)
        self.fc = nn.Linear(self.channels, num_classes)  # 224x224 input size

    def ___make_layers(self, d, blocks, stride):
        strides = [stride] + [1] * (blocks - 1)
        layers = []
        for stride in strides:
            layers.append(ResNeXt_Block(self.channels, self.cardinality, d, stride))
            self.channels = self.cardinality * d * 2
        return nn.Sequential(*layers)

    def forward(self, x):
        out = self.conv1(x)
        out = F.max_pool2d(out, 3, 2, 1)
        out = self.conv2(out)
        out = self.conv3(out)
        out = self.conv4(out)
        out = self.conv5(out)
        out = F.avg_pool2d(out, 7)
        out = out.view(out.size(0), -1)
        out = F.softmax(self.fc(out), dim=1)
        return out



model = ResNeXt([3, 4, 6, 3], 32, 4, 4)
model.to(device)
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 112, 112]           9,408
       BatchNorm2d-2         [-1, 64, 112, 112]             128
         BN_Conv2d-3         [-1, 64, 112, 112]               0
            Conv2d-4          [-1, 128, 56, 56]           8,192
       BatchNorm2d-5          [-1, 128, 56, 56]             256
         BN_Conv2d-6          [-1, 128, 56, 56]               0
            Conv2d-7          [-1, 128, 56, 56]           4,608
       BatchNorm2d-8          [-1, 128, 56, 56]             256
         BN_Conv2d-9          [-1, 128, 56, 56]               0
           Conv2d-10          [-1, 256, 56, 56]          33,024
      BatchNorm2d-11          [-1, 256, 56, 56]             512
           Conv2d-12          [-1, 256, 56, 56]          16,384
      BatchNorm2d-13          [-1, 256, 56, 56]             512
    ResNeXt_Block-14          [-1, 256, 56, 56]               0
           Conv2d-15          [-1, 128, 56, 56]          32,768
      BatchNorm2d-16          [-1, 128, 56, 56]             256
        BN_Conv2d-17          [-1, 128, 56, 56]               0
           Conv2d-18          [-1, 128, 56, 56]           4,608
      BatchNorm2d-19          [-1, 128, 56, 56]             256
        BN_Conv2d-20          [-1, 128, 56, 56]               0
           Conv2d-21          [-1, 256, 56, 56]          33,024
      BatchNorm2d-22          [-1, 256, 56, 56]             512
           Conv2d-23          [-1, 256, 56, 56]          65,536
      BatchNorm2d-24          [-1, 256, 56, 56]             512
    ResNeXt_Block-25          [-1, 256, 56, 56]               0
           Conv2d-26          [-1, 128, 56, 56]          32,768
      BatchNorm2d-27          [-1, 128, 56, 56]             256
        BN_Conv2d-28          [-1, 128, 56, 56]               0
           Conv2d-29          [-1, 128, 56, 56]           4,608
      BatchNorm2d-30          [-1, 128, 56, 56]             256
        BN_Conv2d-31          [-1, 128, 56, 56]               0
           Conv2d-32          [-1, 256, 56, 56]          33,024
      BatchNorm2d-33          [-1, 256, 56, 56]             512
           Conv2d-34          [-1, 256, 56, 56]          65,536
      BatchNorm2d-35          [-1, 256, 56, 56]             512
    ResNeXt_Block-36          [-1, 256, 56, 56]               0
           Conv2d-37          [-1, 256, 56, 56]          65,536
      BatchNorm2d-38          [-1, 256, 56, 56]             512
        BN_Conv2d-39          [-1, 256, 56, 56]               0
           Conv2d-40          [-1, 256, 28, 28]          18,432
      BatchNorm2d-41          [-1, 256, 28, 28]             512
        BN_Conv2d-42          [-1, 256, 28, 28]               0
           Conv2d-43          [-1, 512, 28, 28]         131,584
      BatchNorm2d-44          [-1, 512, 28, 28]           1,024
           Conv2d-45          [-1, 512, 28, 28]         131,072
      BatchNorm2d-46          [-1, 512, 28, 28]           1,024
    ResNeXt_Block-47          [-1, 512, 28, 28]               0
           Conv2d-48          [-1, 256, 28, 28]         131,072
      BatchNorm2d-49          [-1, 256, 28, 28]             512
        BN_Conv2d-50          [-1, 256, 28, 28]               0
           Conv2d-51          [-1, 256, 28, 28]          18,432
      BatchNorm2d-52          [-1, 256, 28, 28]             512
        BN_Conv2d-53          [-1, 256, 28, 28]               0
           Conv2d-54          [-1, 512, 28, 28]         131,584
      BatchNorm2d-55          [-1, 512, 28, 28]           1,024
           Conv2d-56          [-1, 512, 28, 28]         262,144
      BatchNorm2d-57          [-1, 512, 28, 28]           1,024
    ResNeXt_Block-58          [-1, 512, 28, 28]               0
           Conv2d-59          [-1, 256, 28, 28]         131,072
      BatchNorm2d-60          [-1, 256, 28, 28]             512
        BN_Conv2d-61          [-1, 256, 28, 28]               0
           Conv2d-62          [-1, 256, 28, 28]          18,432
      BatchNorm2d-63          [-1, 256, 28, 28]             512
        BN_Conv2d-64          [-1, 256, 28, 28]               0
           Conv2d-65          [-1, 512, 28, 28]         131,584
      BatchNorm2d-66          [-1, 512, 28, 28]           1,024
           Conv2d-67          [-1, 512, 28, 28]         262,144
      BatchNorm2d-68          [-1, 512, 28, 28]           1,024
    ResNeXt_Block-69          [-1, 512, 28, 28]               0
           Conv2d-70          [-1, 256, 28, 28]         131,072
      BatchNorm2d-71          [-1, 256, 28, 28]             512
        BN_Conv2d-72          [-1, 256, 28, 28]               0
           Conv2d-73          [-1, 256, 28, 28]          18,432
      BatchNorm2d-74          [-1, 256, 28, 28]             512
        BN_Conv2d-75          [-1, 256, 28, 28]               0
           Conv2d-76          [-1, 512, 28, 28]         131,584
      BatchNorm2d-77          [-1, 512, 28, 28]           1,024
           Conv2d-78          [-1, 512, 28, 28]         262,144
      BatchNorm2d-79          [-1, 512, 28, 28]           1,024
    ResNeXt_Block-80          [-1, 512, 28, 28]               0
           Conv2d-81          [-1, 512, 28, 28]         262,144
      BatchNorm2d-82          [-1, 512, 28, 28]           1,024
        BN_Conv2d-83          [-1, 512, 28, 28]               0
           Conv2d-84          [-1, 512, 14, 14]          73,728
      BatchNorm2d-85          [-1, 512, 14, 14]           1,024
        BN_Conv2d-86          [-1, 512, 14, 14]               0
           Conv2d-87         [-1, 1024, 14, 14]         525,312
      BatchNorm2d-88         [-1, 1024, 14, 14]           2,048
           Conv2d-89         [-1, 1024, 14, 14]         524,288
      BatchNorm2d-90         [-1, 1024, 14, 14]           2,048
    ResNeXt_Block-91         [-1, 1024, 14, 14]               0
           Conv2d-92          [-1, 512, 14, 14]         524,288
      BatchNorm2d-93          [-1, 512, 14, 14]           1,024
        BN_Conv2d-94          [-1, 512, 14, 14]               0
           Conv2d-95          [-1, 512, 14, 14]          73,728
      BatchNorm2d-96          [-1, 512, 14, 14]           1,024
        BN_Conv2d-97          [-1, 512, 14, 14]               0
           Conv2d-98         [-1, 1024, 14, 14]         525,312
      BatchNorm2d-99         [-1, 1024, 14, 14]           2,048
          Conv2d-100         [-1, 1024, 14, 14]       1,048,576
     BatchNorm2d-101         [-1, 1024, 14, 14]           2,048
   ResNeXt_Block-102         [-1, 1024, 14, 14]               0
          Conv2d-103          [-1, 512, 14, 14]         524,288
     BatchNorm2d-104          [-1, 512, 14, 14]           1,024
       BN_Conv2d-105          [-1, 512, 14, 14]               0
          Conv2d-106          [-1, 512, 14, 14]          73,728
     BatchNorm2d-107          [-1, 512, 14, 14]           1,024
       BN_Conv2d-108          [-1, 512, 14, 14]               0
          Conv2d-109         [-1, 1024, 14, 14]         525,312
     BatchNorm2d-110         [-1, 1024, 14, 14]           2,048
          Conv2d-111         [-1, 1024, 14, 14]       1,048,576
     BatchNorm2d-112         [-1, 1024, 14, 14]           2,048
   ResNeXt_Block-113         [-1, 1024, 14, 14]               0
          Conv2d-114          [-1, 512, 14, 14]         524,288
     BatchNorm2d-115          [-1, 512, 14, 14]           1,024
       BN_Conv2d-116          [-1, 512, 14, 14]               0
          Conv2d-117          [-1, 512, 14, 14]          73,728
     BatchNorm2d-118          [-1, 512, 14, 14]           1,024
       BN_Conv2d-119          [-1, 512, 14, 14]               0
          Conv2d-120         [-1, 1024, 14, 14]         525,312
     BatchNorm2d-121         [-1, 1024, 14, 14]           2,048
          Conv2d-122         [-1, 1024, 14, 14]       1,048,576
     BatchNorm2d-123         [-1, 1024, 14, 14]           2,048
   ResNeXt_Block-124         [-1, 1024, 14, 14]               0
          Conv2d-125          [-1, 512, 14, 14]         524,288
     BatchNorm2d-126          [-1, 512, 14, 14]           1,024
       BN_Conv2d-127          [-1, 512, 14, 14]               0
          Conv2d-128          [-1, 512, 14, 14]          73,728
     BatchNorm2d-129          [-1, 512, 14, 14]           1,024
       BN_Conv2d-130          [-1, 512, 14, 14]               0
          Conv2d-131         [-1, 1024, 14, 14]         525,312
     BatchNorm2d-132         [-1, 1024, 14, 14]           2,048
          Conv2d-133         [-1, 1024, 14, 14]       1,048,576
     BatchNorm2d-134         [-1, 1024, 14, 14]           2,048
   ResNeXt_Block-135         [-1, 1024, 14, 14]               0
          Conv2d-136          [-1, 512, 14, 14]         524,288
     BatchNorm2d-137          [-1, 512, 14, 14]           1,024
       BN_Conv2d-138          [-1, 512, 14, 14]               0
          Conv2d-139          [-1, 512, 14, 14]          73,728
     BatchNorm2d-140          [-1, 512, 14, 14]           1,024
       BN_Conv2d-141          [-1, 512, 14, 14]               0
          Conv2d-142         [-1, 1024, 14, 14]         525,312
     BatchNorm2d-143         [-1, 1024, 14, 14]           2,048
          Conv2d-144         [-1, 1024, 14, 14]       1,048,576
     BatchNorm2d-145         [-1, 1024, 14, 14]           2,048
   ResNeXt_Block-146         [-1, 1024, 14, 14]               0
          Conv2d-147         [-1, 1024, 14, 14]       1,048,576
     BatchNorm2d-148         [-1, 1024, 14, 14]           2,048
       BN_Conv2d-149         [-1, 1024, 14, 14]               0
          Conv2d-150           [-1, 1024, 7, 7]         294,912
     BatchNorm2d-151           [-1, 1024, 7, 7]           2,048
       BN_Conv2d-152           [-1, 1024, 7, 7]               0
          Conv2d-153           [-1, 2048, 7, 7]       2,099,200
     BatchNorm2d-154           [-1, 2048, 7, 7]           4,096
          Conv2d-155           [-1, 2048, 7, 7]       2,097,152
     BatchNorm2d-156           [-1, 2048, 7, 7]           4,096
   ResNeXt_Block-157           [-1, 2048, 7, 7]               0
          Conv2d-158           [-1, 1024, 7, 7]       2,097,152
     BatchNorm2d-159           [-1, 1024, 7, 7]           2,048
       BN_Conv2d-160           [-1, 1024, 7, 7]               0
          Conv2d-161           [-1, 1024, 7, 7]         294,912
     BatchNorm2d-162           [-1, 1024, 7, 7]           2,048
       BN_Conv2d-163           [-1, 1024, 7, 7]               0
          Conv2d-164           [-1, 2048, 7, 7]       2,099,200
     BatchNorm2d-165           [-1, 2048, 7, 7]           4,096
          Conv2d-166           [-1, 2048, 7, 7]       4,194,304
     BatchNorm2d-167           [-1, 2048, 7, 7]           4,096
   ResNeXt_Block-168           [-1, 2048, 7, 7]               0
          Conv2d-169           [-1, 1024, 7, 7]       2,097,152
     BatchNorm2d-170           [-1, 1024, 7, 7]           2,048
       BN_Conv2d-171           [-1, 1024, 7, 7]               0
          Conv2d-172           [-1, 1024, 7, 7]         294,912
     BatchNorm2d-173           [-1, 1024, 7, 7]           2,048
       BN_Conv2d-174           [-1, 1024, 7, 7]               0
          Conv2d-175           [-1, 2048, 7, 7]       2,099,200
     BatchNorm2d-176           [-1, 2048, 7, 7]           4,096
          Conv2d-177           [-1, 2048, 7, 7]       4,194,304
     BatchNorm2d-178           [-1, 2048, 7, 7]           4,096
   ResNeXt_Block-179           [-1, 2048, 7, 7]               0
          Linear-180                    [-1, 4]           8,196
================================================================
Total params: 37,574,724
Trainable params: 37,574,724
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 379.37
Params size (MB): 143.34
Estimated Total Size (MB): 523.28
----------------------------------------------------------------
# 训练循环
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)  # 训练集的大小
    num_batches = len(dataloader)  # 批次数目, (size/batch_size,向上取整)

    train_loss, train_acc = 0, 0  # 初始化训练损失和正确率

    for X, y in dataloader:  # 获取图片及其标签
        X, y = X.to(device), y.to(device)

        # 计算预测误差
        pred = model(X)  # 网络输出
        loss = loss_fn(pred, y)  # 计算网络输出和真实值之间的差距,targets为真实值,计算二者差值即为损失

        # 反向传播
        optimizer.zero_grad()  # grad属性归零
        loss.backward()  # 反向传播
        optimizer.step()  # 每一步自动更新

        # 记录acc与loss
        train_acc += (pred.argmax(1) == y).type(torch.float).sum().item()
        train_loss += loss.item()

    train_acc /= size
    train_loss /= num_batches

    return train_acc, train_loss
def test(dataloader, model, loss_fn):
    size = len(dataloader.dataset)  # 测试集的大小
    num_batches = len(dataloader)  # 批次数目
    test_loss, test_acc = 0, 0

    # 当不进行训练时,停止梯度更新,节省计算内存消耗
    with torch.no_grad():
        for imgs, target in dataloader:
            imgs, target = imgs.to(device), target.to(device)

            # 计算loss
            target_pred = model(imgs)
            loss = loss_fn(target_pred, target)

            test_loss += loss.item()
            test_acc += (target_pred.argmax(1) == target).type(torch.float).sum().item()

    test_acc /= size
    test_loss /= num_batches

    return test_acc, test_loss

七、训练模型

import copy

optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
loss_fn = nn.CrossEntropyLoss()  # 创建损失函数

epochs = 30

train_loss = []
train_acc = []
test_loss = []
test_acc = []

best_acc = 0  # 设置一个最佳准确率,作为最佳模型的判别指标

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_model
    if epoch_test_acc > best_acc:
        best_acc = epoch_test_acc
        best_model = copy.deepcopy(model)

    train_acc.append(epoch_train_acc)
    train_loss.append(epoch_train_loss)
    test_acc.append(epoch_test_acc)
    test_loss.append(epoch_test_loss)

    # 获取当前的学习率
    lr = optimizer.state_dict()['param_groups'][0]['lr']

    template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E}')
    print(template.format(epoch + 1, epoch_train_acc * 100, epoch_train_loss,
                          epoch_test_acc * 100, epoch_test_loss, lr))
Epoch: 1, Train_acc:56.0%, Train_loss:1.173, Test_acc:56.6%, Test_loss:1.160, Lr:1.00E-04
Epoch: 2, Train_acc:62.6%, Train_loss:1.109, Test_acc:58.3%, Test_loss:1.164, Lr:1.00E-04
Epoch: 3, Train_acc:65.0%, Train_loss:1.089, Test_acc:66.0%, Test_loss:1.090, Lr:1.00E-04
Epoch: 4, Train_acc:64.4%, Train_loss:1.098, Test_acc:65.0%, Test_loss:1.085, Lr:1.00E-04
Epoch: 5, Train_acc:67.3%, Train_loss:1.069, Test_acc:68.8%, Test_loss:1.059, Lr:1.00E-04
Epoch: 6, Train_acc:69.2%, Train_loss:1.047, Test_acc:73.0%, Test_loss:1.013, Lr:1.00E-04
Epoch: 7, Train_acc:70.1%, Train_loss:1.042, Test_acc:71.8%, Test_loss:1.029, Lr:1.00E-04
Epoch: 8, Train_acc:71.0%, Train_loss:1.033, Test_acc:66.4%, Test_loss:1.074, Lr:1.00E-04
Epoch: 9, Train_acc:69.6%, Train_loss:1.043, Test_acc:72.0%, Test_loss:1.032, Lr:1.00E-04
Epoch:10, Train_acc:69.5%, Train_loss:1.045, Test_acc:69.7%, Test_loss:1.045, Lr:1.00E-04
Epoch:11, Train_acc:69.5%, Train_loss:1.040, Test_acc:68.5%, Test_loss:1.065, Lr:1.00E-04
Epoch:12, Train_acc:71.1%, Train_loss:1.030, Test_acc:76.0%, Test_loss:0.987, Lr:1.00E-04
Epoch:13, Train_acc:73.1%, Train_loss:1.008, Test_acc:72.0%, Test_loss:1.016, Lr:1.00E-04
Epoch:14, Train_acc:73.6%, Train_loss:1.006, Test_acc:65.5%, Test_loss:1.076, Lr:1.00E-04
Epoch:15, Train_acc:73.0%, Train_loss:1.013, Test_acc:75.1%, Test_loss:0.995, Lr:1.00E-04
Epoch:16, Train_acc:77.8%, Train_loss:0.968, Test_acc:75.5%, Test_loss:0.989, Lr:1.00E-04
Epoch:17, Train_acc:75.0%, Train_loss:0.989, Test_acc:74.6%, Test_loss:0.995, Lr:1.00E-04
Epoch:18, Train_acc:74.7%, Train_loss:0.992, Test_acc:76.2%, Test_loss:0.992, Lr:1.00E-04
Epoch:19, Train_acc:76.4%, Train_loss:0.975, Test_acc:78.3%, Test_loss:0.969, Lr:1.00E-04
Epoch:20, Train_acc:74.8%, Train_loss:0.989, Test_acc:75.1%, Test_loss:0.988, Lr:1.00E-04
Epoch:21, Train_acc:76.5%, Train_loss:0.979, Test_acc:77.2%, Test_loss:0.965, Lr:1.00E-04
Epoch:22, Train_acc:74.5%, Train_loss:0.993, Test_acc:68.8%, Test_loss:1.049, Lr:1.00E-04
Epoch:23, Train_acc:72.9%, Train_loss:1.011, Test_acc:68.5%, Test_loss:1.054, Lr:1.00E-04
Epoch:24, Train_acc:72.6%, Train_loss:1.015, Test_acc:74.4%, Test_loss:1.005, Lr:1.00E-04
Epoch:25, Train_acc:74.3%, Train_loss:1.001, Test_acc:75.8%, Test_loss:0.985, Lr:1.00E-04
Epoch:26, Train_acc:76.0%, Train_loss:0.984, Test_acc:76.5%, Test_loss:0.980, Lr:1.00E-04
Epoch:27, Train_acc:75.6%, Train_loss:0.985, Test_acc:74.1%, Test_loss:0.980, Lr:1.00E-04
Epoch:28, Train_acc:75.7%, Train_loss:0.984, Test_acc:74.6%, Test_loss:0.996, Lr:1.00E-04
Epoch:29, Train_acc:77.2%, Train_loss:0.971, Test_acc:75.3%, Test_loss:0.986, Lr:1.00E-04
Epoch:30, Train_acc:76.0%, Train_loss:0.980, Test_acc:75.1%, Test_loss:0.992, Lr:1.00E-04
Done

八、模型评估

    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
    plt.rcParams['figure.dpi'] = 100  # 分辨率

    epochs_range = range(epoch)

    plt.figure(figsize=(12, 3))
    plt.subplot(1, 2, 1)

    plt.plot(epochs_range, train_acc, label='Training Accuracy')
    plt.plot(epochs_range, test_acc, label='Test Accuracy')
    plt.legend(loc='lower right')
    plt.title('Training and Validation Accuracy')

    plt.subplot(1, 2, 2)
    plt.plot(epochs_range, train_loss, label='Training Loss')
    plt.plot(epochs_range, test_loss, label='Test Loss')
    plt.legend(loc='upper right')
    plt.title('Training and Validation Loss')
    plt.show()

九、总结

这周学习ResNeXt-50实战解析:

            ResNeXt是由何凯明团队在2017年CVPR会议上提出来的新型图像分类网络。ResNeXt是ResNet的升级版,在ResNet的基础上,引入了cardinality的概念,类似于ResNet,ResNeXt也有ResNeXt-50,ResNeXt-101的版本。

分组卷积
        ResNeXt中采用的分组卷机简单来说就是将特征图分为不同的组,再对每组特征图分别进行卷积,这个操作可以有效的降低计算量。在分组卷积中,每个卷积核只处理部分通道,比如下图中,红色卷积核只处理红色的通道,绿色卷积核只处理绿色通道,黄色卷积核只处理黄色通道。此时每个卷积核有2个通道,每个卷积核生成一张特征图。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值