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
Show whitespace changes
Inline
Side-by-side
main.py
View file @
9282a3ad
...
...
@@ -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
...
...
patch_transformer.py
View file @
9282a3ad
...
...
@@ -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
):
...
...
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