-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_bf_cnn.py
executable file
·250 lines (198 loc) · 9.15 KB
/
train_bf_cnn.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
import numpy as np
import torch
import torch.nn as nn
import os
import time
import matplotlib.pyplot as plt
from PIL import Image
import json
from torch.utils.tensorboard import SummaryWriter
import torch.optim as optim
import argparse
import config
from loader import get_train_dataloader, get_test_dataloader
from models.autoencoders import Generator, Encoder_CIFAR, Decoder_CIFAR, Encoder, Decoder
from models.bfcnn import BF_CNN
from utils import batch2im, PSNR
parser = config.get_common_parser()
parser = config.get_train_parser(parser)
args = parser.parse_args()
dev = "cuda:{}".format(args.gpu) if args.gpu>=0 else "cpu"
device = torch.device(dev)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
train_dataloader = get_train_dataloader(args)
test_dataloader = get_test_dataloader(args)
print(len(train_dataloader))
print(len(test_dataloader))
criterion = nn.MSELoss()
def test(net, bfcnn, stddev=0., saved_dir=None, writer=None, epoch=0):
net.eval()
bfcnn.eval()
avg_psnr = 0.
avg_loss = 0.
count = 0.
batch_count = 0.
with torch.no_grad():
for i, data in enumerate(test_dataloader):
inputs = data[0].to(device)
B = inputs.size(0)
code = net.encoder(inputs)
noise = torch.randn_like(code)*stddev
residual = bfcnn(code + noise)
loss = criterion(residual, noise)
outputs = net.decoder(code+noise-residual)
avg_psnr += PSNR(reduction='sum')(outputs, inputs, -1, 1, offset=0)
avg_loss += loss.item()
count += inputs.size(0)
batch_count += 1
if args.show_outputs:
plt.imsave('test.png', batch2im(outputs, 8, 8,
im_height=args.image_size, im_width=args.image_size))
plt.imsave('test_target.png', batch2im(inputs, 8, 8,
im_height=args.image_size, im_width=args.image_size))
break
print('Average PSNR: {:.4f}, Average loss: {:.4f}'.format(avg_psnr/count, avg_loss / batch_count) )
if writer is not None:
writer.add_scalar('PSNR/test', avg_psnr/count, epoch+1)
def train(net, bfcnn, optimizer, num_epoch, stddev=0.,
saved_dir=None, model_name=None, which_epoch=0, writer=None, clip_val=5):
if not os.path.exists(saved_dir):
os.makedirs(saved_dir)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=args.epochs // 2, gamma=0.1)
for epoch in range(which_epoch, num_epoch): # loop over the dataset multiple times
start_time = time.time()
net.eval()
bfcnn.train()
running_loss = 0.
for i, data in enumerate(train_dataloader):
if args.debug and i==10:
break
optimizer.zero_grad()
inputs = data[0].to(device)
B = inputs.size(0)
with torch.no_grad():
code = net.encoder(inputs)
noise = torch.randn_like(code)*stddev
residual = bfcnn(code + noise)
loss_codeword = criterion(residual, noise)
loss = loss_codeword
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % args.print_freq == args.print_freq-1 or i == len(train_dataloader)-1:
with torch.no_grad():
code = code[:B,:,:,:]
noise = noise[:B,:,:,:]
residual = residual[:B,:,:,:]
mse_y = criterion(net.decoder(code + noise), inputs)
mse_z_star = criterion(net.decoder(code), inputs)
mse_dn = criterion(net.decoder(code+noise-residual), inputs)
log_message = "[{:4d}, {:5d}] loss: {:.5f}, MSE y: {:.5f}, MSE z*: {:.5f}, MSE denoised: {:.5f}, ".format(epoch+1, i+1,
running_loss / (i+1),
mse_y.item(),
mse_z_star.item(),
mse_dn.item()
)
print(log_message)
scheduler.step()
if writer is not None:
writer.add_scalar("Loss/train", running_loss / (i+1), epoch+1)
if epoch % args.display_freq == args.display_freq-1:
with torch.no_grad():
outputs_y = net.decoder(code+noise)
outputs_z_star = net.decoder(code)
outputs_dn = net.decoder(code+noise-residual)
targets = Image.fromarray(batch2im(inputs, 2, 2,
im_height=args.train_image_size, im_width=args.train_image_size))
targets.save(os.path.join(saved_dir, "e{:03d}_targets.png".format(epoch+1)))
outputs = Image.fromarray(batch2im(outputs_y, 2, 2,
im_height=args.train_image_size, im_width=args.train_image_size))
outputs.save(os.path.join(saved_dir, "e{:03d}_outputs_y.png".format(epoch+1)))
outputs = Image.fromarray(batch2im(outputs_z_star, 2, 2,
im_height=args.train_image_size, im_width=args.train_image_size))
outputs.save(os.path.join(saved_dir, "e{:03d}_outputs_z_star.png".format(epoch+1)))
outputs = Image.fromarray(batch2im(outputs_dn, 2, 2,
im_height=args.train_image_size, im_width=args.train_image_size))
outputs.save(os.path.join(saved_dir, "e{:03d}_outputs_dn.png".format(epoch+1)))
if epoch % args.save_freq == args.save_freq-1:
torch.save(net.state_dict(),
os.path.join(saved_dir,"{}_{}_e{:03d}.pb".format(model_name,
args.num_channels,
epoch+1)))
torch.save(bfcnn.state_dict(),
os.path.join(saved_dir,"{}_{}_e{:03d}_bfcnn.pb".format(model_name,
args.num_channels,
epoch+1)))
if epoch % args.test_freq == args.test_freq - 1:
test(net, bfcnn, stddev, saved_dir=saved_dir, writer=writer, epoch=epoch)
print('Time Taken: %d sec' % (time.time() - start_time))
print('Finished Training')
test(net, bfcnn, stddev, saved_dir=saved_dir)
def train_model(net, bfcnn, epoch=30, stddev=0., wd=0., model_name="", saved_dir=None, writer=None):
params = bfcnn.parameters()
optimizer = optim.Adam(params, lr=args.lr, betas=(0.9, 0.999), weight_decay=args.weight_decay)
train(net, bfcnn, optimizer,
epoch,
stddev=stddev,
saved_dir=saved_dir,
model_name=model_name,
writer=writer)
def main():
if 'cifar' in args.dataset:
Enc = Encoder_CIFAR
Dec = Decoder_CIFAR
else:
Enc = Encoder
Dec = Decoder
encoder = Enc(num_out=args.num_channels,
num_hidden=args.num_hidden,
num_conv_blocks=args.num_conv_blocks,
num_residual_blocks=args.num_residual_blocks,
normalization=nn.BatchNorm2d,
activation=nn.PReLU,
power_norm=args.power_norm)
decoder = Dec(num_in=args.num_channels,
num_hidden=args.num_hidden,
num_conv_blocks=args.num_conv_blocks,
num_residual_blocks=args.num_residual_blocks,
normalization=nn.BatchNorm2d,
activation=nn.PReLU,
no_tanh=False)
net = Generator(encoder, decoder)
bfcnn = BF_CNN(1, 64, 3, 20, args.num_channels)
print(args)
if args.pretrained_model_path is not None:
try:
filepath = args.pretrained_model_path
print("Try loading "+filepath)
net.load_state_dict(torch.load(filepath, map_location=dev))
except Exception as e:
print(e)
print("Loading Failed. Initializing Networks...")
exit()
net.to(device)
bfcnn.to(device)
if args.eval:
test(net, bfcnn, 10**(-0.05*args.snr))
exit()
saved_dir = args.model_path
if not os.path.exists(saved_dir):
os.makedirs(saved_dir)
writer = SummaryWriter(saved_dir)
with open(os.path.join(saved_dir, 'args.txt'), 'w') as f:
json.dump(vars(args), f, indent=4)
train_model(net,
bfcnn,
epoch=args.epochs,
stddev=10**(-0.05*args.snr),
model_name=args.model_name,
saved_dir=saved_dir,
writer=writer)
torch.save(net.state_dict(),
os.path.join(saved_dir,args.model_name+"_{}.pb".format(args.num_channels)))
torch.save(bfcnn.state_dict(),
os.path.join(saved_dir,args.model_name+"_{}_bfcnn.pb".format(args.num_channels)))
if __name__=='__main__':
main()