-
Notifications
You must be signed in to change notification settings - Fork 13
/
condense_mp.py
562 lines (463 loc) · 19.9 KB
/
condense_mp.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
"""
The original code is created by Jang-Hyun Kim.
GitHub Repository: https://github.com/snu-mllab/Efficient-Dataset-Condensation
"""
import os
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from data import transform_imagenet, transform_cifar, TensorDataset, ImageFolder, save_img
from data import ClassPartMemDataLoader, MultiEpochsDataLoader
from data import MEANS, STDS
from train import define_model, train_epoch
from test import test_data, load_ckpt
from misc import utils
from misc.augment import DiffAug
from math import ceil
import glob
class Synthesizer():
"""Condensed data class
"""
def __init__(self, args, nclass_sub, subclass_list, nchannel, hs, ws, device='cuda'):
self.ipc = args.ipc
self.nclass_sub = nclass_sub
self.subclass_list = subclass_list
self.nchannel = nchannel
self.size = (hs, ws)
self.device = device
self.data = torch.randn(size=(self.nclass_sub * self.ipc, self.nchannel, hs, ws),
dtype=torch.float,
requires_grad=True,
device=self.device)
self.data.data = torch.clamp(self.data.data / 4 + 0.5, min=0., max=1.)
self.targets = torch.tensor(
[np.ones(self.ipc) * self.subclass_list[c] for c in range(nclass_sub)],
dtype=torch.long,
requires_grad=False,
device=self.device).view(-1)
print("\nDefine synthetic data: ", self.data.shape)
self.factor = max(1, args.factor)
self.decode_type = args.decode_type
self.resize = nn.Upsample(size=self.size, mode='bilinear')
print("Factor: ", self.factor)
def init(self, loader, init_type='noise'):
"""Condensed data initialization
"""
if init_type == 'random':
print("Random initialize synset")
for c in range(self.nclass_sub):
img, _ = loader.class_sample(self.subclass_list[c], self.ipc)
self.data.data[self.ipc * c:self.ipc * (c + 1)] = img.data.to(self.device)
elif init_type == 'mix':
print("Mixed initialize synset")
for c in range(self.nclass_sub):
img, _ = loader.class_sample(self.subclass_list[c], self.ipc * self.factor**2)
img = img.data.to(self.device)
s = self.size[0] // self.factor
remained = self.size[0] % self.factor
k = 0
n = self.ipc
h_loc = 0
for i in range(self.factor):
h_r = s + 1 if i < remained else s
w_loc = 0
for j in range(self.factor):
w_r = s + 1 if j < remained else s
img_part = F.interpolate(img[k * n:(k + 1) * n], size=(h_r, w_r))
self.data.data[n * c:n * (c + 1), :, h_loc:h_loc + h_r,
w_loc:w_loc + w_r] = img_part
w_loc += w_r
k += 1
h_loc += h_r
elif init_type == 'noise':
pass
def parameters(self):
parameter_list = [self.data]
return parameter_list
def subsample(self, data, target, max_size=-1):
if (data.shape[0] > max_size) and (max_size > 0):
indices = np.random.permutation(data.shape[0])
data = data[indices[:max_size]]
target = target[indices[:max_size]]
return data, target
def decode_zoom(self, img, target, factor):
"""Uniform multi-formation
"""
h = img.shape[-1]
remained = h % factor
if remained > 0:
img = F.pad(img, pad=(0, factor - remained, 0, factor - remained), value=0.5)
s_crop = ceil(h / factor)
n_crop = factor**2
cropped = []
for i in range(factor):
for j in range(factor):
h_loc = i * s_crop
w_loc = j * s_crop
cropped.append(img[:, :, h_loc:h_loc + s_crop, w_loc:w_loc + s_crop])
cropped = torch.cat(cropped)
data_dec = self.resize(cropped)
target_dec = torch.cat([target for _ in range(n_crop)])
return data_dec, target_dec
def decode_zoom_multi(self, img, target, factor_max):
"""Multi-scale multi-formation
"""
data_multi = []
target_multi = []
for factor in range(1, factor_max + 1):
decoded = self.decode_zoom(img, target, factor)
data_multi.append(decoded[0])
target_multi.append(decoded[1])
return torch.cat(data_multi), torch.cat(target_multi)
def decode_zoom_bound(self, img, target, factor_max, bound=128):
"""Uniform multi-formation with bounded number of synthetic data
"""
bound_cur = bound - len(img)
budget = len(img)
data_multi = []
target_multi = []
idx = 0
decoded_total = 0
for factor in range(factor_max, 0, -1):
decode_size = factor**2
if factor > 1:
n = min(bound_cur // decode_size, budget)
else:
n = budget
decoded = self.decode_zoom(img[idx:idx + n], target[idx:idx + n], factor)
data_multi.append(decoded[0])
target_multi.append(decoded[1])
idx += n
budget -= n
decoded_total += n * decode_size
bound_cur = bound - decoded_total - budget
if budget == 0:
break
data_multi = torch.cat(data_multi)
target_multi = torch.cat(target_multi)
return data_multi, target_multi
def decode(self, data, target, bound=128):
"""Multi-formation
"""
if self.decode_type == 'multi':
data, target = self.decode_zoom_multi(data, target, self.factor)
elif self.decode_type == 'bound':
data, target = self.decode_zoom_bound(data, target, self.factor, bound=bound)
else:
data, target = self.decode_zoom(data, target, self.factor)
return data, target
def sample(self, c, max_size=128):
"""Sample synthetic data per class
"""
idx_from = self.ipc * c
idx_to = self.ipc * (c + 1)
data = self.data[idx_from:idx_to]
target = self.targets[idx_from:idx_to]
data, target = self.decode(data, target, bound=max_size)
data, target = self.subsample(data, target, max_size=max_size)
return data, target
def loader(self, args, augment=True):
"""Data loader for condensed data
"""
if args.dataset == 'imagenet':
train_transform, _ = transform_imagenet(augment=augment,
from_tensor=True,
size=0,
rrc=args.rrc,
rrc_size=self.size[0])
elif args.dataset[:5] == 'cifar':
train_transform, _ = transform_cifar(augment=augment, from_tensor=True)
data, target = self.decode(self.data.detach(), self.targets.detach())
train_dataset = TensorDataset(data.cpu(), target.cpu(), train_transform)
print("Decode condensed data: ", data.shape)
nw = 0 if not augment else args.workers
train_loader = MultiEpochsDataLoader(train_dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=nw,
persistent_workers=nw > 0)
return train_loader
def test(self, args, val_loader, logger, bench=True):
"""Condensed data evaluation
"""
loader = self.loader(args, args.augment)
# Test on current model
test_data(args, loader, val_loader, test_resnet=False, logger=logger)
if bench:
# Test on resnet-10 models
test_data(args, loader, val_loader, test_resnet=True, logger=logger)
def load_resized_data(args, subclass_list):
"""Load original training data (without augmentation) for condensation
"""
if args.dataset == 'cifar100':
train_dataset = datasets.CIFAR100(args.data_dir,
train=True,
transform=transforms.ToTensor())
normalize = transforms.Normalize(mean=MEANS['cifar100'], std=STDS['cifar100'])
transform_test = transforms.Compose([transforms.ToTensor(), normalize])
val_dataset = datasets.CIFAR100(args.data_dir, train=False, transform=transform_test)
train_dataset.nclass = 100
elif args.dataset == 'imagenet':
traindir = os.path.join(args.imagenet_dir, 'train')
valdir = os.path.join(args.imagenet_dir, 'val')
resize = transforms.Compose([
transforms.Resize(args.size),
transforms.CenterCrop(args.size),
transforms.PILToTensor()
])
if args.load_memory: # uint8
transform = None
load_transform = resize
else:
transform = resize
load_transform = None
_, test_transform = transform_imagenet(size=args.size)
train_dataset = ImageFolder(traindir,
transform=transform,
nclass=args.nclass,
seed=args.dseed,
load_memory=args.load_memory,
load_transform=load_transform)
val_dataset = ImageFolder(valdir,
test_transform,
nclass=args.nclass,
seed=args.dseed,
load_memory=False)
# Validate only for the given subclass
indices = []
for i, c in enumerate(val_dataset.targets):
if c in subclass_list:
indices.append(i)
val_dataset = torch.utils.data.Subset(val_dataset, indices)
val_loader = MultiEpochsDataLoader(val_dataset,
batch_size=args.batch_size // 2,
shuffle=False,
persistent_workers=True,
num_workers=4)
assert train_dataset[0][0].shape[-1] == val_dataset[0][0].shape[-1] # width check
return train_dataset, val_loader
def remove_aug(augtype, remove_aug):
aug_list = []
for aug in augtype.split("_"):
if aug not in remove_aug.split("_"):
aug_list.append(aug)
return "_".join(aug_list)
def diffaug(args, device='cuda'):
"""Differentiable augmentation for condensation
"""
aug_type = args.aug_type
normalize = utils.Normalize(mean=MEANS[args.dataset], std=STDS[args.dataset], device=device)
print("Augmentataion Matching: ", aug_type)
augment = DiffAug(strategy=aug_type, batch=True)
aug_batch = transforms.Compose([normalize, augment])
if args.mixup_net == 'cut':
aug_type = remove_aug(aug_type, 'cutout')
print("Augmentataion Net update: ", aug_type)
augment_rand = DiffAug(strategy=aug_type, batch=False)
aug_rand = transforms.Compose([normalize, augment_rand])
return aug_batch, aug_rand
def dist(x, y, method='mse'):
"""Distance objectives
"""
if method == 'mse':
dist_ = (x - y).pow(2).sum()
elif method == 'l1':
dist_ = (x - y).abs().sum()
elif method == 'l1_mean':
n_b = x.shape[0]
dist_ = (x - y).abs().reshape(n_b, -1).mean(-1).sum()
elif method == 'cos':
x = x.reshape(x.shape[0], -1)
y = y.reshape(y.shape[0], -1)
dist_ = torch.sum(1 - torch.sum(x * y, dim=-1) /
(torch.norm(x, dim=-1) * torch.norm(y, dim=-1) + 1e-6))
return dist_
def add_loss(loss_sum, loss):
if loss_sum == None:
return loss
else:
return loss_sum + loss
def matchloss(args, img_real, img_syn, lab_real, lab_syn, model):
"""Matching losses (feature or gradient)
"""
loss = None
if args.match == 'feat':
with torch.no_grad():
feat_tg = model.get_feature(img_real, args.idx_from, args.idx_to)
feat = model.get_feature(img_syn, args.idx_from, args.idx_to)
for i in range(len(feat)):
loss = add_loss(loss, dist(feat_tg[i].mean(0), feat[i].mean(0), method=args.metric))
elif args.match == 'grad':
criterion = nn.CrossEntropyLoss()
output_real = model(img_real)
loss_real = criterion(output_real, lab_real)
g_real = torch.autograd.grad(loss_real, model.parameters())
g_real = list((g.detach() for g in g_real))
output_syn = model(img_syn)
loss_syn = criterion(output_syn, lab_syn)
g_syn = torch.autograd.grad(loss_syn, model.parameters(), create_graph=True)
for i in range(len(g_real)):
if (len(g_real[i].shape) == 1) and not args.bias: # bias, normliazation
continue
if (len(g_real[i].shape) == 2) and not args.fc:
continue
loss = add_loss(loss, dist(g_real[i], g_syn[i], method=args.metric))
return loss
def pretrain_sample(args, model, verbose=False):
"""Load pretrained networks
"""
folder_base = f'./pretrained/{args.datatag}/{args.modeltag}_cut'
folder_list = glob.glob(f'{folder_base}*')
tag = np.random.randint(len(folder_list))
folder = folder_list[tag]
epoch = args.pt_from
if args.pt_num > 1:
epoch = np.random.randint(args.pt_from, args.pt_from + args.pt_num)
ckpt = f'checkpoint{epoch}.pth.tar'
file_dir = os.path.join(folder, ckpt)
load_ckpt(model, file_dir, verbose=verbose)
def condense(args, logger, device='cuda'):
"""Optimize condensed data
"""
# Define subclass list and reverse mapping
cls_from = args.phase * args.nclass_sub
cls_to = (args.phase + 1) * args.nclass_sub
subclass_list = np.arange(cls_from, cls_to)
real_to_idx = {}
for i, c in enumerate(subclass_list):
real_to_idx[c] = i
# Define real dataset and loader
trainset, val_loader = load_resized_data(args, subclass_list)
## This load target subclass data on GPU memory
## One can modify ClassPartMemDataLoader to dynamically read subclass data without pre-loading
## Note, the entire class data is random sampled via the dataloader iterator (used for training networks)
loader_real = ClassPartMemDataLoader(subclass_list,
real_to_idx,
trainset,
batch_size=args.batch_real,
num_workers=args.workers,
shuffle=True,
pin_memory=True,
drop_last=True)
nclass = trainset.nclass
nch, hs, ws = trainset[0][0].shape
# Define syn dataset
synset = Synthesizer(args, args.nclass_sub, subclass_list, nch, hs, ws)
synset.init(loader_real, init_type=args.init)
save_img(os.path.join(args.save_dir, 'init.png'),
synset.data,
unnormalize=False,
dataname=args.dataset)
# Define augmentation function
aug, aug_rand = diffaug(args)
save_img(os.path.join(args.save_dir, f'aug.png'),
aug(synset.sample(0, max_size=args.batch_syn_max)[0]),
unnormalize=True,
dataname=args.dataset)
if not args.test:
synset.test(args, val_loader, logger, bench=False)
# Data distillation
optim_img = torch.optim.SGD(synset.parameters(), lr=args.lr_img, momentum=args.mom_img)
ts = utils.TimeStamp(args.time)
n_iter = args.niter * 100 // args.inner_loop
it_log = n_iter // 50
it_test = [n_iter // 10, n_iter // 5, n_iter // 2, n_iter]
logger(f"\nStart condensing with {args.match} matching for {n_iter} iteration")
args.fix_iter = max(1, args.fix_iter)
for it in range(n_iter):
if it % args.fix_iter == 0:
model = define_model(args, nclass).to(device)
model.train()
optim_net = optim.SGD(model.parameters(),
args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
criterion = nn.CrossEntropyLoss()
if args.pt_from >= 0:
pretrain_sample(args, model)
if args.early > 0:
for _ in range(args.early):
train_epoch(args,
loader_real,
model,
criterion,
optim_net,
aug=aug_rand,
mixup=args.mixup_net)
loss_total = 0
synset.data.data = torch.clamp(synset.data.data, min=0., max=1.)
for ot in range(args.inner_loop):
ts.set()
# Update synset
for c in subclass_list:
img, lab = loader_real.class_sample(c)
img_syn, lab_syn = synset.sample(real_to_idx[c], max_size=args.batch_syn_max)
ts.stamp("data")
n = img.shape[0]
img_aug = aug(torch.cat([img, img_syn]))
ts.stamp("aug")
loss = matchloss(args, img_aug[:n], img_aug[n:], lab, lab_syn, model)
loss_total += loss.item()
ts.stamp("loss")
optim_img.zero_grad()
loss.backward()
optim_img.step()
ts.stamp("backward")
# Net update
if args.n_data > 0:
loader_net = loader_real
for _ in range(args.net_epoch):
train_epoch(args,
loader_net,
model,
criterion,
optim_net,
n_data=args.n_data,
aug=aug_rand,
mixup=args.mixup_net)
ts.stamp("net update")
if (ot + 1) % 10 == 0:
ts.flush()
# Logging
if it % it_log == 0:
logger(
f"{utils.get_time()} (Iter {it:3d}) loss: {loss_total/args.nclass_sub/args.inner_loop:.1f}"
)
if (it + 1) in it_test:
save_img(os.path.join(args.save_dir, f'img{it+1}.png'),
synset.data,
unnormalize=False,
dataname=args.dataset)
torch.save(
[synset.data.detach().cpu(), synset.targets.cpu()],
os.path.join(args.save_dir, f'data.pt'))
print("img and data saved!")
if not args.test:
synset.test(args, val_loader, logger)
if __name__ == '__main__':
import shutil
from misc.utils import Logger
from argument import args
import torch.backends.cudnn as cudnn
import json
cudnn.benchmark = True
assert args.ipc > 0
if args.nclass_sub < 0:
raise AssertionError("Set number of subclasses! (args.nclass_sub)")
if args.phase < 0:
raise AssertionError("Set phase number! (args.phase)")
if args.seed > 0:
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
os.makedirs(args.save_dir, exist_ok=True)
cur_file = os.path.join(os.getcwd(), __file__)
shutil.copy(cur_file, args.save_dir)
logger = Logger(args.save_dir)
logger(f"Save dir: {args.save_dir}")
with open(os.path.join(args.save_dir, 'args.txt'), 'w') as f:
json.dump(args.__dict__, f, indent=2)
condense(args, logger)