first commit
43
Normalize.py
Normal file
@ -0,0 +1,43 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
class Normalize(nn.Module):
|
||||
def __init__(self, mean, std):
|
||||
super(Normalize, self).__init__()
|
||||
self.mean = mean
|
||||
self.std = std
|
||||
|
||||
def forward(self, input):
|
||||
size = input.size()
|
||||
x = input.clone()
|
||||
for i in range(size[1]):
|
||||
x[:, i] = (x[:, i] - self.mean[i]) / self.std[i]
|
||||
return x
|
||||
|
||||
class TfNormalize(nn.Module):
|
||||
|
||||
def __init__(self, mean=0, std=1, mode='tensorflow'):
|
||||
super(TfNormalize, self).__init__()
|
||||
self.mean = mean
|
||||
self.std = std
|
||||
self.mode = mode
|
||||
|
||||
def forward(self, input):
|
||||
size = input.size()
|
||||
x = input.clone()
|
||||
|
||||
if self.mode == 'tensorflow':
|
||||
x = x * 2.0 - 1.0
|
||||
elif self.mode == 'torch':
|
||||
for i in range(size[1]):
|
||||
x[:, i] = (x[:, i] - self.mean[i]) / self.std[i]
|
||||
return x
|
||||
|
||||
|
||||
class Permute(nn.Module):
|
||||
def __init__(self, permutation=[2, 1, 0]):
|
||||
super().__init__()
|
||||
self.permutation = permutation
|
||||
|
||||
def forward(self, input):
|
||||
return input[:, self.permutation]
|
15
README.md
Normal file
@ -0,0 +1,15 @@
|
||||
# Requirements
|
||||
- Python 3.6.13
|
||||
- Pytorch 1.7.1
|
||||
- Torchvision 0.8.2
|
||||
- Numpy 1.19.2
|
||||
- Pillow 8.3.1
|
||||
- Timm 0.4.12
|
||||
- Scipy 1.5.4
|
||||
|
||||
# 生成对抗样本
|
||||
python attack.py --attack ADG --batch_size 1 --model_name vit_base_patch16_224
|
||||
|
||||
# 评估成功率
|
||||
bash run_evaluate.sh model_vit_base_patch16_224-method
|
||||
python evaluate_cnn.py
|
53
attack.py
Normal file
@ -0,0 +1,53 @@
|
||||
import warnings
|
||||
warnings.filterwarnings("ignore")
|
||||
import torch
|
||||
import argparse
|
||||
from torch.utils.data import DataLoader
|
||||
import os
|
||||
import pandas as pd
|
||||
import time
|
||||
import pickle as pkl
|
||||
|
||||
from dataset import AdvDataset
|
||||
from utils import BASE_ADV_PATH, ROOT_PATH
|
||||
import methods
|
||||
|
||||
def arg_parse():
|
||||
parser = argparse.ArgumentParser(description='')
|
||||
parser.add_argument('--attack', type=str, default='', help='the name of specific attack method')
|
||||
parser.add_argument('--gpu', type=str, default='0', help='gpu device.')
|
||||
parser.add_argument('--batch_size', type=int, default=20, metavar='N',
|
||||
help='input batch size for reference (default: 16)')
|
||||
parser.add_argument('--model_name', type=str, default='', help='')
|
||||
parser.add_argument('--filename_prefix', type=str, default='', help='')
|
||||
args = parser.parse_args()
|
||||
args.opt_path = os.path.join(BASE_ADV_PATH, 'model_{}-method_{}'.format(args.model_name, args.attack))
|
||||
if not os.path.exists(args.opt_path):
|
||||
os.makedirs(args.opt_path)
|
||||
return args
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = arg_parse()
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
|
||||
|
||||
dataset = AdvDataset(args.model_name, os.path.join(ROOT_PATH, 'clean_resized_images'))
|
||||
data_loader = DataLoader(dataset, batch_size=args.batch_size, shuffle=False, num_workers=0)
|
||||
print (args.attack, args.model_name)
|
||||
|
||||
attack_method = getattr(methods, args.attack)(args.model_name)
|
||||
|
||||
all_loss_info = {}
|
||||
for batch_idx, batch_data in enumerate(data_loader):
|
||||
if batch_idx%100 == 0:
|
||||
print ('Runing batch_idx', batch_idx)
|
||||
batch_x = batch_data[0]
|
||||
batch_y = batch_data[1]
|
||||
batch_name = batch_data[3]
|
||||
|
||||
adv_inps, loss_info = attack_method(batch_x, batch_y)
|
||||
attack_method._save_images(adv_inps, batch_name, args.opt_path)
|
||||
if loss_info is not None:
|
||||
all_loss_info[batch_name] = loss_info
|
||||
if loss_info is not None:
|
||||
with open(os.path.join(args.opt_path, 'loss_info.json'), 'wb') as opt:
|
||||
pkl.dump(all_loss_info, opt)
|
BIN
clean_resized_images/ILSVRC2012_val_00000031.png
Normal file
After Width: | Height: | Size: 93 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000081.png
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000102.png
Normal file
After Width: | Height: | Size: 84 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000117.png
Normal file
After Width: | Height: | Size: 77 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000120.png
Normal file
After Width: | Height: | Size: 126 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000138.png
Normal file
After Width: | Height: | Size: 94 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000166.png
Normal file
After Width: | Height: | Size: 69 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000262.png
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000299.png
Normal file
After Width: | Height: | Size: 101 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000410.png
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000499.png
Normal file
After Width: | Height: | Size: 82 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000526.png
Normal file
After Width: | Height: | Size: 108 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000537.png
Normal file
After Width: | Height: | Size: 111 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000548.png
Normal file
After Width: | Height: | Size: 50 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000575.png
Normal file
After Width: | Height: | Size: 91 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000602.png
Normal file
After Width: | Height: | Size: 110 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000695.png
Normal file
After Width: | Height: | Size: 96 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000724.png
Normal file
After Width: | Height: | Size: 100 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000781.png
Normal file
After Width: | Height: | Size: 98 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000809.png
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000871.png
Normal file
After Width: | Height: | Size: 115 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000890.png
Normal file
After Width: | Height: | Size: 111 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00000989.png
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001094.png
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001115.png
Normal file
After Width: | Height: | Size: 116 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001343.png
Normal file
After Width: | Height: | Size: 115 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001379.png
Normal file
After Width: | Height: | Size: 105 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001425.png
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001434.png
Normal file
After Width: | Height: | Size: 104 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001435.png
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001512.png
Normal file
After Width: | Height: | Size: 97 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001537.png
Normal file
After Width: | Height: | Size: 89 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001570.png
Normal file
After Width: | Height: | Size: 84 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001638.png
Normal file
After Width: | Height: | Size: 60 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001642.png
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001645.png
Normal file
After Width: | Height: | Size: 116 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001648.png
Normal file
After Width: | Height: | Size: 128 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001655.png
Normal file
After Width: | Height: | Size: 73 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001659.png
Normal file
After Width: | Height: | Size: 84 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001689.png
Normal file
After Width: | Height: | Size: 62 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001753.png
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00001925.png
Normal file
After Width: | Height: | Size: 109 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002002.png
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002073.png
Normal file
After Width: | Height: | Size: 118 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002159.png
Normal file
After Width: | Height: | Size: 73 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002176.png
Normal file
After Width: | Height: | Size: 75 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002183.png
Normal file
After Width: | Height: | Size: 93 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002184.png
Normal file
After Width: | Height: | Size: 122 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002311.png
Normal file
After Width: | Height: | Size: 122 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002347.png
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002464.png
Normal file
After Width: | Height: | Size: 116 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002533.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002583.png
Normal file
After Width: | Height: | Size: 132 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002614.png
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002716.png
Normal file
After Width: | Height: | Size: 82 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002733.png
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002734.png
Normal file
After Width: | Height: | Size: 108 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002815.png
Normal file
After Width: | Height: | Size: 87 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00002932.png
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003096.png
Normal file
After Width: | Height: | Size: 72 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003145.png
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003190.png
Normal file
After Width: | Height: | Size: 106 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003194.png
Normal file
After Width: | Height: | Size: 101 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003317.png
Normal file
After Width: | Height: | Size: 94 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003332.png
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003337.png
Normal file
After Width: | Height: | Size: 75 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003340.png
Normal file
After Width: | Height: | Size: 72 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003381.png
Normal file
After Width: | Height: | Size: 81 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003515.png
Normal file
After Width: | Height: | Size: 74 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003577.png
Normal file
After Width: | Height: | Size: 67 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003584.png
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003607.png
Normal file
After Width: | Height: | Size: 96 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003670.png
Normal file
After Width: | Height: | Size: 64 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003674.png
Normal file
After Width: | Height: | Size: 94 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003706.png
Normal file
After Width: | Height: | Size: 129 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003724.png
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003745.png
Normal file
After Width: | Height: | Size: 108 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003900.png
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003901.png
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003913.png
Normal file
After Width: | Height: | Size: 57 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00003918.png
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004015.png
Normal file
After Width: | Height: | Size: 56 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004171.png
Normal file
After Width: | Height: | Size: 112 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004180.png
Normal file
After Width: | Height: | Size: 112 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004234.png
Normal file
After Width: | Height: | Size: 74 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004238.png
Normal file
After Width: | Height: | Size: 105 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004267.png
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004281.png
Normal file
After Width: | Height: | Size: 114 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004289.png
Normal file
After Width: | Height: | Size: 84 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004309.png
Normal file
After Width: | Height: | Size: 116 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004319.png
Normal file
After Width: | Height: | Size: 98 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004340.png
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004346.png
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004355.png
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004359.png
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004400.png
Normal file
After Width: | Height: | Size: 120 KiB |
BIN
clean_resized_images/ILSVRC2012_val_00004401.png
Normal file
After Width: | Height: | Size: 82 KiB |