-
Notifications
You must be signed in to change notification settings - Fork 12
/
attack_methods.py
34 lines (32 loc) · 1.53 KB
/
attack_methods.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
import torch
import numpy as np
import scipy.stats as st
import torch.nn.functional as F
"""Translation-Invariant https://arxiv.org/abs/1904.02884"""
def gkern(kernlen=15, nsig=3):
x = np.linspace(-nsig, nsig, kernlen)
kern1d = st.norm.pdf(x)
kernel_raw = np.outer(kern1d, kern1d)
kernel = kernel_raw / kernel_raw.sum()
kernel = kernel.astype(np.float32)
gaussian_kernel = np.stack([kernel, kernel, kernel]) # 5*5*3
gaussian_kernel = np.expand_dims(gaussian_kernel, 1) # 1*5*5*3
gaussian_kernel = torch.from_numpy(gaussian_kernel).cuda() # tensor and cuda
return gaussian_kernel
"""Input diversity: https://arxiv.org/abs/1803.06978"""
def DI(x, resize_rate=1.15, diversity_prob=0.5):
assert resize_rate >= 1.0
assert diversity_prob >= 0.0 and diversity_prob <= 1.0
img_size = x.shape[-1]
img_resize = int(img_size * resize_rate)
rnd = torch.randint(low=img_size, high=img_resize, size=(1,), dtype=torch.int32)
rescaled = F.interpolate(x, size=[rnd, rnd], mode='bilinear', align_corners=False)
h_rem = img_resize - rnd
w_rem = img_resize - rnd
pad_top = torch.randint(low=0, high=h_rem.item(), size=(1,), dtype=torch.int32)
pad_bottom = h_rem - pad_top
pad_left = torch.randint(low=0, high=w_rem.item(), size=(1,), dtype=torch.int32)
pad_right = w_rem - pad_left
padded = F.pad(rescaled, [pad_left.item(), pad_right.item(), pad_top.item(), pad_bottom.item()], value=0)
ret = padded if torch.rand(1) < diversity_prob else x
return ret