-
Notifications
You must be signed in to change notification settings - Fork 8
/
train.py
185 lines (158 loc) · 6.25 KB
/
train.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
import argparse
import os
from PIL.Image import ImagePointHandler
import numpy as np
import matplotlib.pyplot as plt
import torchvision
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from tqdm import tqdm
from model_layer import Vgg16_all_layer, Vgg19_all_layer,Res152_all_layer, Dense169_all_layer
import random
from generator import GeneratorResnet
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '2'
parser = argparse.ArgumentParser(description='Transfer towards Black-box Domain')
parser.add_argument('--train_dir', default='', help='the path for imagenet training data')
parser.add_argument('--batch_size', type=int, default=16, help='Number of trainig samples/batch')
parser.add_argument('--epochs', type=int, default=1, help='Number of training epochs')
parser.add_argument('--lr', type=float, default=0.0002, help='Initial learning rate for adam')
parser.add_argument('--eps', type=int, default=10, help='Perturbation Budget')
parser.add_argument('--model_type', type=str, default='vgg16',
help='Model against GAN is trained: vgg16, vgg19 res152, dense169')
parser.add_argument('--RN', type=lambda x: (str(x).lower() == 'true'), default=False,
help='If true, activating the Random Normalization module in training phase')
parser.add_argument('--DA', type=lambda x: (str(x).lower() == 'true'), default=False,
help='If true, activating the Domain-agnostic Attention module in training phase')
args = parser.parse_args()
print(args)
# Normalize (0-1)
eps = args.eps/255.0
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
setup_seed(0)
# GPU
# device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
####################
# Model
####################
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if args.model_type == 'vgg16':
model = Vgg16_all_layer.Vgg16()
layer_idx = 16 # Maxpooling.3
elif args.model_type == 'vgg19':
model = Vgg19_all_layer.Vgg19()
layer_idx = 18 # Maxpooling.3
elif args.model_type == 'res152':
model = Res152_all_layer.Resnet152()
layer_idx = 5 # Conv3_8
elif args.model_type == 'dense169':
model = Dense169_all_layer.Dense169()
layer_idx = 6 # Denseblock.2
else:
raise Exception('Please check the model_type')
if args.RN and args.DA:
save_checkpoint_suffix = 'BIA+RN+DA'
elif args.RN:
save_checkpoint_suffix = 'BIA+RN'
elif args.DA:
save_checkpoint_suffix = 'BIA+DA'
else:
save_checkpoint_suffix = 'BIA'
model = model.to(device)
model.eval()
# Input dimensions
scale_size = 256
img_size = 224
# Generator
netG = GeneratorResnet().to(device)
# Optimizer
optimG = optim.Adam(netG.parameters(), lr=args.lr, betas=(0.5, 0.999))
# Training Data
data_transform = transforms.Compose([
transforms.Resize(scale_size),
transforms.CenterCrop(img_size),
transforms.ToTensor(),
])
def default_normalize(t):
t[:, 0, :, :] = (t[:, 0, :, :] - 0.485) / 0.229
t[:, 1, :, :] = (t[:, 1, :, :] - 0.456) / 0.224
t[:, 2, :, :] = (t[:, 2, :, :] - 0.406) / 0.225
return t
def normalize(t, mean, std):
t[:, 0, :, :] = (t[:, 0, :, :] - mean) / std
t[:, 1, :, :] = (t[:, 1, :, :] - mean) / std
t[:, 2, :, :] = (t[:, 2, :, :] - mean) / std
return t
train_dir = args.train_dir
train_set = datasets.ImageFolder(train_dir, data_transform)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True, num_workers=4, pin_memory=True)
train_size = len(train_set)
print('Training data size:', train_size)
# Loss
criterion = nn.CrossEntropyLoss()
# Training
for epoch in range(args.epochs):
running_loss = 0
for i, (img, _) in enumerate(train_loader):
img = img.to(device)
netG.train()
optimG.zero_grad()
adv = netG(img)
# Projection
adv = torch.min(torch.max(adv, img - eps), img + eps)
adv = torch.clamp(adv, 0.0, 1.0)
# Saving adversarial examples
flag = False
if i <= 1000:
if i % 100 == 0:
flag = True
else:
if i % 2000 == 0:
flag = True
if flag:
plt.subplot(121)
plt.imshow(img[0,...].permute(1,2,0).detach().cpu())
plt.axis('off')
plt.subplot(122)
plt.imshow(adv[0,...].permute(1,2,0).detach().cpu())
plt.axis('off')
save_path = 'output/{}'.format(args.model_type)
if not os.path.exists(save_path):
os.makedirs(save_path)
plt.savefig(os.path.join(save_path, '{}.png'.format(i)), bbox_inches='tight')
if args.RN:
mean = np.random.normal(0.50, 0.08)
std = np.random.normal(0.75, 0.08)
adv_out_slice = model(normalize(adv.clone(), mean, std))[layer_idx]
img_out_slice = model(normalize(img.clone(), mean, std))[layer_idx]
else:
adv_out_slice = model(default_normalize(adv.clone()))[layer_idx]
img_out_slice = model(default_normalize(img.clone()))[layer_idx]
if args.DA:
attention = abs(torch.mean(img_out_slice, dim=1, keepdim=True)).detach()
else:
attention = torch.ones(adv_out_slice.shape).cuda()
loss = torch.cosine_similarity((adv_out_slice*attention).reshape(adv_out_slice.shape[0], -1),
(img_out_slice*attention).reshape(img_out_slice.shape[0], -1)).mean()
loss.backward()
optimG.step()
if i % 100 == 0:
print('Epoch: {0} \t Batch: {1} \t loss: {2:.5f}'.format(epoch, i, running_loss / 100))
running_loss = 0
running_loss += abs(loss.item())
# One epoch
if i % 80000 == 0 and i > 0:
save_checkpoint_dir = 'saved_models/{}'.format(args.model_type)
if not os.path.exists(save_checkpoint_dir):
os.makedirs(save_checkpoint_dir)
save_path = os.path.join(save_checkpoint_dir, 'netG_{}_{}.pth'.format(save_checkpoint_suffix, epoch))
torch.save(netG.state_dict(), save_path)