MCPcopy Create free account
hub / github.com/QWTforGithub/T2LDM / RandomErasing

Class RandomErasing

timm/data/random_erasing.py:25–103  ·  view source on GitHub ↗

Randomly selects a rectangle region in an image and erases its pixels. 'Random Erasing Data Augmentation' by Zhong et al. See https://arxiv.org/pdf/1708.04896.pdf This variant of RandomErasing is intended to be applied to either a batch or single image tensor after

Source from the content-addressed store, hash-verified

23
24
25class RandomErasing:
26 """ Randomly selects a rectangle region in an image and erases its pixels.
27 'Random Erasing Data Augmentation' by Zhong et al.
28 See https://arxiv.org/pdf/1708.04896.pdf
29
30 This variant of RandomErasing is intended to be applied to either a batch
31 or single image tensor after it has been normalized by dataset mean and std.
32 Args:
33 probability: Probability that the Random Erasing operation will be performed.
34 min_area: Minimum percentage of erased area wrt input image area.
35 max_area: Maximum percentage of erased area wrt input image area.
36 min_aspect: Minimum aspect ratio of erased area.
37 mode: pixel color mode, one of 'const', 'rand', or 'pixel'
38 'const' - erase block is constant color of 0 for all channels
39 'rand' - erase block is same per-channel random (normal) color
40 'pixel' - erase block is per-pixel random (normal) color
41 max_count: maximum number of erasing blocks per image, area per box is scaled by count.
42 per-image count is randomly chosen between 1 and this value.
43 """
44
45 def __init__(
46 self,
47 probability=0.5, min_area=0.02, max_area=1/3, min_aspect=0.3, max_aspect=None,
48 mode='const', min_count=1, max_count=None, num_splits=0, device='cuda'):
49 self.probability = probability
50 self.min_area = min_area
51 self.max_area = max_area
52 max_aspect = max_aspect or 1 / min_aspect
53 self.log_aspect_ratio = (math.log(min_aspect), math.log(max_aspect))
54 self.min_count = min_count
55 self.max_count = max_count or min_count
56 self.num_splits = num_splits
57 self.mode = mode.lower()
58 self.rand_color = False
59 self.per_pixel = False
60 if self.mode == 'rand':
61 self.rand_color = True # per block random normal
62 elif self.mode == 'pixel':
63 self.per_pixel = True # per pixel random normal
64 else:
65 assert not self.mode or self.mode == 'const'
66 self.device = device
67
68 def _erase(self, img, chan, img_h, img_w, dtype):
69 if random.random() > self.probability:
70 return
71 area = img_h * img_w
72 count = self.min_count if self.min_count == self.max_count else \
73 random.randint(self.min_count, self.max_count)
74 for _ in range(count):
75 for attempt in range(10):
76 target_area = random.uniform(self.min_area, self.max_area) * area / count
77 aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio))
78 h = int(round(math.sqrt(target_area * aspect_ratio)))
79 w = int(round(math.sqrt(target_area / aspect_ratio)))
80 if w < img_w and h < img_h:
81 top = random.randint(0, img_h - h)
82 left = random.randint(0, img_w - w)

Callers 2

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected