Skip to content

Commit

Permalink
Fix pylint errors
Browse files Browse the repository at this point in the history
Signed-off-by: Teodora Sechkova <[email protected]>
  • Loading branch information
sechkova committed May 24, 2023
1 parent a63e9d0 commit 9a086f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 5 additions & 5 deletions art/attacks/evasion/patchfool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
| Paper link: https://arxiv.org/abs/2203.08392
"""
from typing import TYPE_CHECKING, Optional, Union
from typing import Optional, Union

import numpy as np

Expand Down Expand Up @@ -73,7 +73,7 @@ def __init__(
self.random_start = random_start
self._check_params()

def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None) -> np.ndarray:
def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> np.ndarray:
"""
Generate adversarial samples and return them in an array.
Expand Down Expand Up @@ -110,12 +110,12 @@ def _generate_batch(self, x: "torch.Tensor", y: Optional["torch.Tensor"] = None)
x = x.to(self.estimator.device)
y = y.to(self.estimator.device)

p = self._get_patch_index(x, layer=self.patch_layer)
patches = self._get_patch_index(x, layer=self.patch_layer)

patch_size = self.estimator.patch_size
mask = torch.zeros(x.shape).to(self.estimator.device)

for n, patch_idx in enumerate(p):
for n, patch_idx in enumerate(patches):
row = (patch_idx // (x.shape[2] // patch_size)) * patch_size
col = (patch_idx % (x.shape[2] // patch_size)) * patch_size
mask[n, :, row : row + patch_size, col : col + patch_size] = 1
Expand All @@ -136,7 +136,7 @@ def _generate_batch(self, x: "torch.Tensor", y: Optional["torch.Tensor"] = None)
for i_max_iter in range(self.max_iter):
optim.zero_grad()

loss_att = self._get_attention_loss(x_adv, p)
loss_att = self._get_attention_loss(x_adv, patches)
loss_att_batch = torch.sum(loss_att, dim=1)

model_outputs, _ = self.estimator._predict_framework(x_adv)
Expand Down
12 changes: 9 additions & 3 deletions art/estimators/classification/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,6 @@ def __init__(
be divided by the second one.
:param device_type: Type of device on which the classifier is run, either `gpu` or `cpu`.
"""
import torch

super().__init__(
model=model,
Expand All @@ -1283,12 +1282,19 @@ def __init__(

@property
def patch_size(self):
"""
TODO
"""
return self.model.patch_size

def get_attention_weights(self, x: Union[np.ndarray, "torch.Tensor"], batch_size: int = 128):
def get_attention_weights(self, x: Union[np.ndarray, "torch.Tensor"]) -> "torch.Tensor":
"""
TODO
"""

import torch
from torch import fx
from torchvision.models.feature_extraction import get_graph_node_names, create_feature_extractor
from torchvision.models.feature_extraction import create_feature_extractor

graph: fx.Graph = fx.Tracer().trace(self.model)
# 'need_weights' is set to False in the implementation
Expand Down

0 comments on commit 9a086f5

Please sign in to comment.