-
Notifications
You must be signed in to change notification settings - Fork 23
/
train_autoencoder.py
197 lines (173 loc) · 6.89 KB
/
train_autoencoder.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
# -*- coding:utf-8 -*-
#@Project: NestFuse for image fusion
#@Author: Li Hui, Jiangnan University
#@Email: [email protected]
#@File : train_autoencoder.py
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import sys
import time
import numpy as np
from tqdm import tqdm, trange
import scipy.io as scio
import random
import torch
from torch.optim import Adam
from torch.autograd import Variable
import utils
from net import NestFuse_autoencoder
from args_fusion import args
import pytorch_msssim
def main():
original_imgs_path = utils.list_images(args.dataset)
train_num = 80000
original_imgs_path = original_imgs_path[:train_num]
random.shuffle(original_imgs_path)
for i in range(2,3):
# i = 3
train(i, original_imgs_path)
def train(i, original_imgs_path):
batch_size = args.batch_size
# load network model
# nest_model = FusionNet_gra()
input_nc = 1
output_nc = 1
# true for deeply supervision
# In our paper, deeply supervision strategy was not used.
deepsupervision = False
nb_filter = [64, 112, 160, 208, 256]
nest_model = NestFuse_autoencoder(nb_filter, input_nc, output_nc, deepsupervision)
if args.resume is not None:
print('Resuming, initializing using weight from {}.'.format(args.resume))
nest_model.load_state_dict(torch.load(args.resume))
print(nest_model)
optimizer = Adam(nest_model.parameters(), args.lr)
mse_loss = torch.nn.MSELoss()
ssim_loss = pytorch_msssim.msssim
if args.cuda:
nest_model.cuda()
tbar = trange(args.epochs)
print('Start training.....')
Loss_pixel = []
Loss_ssim = []
Loss_all = []
count_loss = 0
all_ssim_loss = 0.
all_pixel_loss = 0.
for e in tbar:
print('Epoch %d.....' % e)
# load training database
image_set_ir, batches = utils.load_dataset(original_imgs_path, batch_size)
nest_model.train()
count = 0
for batch in range(batches):
image_paths = image_set_ir[batch * batch_size:(batch * batch_size + batch_size)]
img = utils.get_train_images_auto(image_paths, height=args.HEIGHT, width=args.WIDTH, flag=False)
count += 1
optimizer.zero_grad()
img = Variable(img, requires_grad=False)
if args.cuda:
img = img.cuda()
# get fusion image
# encoder
en = nest_model.encoder(img)
# decoder
outputs = nest_model.decoder_train(en)
# resolution loss: between fusion image and visible image
x = Variable(img.data.clone(), requires_grad=False)
ssim_loss_value = 0.
pixel_loss_value = 0.
for output in outputs:
pixel_loss_temp = mse_loss(output, x)
ssim_loss_temp = ssim_loss(output, x, normalize=True)
ssim_loss_value += (1-ssim_loss_temp)
pixel_loss_value += pixel_loss_temp
ssim_loss_value /= len(outputs)
pixel_loss_value /= len(outputs)
# total loss
total_loss = pixel_loss_value + args.ssim_weight[i] * ssim_loss_value
total_loss.backward()
optimizer.step()
all_ssim_loss += ssim_loss_value.item()
all_pixel_loss += pixel_loss_value.item()
if (batch + 1) % args.log_interval == 0:
mesg = "{}\t SSIM weight {}\tEpoch {}:\t[{}/{}]\t pixel loss: {:.6f}\t ssim loss: {:.6f}\t total: {:.6f}".format(
time.ctime(), i, e + 1, count, batches,
all_pixel_loss / args.log_interval,
(args.ssim_weight[i] * all_ssim_loss) / args.log_interval,
(args.ssim_weight[i] * all_ssim_loss + all_pixel_loss) / args.log_interval
)
tbar.set_description(mesg)
Loss_pixel.append(all_pixel_loss / args.log_interval)
Loss_ssim.append(all_ssim_loss / args.log_interval)
Loss_all.append((args.ssim_weight[i] * all_ssim_loss + all_pixel_loss) / args.log_interval)
count_loss = count_loss + 1
all_ssim_loss = 0.
all_pixel_loss = 0.
if (batch + 1) % (200 * args.log_interval) == 0:
# save model
nest_model.eval()
nest_model.cpu()
save_model_filename = args.ssim_path[i] + '/' + "Epoch_" + str(e) + "_iters_" + str(count) + "_" + \
str(time.ctime()).replace(' ', '_').replace(':', '_') + "_" + args.ssim_path[
i] + ".model"
save_model_path = os.path.join(args.save_model_dir_autoencoder, save_model_filename)
torch.save(nest_model.state_dict(), save_model_path)
# save loss data
# pixel loss
loss_data_pixel = Loss_pixel
loss_filename_path = args.save_loss_dir + args.ssim_path[i] + '/' + "loss_pixel_epoch_" + str(
args.epochs) + "_iters_" + str(count) + "_" + str(time.ctime()).replace(' ', '_').replace(':', '_') + "_" + \
args.ssim_path[i] + ".mat"
scio.savemat(loss_filename_path, {'loss_pixel': loss_data_pixel})
# SSIM loss
loss_data_ssim = Loss_ssim
loss_filename_path = args.save_loss_dir + args.ssim_path[i] + '/' + "loss_ssim_epoch_" + str(
args.epochs) + "_iters_" + str(count) + "_" + str(time.ctime()).replace(' ', '_').replace(':', '_') + "_" + \
args.ssim_path[i] + ".mat"
scio.savemat(loss_filename_path, {'loss_ssim': loss_data_ssim})
# all loss
loss_data = Loss_all
loss_filename_path = args.save_loss_dir + args.ssim_path[i] + '/' + "loss_all_epoch_" + str(e) + "_iters_" + \
str(count) + "-" + str(time.ctime()).replace(' ', '_').replace(':', '_') + "_" + \
args.ssim_path[i] + ".mat"
scio.savemat(loss_filename_path, {'loss_all': loss_data})
nest_model.train()
nest_model.cuda()
tbar.set_description("\nCheckpoint, trained model saved at", save_model_path)
# pixel loss
loss_data_pixel = Loss_pixel
loss_filename_path = args.save_loss_dir + args.ssim_path[i] + '/' + "Final_loss_pixel_epoch_" + str(
args.epochs) + "_" + str(
time.ctime()).replace(' ', '_').replace(':', '_') + "_" + args.ssim_path[i] + ".mat"
scio.savemat(loss_filename_path, {'final_loss_pixel': loss_data_pixel})
loss_data_ssim = Loss_ssim
loss_filename_path = args.save_loss_dir + args.ssim_path[i] + '/' + "Final_loss_ssim_epoch_" + str(
args.epochs) + "_" + str(
time.ctime()).replace(' ', '_').replace(':', '_') + "_" + args.ssim_path[i] + ".mat"
scio.savemat(loss_filename_path, {'final_loss_ssim': loss_data_ssim})
# SSIM loss
loss_data = Loss_all
loss_filename_path = args.save_loss_dir + args.ssim_path[i] + '/' + "Final_loss_all_epoch_" + str(
args.epochs) + "_" + str(
time.ctime()).replace(' ', '_').replace(':', '_') + "_" + args.ssim_path[i] + ".mat"
scio.savemat(loss_filename_path, {'final_loss_all': loss_data})
# save model
nest_model.eval()
nest_model.cpu()
save_model_filename = args.ssim_path[i] + '/' "Final_epoch_" + str(args.epochs) + "_" + \
str(time.ctime()).replace(' ', '_').replace(':', '_') + "_" + args.ssim_path[i] + ".model"
save_model_path = os.path.join(args.save_model_dir_autoencoder, save_model_filename)
torch.save(nest_model.state_dict(), save_model_path)
print("\nDone, trained model saved at", save_model_path)
def check_paths(args):
try:
if not os.path.exists(args.vgg_model_dir):
os.makedirs(args.vgg_model_dir)
if not os.path.exists(args.save_model_dir):
os.makedirs(args.save_model_dir)
except OSError as e:
print(e)
sys.exit(1)
if __name__ == "__main__":
main()