forked from AndreaCodegoni/Tiny_model_4_CD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
267 lines (221 loc) · 7.85 KB
/
training.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
import argparse
import os
import shutil
import dataset.dataset as dtset
import torch
import numpy as np
import random
from metrics.metric_tool import ConfuseMatrixMeter
from models.change_classifier import ChangeClassifier as Model
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
def parse_arguments():
# Argument Parser creation
parser = argparse.ArgumentParser(
description="Parameter for data analysis, data cleaning and model training."
)
parser.add_argument(
"--datapath",
type=str,
help="data path",
)
parser.add_argument(
"--log-path",
type=str,
help="log path",
)
group_gpus = parser.add_mutually_exclusive_group()
group_gpus.add_argument(
'--gpu-id',
type=int,
default=0,
help='id of gpu to use '
'(only applicable to non-distributed training)')
parsed_arguments = parser.parse_args()
# create log dir if it doesn't exists
if not os.path.exists(parsed_arguments.log_path):
os.mkdir(parsed_arguments.log_path)
dir_run = sorted(
[
filename
for filename in os.listdir(parsed_arguments.log_path)
if filename.startswith("run_")
]
)
if len(dir_run) > 0:
num_run = int(dir_run[-1].split("_")[-1]) + 1
else:
num_run = 0
parsed_arguments.log_path = os.path.join(
parsed_arguments.log_path, "run_%04d" % num_run + "/"
)
return parsed_arguments
def train(
dataset_train,
dataset_val,
model,
criterion,
optimizer,
scheduler,
logpath,
writer,
epochs,
save_after,
device
):
model = model.to(device)
tool4metric = ConfuseMatrixMeter(n_class=2)
def evaluate(reference, testimg, mask):
# All the tensors on the device:
reference = reference.to(device).float()
testimg = testimg.to(device).float()
mask = mask.to(device).float()
# Evaluating the model:
generated_mask = model(reference, testimg).squeeze(1)
# Loss gradient descend step:
it_loss = criterion(generated_mask, mask)
# Feeding the comparison metric tool:
bin_genmask = (generated_mask.to("cpu") >
0.5).detach().numpy().astype(int)
mask = mask.to("cpu").numpy().astype(int)
tool4metric.update_cm(pr=bin_genmask, gt=mask)
return it_loss
def training_phase(epc):
tool4metric.clear()
print("Epoch {}".format(epc))
model.train()
epoch_loss = 0.0
for (reference, testimg), mask in dataset_train:
# Reset the gradients:
optimizer.zero_grad()
# Loss gradient descend step:
it_loss = evaluate(reference, testimg, mask)
it_loss.backward()
optimizer.step()
# Track metrics:
epoch_loss += it_loss.to("cpu").detach().numpy()
### end of iteration for epoch ###
epoch_loss /= len(dataset_train)
#########
print("Training phase summary")
print("Loss for epoch {} is {}".format(epc, epoch_loss))
writer.add_scalar("Loss/epoch", epoch_loss, epc)
scores_dictionary = tool4metric.get_scores()
writer.add_scalar("IoU class change/epoch",
scores_dictionary["iou_1"], epc)
writer.add_scalar("F1 class change/epoch",
scores_dictionary["F1_1"], epc)
print(
"IoU class change for epoch {} is {}".format(
epc, scores_dictionary["iou_1"]
)
)
print(
"F1 class change for epoch {} is {}".format(
epc, scores_dictionary["F1_1"])
)
print()
writer.flush()
### Save the model ###
if epc % save_after == 0:
torch.save(
model.state_dict(), os.path.join(logpath, "model_{}.pth".format(epc))
)
def validation_phase(epc):
model.eval()
epoch_loss_eval = 0.0
tool4metric.clear()
with torch.no_grad():
for (reference, testimg), mask in dataset_val:
epoch_loss_eval += evaluate(reference,
testimg, mask).to("cpu").numpy()
epoch_loss_eval /= len(dataset_val)
print("Validation phase summary")
print("Loss for epoch {} is {}".format(epc, epoch_loss_eval))
writer.add_scalar("Loss_val/epoch", epoch_loss_eval, epc)
scores_dictionary = tool4metric.get_scores()
writer.add_scalar("IoU_val class change/epoch",
scores_dictionary["iou_1"], epc)
writer.add_scalar("F1_val class change/epoch",
scores_dictionary["F1_1"], epc)
print(
"IoU class change for epoch {} is {}".format(
epc, scores_dictionary["iou_1"]
)
)
print(
"F1 class change for epoch {} is {}".format(
epc, scores_dictionary["F1_1"])
)
print()
for epc in range(epochs):
training_phase(epc)
validation_phase(epc)
# scheduler step
scheduler.step()
def run():
# set the random seed
torch.manual_seed(42)
random.seed(42)
np.random.seed(42)
# Parse arguments:
args = parse_arguments()
# Initialize tensorboard:
writer = SummaryWriter(log_dir=args.log_path)
# Inizialitazion of dataset and dataloader:
trainingdata = dtset.MyDataset(args.datapath, "train")
validationdata = dtset.MyDataset(args.datapath, "val")
data_loader_training = DataLoader(trainingdata, batch_size=8, shuffle=True)
data_loader_val = DataLoader(validationdata, batch_size=8, shuffle=True)
# device setting for training
if torch.cuda.is_available():
device = torch.device(f'cuda:{args.gpu_id}')
else:
device = torch.device('cpu')
print(f'Current Device: {device}\n')
# Initialize the model
model = Model()
restart_from_checkpoint = False
model_path = None
if restart_from_checkpoint:
model.load_state_dict(torch.load(model_path))
print("Checkpoint succesfully loaded")
# print number of parameters
parameters_tot = 0
for nom, param in model.named_parameters():
# print (nom, param.data.shape)
parameters_tot += torch.prod(torch.tensor(param.data.shape))
print("Number of model parameters {}\n".format(parameters_tot))
# define the loss function for the model training.
criterion = torch.nn.BCELoss()
# choose the optimizer in view of the used dataset
# Optimizer with tuned parameters for LEVIR-CD
optimizer = torch.optim.AdamW(model.parameters(), lr=0.00356799066427741,
weight_decay=0.009449677083344786, amsgrad=False)
# Optimizer with tuned parameters for WHU-CD
# optimizer = torch.optim.AdamW(model.parameters(), lr=0.002596776436816101,
# weight_decay=0.008620171028843307, amsgrad=False)
# scheduler for the lr of the optimizer
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, T_max=100)
# copy the configurations
_ = shutil.copytree(
"./models",
os.path.join(args.log_path, "models"),
)
train(
data_loader_training,
data_loader_val,
model,
criterion,
optimizer,
scheduler,
args.log_path,
writer,
epochs=100,
save_after=1,
device=device
)
writer.close()
if __name__ == "__main__":
run()