Diff of /diff_augment.py [000000] .. [277df6]

Switch to unified view

a b/diff_augment.py
1
# Differentiable Augmentation for Data-Efficient GAN Training
2
# Shengyu Zhao, Zhijian Liu, Ji Lin, Jun-Yan Zhu, and Song Han
3
# https://arxiv.org/pdf/2006.10738
4
# https://github.com/mit-han-lab/data-efficient-gans/blob/master/DiffAugment_tf.py
5
6
import tensorflow as tf
7
8
9
def diff_augment(x, policy: str = None, channels_first=False):
10
    if policy:
11
        if channels_first:
12
            x = tf.transpose(x, [0, 2, 3, 1])
13
        for p in policy.split(','):
14
            for f in AUGMENT_FNS[p]:
15
                x = f(x)
16
        if channels_first:
17
            x = tf.transpose(x, [0, 3, 1, 2])
18
    return x
19
20
21
def rand_brightness(x):
22
    magnitude = tf.random.uniform([tf.shape(x)[0], 1, 1, 1]) - 0.5
23
    x = x + magnitude
24
    return x
25
26
27
def rand_saturation(x):
28
    magnitude = tf.random.uniform([tf.shape(x)[0], 1, 1, 1]) * 2
29
    x_mean = tf.reduce_mean(x, axis=3, keepdims=True)
30
    x = (x - x_mean) * magnitude + x_mean
31
    return x
32
33
34
def rand_contrast(x):
35
    magnitude = tf.random.uniform([tf.shape(x)[0], 1, 1, 1]) + 0.5
36
    x_mean = tf.reduce_mean(x, axis=[1, 2, 3], keepdims=True)
37
    x = (x - x_mean) * magnitude + x_mean
38
    return x
39
40
41
def rand_translation(x, ratio=0.125):
42
    batch_size = tf.shape(x)[0]
43
    image_size = tf.shape(x)[1:3]
44
    shift = tf.cast(tf.cast(image_size, tf.float32) * ratio + 0.5, tf.int32)
45
    translation_x = tf.random.uniform([batch_size, 1], -shift[0], shift[0] + 1, dtype=tf.int32)
46
    translation_y = tf.random.uniform([batch_size, 1], -shift[1], shift[1] + 1, dtype=tf.int32)
47
    grid_x = tf.clip_by_value(tf.expand_dims(tf.range(image_size[0], dtype=tf.int32), 0) + translation_x + 1, 0,
48
                              image_size[0] + 1)
49
    grid_y = tf.clip_by_value(tf.expand_dims(tf.range(image_size[1], dtype=tf.int32), 0) + translation_y + 1, 0,
50
                              image_size[1] + 1)
51
    x = tf.gather_nd(tf.pad(x, [[0, 0], [1, 1], [0, 0], [0, 0]]), tf.expand_dims(grid_x, -1), batch_dims=1)
52
    x = tf.transpose(tf.gather_nd(tf.pad(tf.transpose(x, [0, 2, 1, 3]), [[0, 0], [1, 1], [0, 0], [0, 0]]),
53
                                  tf.expand_dims(grid_y, -1), batch_dims=1), [0, 2, 1, 3])
54
    return x
55
56
57
def rand_cutout(x, ratio=0.5):
58
    batch_size = tf.shape(x)[0]
59
    image_size = tf.shape(x)[1:3]
60
    cutout_size = tf.cast(tf.cast(image_size, tf.float32) * ratio + 0.5, tf.int32)
61
    offset_x = tf.random.uniform([tf.shape(x)[0], 1, 1], maxval=image_size[0] + (1 - cutout_size[0] % 2),
62
                                 dtype=tf.int32)
63
    offset_y = tf.random.uniform([tf.shape(x)[0], 1, 1], maxval=image_size[1] + (1 - cutout_size[1] % 2),
64
                                 dtype=tf.int32)
65
    grid_batch, grid_x, grid_y = tf.meshgrid(tf.range(batch_size, dtype=tf.int32),
66
                                             tf.range(cutout_size[0], dtype=tf.int32),
67
                                             tf.range(cutout_size[1], dtype=tf.int32), indexing='ij')
68
    cutout_grid = tf.stack(
69
        [grid_batch, grid_x + offset_x - cutout_size[0] // 2, grid_y + offset_y - cutout_size[1] // 2], axis=-1)
70
    mask_shape = tf.stack([batch_size, image_size[0], image_size[1]])
71
    cutout_grid = tf.maximum(cutout_grid, 0)
72
    cutout_grid = tf.minimum(cutout_grid, tf.reshape(mask_shape - 1, [1, 1, 1, 3]))
73
    mask = tf.maximum(
74
        1 - tf.scatter_nd(cutout_grid, tf.ones([batch_size, cutout_size[0], cutout_size[1]], dtype=tf.float32),
75
                          mask_shape), 0)
76
    x = x * tf.expand_dims(mask, axis=3)
77
    return x
78
79
80
AUGMENT_FNS = {
81
    'color': [rand_brightness, rand_saturation, rand_contrast],
82
    'translation': [rand_translation],
83
    'cutout': [rand_cutout],
84
}