pytorch-image-models

Форк
0
/
random_erasing.py 
117 строк · 4.8 Кб
1
""" Random Erasing (Cutout)
2

3
Originally inspired by impl at https://github.com/zhunzhong07/Random-Erasing, Apache 2.0
4
Copyright Zhun Zhong & Liang Zheng
5

6
Hacked together by / Copyright 2019, Ross Wightman
7
"""
8
import random
9
import math
10

11
import torch
12

13

14
def _get_pixels(per_pixel, rand_color, patch_size, dtype=torch.float32, device='cuda'):
15
    # NOTE I've seen CUDA illegal memory access errors being caused by the normal_()
16
    # paths, flip the order so normal is run on CPU if this becomes a problem
17
    # Issue has been fixed in master https://github.com/pytorch/pytorch/issues/19508
18
    if per_pixel:
19
        return torch.empty(patch_size, dtype=dtype, device=device).normal_()
20
    elif rand_color:
21
        return torch.empty((patch_size[0], 1, 1), dtype=dtype, device=device).normal_()
22
    else:
23
        return torch.zeros((patch_size[0], 1, 1), dtype=dtype, device=device)
24

25

26
class RandomErasing:
27
    """ Randomly selects a rectangle region in an image and erases its pixels.
28
        'Random Erasing Data Augmentation' by Zhong et al.
29
        See https://arxiv.org/pdf/1708.04896.pdf
30

31
        This variant of RandomErasing is intended to be applied to either a batch
32
        or single image tensor after it has been normalized by dataset mean and std.
33
    Args:
34
         probability: Probability that the Random Erasing operation will be performed.
35
         min_area: Minimum percentage of erased area wrt input image area.
36
         max_area: Maximum percentage of erased area wrt input image area.
37
         min_aspect: Minimum aspect ratio of erased area.
38
         mode: pixel color mode, one of 'const', 'rand', or 'pixel'
39
            'const' - erase block is constant color of 0 for all channels
40
            'rand'  - erase block is same per-channel random (normal) color
41
            'pixel' - erase block is per-pixel random (normal) color
42
        max_count: maximum number of erasing blocks per image, area per box is scaled by count.
43
            per-image count is randomly chosen between 1 and this value.
44
    """
45

46
    def __init__(
47
            self,
48
            probability=0.5,
49
            min_area=0.02,
50
            max_area=1/3,
51
            min_aspect=0.3,
52
            max_aspect=None,
53
            mode='const',
54
            min_count=1,
55
            max_count=None,
56
            num_splits=0,
57
            device='cuda',
58
    ):
59
        self.probability = probability
60
        self.min_area = min_area
61
        self.max_area = max_area
62
        max_aspect = max_aspect or 1 / min_aspect
63
        self.log_aspect_ratio = (math.log(min_aspect), math.log(max_aspect))
64
        self.min_count = min_count
65
        self.max_count = max_count or min_count
66
        self.num_splits = num_splits
67
        self.mode = mode.lower()
68
        self.rand_color = False
69
        self.per_pixel = False
70
        if self.mode == 'rand':
71
            self.rand_color = True  # per block random normal
72
        elif self.mode == 'pixel':
73
            self.per_pixel = True  # per pixel random normal
74
        else:
75
            assert not self.mode or self.mode == 'const'
76
        self.device = device
77

78
    def _erase(self, img, chan, img_h, img_w, dtype):
79
        if random.random() > self.probability:
80
            return
81
        area = img_h * img_w
82
        count = self.min_count if self.min_count == self.max_count else \
83
            random.randint(self.min_count, self.max_count)
84
        for _ in range(count):
85
            for attempt in range(10):
86
                target_area = random.uniform(self.min_area, self.max_area) * area / count
87
                aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio))
88
                h = int(round(math.sqrt(target_area * aspect_ratio)))
89
                w = int(round(math.sqrt(target_area / aspect_ratio)))
90
                if w < img_w and h < img_h:
91
                    top = random.randint(0, img_h - h)
92
                    left = random.randint(0, img_w - w)
93
                    img[:, top:top + h, left:left + w] = _get_pixels(
94
                        self.per_pixel,
95
                        self.rand_color,
96
                        (chan, h, w),
97
                        dtype=dtype,
98
                        device=self.device,
99
                    )
100
                    break
101

102
    def __call__(self, input):
103
        if len(input.size()) == 3:
104
            self._erase(input, *input.size(), input.dtype)
105
        else:
106
            batch_size, chan, img_h, img_w = input.size()
107
            # skip first slice of batch if num_splits is set (for clean portion of samples)
108
            batch_start = batch_size // self.num_splits if self.num_splits > 1 else 0
109
            for i in range(batch_start, batch_size):
110
                self._erase(input[i], chan, img_h, img_w, input.dtype)
111
        return input
112

113
    def __repr__(self):
114
        # NOTE simplified state for repr
115
        fs = self.__class__.__name__ + f'(p={self.probability}, mode={self.mode}'
116
        fs += f', count=({self.min_count}, {self.max_count}))'
117
        return fs
118

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.