-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_identity.py
167 lines (141 loc) · 6.4 KB
/
train_identity.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
import argparse, os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
from torch.distributions import kl_divergence, Normal
from torch.utils.tensorboard import SummaryWriter
from torchvision import datasets, transforms
from MAD_VAE import *
from utils.loss_function import *
from utils.classifier import *
from utils.scheduler import *
from utils.dataset import *
# argument parser
def parse_args():
desc = "MAD-VAE for adversarial defense"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--batch_size', type=int, default=512, help='Training batch size')
parser.add_argument('--epochs', type=int, default=10, help='Training epoch numbers')
parser.add_argument('--h_dim', type=int, default=4096, help='Hidden dimensions')
parser.add_argument('--z_dim', type=int, default=128, help='Latent dimensions for images')
parser.add_argument('--image_channels', type=int, default=1, help='Image channels')
parser.add_argument('--image_size', type=int, default=28, help='Image size (default to be squared images)')
parser.add_argument('--num_classes', type=int, default=10, help='Number of image classes')
parser.add_argument('--log_dir', type=str, default='id_logs', help='Logs directory')
parser.add_argument('--lr', type=float, default=0.001, help='Learning rate for the Adam optimizer')
parser.add_argument('--data_root', type=str, default='data', help='Data directory')
parser.add_argument('--model_dir', type=str, default='pretrained_model', help='Pretrained model directory')
parser.add_argument('--use_gpu', type=bool, default=True, help='If use GPU for training')
parser.add_argument('--gpu_num', type=int, default=2, choices=range(0,5), help='GPU numbers available for parallel training')
return parser.parse_args()
# main function
def main():
args = parse_args()
# make directories for pretrained models
if not os.path.exists(args.model_dir):
os.mkdir(args.model_dir)
# prepare dataset
data = np.load('data/xs_mnist.npy') # image data in npy file
labels = np.load('data/ys_mnist.npy') # labels data in npy file
adv_data = np.load('data/advs_mnist.npy') # adversarial image data in npy file
dataset = Dataset(data, labels, adv_data)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.batch_size, shuffle=True, num_workers=6)
# summary writer for tensorboard
writer1 = SummaryWriter(args.log_dir+'/recon_loss')
writer2 = SummaryWriter(args.log_dir+'/img_loss')
writer3 = SummaryWriter(args.log_dir+'/kl_loss')
# create modules needed
model, optimizer, scheduler = init_models(args)
# tratinig steps
step = 0
for epoch in range(1, args.epochs+1):
print('Epoch: {}'.format(epoch))
recon_losses, img_losses, kl_losses, datas, adv_datas, outputs, step = \
train(args, dataloader, model, optimizer, step, epoch)
# write to tensorboard
writer1.add_scalar('recon_loss', np.sum(recon_losses)/len(recon_losses), step)
writer2.add_scalar('img_loss', np.sum(img_losses)/len(img_losses), step)
writer3.add_scalar('kl_loss', np.sum(kl_losses)/len(kl_losses), step)
for i in range(len(datas)):
writer1.add_image('original data', datas[i][0], step)
writer1.add_image('adv data', adv_datas[i][0], step)
writer1.add_image("reconstruct data", outputs[i][0], step)
# print out loss
print("batch {}'s img_recon loss: {:.5f}, recon loss: {:.5f}, kl loss: {:.5f}"\
.format(step, np.sum(img_losses)/len(img_losses), np.sum(recon_losses)/len(recon_losses),\
np.sum(kl_losses)/len(kl_losses)))
scheduler.step()
# save model parameters
if epoch % 5 == 0:
torch.save(model.state_dict(), '{}/identity/params_{}.pt'.format(args.model_dir, epoch))
torch.save(model.state_dict(), '{}/identity/params.pt'.format(args.model_dir))
# training function
def train(args, dataloader, model, optimizer, step, epoch):
# init output lists
recon_losses = list()
img_losses = list()
kl_losses = list()
datas = list()
adv_datas = list()
outputs = list()
# loop for each data pairs
for data, label, adv_data in dataloader:
# initialize
step += 1
if torch.cuda.is_available():
model = model.cuda()
data = data.cuda()
label = label.cuda()
adv_data = adv_data.cuda()
# zero grad for optimizer
optimizer.zero_grad()
# get data and run model
output, dsm, dss, z = model(data)
distribution = Normal(dsm, dss)
# calculate losses
r_loss, img_recon, kld = recon_loss_function(output, data, distribution, step, 0.1)
loss = r_loss
loss.backward()
# clip for gradient
torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
# step optimizer
optimizer.step()
# record results
recon_losses.append(loss.cpu().item())
img_losses.append(img_recon.cpu().item())
kl_losses.append(kld.cpu().item())
outputs.append(output.cpu())
datas.append(data.cpu())
adv_datas.append(adv_data.cpu())
return recon_losses, img_losses, kl_losses, datas, adv_datas, outputs, step
# init models to be used
def init_models(args):
# construct model, classifier and loss module
model = MADVAE(args)
if args.use_gpu and torch.cuda.is_available():
# multi gpu training
if args.gpu_num > 1:
model = torch.nn.DataParallel(model, device_ids=range(args.gpu_num))
model = model.module
# move to cuda
model.apply(weights_init)
model = model.cuda()
print('Using: ', torch.cuda.get_device_name(torch.cuda.current_device()))
# training module
model.train()
# construct optimizer
optimizer = optim.Adam(model.parameters(), lr=args.lr)
scheduler = MinExponentialLR(optimizer, gamma=0.998, minimum=1e-5)
return model, optimizer, scheduler
# initialize model weights
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1 and classname.find('Block') == -1:
m.weight.data.normal_(0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1, 0.02)
m.bias.data.fill_(0)
if __name__ == '__main__':
main()