-
Notifications
You must be signed in to change notification settings - Fork 18
/
DFAD_cifar.py
185 lines (155 loc) · 7.79 KB
/
DFAD_cifar.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
from __future__ import print_function
import argparse
import torch
import torch.nn.functional as F
import torch.nn as nn
import torch.optim as optim
import network
from utils.visualizer import VisdomPlotter
from utils.misc import pack_images, denormalize
from dataloader import get_dataloader
import os, random
import numpy as np
import torchvision
vp = VisdomPlotter('15550', env='DFAD-cifar')
def train(args, teacher, student, generator, device, optimizer, epoch):
teacher.eval()
student.train()
generator.train()
optimizer_S, optimizer_G = optimizer
for i in range( args.epoch_itrs ):
for k in range(5):
z = torch.randn( (args.batch_size, args.nz, 1, 1) ).to(device)
optimizer_S.zero_grad()
fake = generator(z).detach()
t_logit = teacher(fake)
s_logit = student(fake)
loss_S = F.l1_loss(s_logit, t_logit.detach())
loss_S.backward()
optimizer_S.step()
z = torch.randn( (args.batch_size, args.nz, 1, 1) ).to(device)
optimizer_G.zero_grad()
generator.train()
fake = generator(z)
t_logit = teacher(fake)
s_logit = student(fake)
#loss_G = - torch.log( F.l1_loss( s_logit, t_logit )+1)
loss_G = - F.l1_loss( s_logit, t_logit )
loss_G.backward()
optimizer_G.step()
if i % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tG_Loss: {:.6f} S_loss: {:.6f}'.format(
epoch, i, args.epoch_itrs, 100*float(i)/float(args.epoch_itrs), loss_G.item(), loss_S.item()))
vp.add_scalar('Loss_S', (epoch-1)*args.epoch_itrs+i, loss_S.item())
vp.add_scalar('Loss_G', (epoch-1)*args.epoch_itrs+i, loss_G.item())
def test(args, student, generator, device, test_loader, epoch=0):
student.eval()
generator.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for i, (data, target) in enumerate(test_loader):
data, target = data.to(device), target.to(device)
z = torch.randn( (data.shape[0], args.nz, 1, 1), device=data.device, dtype=data.dtype )
fake = generator(z)
output = student(data)
if i==0:
vp.add_image( 'input', pack_images( denormalize(data,(0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)).clamp(0,1).detach().cpu().numpy() ) )
vp.add_image( 'generated', pack_images( denormalize(fake,(0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)).clamp(0,1).detach().cpu().numpy() ) )
test_loss += F.cross_entropy(output, target, reduction='sum').item() # sum up batch loss
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.4f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
acc = correct/len(test_loader.dataset)
return acc
def main():
# Training settings
parser = argparse.ArgumentParser(description='DFAD CIFAR')
parser.add_argument('--batch_size', type=int, default=256, metavar='N',
help='input batch size for training (default: 256)')
parser.add_argument('--test_batch_size', type=int, default=128, metavar='N',
help='input batch size for testing (default: 128)')
parser.add_argument('--epochs', type=int, default=500, metavar='N',
help='number of epochs to train (default: 500)')
parser.add_argument('--epoch_itrs', type=int, default=50)
parser.add_argument('--lr_S', type=float, default=0.1, metavar='LR',
help='learning rate (default: 0.1)')
parser.add_argument('--lr_G', type=float, default=1e-3,
help='learning rate (default: 0.1)')
parser.add_argument('--data_root', type=str, default='data')
parser.add_argument('--dataset', type=str, default='cifar10', choices=['cifar10', 'cifar100'],
help='dataset name (default: cifar10)')
parser.add_argument('--model', type=str, default='resnet18_8x', choices=['resnet18_8x'],
help='model name (default: resnet18_8x)')
parser.add_argument('--weight_decay', type=float, default=5e-4)
parser.add_argument('--momentum', type=float, default=0.9, metavar='M',
help='SGD momentum (default: 0.9)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--ckpt', type=str, default='checkpoint/teacher/cifar10-resnet34_8x.pt')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--nz', type=int, default=256)
parser.add_argument('--test-only', action='store_true', default=False)
parser.add_argument('--download', action='store_true', default=False)
parser.add_argument('--step_size', type=int, default=100, metavar='S')
parser.add_argument('--scheduler', action='store_true', default=False)
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
print(args)
_, test_loader = get_dataloader(args)
num_classes = 10 if args.dataset=='cifar10' else 100
teacher = network.resnet_8x.ResNet34_8x(num_classes=num_classes)
student = network.resnet_8x.ResNet18_8x(num_classes=num_classes)
generator = network.gan.GeneratorA(nz=args.nz, nc=3, img_size=32)
teacher.load_state_dict( torch.load( args.ckpt ) )
print("Teacher restored from %s"%(args.ckpt))
teacher = teacher.to(device)
student = student.to(device)
generator = generator.to(device)
teacher.eval()
optimizer_S = optim.SGD( student.parameters(), lr=args.lr_S, weight_decay=args.weight_decay, momentum=0.9 )
optimizer_G = optim.Adam( generator.parameters(), lr=args.lr_G )
if args.scheduler:
scheduler_S = optim.lr_scheduler.MultiStepLR(optimizer_S, [100, 200], 0.1)
scheduler_G = optim.lr_scheduler.MultiStepLR(optimizer_G, [100, 200], 0.1)
best_acc = 0
if args.test_only:
acc = test(args, student, generator, device, test_loader)
return
acc_list = []
for epoch in range(1, args.epochs + 1):
# Train
if args.scheduler:
scheduler_S.step()
scheduler_G.step()
train(args, teacher=teacher, student=student, generator=generator, device=device, optimizer=[optimizer_S, optimizer_G], epoch=epoch)
# Test
acc = test(args, student, generator, device, test_loader, epoch)
acc_list.append(acc)
if acc>best_acc:
best_acc = acc
torch.save(student.state_dict(),"checkpoint/student/%s-%s.pt"%(args.dataset, 'resnet18_8x'))
torch.save(generator.state_dict(),"checkpoint/student/%s-%s-generator.pt"%(args.dataset, 'resnet18_8x'))
vp.add_scalar('Acc', epoch, acc)
print("Best Acc=%.6f"%best_acc)
import csv
os.makedirs('log', exist_ok=True)
with open('log/DFAD-%s.csv'%(args.dataset), 'a') as f:
writer = csv.writer(f)
writer.writerow(acc_list)
if __name__ == '__main__':
main()