-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_cls.py
140 lines (123 loc) · 7.45 KB
/
test_cls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import numpy as np
import argparse
from tools.utils import *
from torch import nn
from dataset.scene_dataset import *
from torch.utils import data
import tools.model as models
def main(args):
if args.dataID==1:
DataName = 'UCM'
num_classes = 21
classname = ('agricultural','airplane','baseballdiamond',
'beach','buildings','chaparral',
'denseresidential','forest','freeway',
'golfcourse','harbor','intersection',
'mediumresidential','mobilehomepark','overpass',
'parkinglot','river','runway',
'sparseresidential','storagetanks','tenniscourt')
elif args.dataID==2:
DataName = 'AID'
num_classes = 30
classname = ('airport','bareland','baseballfield',
'beach','bridge','center',
'church','commercial','denseresidential',
'desert','farmland','forest',
'industrial','meadow','mediumresidential',
'mountain','parking','park',
'playground','pond','port',
'railwaystation','resort','river',
'school','sparseresidential','square',
'stadium','storagetanks','viaduct')
adv_root_dir = args.save_path_prefix+DataName+'_adv/'+args.attack_func+'/'+args.surrogate_model+'/'
composed_transforms = transforms.Compose([
transforms.Resize(size=(args.crop_size,args.crop_size)),
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))])
adv_loader = data.DataLoader(
scene_dataset(root_dir=adv_root_dir,pathfile='./dataset/'+DataName+'_test.txt', transform=composed_transforms, mode='adv'),
batch_size=args.val_batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=True)
clean_loader = data.DataLoader(
scene_dataset(root_dir=args.root_dir,pathfile='./dataset/'+DataName+'_test.txt', transform=composed_transforms),
batch_size=args.val_batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=True)
###################Target target_model Definition###################
if args.target_model=='alexnet':
target_model = models.alexnet(pretrained=False)
target_model.classifier._modules['6'] = nn.Linear(4096, num_classes)
elif args.target_model=='vgg11':
target_model = models.vgg11(pretrained=False)
target_model.classifier._modules['6'] = nn.Linear(4096, num_classes)
elif args.target_model=='vgg16':
target_model = models.vgg16(pretrained=False)
target_model.classifier._modules['6'] = nn.Linear(4096, num_classes)
elif args.target_model=='vgg19':
target_model = models.vgg19(pretrained=False)
target_model.classifier._modules['6'] = nn.Linear(4096, num_classes)
elif args.target_model=='resnet18':
target_model = models.resnet18(pretrained=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
elif args.target_model=='resnet50':
target_model = models.resnet50(pretrained=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
elif args.target_model=='resnet101':
target_model = models.resnet101(pretrained=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
elif args.target_model=='resnext50_32x4d':
target_model = models.resnext50_32x4d(pretrained=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
elif args.target_model=='resnext101_32x8d':
target_model = models.resnext101_32x8d(pretrained=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
elif args.target_model=='densenet121':
target_model = models.densenet121(pretrained=False)
target_model.classifier = nn.Linear(1024, num_classes)
elif args.target_model=='densenet169':
target_model = models.densenet169(pretrained=False)
target_model.classifier = nn.Linear(1664, num_classes)
elif args.target_model=='densenet201':
target_model = models.densenet201(pretrained=False)
target_model.classifier = nn.Linear(1920, num_classes)
elif args.target_model=='inception':
target_model = models.inception_v3(pretrained=True, aux_logits=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
elif args.target_model=='regnet_x_400mf':
target_model = models.regnet_x_400mf(pretrained=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
elif args.target_model=='regnet_x_8gf':
target_model = models.regnet_x_8gf(pretrained=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
elif args.target_model=='regnet_x_16gf':
target_model = models.regnet_x_16gf(pretrained=False)
target_model.fc = torch.nn.Linear(target_model.fc.in_features, num_classes)
dirpath = args.save_path_prefix+DataName+'/Pretrain/'+args.target_model+'/'
model_path = os.listdir(dirpath)
for filename in model_path:
filepath = os.path.join(dirpath, filename)
if os.path.isfile(filepath) and filename.lower().endswith('.pth'):
print(os.path.join(dirpath, filename))
model_path_resume = os.path.join(dirpath, filename)
saved_state_dict = torch.load(model_path_resume)
new_params = target_model.state_dict().copy()
for i,j in zip(saved_state_dict,new_params):
new_params[j] = saved_state_dict[i]
target_model.load_state_dict(new_params)
target_model = torch.nn.DataParallel(target_model).cuda()
target_model.eval()
OA_clean,_ = test_acc(target_model,classname, clean_loader, 1,num_classes,print_per_batches=10)
OA_adv,_ = test_acc(target_model,classname, adv_loader, 1,num_classes,print_per_batches=10)
print('Clean Test Set OA:',OA_clean*100)
print(args.attack_func+' Test Set OA:',OA_adv*100)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataID', type=int, default=1)
parser.add_argument('--root_dir', type=str, default='/iarai/home/yonghao.xu/Data/',help='dataset path.')
parser.add_argument('--surrogate_model', type=str, default='resnet18',help='alexnet,resnet18,densenet121,regnet_x_400mf')
parser.add_argument('--target_model', type=str, default='inception',
help='alexnet,vgg11,vgg16,vgg19,inception,resnet18,resnet50,resnet101,resnext50_32x4d,resnext101_32x8d,densenet121,densenet169,densenet201,regnet_x_400mf,regnet_x_8gf,regnet_x_16gf')
parser.add_argument('--save_path_prefix', type=str,default='./')
parser.add_argument('--crop_size', type=int, default=256)
parser.add_argument('--val_batch_size', type=int, default=32)
parser.add_argument('--num_workers', type=int, default=1)
parser.add_argument('--attack_func', type=str, default='fgsm',help='fgsm,ifgsm,cw,tpgd,jitter,mixup,mixcut')
main(parser.parse_args())