zoukankan      html  css  js  c++  java
  • 图片 Augmentation整理

    Augmentation

    原图:
    在这里插入图片描述

    Flipping 翻转

    在这里插入图片描述
    在这里插入图片描述

    Grayscale

    在这里插入图片描述

    Equalize 均衡直方图

    在这里插入图片描述

    Posterize 减少颜色通道位数

    在这里插入图片描述

    Cropping

    在这里插入图片描述

    Rotation

    在这里插入图片描述

    Translation

    在这里插入图片描述

    Noise injection

    在这里插入图片描述
    在这里插入图片描述

    Hue 色调

    在这里插入图片描述

    Brightness

    在这里插入图片描述

    Saturation

    在这里插入图片描述

    Contrast

    在这里插入图片描述

    Erasing 抹去

    在这里插入图片描述

    Augmix

    在这里插入图片描述

    代码

    config.json:

    
    {
      "Crop": {
        "size": [100, 200]
      },
      "Rotate": {
        "degrees": 20
      },
      "Translate": {
        "translate": [0.3, 0.3]
      },
      "Scale": {
        "scale": [0.7, 1.3]
      },
      "Shear": {
        "shear": [10, 100, 10, 100]
      },
      "Hflip": {},
      "Vflip": {},
      "Equalize": {
        "mask": null
      },
      "Posterize": {
        "bits" : 2
      },
      "Grayscale": {
        "num_output_channels": 1
      },
      "Normalnoise": {
        "loc": 0.0,
        "scale": 0.1
      },
      "Uniformnoise": {
        "alpha": 0.2
      },
      "Contrast": {
        "contrast": 0.8
      },
      "Brightness": {
        "brightness": 0.7
      },
      "Saturation": {
        "saturation": 0.7
      },
      "Hue": {
        "hue": 0.5
      },
      "Erase": {
        "value": 100
      }
    
    }
    
    
    """
    ops.py
    """
    
    import torch
    import torchvision.transforms.functional as F
    import PIL.ImageOps
    import random
    from torchvision import transforms
    import scipy.stats
    from collections.abc import Iterable
    
    
    def special(cls, name, *args, **kwargs):
        class Newclass(cls):
            def __init__(self, *newargs, **newkwargs):
                newargs += args
                newkwargs.update(kwargs)
                super(Newclass, self).__init__(*newargs, **newkwargs)
        Newclass.__name__ = name
        return Newclass
    
    
    class Hflip:
        """
        水平翻转
        """
        def __call__(self, img):
            return F.hflip(img)
    
    class Vflip:
        """
        上下翻转
        """
        def __call__(self, img):
            return F.vflip(img)
    
    class Equalize:
        """
        均衡图像直方图
        """
        def __init__(self, mask):
            self.mask = mask
    
        def __call__(self, img):
            return PIL.ImageOps.equalize(img, self.mask)
    
    class Posterize:
        """
        减少每个颜色通道的位数
        """
        def __init__(self, bits):
            self.bits = bits
    
        def __call__(self, img):
            return PIL.ImageOps.posterize(img, self.bits)
    
    class Grayscale:
        def __init__(self, num_output_channels=1):
            self.num_output_channels = num_output_channels
    
        def __call__(self, img):
            return F.to_grayscale(img, self.num_output_channels)
    
    class Normalnoise:
        def __init__(self, loc, scale):
            self.loc = loc
            self.scale = scale
    
        def __call__(self, img):
            img = F.to_tensor(img)
            img += (torch.randn_like(img) + self.loc) * self.scale
            img = torch.clamp(img, 0., 1.)
            return F.to_pil_image(img)
    
    class Uniformnoise:
        def __init__(self, alpha):
            if alpha < 0 or alpha > 1:
                raise ValueError("alpha should be in [0, 1]...")
            self.alpha = alpha
    
        def __call__(self, img):
            img = F.to_tensor(img)
            img += torch.rand_like(img) * self.alpha
            img = torch.clamp(img, 0., 1.)
            return F.to_pil_image(img)
    
    class Erase:
    
        def __init__(self, value):
            self.value = value
    
        def __call__(self, img):
            img = F.to_tensor(img)
            H, W = img.size()[-2:]
            lui = torch.randint(0, H, (1,)).item()
            luj = torch.randint(0, W, (1,)).item()
            rbi = torch.randint(lui, H, (1,)).item()
            rbj = torch.randint(luj, W, (1,)).item()
            h = rbj - luj
            w = rbi - lui
    
            return F.to_pil_image(F.erase(img, lui, luj, h, w, 0))
    
    class Perspective:
    
        def __init__(self, startpoints, endpoints, interpolation=3):
            self.startpoints = startpoints
            self.endpoints = endpoints
            self.interpolation = interpolation
    
        def __call__(self, img):
            return F.perspective(img, self.startpoints,
                                 self.endpoints, self.interpolation)
    
    
    Translate = special(transforms.RandomAffine, "Translate", degrees = 0)
    Scale = special(transforms.RandomAffine, "Scale", degrees = 0)
    Shear = special(transforms.RandomAffine, "Shear", degrees=0)
    Rotate = special(transforms.RandomAffine, "Rotate")
    Brightness = special(transforms.ColorJitter, "Brightness")
    Contrast = special(transforms.ColorJitter, "Contrast")
    Saturation = special(transforms.ColorJitter, "Saturation")
    Hue = special(transforms.ColorJitter, "Hue")
    Crop = transforms.RandomCrop
    
    
    
    class Augmix:
    
        def __init__(self, ops, k=3, alpha=1, beta=1):
            self.ops = ops
            self.k = k
            if isinstance(alpha, Iterable):
                self.alpha = alpha
            else:
                self.alpha = [alpha] * k
            self.beta = beta
    
        def get_params(self):
            op1, op2, op3 = random.sample(self.ops, 3)
            op12 = transforms.Compose([op1, op2])
            op123 = transforms.Compose([op1, op2, op3])
            return random.sample([op1, op12, op123], 1)[0]
    
        def __call__(self, img):
            weights = scipy.stats.dirichlet.rvs(self.alpha)[0]
            img_tensor = F.to_tensor(img)
            xaug = torch.zeros_like(img_tensor)
            for i in range(self.k):
                opschain = self.get_params()
                temp = weights[i] * F.to_tensor(opschain(img))
                xaug += temp
            m = scipy.stats.beta.rvs(self.beta, self.beta, size=1)[0]
            new_img = m * img_tensor + (1 - m) * xaug
            return F.to_pil_image(new_img)
    
    
    
    
    
    
    
    
  • 相关阅读:
    Linux内核的异常修复原理
    sudo: insmod: command not found
    在Qemu+ARM上运行Minix3内核
    2021.34 面对干扰
    2021.33 实践
    selenium+python自动化106
    python测试开发django-111.模型管理器(models.Manager)
    python笔记64
    python笔记63
    python笔记62
  • 原文地址:https://www.cnblogs.com/MTandHJ/p/12555156.html
Copyright © 2011-2022 走看看