-
Notifications
You must be signed in to change notification settings - Fork 1
/
cl_scl_FAT.py
executable file
·221 lines (168 loc) · 8.83 KB
/
cl_scl_FAT.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.models as models
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import os
from networks import Our_ResNet
from loss import SupConLoss
from attacks import pgd_linf_end2end
import argparse
def parse_option():
parser = argparse.ArgumentParser('argument for training and test')
parser.add_argument('--method', type=str, default='SimCLR',
choices=['SimCLR', 'SupCon'], help='contrastive learning methods')
parser.add_argument('--Scenario', type=str, default='AT-Full AT',
choices=['AT-Full AT', 'ST-Full AT'], help='two different scenario of training')
parser.add_argument('--reload_model', type=bool, default= False, help='reloading the trained model')
parser.add_argument('--batch_size', type=int, default=256,
help='batch_size')
parser.add_argument('--numEpochs', type=int, default=200,
help='number of training epochs')
parser.add_argument('--num_workers', type=int, default=4,
help='num of workers to use')
parser.add_argument('--projectionDim', type=int, default=100,help='projection dimension')
parser.add_argument('--temp', type=float, default=0.07,
help='temperature for loss function')
parser.add_argument('--learningRate', type=float, default=3e-4)
parser.add_argument('--featuresDim', type=int, default=2048, help='ResNet50 output feature dimension')
parser.add_argument('--trial', type=int, default=0,help='id for recording runs')
parser.add_argument('--model', type=str, default='resnet50')
parser.add_argument('--dataset', type=str, default='cifar10',
choices=['cifar10', 'cifar100'], help='dataset')
parser.add_argument('--eps_AT', type=float, default=(8/255), help='eps for Adversarial Training')
parser.add_argument('--iter_AT', type=int, default=5, help='number of iterations for generating adversarial in Adversarial Training')
parser.add_argument('--eps_t2', type=float, default=(4/255), help='eps for adversarial:threat model II')
parser.add_argument('--iter_t2', type=int, default=40, help='numer of iterations for generating adversarial:threat model II')
parser.add_argument('--alpha', type=float, default=1e-2, help='movement multiplier per iteration in adversarial examples')
opt = parser.parse_args()
opt.model_name = '{}_{}_{}_bsz_{}_epoch_{}_trial_{}'.\
format(opt.method, opt.dataset, opt.model, opt.batch_size, opt.numEpochs, opt.trial)
if opt.Scenario == 'AT-Full AT':
opt.load_path = './save/AT/{}_models'.format(opt.dataset)
opt.save_path = './save/AT-Full_AT/{}_models'.format(opt.dataset)
else:
opt.load_path = './save/ST/{}_models'.format(opt.dataset)
opt.save_path = './save/ST-Full_AT/{}_models'.format(opt.dataset)
if not os.path.isdir(opt.save_path):
os.makedirs(opt.save_path)
if opt.dataset == 'cifar10':
opt.n_classes = 10
elif opt.dataset == 'cifar100':
opt.n_classes = 100
else:
raise ValueError('dataset not supported: {}'.format(opt.dataset))
return opt
def set_loader(opt):
trainEvalTransform = transforms.Compose([
transforms.ToTensor()])
testTransform = transforms.Compose([
transforms.ToTensor()])
if opt.dataset == 'cifar10':
trainEvalDataset = torchvision.datasets.CIFAR10(root='./data/' ,train=True, transform=trainEvalTransform, download=True)
testDataset = torchvision.datasets.CIFAR10(root='./data/' ,train=False, transform=testTransform)
elif opt.dataset == 'cifar100':
trainEvalDataset = torchvision.datasets.CIFAR100(root='./data/' ,train=True, transform=trainEvalTransform, download=True)
testDataset = torchvision.datasets.CIFAR100(root='./data/' ,train=False, transform=testTransform)
else:
raise ValueError('dataset not supported: {}'.format(opt.dataset))
trainEvalLoader = torch.utils.data.DataLoader(dataset=trainEvalDataset, batch_size= opt.batch_size, num_workers= opt.num_workers, pin_memory=True, shuffle=True , drop_last=True)
testLoader = torch.utils.data.DataLoader(dataset=testDataset, batch_size= opt.batch_size, num_workers= opt.num_workers, pin_memory=True, shuffle=False, drop_last=True )
return trainEvalLoader,testLoader
def set_models(opt,device):
ResNet = Our_ResNet()
Encoder = ResNet.to(device)
MLP = nn.Sequential( nn.Linear(opt.featuresDim, opt.featuresDim ),
nn.ReLU(inplace=True),
nn.Linear(opt.featuresDim, opt.projectionDim ) )
MLP = MLP.to(device)
Linear = nn.Linear(opt.featuresDim,opt.n_classes)
Linear = Linear.to(device)
class EncoderWithHead(nn.Module):
def __init__(self, encoder, head):
super(EncoderWithHead, self).__init__()
self.encoder = encoder
self.head = head
def forward(self, x):
out = F.normalize(self.head(self.encoder(x)),dim=1)
return out
CLNet = EncoderWithHead(Encoder, MLP)
EvalNet = EncoderWithHead(Encoder, Linear)
return CLNet,EvalNet
def trainEvalNet_Ro(opt,trainEvalLoader,EvalNet,criterion,optimizer,device):
totalStep = len(trainEvalLoader)
EvalNet.encoder.train()
EvalNet.head.train()
for epoch in range(opt.numEpochs):
for i, (X, labels) in enumerate(trainEvalLoader):
X = X.to(device)
labels = labels.to(device)
delta = pgd_linf_end2end(EvalNet, X, labels, opt.eps_AT, opt.alpha, opt.iter_AT)
X_adv = (X + delta)
h = EvalNet.encoder(X)
h_adv = EvalNet.encoder(X_adv)
Z = EvalNet.head(h)
Z_adv = EvalNet.head(h_adv)
loss1 = criterion(Z, labels)
loss2 = criterion(Z_adv, labels)
loss = loss1 + loss2
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 1 == 0:
test_Accuracy = 0 #testAccuracy()
print ("Epoch [{}/{}], Step [{}/{}] Loss: {:.4f}".format(epoch+1, opt.numEpochs, i+1, totalStep, loss.item()),flush=True)
PATH = opt.save_path+'/EvalNet_'+opt.model_name+'.pt'
torch.save(EvalNet.state_dict(), PATH)
def testEvalNet(opt,testLoader,EvalNet,device):
EvalNet.encoder.eval()
EvalNet.head.eval()
total_acc_test = 0
for i, (X, labels) in enumerate(testLoader):
X = X.to(device)
labels = labels.to(device)
Z = EvalNet(X)
total_acc_test += (Z.max(dim=1)[1] == labels).sum().item()
print('Acc_Test =', total_acc_test / len(testLoader.dataset),sep="\t")
return total_acc_test / len(testLoader.dataset)
def testEvalNet_adv_end2end(opt,testLoader,EvalNet,device):
totalStep = len(testLoader)
EvalNet.encoder.eval()
EvalNet.head.eval()
total_acc_test = 0
for i, (X, labels) in enumerate(testLoader):
X = X.to(device)
labels = labels.to(device)
delta = pgd_linf_end2end(EvalNet, X, labels, opt.eps_t2, opt.alpha, opt.iter_t2)
X_adv = (X + delta)
Z2 = EvalNet(X_adv)
predicted2 = Z2.argmax(1)
total_acc_test += (Z2.max(dim=1)[1] == labels).sum().item()
print('Acc_Test =', total_acc_test / len(testLoader.dataset),sep="\t")
return total_acc_test/len(testLoader.dataset)
def main():
opt = parse_option()
trainEvalLoader,testLoader = set_loader(opt)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
CLNet,EvalNet =set_models(opt,device)
# Loading the Trained Model
PATH = opt.load_path+'/CLNet_'+opt.model_name+'.pt'
CLNet.load_state_dict(torch.load(PATH))
if opt.reload_model == True:
PATH = opt.save_path+'/EvalNet_'+opt.model_name+'.pt'
EvalNet.load_state_dict(torch.load(PATH))
else:
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(EvalNet.parameters(), lr=opt.learningRate)
trainEvalNet_Ro(opt,trainEvalLoader,EvalNet,criterion,optimizer,device)
# Test on Clean Data
testEvalNet(opt,testLoader,EvalNet,device)
# Test under Threat Model II
testEvalNet_adv_end2end(opt,testLoader,EvalNet,device)
if __name__ == '__main__':
main()