Skip to content

Commit 35781a5

Browse files
authored
Merge pull request #2440 from CNOCycle/attack/snal
Added support for SNAL attack
2 parents 7983374 + ce3aec7 commit 35781a5

File tree

9 files changed

+2635
-4
lines changed

9 files changed

+2635
-4
lines changed

art/attacks/evasion/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from art.attacks.evasion.shapeshifter import ShapeShifter
6464
from art.attacks.evasion.simba import SimBA
6565
from art.attacks.evasion.spatial_transformation import SpatialTransformation
66+
from art.attacks.evasion.steal_now_attack_later.steal_now_attack_later import SNAL
6667
from art.attacks.evasion.square_attack import SquareAttack
6768
from art.attacks.evasion.pixel_threshold import ThresholdAttack
6869
from art.attacks.evasion.universal_perturbation import UniversalPerturbation

art/attacks/evasion/steal_now_attack_later/__init__.py

Whitespace-only changes.

art/attacks/evasion/steal_now_attack_later/bbox_ioa.py

Lines changed: 698 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# pylint: disable=C0114
2+
# BSD 3-Clause License
3+
#
4+
# Copyright (c) Soumith Chintala 2016,
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
#
10+
# * Redistributions of source code must retain the above copyright notice, this
11+
# list of conditions and the following disclaimer.
12+
#
13+
# * Redistributions in binary form must reproduce the above copyright notice,
14+
# this list of conditions and the following disclaimer in the documentation
15+
# and/or other materials provided with the distribution.
16+
#
17+
# * Neither the name of the copyright holder nor the names of its
18+
# contributors may be used to endorse or promote products derived from
19+
# this software without specific prior written permission.
20+
#
21+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
from typing import TYPE_CHECKING
33+
34+
if TYPE_CHECKING:
35+
import torch
36+
37+
38+
def drop_block2d(x: "torch.Tensor", prob: float, block_size: int):
39+
"""
40+
=== NOTE ===
41+
This function is modified from torchvision (torchvision/ops/drop_block.py)
42+
BSD 3-Clause License
43+
=== ==== ===
44+
:param x (Tensor[N, C, H, W]): The input tensor or 4-dimensions with the first one
45+
being its batch i.e. a batch with ``N`` rows.
46+
:param prob (float): Probability of an element to be dropped.
47+
:param block_size (int): Size of the block to drop.
48+
49+
:return: Tensor[N, C, H, W]: The mask of activate pixels.
50+
"""
51+
import torch
52+
53+
if prob < 0.0 or prob > 1.0:
54+
raise ValueError(f"drop probability has to be between 0 and 1, but got {prob}.")
55+
if x.ndim != 4:
56+
raise ValueError(f"input should be 4 dimensional. Got {x.ndim} dimensions.")
57+
58+
N, _, H, W = x.size() # pylint: disable=C0103
59+
block_size = min(block_size, W, H)
60+
# compute the gamma of Bernoulli distribution
61+
gamma = (prob * H * W) / ((block_size**2) * ((H - block_size + 1) * (W - block_size + 1)))
62+
noise = torch.empty((N, 1, H - block_size + 1, W - block_size + 1), dtype=x.dtype, device=x.device)
63+
noise.bernoulli_(gamma)
64+
65+
noise = torch.nn.functional.pad(noise, [block_size // 2] * 4, value=0)
66+
noise = torch.nn.functional.max_pool2d(
67+
noise, stride=(1, 1), kernel_size=(block_size, block_size), padding=block_size // 2
68+
)
69+
mask = 1 - noise
70+
return mask

0 commit comments

Comments
 (0)