forked from aailabkaist/DiffRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
180 lines (155 loc) · 7.78 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
import click
import os
import classifier_lib
import torch
import numpy as np
import glob
import dnnlib
import torchvision.transforms as transforms
import torch.utils.data as data
def npz_concat(filenames):
for file in filenames:
samples = np.load(file)['samples']
try:
data = np.concatenate((data, samples))
except:
data = samples
return data
def npz_concat_cond(filenames):
for file in filenames:
samples = np.load(file)['samples']
label = np.load(file)['label']
try:
data = np.concatenate((data, samples))
data_label = np.concatenate((data_label, label))
except:
data = samples
data_label = label
return data, data_label
class BasicDataset(data.Dataset):
def __init__(self, x_np, y_np, transform=transforms.ToTensor()):
super(BasicDataset, self).__init__()
self.x = x_np
self.y = y_np
self.transform = transform
def __getitem__(self, index):
return self.transform(self.x[index]), self.y[index]
def __len__(self):
return len(self.x)
class BasicDatasetCond(data.Dataset):
def __init__(self, x_np, y_np, cond_np, transform=transforms.ToTensor()):
super(BasicDatasetCond, self).__init__()
self.x = x_np
self.y = y_np
self.cond = cond_np
self.transform = transform
def __getitem__(self, index):
return self.transform(self.x[index]), self.y[index], self.cond[index]
def __len__(self):
return len(self.x)
@click.command()
@click.option('--savedir', help='Save directory', metavar='PATH', type=str, required=True, default="/checkpoints/discriminator/cifar_uncond")
@click.option('--gendir', help='Fake sample directory', metavar='PATH', type=str, required=True, default="/samples/cifar_uncond_vanilla")
@click.option('--datadir', help='Real sample directory', metavar='PATH', type=str, required=True, default="/data/true_data.npz")
@click.option('--img_resolution', help='Image resolution', metavar='INT', type=click.IntRange(min=1), default=32)
@click.option('--cond', help='Is it conditional?', metavar='INT', type=click.IntRange(min=0), default=0)
@click.option('--pretrained_classifier_ckpt', help='Path of classifier', metavar='STR', type=str, default='/checkpoints/ADM_classifier/32x32_classifier.pt')
@click.option('--num_data', help='Num samples', metavar='INT', type=click.IntRange(min=1), default=50000)
@click.option('--batch_size', help='Num samples', metavar='INT', type=click.IntRange(min=1), default=128)
@click.option('--epoch', help='Num samples', metavar='INT', type=click.IntRange(min=1), default=50)
@click.option('--lr', help='Learning rate', metavar='FLOAT', type=click.FloatRange(min=0),default=3e-4)
@click.option('--device', help='Device', metavar='STR', type=str, default='cuda:0')
def main(**kwargs):
opts = dnnlib.EasyDict(kwargs)
gendir = os.getcwd() + opts.gendir
savedir = os.getcwd() + opts.savedir
datadir = os.getcwd() + opts.datadir
os.makedirs(savedir, exist_ok=True)
## Prepare real data
if not opts.cond:
real_data = np.load(datadir)['arr_0']
else:
real_data = np.load(datadir)['samples']
real_label = np.load(datadir)['label']
real_label = np.eye(10)[real_label]
## Prepare fake data
if not opts.cond:
if not os.path.exists(os.path.join(gendir, 'gen_data_for_discriminator_training.npz')):
filenames = np.sort(glob.glob(os.path.join(gendir, 'sample*.npz')))
gen_data = npz_concat(filenames)
np.savez_compressed(os.path.join(gendir, 'gen_data_for_discriminator_training.npz'), samples=gen_data)
else:
gen_data = np.load(os.path.join(gendir, 'gen_data_for_discriminator_training.npz'))['samples']
else:
if not os.path.exists(os.path.join(gendir, 'gen_data_for_discriminator_training.npz')):
filenames = np.sort(glob.glob(os.path.join(gendir, 'sample*.npz')))
gen_data, gen_label = npz_concat_cond(filenames)
np.savez_compressed(os.path.join(gendir, 'gen_data_for_discriminator_training.npz'), samples=gen_data, label=gen_label)
else:
gen_data = np.load(os.path.join(gendir, 'gen_data_for_discriminator_training.npz'))['samples']
gen_label = np.load(os.path.join(gendir, 'gen_data_for_discriminator_training.npz'))['label']
gen_label = gen_label[:opts.num_data]
## Combine the fake / real
real_data = real_data[:opts.num_data]
gen_data = gen_data[:opts.num_data]
train_data = np.concatenate((real_data, gen_data))
train_label = torch.zeros(train_data.shape[0])
train_label[:real_data.shape[0]] = 1.
transform = transforms.Compose([transforms.ToTensor()])
if not opts.cond:
train_dataset = BasicDataset(train_data, train_label, transform)
else:
condition_label = np.concatenate((real_label, gen_label))
train_dataset = BasicDatasetCond(train_data, train_label, condition_label, transform)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=opts.batch_size, num_workers=0, shuffle=True, drop_last=True)
## Extractor & Disciminator
pretrained_classifier = classifier_lib.load_classifier(opts.pretrained_classifier_ckpt, opts.img_resolution, opts.device, eval=False)
discriminator = classifier_lib.load_discriminator(None, opts.device, opts.cond, eval=False)
## Prepare training
vpsde = classifier_lib.vpsde()
optimizer = torch.optim.Adam(discriminator.parameters(), lr=opts.lr, weight_decay=1e-7)
loss = torch.nn.BCELoss()
scaler = lambda x: 2. * x - 1.
## Training
for i in range(opts.epoch):
outs = []
cors = []
num_data = 0
for data in train_loader:
optimizer.zero_grad()
if not opts.cond:
inputs, labels = data
else:
inputs, labels, cond = data
cond = cond.to(opts.device)
inputs = inputs.to(opts.device)
labels = labels.to(opts.device)
inputs = scaler(inputs)
## Data perturbation
t, _ = vpsde.get_diffusion_time(inputs.shape[0], inputs.device)
mean, std = vpsde.marginal_prob(t)
z = torch.randn_like(inputs)
perturbed_inputs = mean[:, None, None, None] * inputs + std[:, None, None, None] * z
## Forward
with torch.no_grad():
pretrained_feature = pretrained_classifier(perturbed_inputs, timesteps=t, feature=True)
if not opts.cond:
label_prediction = discriminator(pretrained_feature, t, sigmoid=True).view(-1)
else:
label_prediction = discriminator(pretrained_feature, t, sigmoid=True, condition=cond).view(-1)
## Backward
out = loss(label_prediction, labels)
out.backward()
optimizer.step()
## Report
cor = ((label_prediction > 0.5).float() == labels).float().mean()
outs.append(out.item())
cors.append(cor.item())
num_data += inputs.shape[0]
print(f"{i}-th epoch BCE loss: {np.mean(outs)}, correction rate: {np.mean(cors)}")
## Save
torch.save(discriminator.state_dict(), savedir + f"/discriminator_{i+1}.pt")
#----------------------------------------------------------------------------
if __name__ == "__main__":
main()
#----------------------------------------------------------------------------