• 【Pytorch 第四讲】图像分类的Tricks


    1. 标签平滑

    在分类问题中,最后一层一般是全连接层,然后对应标签的one-hot编码,即把对应类别的值编码为1,其他为0。这种编码方式和通过降低交叉熵损失来调整参数的方式结合起来,会有一些问题。这种方式会鼓励模型对不同类别的输出分数差异非常大,或者说,模型过分相信它的判断。但是,对于一个由多人标注的数据集,不同人标注的准则可能不同,每个人的标注也可能会有一些错误。模型对标签的过分相信会导致过拟合。

     标签平滑(Label-smoothing regularization,LSR)是应对该问题的有效方法之一,它的具体思想是降低我们对于标签的信任,例如我们可以将损失的目标值从1稍微降到0.9,或者将从0稍微升到0.1。

    总的来说,LSR是一种通过在标签y中加入噪声,实现对模型约束,降低模型过拟合程度的一种正则化方法。

    1. importtorch
    2. importtorch.nn as nn
    3. class LSR(nn.Module):
    4. def __init__(self, e=0.1, reduction='mean'):
    5. super().__init__()
    6. self.log_softmax = nn.LogSoftmax(dim=1)
    7. self.e = e
    8. self.reduction = reduction
    9. def _one_hot(self, labels, classes, value=1):
    10. """
    11. Convert labels to one hot vectors
    12. Args:
    13. labels: torch tensor in format [label1, label2, label3, ...]
    14. classes: int, number of classes
    15. value: label value in one hot vector, default to 1
    16. Returns:
    17. return one hot format labels in shape [batchsize, classes]
    18. """
    19. one_hot = torch.zeros(labels.size(0), classes)
    20. #labels and value_added size must match
    21. labels = labels.view(labels.size(0), -1)
    22. value_added = torch.Tensor(labels.size(0), 1).fill_(value)
    23. value_added = value_added.to(labels.device)
    24. one_hot = one_hot.to(labels.device)
    25. one_hot.scatter_add_(1, labels, value_added)
    26. return one_hot
    27. def _smooth_label(self, target, length, smooth_factor):
    28. """convert targets to one-hot format, and smooth them.
    29. Args:
    30. target: target in form with [label1, label2, label_batchsize]
    31. length: length of one-hot format(number of classes)
    32. smooth_factor: smooth factor for label smooth
    33. Returns:
    34. smoothed labels in one hot format
    35. """
    36. one_hot = self._one_hot(target, length, value=1 - smooth_factor)
    37. one_hot += smooth_factor / length
    38. return one_hot.to(target.device)

     2. 随即裁减拼接

    随机图像裁剪和拼接(Random Image Cropping and Patching)是一种数据增强技术,广泛应用于计算机视觉任务,特别是在深度学习模型的训练过程中。这种技术通过随机地裁剪图像的区域以及对这些区域进行重新排列或拼接,来生成视觉上多样化的训练样本。其目的和作用主要包括以下几点:

    1.增加数据多样性

    在训练深度学习模型时,数据的多样性是非常重要的。随机图像裁剪和拼接通过创造出视觉上不同的图像样本,有助于增加训练数据的多样性。这种多样性可以帮助模型学习到更加泛化的特征,从而在面对未见过的数据时能够做出更好的预测。

    2. 减少过拟合

    过拟合是深度学习中常见的问题,发生在模型对训练数据学得太好,以至于失去了泛化能力,对新的、未见过的数据表现不佳。通过随机图像裁剪和拼接,每次训练时输入模型的图像都有所不同,这可以有效减少模型对特定图像细节的依赖,从而减少过拟合的风险。

    3. 提高模型鲁棒性

    通过随机变化图像的外观(如位置、尺寸和上下文),随机图像裁剪和拼接强迫模型学习到更加鲁棒的特征,这些特征对于图像的具体排列和外观不那么敏感。这样,模型在遇到新的、不同排列或裁剪的图像时,更有可能做出正确的识别和预测。

    4. 支持不同尺寸的输入

    在某些情况下,模型可能需要处理不同尺寸的输入图像。随机图像裁剪和拼接可以在一定程度上模拟这种情况,训练模型能够更好地处理不同大小的图像,无需对图像进行标准化的预处理步骤。

     代码: 裁减

    1. import torch
    2. from torchvision import transforms
    3. from PIL import Image
    4. # 定义一个transform,其中包括随机裁剪和随机水平翻转
    5. transform = transforms.Compose([
    6. transforms.RandomResizedCrop(size=(224, 224)), # 随机裁剪到224x224
    7. transforms.RandomHorizontalFlip(), # 随机水平翻转
    8. # 可以添加更多的transforms,例如转换到Tensor,标准化等
    9. transforms.ToTensor()
    10. ])
    11. # 打开图像
    12. image = Image.open("path/to/your/image.jpg")
    13. # 应用transform
    14. transformed_image = transform(image)
    15. # 因为transforms.ToTensor()将PIL图像转换为了Tensor,
    16. # 我们需要使用matplotlib等库来可视化处理后的Tensor图像。
    17. import matplotlib.pyplot as plt
    18. plt.imshow(transformed_image.permute(1, 2, 0))
    19. plt.show()

     RICAP 代码:

    1. beta = 0.3 # hyperparameter
    2. for (images, targets) in train_loader:
    3. # get the image size
    4. I_x, I_y = images.size()[2:]
    5. # draw a boundry position (w, h)
    6. w = int(np.round(I_x * np.random.beta(beta, beta)))
    7. h = int(np.round(I_y * np.random.beta(beta, beta)))
    8. w_ = [w, I_x - w, w, I_x - w]
    9. h_ = [h, h, I_y - h, I_y - h]
    10. # select and crop four images
    11. cropped_images = {}
    12. c_ = {}
    13. W_ = {}
    14. for k in range(4):
    15. index = torch.randperm(images.size(0))
    16. x_k = np.random.randint(0, I_x - w_[k] + 1)
    17. y_k = np.random.randint(0, I_y - h_[k] + 1)
    18. cropped_images[k] = images[index][:, :, x_k:x_k + w_[k], y_k:y_k + h_[k]]
    19. c_[k] = target[index].cuda()
    20. W_[k] = w_[k] * h_[k] / (I_x * I_y)
    21. # patch cropped images
    22. patched_images = torch.cat(
    23. (torch.cat((cropped_images[0], cropped_images[1]), 2),
    24. torch.cat((cropped_images[2], cropped_images[3]), 2)),
    25. 3)
    26. #patched_images = patched_images.cuda()
    27. # get output
    28. output = model(patched_images)
    29. # calculate loss and accuracy
    30. loss = sum([W_[k] * criterion(output, c_[k]) for k in range(4)])
    31. acc = sum([W_[k] * accuracy(output, c_[k])[0] for k in range(4)])

     3.知识蒸馏

    提高几乎所有机器学习算法性能的一种非常简单的方法是在相同的数据上训练许多不同的模型,然后对它们的预测进行平均。但是使用所有的模型集成进行预测是比较麻烦的,并且可能计算量太大而无法部署到大量用户。Knowledge Distillation(知识蒸馏)方法就是应对这种问题的有效方法之一。

    在知识蒸馏方法中,我们使用一个教师模型来帮助当前的模型(学生模型)训练。教师模型是一个较高准确率的预训练模型,因此学生模型可以在保持模型复杂度不变的情况下提升准确率。比如,可以使用ResNet-152作为教师模型来帮助学生模型ResNet-50训练。在训练过程中,我们会加一个蒸馏损失来惩罚学生模型和教师模型的输出之间的差异。

    给定输入,假定p是真正的概率分布,z和r分别是学生模型和教师模型最后一个全连接层的输出。之前我们会用交叉熵损失l(p,softmax(z))来度量p和z之间的差异,这里的蒸馏损失同样用交叉熵。所以,使用知识蒸馏方法总的损失函数是

    1. !pip install torch torchvision
    2. import torch
    3. import torch.nn as nn
    4. import torch.nn.functional as F
    5. import torchvision.models as models
    6. # 定义学生模型
    7. class StudentModel(nn.Module):
    8. def __init__(self):
    9. super(StudentModel, self).__init__()
    10. self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
    11. self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
    12. self.pool = nn.MaxPool2d(2, 2)
    13. self.fc1 = nn.Linear(64 * 56 * 56, 128)
    14. self.fc2 = nn.Linear(128, 10) # 假设是10分类任务
    15. def forward(self, x):
    16. x = F.relu(self.conv1(x))
    17. x = self.pool(F.relu(self.conv2(x)))
    18. x = torch.flatten(x, 1)
    19. x = F.relu(self.fc1(x))
    20. x = self.fc2(x)
    21. return x
    22. # 使用预训练的ResNet作为教师模型
    23. teacher_model = models.resnet18(pretrained=True)
    24. # 初始化学生模型
    25. student_model = StudentModel()
    26. def distillation_loss(y_student, y_teacher, T):
    27. """
    28. 计算蒸馏损失
    29. :param y_student: 学生模型的输出
    30. :param y_teacher: 教师模型的输出
    31. :param T: 温度参数
    32. :return: 蒸馏损失
    33. """
    34. loss = F.kl_div(F.log_softmax(y_student / T, dim=1),
    35. F.softmax(y_teacher / T, dim=1),
    36. reduction='batchmean') * (T * T)
    37. return loss
    38. import torch.optim as optim
    39. # 定义优化器和损失函数
    40. optimizer = optim.SGD(student_model.parameters(), lr=0.001, momentum=0.9)
    41. criterion = nn.CrossEntropyLoss()
    42. # 假设我们有一个数据加载器
    43. # trainloader = ...
    44. # 训练学生模型
    45. for epoch in range(10): # 遍历数据集多次
    46. for inputs, labels in trainloader:
    47. optimizer.zero_grad()
    48. # 获取教师模型和学生模型的输出
    49. with torch.no_grad():
    50. outputs_teacher = teacher_model(inputs)
    51. outputs_student = student_model(inputs)
    52. # 计算蒸馏损失和分类损失
    53. loss_distillation = distillation_loss(outputs_student, outputs_teacher, T=20)
    54. loss_classification = criterion(outputs_student, labels)
    55. # 总损失是蒸馏损失和分类损失的加

     4. Cutout

    Cutout是一种新的正则化方法。原理是在训练时随机把图片的一部分减掉,这样能提高模型的鲁棒性。它的来源是计算机视觉任务中经常遇到的物体遮挡问题。通过cutout生成一些类似被遮挡的物体,不仅可以让模型在遇到遮挡问题时表现更好,还能让模型在做决定时更多地考虑环境(context)。

    1. import torch
    2. import numpy as np
    3. class Cutout(object):
    4. """Randomly mask out one or more patches from an image.
    5. Args:
    6. n_holes (int): Number of patches to cut out of each image.
    7. length (int): The length (in pixels) of each square patch.
    8. """
    9. def __init__(self, n_holes, length):
    10. self.n_holes = n_holes
    11. self.length = length
    12. def __call__(self, img):
    13. """
    14. Args:
    15. img (Tensor): Tensor image of size (C, H, W).
    16. Returns:
    17. Tensor: Image with n_holes of dimension length x length cut out of it.
    18. """
    19. h = img.size(1)
    20. w = img.size(2)
    21. mask = np.ones((h, w), np.float32)
    22. for n in range(self.n_holes):
    23. y = np.random.randint(h)
    24. x = np.random.randint(w)
    25. y1 = np.clip(y - self.length // 2, 0, h)
    26. y2 = np.clip(y + self.length // 2, 0, h)
    27. x1 = np.clip(x - self.length // 2, 0, w)
    28. x2 = np.clip(x + self.length // 2, 0, w)
    29. mask[y1: y2, x1: x2] = 0.
    30. mask = torch.from_numpy(mask)
    31. mask = mask.expand_as(img)
    32. img = img * mask
    33. return img

     5. mixup training

    Mixup training 是一种数据增强技术,用于提高深度学习模型的泛化能力和性能。这种技术最初由Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz 在论文 "mixup: Beyond Empirical Risk Minimization" 中提出。Mixup 通过在图像和标签级别进行线性插值,生成新的训练样本,从而增加训练数据的多样性。

    其中,λ是从Beta(α, α)随机采样的数,在[0,1]之间。在训练过程中,仅使用(xhat, yhat)。Mixup方法主要增强了训练样本之间的线性表达,增强网络的泛化能力,不过mixup方法需要较长的时间才能收敛得比较好。

    实现1:

    1. for (images, labels) in train_loader:
    2. l = np.random.beta(mixup_alpha, mixup_alpha)
    3. index = torch.randperm(images.size(0))
    4. images_a, images_b = images, images[index]
    5. labels_a, labels_b = labels, labels[index]
    6. mixed_images = l * images_a + (1 - l) * images_b
    7. outputs = model(mixed_images)
    8. loss = l * criterion(outputs, labels_a) + (1 - l) * criterion(outputs, labels_b)
    9. acc = l * accuracy(outputs, labels_a)[0] + (1 - l) * accuracy(outputs, labels_b)[0]

    实现2:

    1. for i,(images,target) in enumerate(train_loader):
    2. # 1.input output
    3. images = images.cuda(non_blocking=True)
    4. target = torch.from_numpy(np.array(target)).float().cuda(non_blocking=True)
    5. # 2.mixup
    6. alpha=config.alpha
    7. lam = np.random.beta(alpha,alpha)
    8. #randperm返回1~images.size(0)的一个随机排列
    9. index = torch.randperm(images.size(0)).cuda()
    10. inputs = lam*images + (1-lam)*images[index,:]
    11. targets_a, targets_b = target, target[index]
    12. outputs = model(inputs)
    13. loss = lam * criterion(outputs, targets_a) + (1 - lam) * criterion(outputs, targets_b)
    14. # 3.backward
    15. optimizer.zero_grad() # reset gradient
    16. loss.backward()

     6. AdaBound

    AdaBound 是一种优化算法,旨在结合 Adam 优化器的快速收敛性和 SGD 优化器在训练后期的稳定性。AdaBound 通过动态调整每一步的学习率,使其在训练过程中逐渐从 Adam 转变为 SGD。这种方法的关键在于,它为每个参数设置了动态的学习率边界,以确保学习率不会过高或过低。AdaBound 最初由 Luo 等人在论文 "Adaptive Gradient Methods with Dynamic Bound of Learning Rate" 中提出。

    1. AdaBound 与 AdamW 和 SGD 的区别

    • AdaBound:结合了 Adam 和 SGD 的优点,通过动态调整学习率边界来模仿 SGD 的学习率调度,旨在克服 Adam 在训练后期可能出现的性能下降问题。
    • AdamW:是 Adam 的一个变种,引入了权重衰减(L2 正则化)作为优化的一部分,而不是作为损失函数的一部分,以解决 Adam 优化器在正则化时可能出现的问题。
    • SGD:随机梯度下降,是最基本的优化算法,通常配合动量(Momentum)和学习率调度器使用,以加快收敛速度并提高模型的最终性能。

    2. 安装adabound

    pip install adabound
    

    3. 使用adabound

    optimizer = adabound.AdaBound(model.parameters(), lr=1e-3, final_lr=0.1)
    

    7. AutoAugment

    AutoAugment 是一种自动化的数据增强技术,主要应用于深度学习和机器学习领域,特别是在图像识别和处理任务中。其核心目标是自动发现最优的数据增强策略,从而提高模型的性能和泛化能力。数据增强是指在不改变原始数据标签的情况下,通过各种方法扩充训练数据集的技术,如旋转、翻转、缩放图像等,来提升模型对新未见数据的适应性和鲁棒性。

     AutoAugment 通过搜索最优的数据增强策略来实现自动化增强。它利用强化学习,特别是一种称为控制器的模型,来预测给定任务的最佳增强策略。这个控制器通过尝试不同的增强操作(如图像的色彩平衡调整、剪裁、旋转等)并观察它们对模型性能的影响,来学习哪些操作可以提高模型的准确率

    • 提高模型准确性:通过增加数据多样性,减少过拟合,使模型在未见过的数据上表现更好。
    • 减少手工调参需求:自动化寻找最佳数据增强策略,减轻了人工选择和调整数据增强方法的负担。
    • 增强模型泛化能力:更广泛的数据表示有助于模型在多种条件下保持性能,尤其是在现实世界的应用中。
    1. """AutoAugment data augmentation method based on
    2. `"AutoAugment: Learning Augmentation Strategies
    3. from Data" `_.
    4. If the image is torch Tensor,
    5. it should be of type torch.uint8, and it is expected
    6. to have [..., 1 or 3, H, W] shape,
    7. where ... means an arbitrary number of leading dimensions.
    8. If img is PIL Image, it is expected to be in mode "L" or "RGB".
    9. Args:
    10. policy (AutoAugmentPolicy): Desired policy enum defined by
    11. :class:`torchvision.transforms.autoaugment.AutoAugmentPolicy`. Default is ``AutoAugmentPolicy.IMAGENET``.
    12. interpolation (InterpolationMode): Desired interpolation enum defined by
    13. :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
    14. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
    15. fill (sequence or number, optional): Pixel fill value for the area outside the transformed

     

    1. import torch
    2. import torchvision
    3. from torchvision import datasets, transforms
    4. from torchvision.transforms import AutoAugment, AutoAugmentPolicy
    5. # 定义数据预处理和增强
    6. #AutoAugmentPolicy 可以根据你的需求选择不同的策略(如 CIFAR10、SVHN 策略等)
    7. transform = transforms.Compose([
    8. transforms.Resize((32, 32)), # 调整图像大小
    9. AutoAugment(policy=AutoAugmentPolicy.IMAGENET), # 应用 AutoAugment,这里以 ImageNet 策略为例
    10. transforms.ToTensor(), # 将图像转换为 PyTorch 张量
    11. transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # 归一化
    12. ])
    13. # 加载数据集(以 CIFAR10 为例)
    14. train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
    15. # 创建数据加载器
    16. train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)
    17. # 下面可以定义和训练你的模型...

  • 相关阅读:
    PbootCMS 应用快速迁移至浪潮信息 KeyarchOS 云服务器
    为什么短信验证码要设置有效期?
    Java Virtual Machine(一)
    记一次使用LruCache并探究
    【Vue基础一】
    【SysBench】Linux 安装 sysbench-1.20
    基于智能优化算法PSO/GWO/AFO+柔性车间生产调度(Matlab代码实现)
    【区块链实战】如何创建一个带参数的智能合约
    JavaSE 基础(十三)网络编程
    一文详解 implementation api embed
  • 原文地址:https://blog.csdn.net/qq_40837542/article/details/136404651