Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Pavlo Beylin
MaD Patch Yolov5
Commits
9282a3ad
Commit
9282a3ad
authored
Sep 22, 2021
by
Pavlo Beylin
Browse files
Implement obfuscation.
parent
4d3c9758
Changes
2
Hide whitespace changes
Inline
Side-by-side
main.py
View file @
9282a3ad
...
@@ -234,7 +234,7 @@ if __name__ == "__main__":
...
@@ -234,7 +234,7 @@ if __name__ == "__main__":
pass
pass
# iou, pred = get_best_prediction(bounding_box, raw_results, 15) # get cats
# 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
iou
,
pred
=
get_best_prediction
(
bounding_box
,
raw_results
,
11
)
# get stop signs
# pred = get_best_prediction(bounding_box, raw_results, 42) # get forked
# pred = get_best_prediction(bounding_box, raw_results, 42) # get forked
...
...
patch_transformer.py
View file @
9282a3ad
...
@@ -20,7 +20,7 @@ class PatchApplier(nn.Module):
...
@@ -20,7 +20,7 @@ class PatchApplier(nn.Module):
super
(
PatchApplier
,
self
).
__init__
()
super
(
PatchApplier
,
self
).
__init__
()
def
forward
(
self
,
img
,
patch
):
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
return
img
...
@@ -70,6 +70,7 @@ class MedianPool2d(nn.Module):
...
@@ -70,6 +70,7 @@ class MedianPool2d(nn.Module):
x
=
x
.
contiguous
().
view
(
x
.
size
()[:
4
]
+
(
-
1
,)).
median
(
dim
=-
1
)[
0
]
x
=
x
.
contiguous
().
view
(
x
.
size
()[:
4
]
+
(
-
1
,)).
median
(
dim
=-
1
)[
0
]
return
x
return
x
class
PatchTransformer
(
nn
.
Module
):
class
PatchTransformer
(
nn
.
Module
):
"""PatchTransformer: transforms batch of patches
"""PatchTransformer: transforms batch of patches
...
@@ -101,7 +102,19 @@ class PatchTransformer(nn.Module):
...
@@ -101,7 +102,19 @@ class PatchTransformer(nn.Module):
'''
'''
def
forward
(
self
,
adv_patch
,
lab_batch
,
img_size_x
,
img_size_y
,
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 = F.conv2d(adv_patch.unsqueeze(0),self.kernel,padding=(2,2))
adv_patch
=
self
.
medianpooler
(
adv_patch
.
unsqueeze
(
0
))
adv_patch
=
self
.
medianpooler
(
adv_patch
.
unsqueeze
(
0
))
# Determine size of padding
# Determine size of padding
...
@@ -131,7 +144,7 @@ class PatchTransformer(nn.Module):
...
@@ -131,7 +144,7 @@ class PatchTransformer(nn.Module):
noise
=
torch
.
cuda
.
FloatTensor
(
adv_batch
.
size
()).
uniform_
(
-
1
,
1
)
*
self
.
noise_factor
noise
=
torch
.
cuda
.
FloatTensor
(
adv_batch
.
size
()).
uniform_
(
-
1
,
1
)
*
self
.
noise_factor
# Apply contrast/brightness/noise, clamp
# Apply contrast/brightness/noise, clamp
adv_batch
=
adv_batch
# * contrast + brightness + noise
adv_batch
=
adv_batch
# * contrast + brightness + noise
adv_batch
=
torch
.
clamp
(
adv_batch
,
0.000001
,
0.99999
)
adv_batch
=
torch
.
clamp
(
adv_batch
,
0.000001
,
0.99999
)
...
@@ -194,8 +207,6 @@ class PatchTransformer(nn.Module):
...
@@ -194,8 +207,6 @@ class PatchTransformer(nn.Module):
sin
=
torch
.
sin
(
angle
)
sin
=
torch
.
sin
(
angle
)
cos
=
torch
.
cos
(
angle
)
cos
=
torch
.
cos
(
angle
)
# Theta = rotation,rescale matrix
# Theta = rotation,rescale matrix
theta
=
torch
.
cuda
.
FloatTensor
(
anglesize
,
2
,
3
).
fill_
(
0
)
theta
=
torch
.
cuda
.
FloatTensor
(
anglesize
,
2
,
3
).
fill_
(
0
)
theta
[:,
0
,
0
]
=
cos
/
scale_x
theta
[:,
0
,
0
]
=
cos
/
scale_x
...
@@ -242,7 +253,7 @@ class PatchTransformer(nn.Module):
...
@@ -242,7 +253,7 @@ class PatchTransformer(nn.Module):
if
gauss
:
if
gauss
:
adv_batch_t
=
self
.
gauss_filter
(
adv_batch_t
,
random
.
random
()
*
max_sigma
)
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
):
def
gauss_filter
(
self
,
img
,
sigma
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment