Commit 9282a3ad authored by Pavlo Beylin's avatar Pavlo Beylin
Browse files

Implement obfuscation.

parent 4d3c9758
......@@ -234,7 +234,7 @@ if __name__ == "__main__":
pass
# iou, pred = get_best_prediction(bounding_box, raw_results, 15) # get cats
iou, pred = get_best_prediction(bounding_box, raw_results, 12) # get parking meters
# iou, pred = get_best_prediction(bounding_box, raw_results, 12) # get parking meters
iou, pred = get_best_prediction(bounding_box, raw_results, 11) # get stop signs
# pred = get_best_prediction(bounding_box, raw_results, 42) # get forked
......
......@@ -20,7 +20,7 @@ class PatchApplier(nn.Module):
super(PatchApplier, self).__init__()
def forward(self, img, patch):
img = torch.where(patch < 1e-05, img/256, patch) * 256
img = torch.where(patch < 1e-05, img / 256, patch) * 256
return img
......@@ -70,6 +70,7 @@ class MedianPool2d(nn.Module):
x = x.contiguous().view(x.size()[:4] + (-1,)).median(dim=-1)[0]
return x
class PatchTransformer(nn.Module):
"""PatchTransformer: transforms batch of patches
......@@ -101,7 +102,19 @@ class PatchTransformer(nn.Module):
'''
def forward(self, adv_patch, lab_batch, img_size_x, img_size_y,
do_rotate=True, rand_loc=True, rand_size=True, gauss=True, max_sigma=6, rand_squeeze=True):
do_rotate=True, rand_loc=True, rand_size=True, gauss=True, max_sigma=6,
obfuscate=True, max_obs_part=1, rand_squeeze=True):
# Obfuscation
if obfuscate:
max_x = adv_patch.shape[1]
max_y = adv_patch.shape[2]
obfuscation_root = [int(random.random() * max_x), int(random.random() * max_y)]
obfuscation_shape = [int(random.random() * max_obs_part * max_x),
int(random.random() * max_obs_part * max_y)]
adv_patch[:, obfuscation_root[0]:obfuscation_root[0] + obfuscation_shape[0],
obfuscation_root[1]:obfuscation_root[1] + obfuscation_shape[1]] = 0.001
# adv_patch = F.conv2d(adv_patch.unsqueeze(0),self.kernel,padding=(2,2))
adv_patch = self.medianpooler(adv_patch.unsqueeze(0))
# Determine size of padding
......@@ -194,8 +207,6 @@ class PatchTransformer(nn.Module):
sin = torch.sin(angle)
cos = torch.cos(angle)
# Theta = rotation,rescale matrix
theta = torch.cuda.FloatTensor(anglesize, 2, 3).fill_(0)
theta[:, 0, 0] = cos / scale_x
......@@ -242,7 +253,7 @@ class PatchTransformer(nn.Module):
if gauss:
adv_batch_t = self.gauss_filter(adv_batch_t, random.random() * max_sigma)
return adv_batch_t #* msk_batch_t
return adv_batch_t # * msk_batch_t
def gauss_filter(self, img, sigma):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment