This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
train_old_version.py
424 lines (336 loc) · 17.6 KB
/
train_old_version.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
#!/usr/bin/env python
import sys
import os
import torch
import torch.nn
import torch.optim
import torchvision
import torch.nn.functional as F
import math
import numpy as np
import tqdm
from model import *
from imp_subnet import *
import torchvision.transforms as T
import config as c
from tensorboardX import SummaryWriter
from datasets import trainloader, testloader
import viz
import modules.module_util as mutil
import modules.Unet_common as common
import warnings
from vgg_loss import VGGLoss
warnings.filterwarnings("ignore")
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
def computePSNR(origin, pred):
origin = np.array(origin)
origin = origin.astype(np.float32)
pred = np.array(pred)
pred = pred.astype(np.float32)
mse = np.mean((origin / 1.0 - pred / 1.0) ** 2)
if mse < 1.0e-10:
return 100
if mse > 1.0e15:
return -100
return 10 * math.log10(255.0 ** 2 / mse)
def gauss_noise(shape):
noise = torch.zeros(shape).to(device)
for i in range(noise.shape[0]):
noise[i] = torch.randn(noise[i].shape).to(device)
return noise
def guide_loss(output, bicubic_image):
loss_fn = torch.nn.MSELoss(reduce=True, size_average=False)
loss = loss_fn(output, bicubic_image)
return loss.to(device)
def reconstruction_loss(rev_input, input):
loss_fn = torch.nn.MSELoss(reduce=True, size_average=False)
loss = loss_fn(rev_input, input)
return loss.to(device)
def imp_loss(output, resi):
loss_fn = torch.nn.MSELoss(reduce=True, size_average=False)
loss = loss_fn(output, resi)
return loss.to(device)
def low_frequency_loss(ll_input, gt_input):
loss_fn = torch.nn.L1Loss(reduce=True, size_average=False)
loss = loss_fn(ll_input, gt_input)
return loss.to(device)
def distr_loss(noise):
loss_fn = torch.nn.MSELoss(reduce=True, size_average=False)
loss = loss_fn(noise, torch.zeros(noise.shape).cuda())
return loss.to(device)
# 网络参数数量
def get_parameter_number(net):
total_num = sum(p.numel() for p in net.parameters())
trainable_num = sum(p.numel() for p in net.parameters() if p.requires_grad)
return {'Total': total_num, 'Trainable': trainable_num}
def load(name, net, optim):
state_dicts = torch.load(name)
network_state_dict = {k: v for k, v in state_dicts['net'].items() if 'tmp_var' not in k}
net.load_state_dict(network_state_dict)
try:
optim.load_state_dict(state_dicts['opt'])
except:
print('Cannot load optimizer for some reason or other')
def init_net3(mod):
for key, param in mod.named_parameters():
if param.requires_grad:
param.data = 0.1 * torch.randn(param.data.shape).to(device)
#####################
# Model initialize: #
#####################
net1 = Model_1()
net2 = Model_2()
net3 = ImpMapBlock()
net1.cuda()
net2.cuda()
net3.cuda()
init_model(net1)
init_model(net2)
init_net3(net3)
net1 = torch.nn.DataParallel(net1, device_ids=c.device_ids)
net2 = torch.nn.DataParallel(net2, device_ids=c.device_ids)
net3 = torch.nn.DataParallel(net3, device_ids=c.device_ids)
para1 = get_parameter_number(net1)
para2 = get_parameter_number(net2)
para3 = get_parameter_number(net3)
print(para1)
print(para2)
print(para3)
params_trainable1 = (list(filter(lambda p: p.requires_grad, net1.parameters())))
params_trainable2 = (list(filter(lambda p: p.requires_grad, net2.parameters())))
params_trainable3 = (list(filter(lambda p: p.requires_grad, net3.parameters())))
optim1 = torch.optim.Adam(params_trainable1, lr=c.lr, betas=c.betas, eps=1e-6, weight_decay=c.weight_decay)
optim2 = torch.optim.Adam(params_trainable2, lr=c.lr, betas=c.betas, eps=1e-6, weight_decay=c.weight_decay)
optim3 = torch.optim.Adam(params_trainable3, lr=c.lr3, betas=c.betas, eps=1e-6, weight_decay=c.weight_decay)
weight_scheduler1 = torch.optim.lr_scheduler.StepLR(optim1, c.weight_step, gamma=c.gamma)
weight_scheduler2 = torch.optim.lr_scheduler.StepLR(optim2, c.weight_step, gamma=c.gamma)
weight_scheduler3 = torch.optim.lr_scheduler.StepLR(optim3, c.weight_step, gamma=c.gamma)
dwt = common.DWT()
iwt = common.IWT()
if c.tain_next:
load(c.MODEL_PATH + c.suffix_load + '_1.pt', net1, optim1)
load(c.MODEL_PATH + c.suffix_load + '_2.pt', net2, optim2)
load(c.MODEL_PATH + c.suffix_load + '_3.pt', net3, optim3)
if c.pretrain:
load(c.PRETRAIN_PATH + c.suffix_pretrain + '_1.pt', net1, optim1)
load(c.PRETRAIN_PATH + c.suffix_pretrain + '_2.pt', net2, optim2)
if c.PRETRAIN_PATH_3 is not None:
load(c.PRETRAIN_PATH_3 + c.suffix_pretrain_3 + '_3.pt', net3, optim3)
try:
writer = SummaryWriter(comment='hinet', filename_suffix="steg")
for i_epoch in range(c.epochs):
i_epoch = i_epoch + c.trained_epoch + 1
loss_history = []
loss_history_g1 = []
loss_history_g2 = []
loss_history_r1 = []
loss_history_r2 = []
loss_history_imp = []
#################
# train: #
#################
for i_batch, data in enumerate(trainloader):
# data preparation
data = data.to(device)
cover = data[:data.shape[0] // 3] # channels = 3
secret_1 = data[data.shape[0] // 3: 2 * (data.shape[0] // 3)]
secret_2 = data[2 * (data.shape[0] // 3): 3 * (data.shape[0] // 3)]
cover_dwt = dwt(cover) # channels = 12
cover_dwt_low = cover_dwt.narrow(1, 0, c.channels_in) # channels = 3
secret_dwt_1 = dwt(secret_1)
secret_dwt_2 = dwt(secret_2)
input_dwt_1 = torch.cat((cover_dwt, secret_dwt_1), 1) # channels = 24
#################
# forward1: #
#################
output_dwt_1 = net1(input_dwt_1) # channels = 24
output_steg_dwt_1 = output_dwt_1.narrow(1, 0, 4 * c.channels_in) # channels = 12
output_steg_dwt_low_1 = output_steg_dwt_1.narrow(1, 0, c.channels_in) # channels = 3
output_z_dwt_1 = output_dwt_1.narrow(1, 4 * c.channels_in, 4 * c.channels_in) # channels = 12
# get steg1
output_steg_1 = iwt(output_steg_dwt_1).to(device) # channels = 3
#################
# forward2: #
#################
if c.use_imp_map:
imp_map = net3(cover, secret_1, output_steg_1) # channels = 3
else:
imp_map = torch.zeros(cover.shape).cuda()
impmap_loss = imp_loss(imp_map, cover - output_steg_1)
imp_map_dwt = dwt(imp_map) # channels = 12
input_dwt_2 = torch.cat((output_steg_dwt_1, imp_map_dwt), 1) # 24, without secret2
input_dwt_2 = torch.cat((input_dwt_2, secret_dwt_2), 1) # 36
output_dwt_2 = net2(input_dwt_2) # channels = 36
output_steg_dwt_2 = output_dwt_2.narrow(1, 0, 4 * c.channels_in) # channels = 12
output_steg_dwt_low_2 = output_steg_dwt_2.narrow(1, 0, c.channels_in) # channels = 3
output_z_dwt_2 = output_dwt_2.narrow(1, 4 * c.channels_in, output_dwt_2.shape[1] - 4 * c.channels_in) # channels = 24
# get steg2
output_steg_2 = iwt(output_steg_dwt_2).to(device) # channels = 3
#################
# backward2: #
#################
output_z_guass_1 = gauss_noise(output_z_dwt_1.shape) # channels = 12
output_z_guass_2 = gauss_noise(output_z_dwt_2.shape) # channels = 24
output_rev_dwt_2 = torch.cat((output_steg_dwt_2, output_z_guass_2), 1) # channels = 36
rev_dwt_2 = net2(output_rev_dwt_2, rev=True) # channels = 36
rev_steg_dwt_1 = rev_dwt_2.narrow(1, 0, 4 * c.channels_in) # channels = 12
rev_secret_dwt_2 = rev_dwt_2.narrow(1, 4 * c.channels_in, 4 * c.channels_in) # channels = 12
rev_steg_1 = iwt(rev_steg_dwt_1).to(device) # channels = 3
rev_secret_2 = iwt(rev_secret_dwt_2).to(device) # channels = 3
#################
# backward1: #
#################
output_rev_dwt_1 = torch.cat((rev_steg_dwt_1, output_z_guass_1), 1) # channels = 24
rev_dwt_1 = net1(output_rev_dwt_1, rev=True) # channels = 36
rev_secret_dwt = rev_dwt_1.narrow(1, 4 * c.channels_in, 4 * c.channels_in) # channels = 12
rev_secret_1 = iwt(rev_secret_dwt).to(device)
#################
# loss: #
#################
g_loss_1 = guide_loss(output_steg_1.cuda(), cover.cuda())
g_loss_2 = guide_loss(output_steg_2.cuda(), cover.cuda())
l_loss_1 = guide_loss(output_steg_dwt_low_1.cuda(), cover_dwt_low.cuda())
l_loss_2 = guide_loss(output_steg_dwt_low_2.cuda(), cover_dwt_low.cuda())
r_loss_1 = reconstruction_loss(rev_secret_1, secret_1)
r_loss_2 = reconstruction_loss(rev_secret_2, secret_2)
total_loss = c.lamda_reconstruction_1 * r_loss_1 + c.lamda_reconstruction_2 * r_loss_2 + c.lamda_guide_1 * g_loss_1\
+ c.lamda_guide_2 * g_loss_2 + c.lamda_low_frequency_1 * l_loss_1 + c.lamda_low_frequency_2 * l_loss_2
total_loss.backward()
if c.optim_step_1:
optim1.step()
if c.optim_step_2:
optim2.step()
if c.optim_step_3:
optim3.step()
optim1.zero_grad()
optim2.zero_grad()
optim3.zero_grad()
loss_history.append([total_loss.item(), 0.])
loss_history_g1.append(g_loss_1.item())
loss_history_g2.append(g_loss_2.item())
loss_history_r1.append(r_loss_1.item())
loss_history_r2.append(r_loss_2.item())
loss_history_imp.append(impmap_loss.item())
#################
# val: #
#################
if i_epoch % c.val_freq == 1:
with torch.no_grad():
psnr_s1 = []
psnr_s2 = []
psnr_c1 = []
psnr_c2 = []
net1.eval()
net2.eval()
net3.eval()
for x in testloader:
x = x.to(device)
cover = x[:x.shape[0] // 3] # channels = 3
secret_1 = x[x.shape[0] // 3: 2 * x.shape[0] // 3]
secret_2 = x[2 * x.shape[0] // 3: 3 * x.shape[0] // 3]
cover_dwt = dwt(cover) # channels = 12
secret_dwt_1 = dwt(secret_1)
secret_dwt_2 = dwt(secret_2)
input_dwt_1 = torch.cat((cover_dwt, secret_dwt_1), 1) # channels = 24
#################
# forward1: #
#################
output_dwt_1 = net1(input_dwt_1) # channels = 24
output_steg_dwt_1 = output_dwt_1.narrow(1, 0, 4 * c.channels_in) # channels = 12
output_z_dwt_1 = output_dwt_1.narrow(1, 4 * c.channels_in, 4 * c.channels_in) # channels = 12
# get steg1
output_steg_1 = iwt(output_steg_dwt_1).to(device) # channels = 3
#################
# forward2: #
#################
if c.use_imp_map:
imp_map = net3(cover, secret_1, output_steg_1) # channels = 3
else:
imp_map = torch.zeros(cover.shape).cuda()
imp_map_dwt = dwt(imp_map) # channels = 12
input_dwt_2 = torch.cat((output_steg_dwt_1, imp_map_dwt), 1) # 24, without secret2
input_dwt_2 = torch.cat((input_dwt_2, secret_dwt_2), 1) # 36
output_dwt_2 = net2(input_dwt_2) # channels = 36
output_steg_dwt_2 = output_dwt_2.narrow(1, 0, 4 * c.channels_in) # channels = 12
output_z_dwt_2 = output_dwt_2.narrow(1, 4 * c.channels_in, output_dwt_2.shape[1] - 4 * c.channels_in) # channels = 24
# get steg2
output_steg_2 = iwt(output_steg_dwt_2).to(device) # channels = 3
#################
# backward2: #
#################
output_z_guass_1 = gauss_noise(output_z_dwt_1.shape) # channels = 12
output_z_guass_2 = gauss_noise(output_z_dwt_2.shape) # channels = 24
output_rev_dwt_2 = torch.cat((output_steg_dwt_2, output_z_guass_2), 1) # channels = 36
rev_dwt_2 = net2(output_rev_dwt_2, rev=True) # channels = 36
rev_steg_dwt_1 = rev_dwt_2.narrow(1, 0, 4 * c.channels_in) # channels = 12
rev_secret_dwt_2 = rev_dwt_2.narrow(1, output_dwt_2.shape[1] - 4 * c.channels_in, 4 * c.channels_in) # channels = 12
rev_steg_1 = iwt(rev_steg_dwt_1).to(device) # channels = 3
rev_secret_2 = iwt(rev_secret_dwt_2).to(device) # channels = 3
#################
# backward1: #
#################
output_rev_dwt_1 = torch.cat((rev_steg_dwt_1, output_z_guass_1), 1) # channels = 24
rev_dwt_1 = net1(output_rev_dwt_1, rev=True) # channels = 24
rev_secret_dwt = rev_dwt_1.narrow(1, rev_dwt_1.shape[1] - 4 * c.channels_in, 4 * c.channels_in) # channels = 12
rev_secret_1 = iwt(rev_secret_dwt).to(device)
secret_rev1_255 = rev_secret_1.cpu().numpy().squeeze() * 255
secret_rev2_255 = rev_secret_2.cpu().numpy().squeeze() * 255
secret_1_255 = secret_1.cpu().numpy().squeeze() * 255
secret_2_255 = secret_2.cpu().numpy().squeeze() * 255
cover_255 = cover.cpu().numpy().squeeze() * 255
steg_1_255 = output_steg_1.cpu().numpy().squeeze() * 255
steg_2_255 = output_steg_2.cpu().numpy().squeeze() * 255
psnr_temp1 = computePSNR(secret_rev1_255, secret_1_255)
psnr_s1.append(psnr_temp1)
psnr_temp2 = computePSNR(secret_rev2_255, secret_2_255)
psnr_s2.append(psnr_temp2)
psnr_temp_c1 = computePSNR(cover_255, steg_1_255)
psnr_c1.append(psnr_temp_c1)
psnr_temp_c2 = computePSNR(cover_255, steg_2_255)
psnr_c2.append(psnr_temp_c2)
writer.add_scalars("PSNR", {"S1 average psnr": np.mean(psnr_s1)}, i_epoch)
writer.add_scalars("PSNR", {"C1 average psnr": np.mean(psnr_c1)}, i_epoch)
writer.add_scalars("PSNR", {"S2 average psnr": np.mean(psnr_s2)}, i_epoch)
writer.add_scalars("PSNR", {"C2 average psnr": np.mean(psnr_c2)}, i_epoch)
epoch_losses = np.mean(np.array(loss_history), axis=0)
epoch_losses[1] = np.log10(optim1.param_groups[0]['lr'])
epoch_losses_g1 = np.mean(np.array(loss_history_g1))
epoch_losses_g2 = np.mean(np.array(loss_history_g2))
epoch_losses_r1 = np.mean(np.array(loss_history_r1))
epoch_losses_r2 = np.mean(np.array(loss_history_r2))
epoch_losses_imp = np.mean(np.array(loss_history_imp))
viz.show_loss(epoch_losses)
writer.add_scalars("Train", {"Train_Loss": epoch_losses[0]}, i_epoch)
writer.add_scalars("Train", {"g1_Loss": epoch_losses_g1}, i_epoch)
writer.add_scalars("Train", {"g2_Loss": epoch_losses_g2}, i_epoch)
writer.add_scalars("Train", {"r1_Loss": epoch_losses_r1}, i_epoch)
writer.add_scalars("Train", {"r2_Loss": epoch_losses_r2}, i_epoch)
writer.add_scalars("Train", {"imp_Loss": epoch_losses_imp}, i_epoch)
if i_epoch > 0 and (i_epoch % c.SAVE_freq) == 0:
torch.save({'opt': optim1.state_dict(),
'net': net1.state_dict()}, c.MODEL_PATH + 'model_checkpoint_%.5i_1' % i_epoch + '.pt')
torch.save({'opt': optim2.state_dict(),
'net': net2.state_dict()}, c.MODEL_PATH + 'model_checkpoint_%.5i_2' % i_epoch + '.pt')
torch.save({'opt': optim3.state_dict(),
'net': net3.state_dict()}, c.MODEL_PATH + 'model_checkpoint_%.5i_3' % i_epoch + '.pt')
weight_scheduler1.step()
weight_scheduler2.step()
weight_scheduler3.step()
torch.save({'opt': optim1.state_dict(),
'net': net1.state_dict()}, c.MODEL_PATH + 'model_1' + '.pt')
torch.save({'opt': optim2.state_dict(),
'net': net2.state_dict()}, c.MODEL_PATH + 'model_2' + '.pt')
torch.save({'opt': optim3.state_dict(),
'net': net3.state_dict()}, c.MODEL_PATH + 'model_3' + '.pt')
writer.close()
except:
if c.checkpoint_on_error:
torch.save({'opt': optim1.state_dict(),
'net': net1.state_dict()}, c.MODEL_PATH + 'model_ABORT_1' + '.pt')
torch.save({'opt': optim2.state_dict(),
'net': net2.state_dict()}, c.MODEL_PATH + 'model_ABORT_2' + '.pt')
torch.save({'opt': optim3.state_dict(),
'net': net3.state_dict()}, c.MODEL_PATH + 'model_ABORT_3' + '.pt')
raise
finally:
viz.signal_stop()