-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChannelAug.py
202 lines (161 loc) · 6.51 KB
/
ChannelAug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from __future__ import absolute_import
from torchvision.transforms import *
#from PIL import Image
import random
import math
#import numpy as np
#import torch
class ChannelExchange(object):
""" Adaptive selects a channel or two channels.
Args:
probability: The probability that the Random Erasing operation will be performed.
sl: Minimum proportion of erased area against input image.
sh: Maximum proportion of erased area against input image.
r1: Minimum aspect ratio of erased area.
mean: Erasing value.
"""
def __init__(self, gray = 2):
self.gray = gray
def __call__(self, img):
idx = random.randint(0, self.gray)
if idx ==0:
# random select R Channel
img[1, :,:] = img[0,:,:]
img[2, :,:] = img[0,:,:]
elif idx ==1:
# random select B Channel
img[0, :,:] = img[1,:,:]
img[2, :,:] = img[1,:,:]
elif idx ==2:
# random select G Channel
img[0, :,:] = img[2,:,:]
img[1, :,:] = img[2,:,:]
else:
tmp_img = 0.2989 * img[0,:,:] + 0.5870 * img[1,:,:] + 0.1140 * img[2,:,:]
img[0,:,:] = tmp_img
img[1,:,:] = tmp_img
img[2,:,:] = tmp_img
return img
class ChannelAdap(object):
""" Adaptive selects a channel or two channels.
Args:
probability: The probability that the Random Erasing operation will be performed.
sl: Minimum proportion of erased area against input image.
sh: Maximum proportion of erased area against input image.
r1: Minimum aspect ratio of erased area.
mean: Erasing value.
"""
def __init__(self, probability = 0.5):
self.probability = probability
def __call__(self, img):
# if random.uniform(0, 1) > self.probability:
# return img
idx = random.randint(0, 3)
if idx ==0:
# random select R Channel
img[1, :,:] = img[0,:,:]
img[2, :,:] = img[0,:,:]
elif idx ==1:
# random select B Channel
img[0, :,:] = img[1,:,:]
img[2, :,:] = img[1,:,:]
elif idx ==2:
# random select G Channel
img[0, :,:] = img[2,:,:]
img[1, :,:] = img[2,:,:]
else:
img = img
return img
class ChannelAdapGray(object):
""" Adaptive selects a channel or two channels.
Args:
probability: The probability that the Random Erasing operation will be performed.
sl: Minimum proportion of erased area against input image.
sh: Maximum proportion of erased area against input image.
r1: Minimum aspect ratio of erased area.
mean: Erasing value.
"""
def __init__(self, probability = 0.5):
self.probability = probability
def __call__(self, img):
# if random.uniform(0, 1) > self.probability:
# return img
idx = random.randint(0, 3)
if idx ==0:
# random select R Channel
img[1, :,:] = img[0,:,:]
img[2, :,:] = img[0,:,:]
elif idx ==1:
# random select B Channel
img[0, :,:] = img[1,:,:]
img[2, :,:] = img[1,:,:]
elif idx ==2:
# random select G Channel
img[0, :,:] = img[2,:,:]
img[1, :,:] = img[2,:,:]
else:
if random.uniform(0, 1) > self.probability:
# return img
img = img
else:
tmp_img = 0.2989 * img[0,:,:] + 0.5870 * img[1,:,:] + 0.1140 * img[2,:,:]
img[0,:,:] = tmp_img
img[1,:,:] = tmp_img
img[2,:,:] = tmp_img
return img
class Gray(object):
""" Adaptive selects a channel or two channels.
Args:
probability: The probability that the Random Erasing operation will be performed.
sl: Minimum proportion of erased area against input image.
sh: Maximum proportion of erased area against input image.
r1: Minimum aspect ratio of erased area.
mean: Erasing value.
"""
def __init__(self, probability = 0.5):
self.probability = probability
def __call__(self, img):
# if random.uniform(0, 1) > self.probability:
# return img
tmp_img = 0.2989 * img[0,:,:] + 0.5870 * img[1,:,:] + 0.1140 * img[2,:,:]
img[0,:,:] = tmp_img
img[1,:,:] = tmp_img
img[2,:,:] = tmp_img
return img
class ChannelRandomErasing(object):
""" 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
Args:
probability: The probability that the Random Erasing operation will be performed.
sl: Minimum proportion of erased area against input image.
sh: Maximum proportion of erased area against input image.
r1: Minimum aspect ratio of erased area.
mean: Erasing value.
"""
def __init__(self, probability = 0.5, sl = 0.02, sh = 0.4, r1 = 0.3, mean=[0.4914, 0.4822, 0.4465]):
self.probability = probability
self.mean = mean
self.sl = sl
self.sh = sh
self.r1 = r1
def __call__(self, img):
if random.uniform(0, 1) > self.probability:
return img
for attempt in range(100):
area = img.size()[1] * img.size()[2]
target_area = random.uniform(self.sl, self.sh) * area
aspect_ratio = random.uniform(self.r1, 1/self.r1)
h = int(round(math.sqrt(target_area * aspect_ratio)))
w = int(round(math.sqrt(target_area / aspect_ratio)))
if w < img.size()[2] and h < img.size()[1]:
x1 = random.randint(0, img.size()[1] - h)
y1 = random.randint(0, img.size()[2] - w)
if img.size()[0] == 3:
img[0, x1:x1+h, y1:y1+w] = self.mean[0]
img[1, x1:x1+h, y1:y1+w] = self.mean[1]
img[2, x1:x1+h, y1:y1+w] = self.mean[2]
else:
img[0, x1:x1+h, y1:y1+w] = self.mean[0]
return img
return img