-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcgd_stochastic.py
195 lines (184 loc) · 8.12 KB
/
cgd_stochastic.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
import os
import time
import torch
import torch.nn as nn
import torchvision.utils as vutils
from torch.utils.data import DataLoader
from optims import SCGD, SCG
from train_utils import get_data, weights_init_d, weights_init_g, \
get_diff, save_checkpoint, lr_scheduler, generate_data, icrScheduler, get_model
from losses import get_loss
from utils import cgd_trainer
# seed = torch.randint(0, 1000000, (1,))
seed = 2020
torch.manual_seed(seed=seed)
print('random seed : %d' % seed)
def train(config, tols, milestone, n=2, device='cpu'):
lr_d = config['lr_d']
lr_g = config['lr_g']
optim_type = config['optimizer']
z_dim = config['z_dim']
model_name = config['model']
epoch_num = config['epoch_num']
show_iter = config['show_iter']
loss_name = config['loss_type']
l2_penalty = config['d_penalty']
logdir = config['logdir']
start_n = config['startn']
dataset = get_data(dataname=config['dataset'], path='../datas/%s' % config['datapath'])
dataloader = DataLoader(dataset=dataset, batch_size=config['batchsize'],
shuffle=True, num_workers=4)
D, G = get_model(model_name=model_name, z_dim=z_dim)
D.apply(weights_init_d).to(device)
G.apply(weights_init_g).to(device)
if optim_type == 'SCGD':
optimizer = SCGD(max_params=G.parameters(), min_params=D.parameters(),
lr_max=lr_g, lr_min=lr_d,
tol=tols['tol'], atol=tols['atol'],
device=device, solver='cg')
scheduler = lr_scheduler(optimizer=optimizer, milestone=milestone)
if config['checkpoint'] is not None:
startPoint = config['checkpoint']
chk = torch.load(startPoint)
D.load_state_dict(chk['D'])
G.load_state_dict(chk['G'])
optimizer.load_state_dict(chk['optim'])
print('Start from %s' % startPoint)
gpu_num = config['gpu_num']
if gpu_num > 1:
D = nn.DataParallel(D, list(range(gpu_num)))
G = nn.DataParallel(G, list(range(gpu_num)))
timer = time.time()
count = 0
if model_name == 'DCGAN' or model_name == 'DCGAN-WBN':
fixed_noise = torch.randn((64, z_dim, 1, 1), device=device)
else:
fixed_noise = torch.randn((64, z_dim), device=device)
for e in range(epoch_num):
scheduler.step(epoch=e)
print('======Epoch: %d / %d======' % (e, epoch_num))
for real_x in dataloader:
optimizer.zero_grad()
real_x = real_x[0].to(device)
d_real = D(real_x)
if model_name == 'DCGAN' or model_name == 'DCGAN-WBN':
z = torch.randn((d_real.shape[0], z_dim, 1, 1), device=device)
else:
z = torch.randn((d_real.shape[0], z_dim), device=device)
fake_x = G(z)
d_fake = D(fake_x)
loss = get_loss(name=loss_name, g_loss=False,
d_real=d_real, d_fake=d_fake,
l2_weight=l2_penalty, D=D)
optimizer.step(loss)
if (count + 1) % n == 0:
optimizer.update(n)
if count % show_iter == 0:
time_cost = time.time() - timer
print('Iter :%d , Loss: %.5f, time: %.3fs'
% (count, loss.item(), time_cost))
timer = time.time()
with torch.no_grad():
fake_img = G(fixed_noise).detach()
path = 'figs/%s_%s/' % (config['dataset'], logdir)
if not os.path.exists(path):
os.makedirs(path)
vutils.save_image(fake_img, path + 'iter_%d.png' % (count + start_n), normalize=True)
save_checkpoint(path=logdir,
name='%s-%s%.3f_%d.pth' % (optim_type, model_name, lr_g, count + start_n),
D=D, G=G, optimizer=optimizer)
count += 1
def train_scg(config, tols, milestone, device='cpu'):
lr_d = config['lr_d']
lr_g = config['lr_g']
optim_type = config['optimizer']
z_dim = config['z_dim']
model_name = config['model']
epoch_num = config['epoch_num']
show_iter = config['show_iter']
loss_name = config['loss_type']
l2_penalty = config['d_penalty']
logdir = config['logdir']
start_n = config['startn']
dataset = get_data(dataname=config['dataset'], path='../datas/%s' % config['datapath'])
dataloader = DataLoader(dataset=dataset, batch_size=config['batchsize'],
shuffle=True, num_workers=4)
inner_loader = DataLoader(dataset=dataset, batch_size=config['batchsize'],
shuffle=True, num_workers=4)
D, G = get_model(model_name=model_name, z_dim=z_dim)
D.apply(weights_init_d).to(device)
G.apply(weights_init_g).to(device)
optimizer = SCG(max_params=G.parameters(), min_params=D.parameters(),
lr_max=lr_g, lr_min=lr_d,
tol=tols['tol'], atol=tols['atol'],
dataloader=inner_loader,
device=device, solver='cg')
scheduler = lr_scheduler(optimizer=optimizer, milestone=milestone)
if config['checkpoint'] is not None:
startPoint = config['checkpoint']
chk = torch.load(startPoint)
D.load_state_dict(chk['D'])
G.load_state_dict(chk['G'])
optimizer.load_state_dict(chk['optim'])
print('Start from %s' % startPoint)
gpu_num = config['gpu_num']
if gpu_num > 1:
D = nn.DataParallel(D, list(range(gpu_num)))
G = nn.DataParallel(G, list(range(gpu_num)))
timer = time.time()
count = 0
if model_name == 'DCGAN' or model_name == 'DCGAN-WBN':
fixed_noise = torch.randn((64, z_dim, 1, 1), device=device)
else:
fixed_noise = torch.randn((64, z_dim), device=device)
for e in range(epoch_num):
scheduler.step(epoch=e)
print('======Epoch: %d / %d======' % (e, epoch_num))
for real_x in dataloader:
optimizer.zero_grad()
real_x = real_x[0]
if model_name == 'DCGAN' or model_name == 'DCGAN-WBN':
z = torch.randn((real_x.shape[0], z_dim, 1, 1), device=device)
else:
z = torch.randn((real_x.shape[0], z_dim), device=device)
def closure(train_x):
train_x = train_x.to(device)
fake_x = G(z)
d_fake = D(fake_x)
d_real = D(train_x)
loss = get_loss(name=loss_name, g_loss=False,
d_real=d_real, d_fake=d_fake,
l2_weight=l2_penalty, D=D)
return loss
loss = optimizer.step(closure=closure, img=real_x)
if count % show_iter == 0:
time_cost = time.time() - timer
print('Iter :%d , Loss: %.5f, time: %.3fs'
% (count, loss.item(), time_cost))
timer = time.time()
with torch.no_grad():
fake_img = G(fixed_noise).detach()
path = 'figs/%s_%s/' % (config['dataset'], logdir)
if not os.path.exists(path):
os.makedirs(path)
vutils.save_image(fake_img, path + 'iter_%d.png' % (count + start_n), normalize=True)
save_checkpoint(path=logdir,
name='%s-%s%.3f_%d.pth' % (optim_type, model_name, lr_g, count + start_n),
D=D, G=G, optimizer=optimizer)
count += 1
if __name__ == '__main__':
torch.backends.cudnn.benchmark = True
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(device)
parser = cgd_trainer()
parser.add_argument('--strategy', type=str, default='scg')
config = vars(parser.parse_args())
print(config)
lr_g = config['lr_g']
lr_d = config['lr_d']
milestones = {'0': (lr_g, lr_d)}
tols = {'tol': config['tol'], 'atol': config['atol']}
if config['strategy'] == 'scg':
train_scg(config=config, tols=tols, milestone=milestones, device=device)
else:
train(config=config, tols=tols, milestone=milestones, n=3, device=device)