forked from huawei-noah/Efficient-Computing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAFL-train.py
204 lines (171 loc) · 7.84 KB
/
DAFL-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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#Copyright (C) 2019. Huawei Technologies Co., Ltd. All rights reserved.
#This program is free software; you can redistribute it and/or modify it under the terms of the BSD 3-Clause License.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD 3-Clause License for more details.
import argparse
import os
import numpy as np
import math
import sys
import pdb
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch
from torchvision.datasets.mnist import MNIST
from lenet import LeNet5Half
from torchvision.datasets import CIFAR10
from torchvision.datasets import CIFAR100
import resnet
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='MNIST', choices=['MNIST','cifar10','cifar100'])
parser.add_argument('--data', type=str, default='/cache/data/')
parser.add_argument('--teacher_dir', type=str, default='/cache/models/')
parser.add_argument('--n_epochs', type=int, default=200, help='number of epochs of training')
parser.add_argument('--batch_size', type=int, default=512, help='size of the batches')
parser.add_argument('--lr_G', type=float, default=0.2, help='learning rate')
parser.add_argument('--lr_S', type=float, default=2e-3, help='learning rate')
parser.add_argument('--latent_dim', type=int, default=100, help='dimensionality of the latent space')
parser.add_argument('--img_size', type=int, default=32, help='size of each image dimension')
parser.add_argument('--channels', type=int, default=1, help='number of image channels')
parser.add_argument('--oh', type=float, default=1, help='one hot loss')
parser.add_argument('--ie', type=float, default=5, help='information entropy loss')
parser.add_argument('--a', type=float, default=0.1, help='activation loss')
parser.add_argument('--output_dir', type=str, default='/cache/models/')
opt = parser.parse_args()
img_shape = (opt.channels, opt.img_size, opt.img_size)
cuda = True
accr = 0
accr_best = 0
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.init_size = opt.img_size // 4
self.l1 = nn.Sequential(nn.Linear(opt.latent_dim, 128*self.init_size**2))
self.conv_blocks0 = nn.Sequential(
nn.BatchNorm2d(128),
)
self.conv_blocks1 = nn.Sequential(
nn.Conv2d(128, 128, 3, stride=1, padding=1),
nn.BatchNorm2d(128, 0.8),
nn.LeakyReLU(0.2, inplace=True),
)
self.conv_blocks2 = nn.Sequential(
nn.Conv2d(128, 64, 3, stride=1, padding=1),
nn.BatchNorm2d(64, 0.8),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, opt.channels, 3, stride=1, padding=1),
nn.Tanh(),
nn.BatchNorm2d(opt.channels, affine=False)
)
def forward(self, z):
out = self.l1(z)
out = out.view(out.shape[0], 128, self.init_size, self.init_size)
img = self.conv_blocks0(out)
img = nn.functional.interpolate(img,scale_factor=2)
img = self.conv_blocks1(img)
img = nn.functional.interpolate(img,scale_factor=2)
img = self.conv_blocks2(img)
return img
generator = Generator().cuda()
teacher = torch.load(opt.teacher_dir + 'teacher').cuda()
teacher.eval()
criterion = torch.nn.CrossEntropyLoss().cuda()
teacher = nn.DataParallel(teacher)
generator = nn.DataParallel(generator)
def kdloss(y, teacher_scores):
p = F.log_softmax(y, dim=1)
q = F.softmax(teacher_scores, dim=1)
l_kl = F.kl_div(p, q, size_average=False) / y.shape[0]
return l_kl
if opt.dataset == 'MNIST':
# Configure data loader
net = LeNet5Half().cuda()
net = nn.DataParallel(net)
data_test = MNIST(opt.data,
train=False,
transform=transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
data_test_loader = DataLoader(data_test, batch_size=64, num_workers=1, shuffle=False)
# Optimizers
optimizer_G = torch.optim.Adam(generator.parameters(), lr=opt.lr_G)
optimizer_S = torch.optim.Adam(net.parameters(), lr=opt.lr_S)
if opt.dataset != 'MNIST':
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
if opt.dataset == 'cifar10':
net = resnet.ResNet18().cuda()
net = nn.DataParallel(net)
data_test = CIFAR10(opt.data,
train=False,
transform=transform_test)
if opt.dataset == 'cifar100':
net = resnet.ResNet18(num_classes=100).cuda()
net = nn.DataParallel(net)
data_test = CIFAR100(opt.data,
train=False,
transform=transform_test)
data_test_loader = DataLoader(data_test, batch_size=opt.batch_size, num_workers=0)
# Optimizers
optimizer_G = torch.optim.Adam(generator.parameters(), lr=opt.lr_G)
optimizer_S = torch.optim.SGD(net.parameters(), lr=opt.lr_S, momentum=0.9, weight_decay=5e-4)
def adjust_learning_rate(optimizer, epoch, learing_rate):
if epoch < 800:
lr = learing_rate
elif epoch < 1600:
lr = 0.1*learing_rate
else:
lr = 0.01*learing_rate
for param_group in optimizer.param_groups:
param_group['lr'] = lr
# ----------
# Training
# ----------
batches_done = 0
for epoch in range(opt.n_epochs):
total_correct = 0
avg_loss = 0.0
if opt.dataset != 'MNIST':
adjust_learning_rate(optimizer_S, epoch, opt.lr_S)
for i in range(120):
net.train()
z = Variable(torch.randn(opt.batch_size, opt.latent_dim)).cuda()
optimizer_G.zero_grad()
optimizer_S.zero_grad()
gen_imgs = generator(z)
outputs_T, features_T = teacher(gen_imgs, out_feature=True)
pred = outputs_T.data.max(1)[1]
loss_activation = -features_T.abs().mean()
loss_one_hot = criterion(outputs_T,pred)
softmax_o_T = torch.nn.functional.softmax(outputs_T, dim = 1).mean(dim = 0)
loss_information_entropy = (softmax_o_T * torch.log10(softmax_o_T)).sum()
loss = loss_one_hot * opt.oh + loss_information_entropy * opt.ie + loss_activation * opt.a
loss_kd = kdloss(net(gen_imgs.detach()), outputs_T.detach())
loss += loss_kd
loss.backward()
optimizer_G.step()
optimizer_S.step()
if i == 1:
print ("[Epoch %d/%d] [loss_oh: %f] [loss_ie: %f] [loss_a: %f] [loss_kd: %f]" % (epoch, opt.n_epochs,loss_one_hot.item(), loss_information_entropy.item(), loss_activation.item(), loss_kd.item()))
with torch.no_grad():
for i, (images, labels) in enumerate(data_test_loader):
images = images.cuda()
labels = labels.cuda()
net.eval()
output = net(images)
avg_loss += criterion(output, labels).sum()
pred = output.data.max(1)[1]
total_correct += pred.eq(labels.data.view_as(pred)).sum()
avg_loss /= len(data_test)
print('Test Avg. Loss: %f, Accuracy: %f' % (avg_loss.data.item(), float(total_correct) / len(data_test)))
accr = round(float(total_correct) / len(data_test), 4)
if accr > accr_best:
torch.save(net,opt.output_dir + 'student')
accr_best = accr