-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from cassidylaidlaw/pypi-package
Configure PyPI package
- Loading branch information
Showing
43 changed files
with
5,596 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
MIT License | ||
Copyright (c) 2018 YOUR NAME | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Pretrained models | ||
|
||
This directory contains pretrained models for CIFAR-10 from the paper. All models use the ResNet-32 architecture in `recoloradv.mister_ed.cifar10.cifar_resnets`. The pretrained models include | ||
* A normally-trained model: [normal.resnet32.pt](normal.resnet32.pt). | ||
* A model trained on black-and-white images: [bw.resnet32.pt](bw.resnet32.pt). | ||
* Models trained on combinations of ReColorAdv, StAdv, and delta (L-infinity) attacks: [delta.resnet32.pt](delta.resnet32.pt), [stadv.resnet32.pt](stadv.resnet32.pt), [recoloradv.resnet32.pt](recoloradv.resnet32.pt), [stadv_delta.resnet32.pt](stadv_delta.resnet32.pt), [recoloradv_stadv.resnet32.pt](recoloradv_stadv.resnet32.pt), [recoloradv_delta.resnet32.pt](recoloradv_delta.resnet32.pt), and [recoloradv_stadv_delta.resnet32.pt](recoloradv_stadv_delta.resnet32.pt). | ||
|
||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
import torch | ||
import argparse | ||
import sys | ||
import os | ||
from torch import optim | ||
from torch.utils.data import DataLoader | ||
from torchvision.models import resnet50 | ||
from torchvision.datasets import ImageNet | ||
from torchvision import transforms | ||
|
||
# mister_ed | ||
from recoloradv.mister_ed import loss_functions as lf | ||
from recoloradv.mister_ed import adversarial_training as advtrain | ||
from recoloradv.mister_ed import adversarial_perturbations as ap | ||
from recoloradv.mister_ed import adversarial_attacks as aa | ||
from recoloradv.mister_ed import spatial_transformers as st | ||
from recoloradv.mister_ed.utils import pytorch_utils as utils | ||
from recoloradv.mister_ed.cifar10 import cifar_loader | ||
|
||
# ReColorAdv | ||
from recoloradv import perturbations as pt | ||
from recoloradv import color_transformers as ct | ||
from recoloradv import color_spaces as cs | ||
from recoloradv.utils import get_attack_from_name, load_pretrained_cifar10_model | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description='Evaluate a model trained on CIFAR-10 ' | ||
'against ReColorAdv and other attacks' | ||
) | ||
|
||
parser.add_argument('--checkpoint', type=str, | ||
help='checkpoint to evaluate') | ||
parser.add_argument('--attack', type=str, | ||
help='attack to run, such as "recoloradv" or ' | ||
'"stadv+delta"') | ||
parser.add_argument('--batch_size', type=int, default=100, | ||
help='number of examples/minibatch') | ||
parser.add_argument('--num_batches', type=int, required=False, | ||
help='number of batches (default entire dataset)') | ||
args = parser.parse_args() | ||
|
||
model, normalizer = load_pretrained_cifar10_model(args.checkpoint) | ||
val_loader = cifar_loader.load_cifar_data('val', batch_size=args.batch_size) | ||
|
||
model.eval() | ||
if torch.cuda.is_available(): | ||
model.cuda() | ||
|
||
attack = get_attack_from_name(args.attack, model, normalizer) | ||
|
||
batches_correct = [] | ||
for batch_index, (inputs, labels) in enumerate(val_loader): | ||
if ( | ||
args.num_batches is not None and | ||
batch_index >= args.num_batches | ||
): | ||
break | ||
|
||
if torch.cuda.is_available(): | ||
inputs = inputs.cuda() | ||
labels = labels.cuda() | ||
|
||
adv_inputs = attack.attack( | ||
inputs, | ||
labels, | ||
)[0] | ||
with torch.no_grad(): | ||
adv_logits = model(normalizer(adv_inputs)) | ||
batch_correct = (adv_logits.argmax(1) == labels).detach() | ||
|
||
batch_accuracy = batch_correct.float().mean().item() | ||
print(f'BATCH {batch_index:05d}', | ||
f'accuracy = {batch_accuracy * 100:.1f}', | ||
sep='\t') | ||
batches_correct.append(batch_correct) | ||
|
||
accuracy = torch.cat(batches_correct).float().mean().item() | ||
print('OVERALL ', | ||
f'accuracy = {accuracy * 100:.1f}', | ||
sep='\t') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Code in this directory is adapted from the [`mister_ed`](https://github.com/revbucket/mister_ed) library. |
Empty file.
Oops, something went wrong.