-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
154 lines (119 loc) · 5.4 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
# ------------------------------------------------------------------
# Copyright (c) 2021, Zi-Rong Jin, Tian-Jing Zhang, Cheng Jin, and
# Liang-Jian Deng, All rights reserved.
#
# This work is licensed under GNU Affero General Public License
# v3.0 International To view a copy of this license, see the
# LICENSE file.
#
# This file is running on WorldView-3 dataset. For other dataset
# (i.e., QuickBird and GaoFen-2), please change the corresponding
# inputs.
# ------------------------------------------------------------------
import os
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
from data import Dataset_Pro
import scipy.io as sio
from model import WSDFNet
import numpy as np
import shutil
from torch.utils.tensorboard import SummaryWriter
# ================== Pre-test =================== #
def load_set(file_path):
data = sio.loadmat(file_path) # HxWxC=256x256x8
# tensor type:
lms = torch.from_numpy(
data['lms'] / 2047.0).permute(2, 0, 1) # CxHxW = 8x256x256
ms = torch.from_numpy((data['ms'] / 2047.0)
).permute(2, 0, 1) # CxHxW= 8x64x64
pan = torch.from_numpy((data['pan'] / 2047.0)) # HxW = 256x256
return lms, ms, pan
# ================== Pre-Define =================== #
SEED = 10
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)
cudnn.deterministic = True
# ============= HYPER PARAMS(Pre-Defined) ==========#
lr = 0.0003
epochs = 500
ckpt = 50
batch_size = 32
device = torch.device('cuda:0')
model = WSDFNet().to(device)
criterion = nn.MSELoss().to(device)
optimizer = optim.Adam(model.parameters(), lr=lr,
betas=(0.9, 0.999)) # optimizer 1
if os.path.exists('train_logs'): # for tensorboard: copy dir of train_logs
# ---> console (see tensorboard): tensorboard --logdir = dir of train_logs
shutil.rmtree('train_logs')
writer = SummaryWriter('train_logs')
def save_checkpoint(model, epoch): # save model function
model_out_path = 'Weights/WSDFNET_{}.pth'.format(epoch)
# if not os.path.exists(model_out_path):
# os.makedirs(model_out_path)
torch.save(model.state_dict(), model_out_path)
###################################################################
# ------------------- Main Train (Run second)----------------------
###################################################################
def train(training_data_loader, validate_data_loader):
print('Start training...')
for epoch in range(epochs):
epoch += 1
epoch_train_loss, epoch_val_loss = [], []
# ============Epoch Train=============== #
model.train()
for iteration, batch in enumerate(training_data_loader, 1):
gt, lms, _, _, pan = Variable(batch[0], requires_grad=False).to(device), \
Variable(batch[1]).to(device), \
batch[2], \
batch[3], \
Variable(batch[4]).to(device)
optimizer.zero_grad() # fixed
out = model(pan, lms)
loss = criterion(out, gt) # compute loss
# save all losses into a vector for one epoch
epoch_train_loss.append(loss.item())
loss.backward() # fixed
optimizer.step() # fixed
# lr_scheduler.step() # update lr
# compute the mean value of all losses, as one epoch loss
t_loss = np.nanmean(np.array(epoch_train_loss))
# write to tensorboard to check
writer.add_scalar('train/loss', t_loss, epoch)
# print loss for each epoch
print('Epoch: {}/{} training loss: {:.7f}'.format(epochs, epoch, t_loss))
if epoch % ckpt == 0: # if each ckpt epochs, then start to save model
save_checkpoint(model, epoch)
model.eval()
with torch.no_grad():
for iteration, batch in enumerate(validate_data_loader, 1):
gt, lms, _, _, pan = Variable(batch[0], requires_grad=False).to(device), \
Variable(batch[1]).to(device), \
batch[2], \
batch[3], \
Variable(batch[4]).to(device)
out = model(pan, lms)
loss = criterion(out, gt)
epoch_val_loss.append(loss.item())
v_loss = np.nanmean(np.array(epoch_val_loss))
writer.add_scalar('val/loss', v_loss, epoch)
print('validate loss: {:.7f}'.format(v_loss))
writer.close() # close tensorboard
###################################################################
# ------------------- Main Function (Run first) -------------------
###################################################################
if __name__ == "__main__":
train_set = Dataset_Pro('/path/to/train/set') # creat data for training
training_data_loader = DataLoader(dataset=train_set, num_workers=0, batch_size=batch_size, shuffle=True,
pin_memory=True, drop_last=True) # put training data to DataLoader for batches
# creat data for validation
validate_set = Dataset_Pro('/path/to/validate/set')
validate_data_loader = DataLoader(dataset=validate_set, num_workers=0, batch_size=batch_size, shuffle=True,
pin_memory=True, drop_last=True) # put training data to DataLoader for batches
train(training_data_loader, validate_data_loader)