-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaugment.py
147 lines (122 loc) · 5.22 KB
/
augment.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
import random
import numpy as np
import tensorflow as tf
import tensorflow_addons as tfa
mean_std = [[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]]
class Augment:
def __init__(self, args, mode='train'):
self.args = args
self.mode = mode
self.mean, self.std = mean_std
def _augmentv1(self, x, shape, coord=[[[0., 0., 1., 1.]]]):
x = self._crop(x, shape, coord)
x = self._resize(x)
x = self._random_grayscale(x, p=.2)
x = self._color_jitter(x)
x = self._random_hflip(x)
x = self._standardize(x)
return x
def _augmentv2(self, x, shape, radius, coord=[[[0., 0., 1., 1.]]]):
x = self._crop(x, shape, coord)
x = self._resize(x)
x = self._random_color_jitter(x, p=.8)
x = self._random_grayscale(x, p=.2)
x = self._random_gaussian_blur(x, radius, p=.5)
x = self._random_hflip(x)
x = self._standardize(x)
return x
def _augment_lincls(self, x, shape, coord=[[[0., 0., 1., 1.]]]):
x = self._crop(x, shape, coord)
x = self._resize(x)
x = self._standardize(x)
return x
def _standardize(self, x):
x = tf.cast(x, tf.float32)
x /= 255.
x -= self.mean
x /= self.std
return x
def _crop(self, x, shape, coord=[[[0., 0., 1., 1.]]]):
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
image_size=shape,
bounding_boxes=coord,
area_range=(.2, 1.),
max_attempts=100,
use_image_if_no_bounding_boxes=True)
offset_height, offset_width, _ = tf.unstack(bbox_begin)
target_height, target_width, _ = tf.unstack(bbox_size)
x = tf.slice(x, [offset_height, offset_width, 0], [target_height, target_width, 3])
return x
def _resize(self, x):
x = tf.image.resize(x, (self.args.img_size, self.args.img_size))
x = tf.saturate_cast(x, tf.uint8)
return x
def _color_jitter(self, x, _jitter_idx=[0, 1, 2, 3]):
random.shuffle(_jitter_idx)
_jitter_list = [
self._brightness,
self._contrast,
self._saturation,
self._hue]
for idx in _jitter_idx:
x = _jitter_list[idx](x)
return x
def _random_color_jitter(self, x, p=.8):
if tf.less(tf.random.uniform(shape=[], minval=0, maxval=1, dtype=tf.float32), tf.cast(p, tf.float32)):
x = self._color_jitter(x)
return x
def _brightness(self, x):
''' Brightness in torchvision is implemented about multiplying the factor to image,
but tensorflow.image is just implemented about adding the factor to image.
In tensorflow.image.adjust_brightness,
For regular images, `delta` should be in the range `[0,1)`,
as it is added to the image in floating point representation,
where pixel values are in the `[0,1)` range.
adjusted = math_ops.add(
flt_image, math_ops.cast(delta, flt_image.dtype), name=name)
However in torchvision docs,
Args:
brightness (float or tuple of float (min, max)): How much to jitter brightness.
brightness_factor is chosen uniformly from [max(0, 1 - brightness), 1 + brightness]
or the given [min, max]. Should be non negative numbers.
In torchvision.transforms.functional_tensor,
return _blend(img, torch.zeros_like(img), brightness_factor)
where _blend
return brightness * img1
'''
# x = tf.image.random_brightness(x, max_delta=self.args.brightness)
x = tf.cast(x, tf.float32)
delta = tf.random.uniform(
shape=[],
minval=1-self.args.brightness,
maxval=1+self.args.brightness,
dtype=tf.float32)
x *= delta
x = tf.saturate_cast(x, tf.uint8)
return x
def _contrast(self, x):
x = tf.image.random_contrast(x, lower=max(0, 1-self.args.contrast), upper=1+self.args.contrast)
x = tf.saturate_cast(x, tf.uint8)
return x
def _saturation(self, x):
x = tf.image.random_saturation(x, lower=max(0, 1-self.args.contrast), upper=1+self.args.contrast)
x = tf.saturate_cast(x, tf.uint8)
return x
def _hue(self, x):
x = tf.image.random_hue(x, max_delta=self.args.hue)
x = tf.saturate_cast(x, tf.uint8)
return x
def _grayscale(self, x):
return tf.image.rgb_to_grayscale(x) # after expand_dims
def _random_grayscale(self, x, p=.2):
if tf.less(tf.random.uniform(shape=[], minval=0, maxval=1, dtype=tf.float32), tf.cast(p, tf.float32)):
x = self._grayscale(x)
x = tf.tile(x, [1, 1, 3])
return x
def _random_hflip(self, x):
return tf.image.random_flip_left_right(x)
def _random_gaussian_blur(self, x, radius, p=.5):
if tf.less(tf.random.uniform(shape=[], minval=0, maxval=1, dtype=tf.float32), tf.cast(p, tf.float32)):
x = tfa.image.gaussian_filter2d(x, filter_shape=radius)
return x