前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >测试数据增强_预测模型最佳cutoff值

测试数据增强_预测模型最佳cutoff值

作者头像
全栈程序员站长
发布2022-11-09 17:02:33
3650
发布2022-11-09 17:02:33
举报

大家好,又见面了,我是你们的朋友全栈君。

cutout是2017年提出的一种数据增强方法,想法比较简单,即在训练时随机裁剪掉图像的一部分,也可以看作是一种类似dropout的正则化方法。

Improved Regularization of Convolutional Neural Networks with Cutout

paper: https://arxiv.org/pdf/1708.04552.pdf

code: https://github.com/uoguelph-mlrg/Cutout


cutout采用的操作是随机裁剪掉图像中的一块正方形区域,并在原图中补0。由于作者在cutout早期版本中使用了不规则大小区域的方式,但是对比发现,固定大小区域能达到同等的效果,因此就没必要这么麻烦去生成不规则区域了。

实现代码比较简单,cutout.py,如下:

代码语言:javascript
复制
import torch
import numpy as np


class Cutout(object):
    """Randomly mask out one or more patches from an image.

    Args:
        n_holes (int): Number of patches to cut out of each image.
        length (int): The length (in pixels) of each square patch.
    """
    def __init__(self, n_holes=1, length=16):
        self.n_holes = n_holes
        self.length = length

    def __call__(self, img):
        """
        Args:
            img (Tensor): Tensor image of size (C, H, W).
        Returns:
            Tensor: Image with n_holes of dimension length x length cut out of it.
        """
        h = img.size(1)
        w = img.size(2)

        mask = np.ones((h, w), np.float32)

        for n in range(self.n_holes):
            y = np.random.randint(h)
            x = np.random.randint(w)

            y1 = np.clip(y - self.length // 2, 0, h)
            y2 = np.clip(y + self.length // 2, 0, h)
            x1 = np.clip(x - self.length // 2, 0, w)
            x2 = np.clip(x + self.length // 2, 0, w)

            mask[y1: y2, x1: x2] = 0.

        mask = torch.from_numpy(mask)
        mask = mask.expand_as(img)
        img = img * mask

        return img

上面代码中有两个参数,具体如下:

n_holes:表示裁剪掉的图像块的数目,默认都是设置为1;

length:每个正方形块的边长,作者经过多轮尝试后,不同数据集最优设置不同,CIFAR10为16,CIFAR100为8,SVHN为20;# 这里觉得挺麻烦的,cutout调参很重要

看看在图像上cutout是什么效果,代码如下:

代码语言:javascript
复制
import cv2
from torchvision import transforms
from cutout import Cutout

# 执行cutout
img = cv2.imread('cat.png')
img = transforms.ToTensor()(img)
cut = Cutout(length=100)
img = cut(img)

# cutout图像写入本地
img = img.mul(255).byte()
img = img.numpy().transpose((1, 2, 0))
cv2.imwrite('cutout.png', img)

由于原图比较大,这里把正方形边长调到了100,效果如下:

测试数据增强_预测模型最佳cutoff值
测试数据增强_预测模型最佳cutoff值

实际训练看看效果到底怎么样,为了保证公平,训练时参数统一,且每种模型训练了8次以减少随机性,结果见下表。

Method

CIFAR-10

CIFAR-100

ResNet-50

96.76/96.82/96.81/96.79 96.72/96.69/96.60/96.82 (96.75)

83.80/83.66/84.19/83.26 83.89/83.90/83.57/83.69 (83.74)

ResNet-50+cutout

96.73/96.58/96.78/96.65 96.65/96.58/96.77/96.65 (96.67)

83.63/83.78/83.80/83.49 83.92/83.57/83.71/83.60 (83.69)

从实验结果来看,在CIFAR10和CIFAR100这两个数据集上使用cutout,训练出来的模型精度都会掉一点。看来cutout涨点并没有那么容易,和调参、模型深度、数据集都有很大的关系。


数据增强实测之Random Erasing_一个菜鸟的奋斗-CSDN博客

数据增强实测之mixup_一个菜鸟的奋斗-CSDN博客

数据增强实测之RICAP_一个菜鸟的奋斗-CSDN博客

数据增强实测之GridMask_一个菜鸟的奋斗-CSDN博客

数据增强实测之Hide-and-Seek_一个菜鸟的奋斗-CSDN博客

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月25日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档