-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgridmask_tf.py
50 lines (36 loc) · 2.06 KB
/
gridmask_tf.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
import numpy as np
import tensorflow as tf
from rotate_tf import transform, random_rotate
def GridMask(image_height, image_width, d1, d2, rotate_angle=1, ratio=0.5):
h, w = image_height, image_width
hh = int(np.ceil(np.sqrt(h*h+w*w)))
hh = hh+1 if hh%2==1 else hh
d = tf.random.uniform(shape=[], minval=d1, maxval=d2, dtype=tf.int32)
l = tf.cast(tf.cast(d,tf.float32)*ratio+0.5, tf.int32)
st_h = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
st_w = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
y_ranges = tf.range(-1 * d + st_h, -1 * d + st_h + l)
x_ranges = tf.range(-1 * d + st_w, -1 * d + st_w + l)
for i in range(0, hh//d+1):
s1 = i * d + st_h
s2 = i * d + st_w
y_ranges = tf.concat([y_ranges, tf.range(s1,s1+l)], axis=0)
x_ranges = tf.concat([x_ranges, tf.range(s2,s2+l)], axis=0)
x_clip_mask = tf.logical_or(x_ranges <0 , x_ranges > hh-1)
y_clip_mask = tf.logical_or(y_ranges <0 , y_ranges > hh-1)
clip_mask = tf.logical_or(x_clip_mask, y_clip_mask)
x_ranges = tf.boolean_mask(x_ranges, tf.logical_not(clip_mask))
y_ranges = tf.boolean_mask(y_ranges, tf.logical_not(clip_mask))
hh_ranges = tf.tile(tf.range(0,hh), [tf.cast(tf.reduce_sum(tf.ones_like(x_ranges)), tf.int32)])
x_ranges = tf.repeat(x_ranges, hh)
y_ranges = tf.repeat(y_ranges, hh)
y_hh_indices = tf.transpose(tf.stack([y_ranges, hh_ranges]))
x_hh_indices = tf.transpose(tf.stack([hh_ranges, x_ranges]))
y_mask_sparse = tf.SparseTensor(tf.cast(y_hh_indices, tf.int64), tf.zeros_like(y_ranges), [hh, hh])
y_mask = tf.sparse.to_dense(y_mask_sparse, 1, False)
x_mask_sparse = tf.SparseTensor(tf.cast(x_hh_indices, tf.int64), tf.zeros_like(x_ranges), [hh, hh])
x_mask = tf.sparse.to_dense(x_mask_sparse, 1, False)
mask = tf.expand_dims( tf.clip_by_value(x_mask + y_mask, 0, 1), axis=-1)
mask = random_rotate(mask, rotate_angle, [hh, hh, 1])
mask = tf.image.crop_to_bounding_box(mask, (hh-h)//2, (hh-w)//2, image_height, image_width)
return mask