Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
POSTECH-CVLab
GitHub Repository: POSTECH-CVLab/PyTorch-StudioGAN
Path: blob/master/src/utils/diffaug.py
809 views
1
"""
2
Copyright (c) 2020, Shengyu Zhao, Zhijian Liu, Ji Lin, Jun-Yan Zhu, and Song Han
3
All rights reserved.
4
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
7
8
* Redistributions of source code must retain the above copyright notice, this
9
list of conditions and the following disclaimer.
10
11
* Redistributions in binary form must reproduce the above copyright notice,
12
this list of conditions and the following disclaimer in the documentation
13
and/or other materials provided with the distribution.
14
15
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
"""
26
27
### Differentiable Augmentation for Data-Efficient GAN Training (https://arxiv.org/abs/2006.10738)
28
### Shengyu Zhao, Zhijian Liu, Ji Lin, Jun-Yan Zhu, and Song Han
29
### https://github.com/mit-han-lab/data-efficient-gans
30
31
import torch
32
import torch.nn.functional as F
33
34
35
def apply_diffaug(x, policy="color,translation,cutout", channels_first=True):
36
if policy:
37
if not channels_first:
38
x = x.permute(0, 3, 1, 2)
39
for p in policy.split(","):
40
for f in AUGMENT_FNS[p]:
41
x = f(x)
42
if not channels_first:
43
x = x.permute(0, 2, 3, 1)
44
x = x.contiguous()
45
return x
46
47
48
def rand_brightness(x):
49
x = x + (torch.rand(x.size(0), 1, 1, 1, dtype=x.dtype, device=x.device) - 0.5)
50
return x
51
52
53
def rand_saturation(x):
54
x_mean = x.mean(dim=1, keepdim=True)
55
x = (x - x_mean) * (torch.rand(x.size(0), 1, 1, 1, dtype=x.dtype, device=x.device) * 2) + x_mean
56
return x
57
58
59
def rand_contrast(x):
60
x_mean = x.mean(dim=[1, 2, 3], keepdim=True)
61
x = (x - x_mean) * (torch.rand(x.size(0), 1, 1, 1, dtype=x.dtype, device=x.device) + 0.5) + x_mean
62
return x
63
64
65
def rand_translation(x, ratio=0.125):
66
shift_x, shift_y = int(x.size(2) * ratio + 0.5), int(x.size(3) * ratio + 0.5)
67
translation_x = torch.randint(-shift_x, shift_x + 1, size=[x.size(0), 1, 1], device=x.device)
68
translation_y = torch.randint(-shift_y, shift_y + 1, size=[x.size(0), 1, 1], device=x.device)
69
grid_batch, grid_x, grid_y = torch.meshgrid(
70
torch.arange(x.size(0), dtype=torch.long, device=x.device),
71
torch.arange(x.size(2), dtype=torch.long, device=x.device),
72
torch.arange(x.size(3), dtype=torch.long, device=x.device),
73
)
74
grid_x = torch.clamp(grid_x + translation_x + 1, 0, x.size(2) + 1)
75
grid_y = torch.clamp(grid_y + translation_y + 1, 0, x.size(3) + 1)
76
x_pad = F.pad(x, [1, 1, 1, 1, 0, 0, 0, 0])
77
x = x_pad.permute(0, 2, 3, 1).contiguous()[grid_batch, grid_x, grid_y].permute(0, 3, 1, 2)
78
return x
79
80
81
def rand_cutout(x, ratio=0.5):
82
cutout_size = int(x.size(2) * ratio + 0.5), int(x.size(3) * ratio + 0.5)
83
offset_x = torch.randint(0, x.size(2) + (1 - cutout_size[0] % 2), size=[x.size(0), 1, 1], device=x.device)
84
offset_y = torch.randint(0, x.size(3) + (1 - cutout_size[1] % 2), size=[x.size(0), 1, 1], device=x.device)
85
grid_batch, grid_x, grid_y = torch.meshgrid(
86
torch.arange(x.size(0), dtype=torch.long, device=x.device),
87
torch.arange(cutout_size[0], dtype=torch.long, device=x.device),
88
torch.arange(cutout_size[1], dtype=torch.long, device=x.device),
89
)
90
grid_x = torch.clamp(grid_x + offset_x - cutout_size[0] // 2, min=0, max=x.size(2) - 1)
91
grid_y = torch.clamp(grid_y + offset_y - cutout_size[1] // 2, min=0, max=x.size(3) - 1)
92
mask = torch.ones(x.size(0), x.size(2), x.size(3), dtype=x.dtype, device=x.device)
93
mask[grid_batch, grid_x, grid_y] = 0
94
x = x * mask.unsqueeze(1)
95
return x
96
97
98
AUGMENT_FNS = {
99
"color": [rand_brightness, rand_saturation, rand_contrast],
100
"translation": [rand_translation],
101
"cutout": [rand_cutout],
102
}
103
104