Skip to content

Commit

Permalink
Merge pull request #107 from msalihs/migrate-to-github-actions
Browse files Browse the repository at this point in the history
Migrate to GitHub actions
  • Loading branch information
CaesarQ authored May 29, 2022
2 parents cc8ccf9 + 13ee1dd commit e063e34
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 106 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
Expand All @@ -27,7 +24,24 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install flake8 pytest pytest-cov codecov
python -m pip install torch
python -m pip install torchvision
python -m pip install tensorflow==1.14.0
python -m pip install scipy
python -m pip install foolbox==1.3.2
python -m pip install Keras==2.2.2
python -m pip install git+https://github.com/tensorflow/cleverhans.git@336b9f4ed95dccc7f0d12d338c2038c53786ab70
- name: Install package
run: |
python -m pip install -e .
- name: Lint with flake8
run: |
flake8 . --exclude=build/*
- name: Test with pytest
run: |
pytest
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
files: ./cov.xml
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

12 changes: 6 additions & 6 deletions advertorch/attacks/deepfool.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def __init__(self, predict, num_classes=None, nb_iter=50, eps=0.1,
self.overshoot = overshoot
self.targeted = targeted

def is_adv(self, logits, y):
def is_adv(self, logits, y):
# criterion
y_hat = logits.argmax(-1)
is_adv = y_hat != y
return is_adv

def get_deltas_logits(self, x, k, classes):
def get_deltas_logits(self, x, k, classes):
# definition of loss_fn
N = len(classes)
rows = range(N)
Expand All @@ -78,18 +78,18 @@ def get_deltas_logits(self, x, k, classes):
'deltas': delta_logits,
'logits': logits}

def get_grads(self, x, k, classes):
def get_grads(self, x, k, classes):
deltas_logits = self.get_deltas_logits(x, k, classes)
deltas_logits['sum_deltas'].backward()
deltas_logits['grads'] = x.grad.clone()
x.grad.data.zero_()
return deltas_logits

def get_distances(self, deltas, grads):
def get_distances(self, deltas, grads):
return abs(deltas) / (
grads.flatten(start_dim=2, end_dim=-1).abs().sum(axis=-1) + 1e-8)

def get_perturbations(self, distances, grads):
def get_perturbations(self, distances, grads):
return self.atleast_kd(distances, grads.ndim) * grads.sign()

def atleast_kd(self, x, k):
Expand Down Expand Up @@ -177,7 +177,7 @@ def perturb(self, x, y=None):
x,
x0 + (1.0 + self.overshoot) * p_total,
) # =x_{i+1}

x = clamp(x, min=self.clip_min, max=self.clip_max).clone().detach().requires_grad_() # noqa

return x.detach()
11 changes: 0 additions & 11 deletions advertorch/attacks/fast_adaptive_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from __future__ import unicode_literals

import torch
import collections
import time

try:
Expand All @@ -27,16 +26,6 @@

DEFAULT_EPS_DICT_BY_NORM = {'Linf': .3, 'L2': 1., 'L1': 5.0}

def zero_gradients(x):
if isinstance(x, torch.Tensor):
if x.grad is not None:
x.grad.detach_()
x.grad.zero_()
elif isinstance(x, collections.abc.Iterable):
for elem in x:
zero_gradients(elem)



class FABAttack(Attack, LabelMixin):
"""
Expand Down
4 changes: 2 additions & 2 deletions advertorch/attacks/lbfgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def _update_loss_coeffs(
if coeff_upper_bound[ii] < UPPER_CHECK:
loss_coeffs[ii] = (coeff_lower_bound[ii] +
coeff_upper_bound[ii]) / 2

else:
coeff_lower_bound[ii] = max(
coeff_lower_bound[ii], loss_coeffs[ii])

if coeff_upper_bound[ii] < UPPER_CHECK:
loss_coeffs[ii] = (coeff_lower_bound[ii] +
coeff_upper_bound[ii]) / 2
Expand Down
2 changes: 1 addition & 1 deletion advertorch/attacks/localsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def perturb_single(self, x, y):
for col in range(
int(colcenter) - self.d, int(colcenter) + self.d + 1)]
pxy = list(set((row, col) for row, col in pxy if (
0 <= row < x.shape[2] and 0 <= col < x.shape[1])))
0 <= row < x.shape[2] and 0 <= col < x.shape[1])))
pxy = torch.FloatTensor(pxy)
ii += 1
if best_img is None:
Expand Down
38 changes: 27 additions & 11 deletions advertorch/attacks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import print_function
from __future__ import unicode_literals

import collections
import numpy as np
import torch

Expand All @@ -32,6 +33,9 @@ def zero_gradients(x):
if x.grad is not None:
x.grad.detach_()
x.grad.zero_()
elif isinstance(x, collections.abc.Iterable):
for elem in x:
zero_gradients(elem)


def rand_init_delta(delta, x, ord, eps, clip_min, clip_max):
Expand Down Expand Up @@ -59,7 +63,8 @@ def rand_init_delta(delta, x, ord, eps, clip_min, clip_max):
delta.data = clamp_by_pnorm(delta.data, ord, eps)
elif ord == 1:
ini = laplace.Laplace(
loc=delta.new_tensor(0), scale=delta.new_tensor(1))
loc=delta.new_tensor(0), scale=delta.new_tensor(1)
)
delta.data = ini.sample(delta.data.shape)
delta.data = normalize_by_pnorm(delta.data, p=1)
ray = uniform.Uniform(0, eps).sample()
Expand All @@ -69,8 +74,7 @@ def rand_init_delta(delta, x, ord, eps, clip_min, clip_max):
error = "Only ord = inf, ord = 1 and ord = 2 have been implemented"
raise NotImplementedError(error)

delta.data = clamp(
x + delta.data, min=clip_min, max=clip_max) - x
delta.data = clamp(x + delta.data, min=clip_min, max=clip_max) - x
return delta.data


Expand Down Expand Up @@ -104,8 +108,8 @@ def __call__(self, *args):


def multiple_mini_batch_attack(
adversary, loader, device="cuda", save_adv=False,
norm=None, num_batch=None):
adversary, loader, device="cuda", save_adv=False, norm=None, num_batch=None
):
lst_label = []
lst_pred = []
lst_advpred = []
Expand All @@ -116,13 +120,16 @@ def multiple_mini_batch_attack(
norm = _norm_convert_dict[norm]

if norm == "inf":

def dist_func(x, y):
return (x - y).view(x.size(0), -1).max(dim=1)[0]

elif norm == 1 or norm == 2:
from advertorch.utils import _get_norm_batch

def dist_func(x, y):
return _get_norm_batch(x - y, norm)

else:
assert norm is None

Expand All @@ -143,8 +150,12 @@ def dist_func(x, y):
if idx_batch == num_batch:
break

return torch.cat(lst_label), torch.cat(lst_pred), torch.cat(lst_advpred), \
torch.cat(lst_dist) if norm is not None else None
return (
torch.cat(lst_label),
torch.cat(lst_pred),
torch.cat(lst_advpred),
torch.cat(lst_dist) if norm is not None else None,
)


class MarginalLoss(_Loss):
Expand Down Expand Up @@ -174,8 +185,9 @@ def forward(self, logits, targets): # pylint: disable=arguments-differ


class ChooseBestAttack(Attack, LabelMixin):
def __init__(self, predict, base_adversaries, loss_fn=None,
targeted=False):
def __init__(
self, predict, base_adversaries, loss_fn=None, targeted=False
):
self.predict = predict
self.base_adversaries = base_adversaries
self.loss_fn = loss_fn
Expand Down Expand Up @@ -221,5 +233,9 @@ def attack_whole_dataset(adversary, loader, device="cuda"):
lst_pred.append(pred)
lst_advpred.append(advpred)
lst_adv.append(adv)
return torch.cat(lst_adv), torch.cat(lst_label), torch.cat(lst_pred), \
torch.cat(lst_advpred)
return (
torch.cat(lst_adv),
torch.cat(lst_label),
torch.cat(lst_pred),
torch.cat(lst_advpred),
)
23 changes: 16 additions & 7 deletions advertorch/defenses/smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class AverageSmoothing2D(ConvSmoothing2D):

def __init__(self, channels, kernel_size):
kernel = torch.ones((channels, 1, kernel_size, kernel_size)) / (
kernel_size * kernel_size)
kernel_size * kernel_size
)
super(AverageSmoothing2D, self).__init__(kernel)


Expand All @@ -93,11 +94,18 @@ def _generate_conv2d_from_smoothing_kernel(kernel):
if _is_even(kernel_size):
raise NotImplementedError(
"Even number kernel size not supported yet, kernel_size={}".format(
kernel_size))
kernel_size
)
)

filter_ = nn.Conv2d(
in_channels=channels, out_channels=channels, kernel_size=kernel_size,
groups=channels, padding=kernel_size // 2, bias=False)
in_channels=channels,
out_channels=channels,
kernel_size=kernel_size,
groups=channels,
padding=kernel_size // 2,
bias=False,
)

filter_.weight.data = kernel
filter_.weight.requires_grad = False
Expand All @@ -111,12 +119,13 @@ def _generate_gaussian_kernel(sigma, channels, kernel_size=None):
vecx = torch.arange(kernel_size).float()
vecy = torch.arange(kernel_size).float()
gridxy = _meshgrid(vecx, vecy)
mean = (kernel_size - 1) / 2.
mean = (kernel_size - 1) / 2.0
var = sigma ** 2

gaussian_kernel = (
1. / (2. * math.pi * var) *
torch.exp(-(gridxy - mean).pow(2).sum(dim=0) / (2 * var))
1.0
/ (2.0 * math.pi * var)
* torch.exp(-(gridxy - mean).pow(2).sum(dim=0) / (2 * var))
)

gaussian_kernel /= torch.sum(gaussian_kernel)
Expand Down
2 changes: 1 addition & 1 deletion advertorch/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from advertorch.defenses import GaussianSmoothing2D
from advertorch.defenses import BinaryFilter

#blackbox
# blackbox
from advertorch.attacks import LinfGenAttack
from advertorch.attacks import L2GenAttack
from advertorch.attacks import LinfNAttack
Expand Down
3 changes: 1 addition & 2 deletions external_tests/test_attacks_on_foolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ def compare_attacks(key, item):
AdvertorchAttack = key
fmodel = foolbox.models.PyTorchModel(
model, bounds=(0, 1),
num_classes=NUM_CLASS,
device="cpu"
num_classes=NUM_CLASS
)
fb_adversary = item["fb_class"](fmodel)
fb_kwargs = merge2dicts(item["kwargs"], item["fb_kwargs"])
Expand Down
Loading

0 comments on commit e063e34

Please sign in to comment.