-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_GGS.py
290 lines (257 loc) · 15.4 KB
/
train_GGS.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
import torch
from torch import nn
from torch.utils.data import DataLoader
from models.model import create_model_ggs
from data.datasets.Replica_Semantic import Replica_Semantic
from data.datasets.ScanNetpp_Semantic import ScanNetpp_Semantic
from data.datasets.ScanNet_Semantic import ScanNet_Semantic
import argparse
import os
import argparse
from configs.configs import update_configs, get_configs
from core.functions import train_gen_semantic, test_gen_semantic, train_gen_depth, test_gen_depth
from core.criterion import LossCR
from utils.utils import create_lr_scheduler
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, required=True,
help='config file path')
args = parser.parse_args()
# set device
device = 'cuda:1' if torch.cuda.is_available() else 'cpu'
# create the config
cfg = get_configs()
update_configs(cfg, args.config)
# Directory for saving model weights
output_dir = os.path.join(cfg.TRAIN.OUTPUT_DIR, 'experiments', cfg.NAME)
weights_dir = os.path.join(output_dir, 'model_weights')
os.makedirs(weights_dir, exist_ok=True)
scene_list = [cfg.TRAIN.SCENE_TO_FINETUNE] if cfg.TRAIN.FINETUNE else cfg.DATASET.SCENE_LIST_TRAIN
if cfg.DATASET.NAME == 'replica':
train_dataset = Replica_Semantic(scene_list=scene_list,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='train',
task=cfg.TASK,
finetune=cfg.TRAIN.FINETUNE,
compute_semantic_weights=cfg.TRAIN.COMPUTE_SEMANTIC_WEIGHTS)
test_dataset_nove_view = Replica_Semantic(scene_list=scene_list,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='test',
task=cfg.TASK,
finetune=cfg.TRAIN.FINETUNE)
elif cfg.DATASET.NAME == 'scannetpp':
train_dataset = ScanNetpp_Semantic(root_dir=cfg.DATASET.ROOT_DIR,
scene_names=scene_list,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='train',
task=cfg.TASK,
finetune=cfg.TRAIN.FINETUNE,
compute_semantic_weights=cfg.TRAIN.COMPUTE_SEMANTIC_WEIGHTS)
test_dataset_nove_view = ScanNetpp_Semantic(root_dir=cfg.DATASET.ROOT_DIR,
scene_names=scene_list,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='test',
task=cfg.TASK,
finetune=cfg.TRAIN.FINETUNE)
elif cfg.DATASET.NAME == 'scannet':
train_dataset = ScanNet_Semantic(root_dir=cfg.DATASET.ROOT_DIR,
scene_names=scene_list,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='train',
task=cfg.TASK,
finetune=cfg.TRAIN.FINETUNE,
compute_semantic_weights=cfg.TRAIN.COMPUTE_SEMANTIC_WEIGHTS)
test_dataset_nove_view = ScanNet_Semantic(root_dir=cfg.DATASET.ROOT_DIR,
scene_names=scene_list,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='test',
task=cfg.TASK,
finetune=cfg.TRAIN.FINETUNE)
train_loader = DataLoader(train_dataset, batch_size=cfg.TRAIN.BATCH_SIZE, shuffle=True)
test_loader_nove_view = DataLoader(test_dataset_nove_view, batch_size=cfg.TRAIN.BATCH_SIZE, shuffle=False)
if not cfg.TRAIN.FINETUNE:
if cfg.DATASET.NAME == 'replica':
test_dataset = Replica_Semantic(scene_list=cfg.DATASET.SCENE_LIST_TEST,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='test',
task=cfg.TASK)
elif cfg.DATASET.NAME == 'scannetpp':
test_dataset = ScanNetpp_Semantic(root_dir=cfg.DATASET.ROOT_DIR,
scene_names=cfg.DATASET.SCENE_LIST_TEST,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='test',
split='test',
task=cfg.TASK)
elif cfg.DATASET.NAME == 'scannet':
test_dataset = ScanNet_Semantic(root_dir=cfg.DATASET.ROOT_DIR,
scene_names=cfg.DATASET.SCENE_LIST_TEST,
id2name=cfg.DATASET.ID2NAME,
color_mapping=cfg.DATASET.COLOR_MAPPING,
class_weights=cfg.DATASET.CLASS_WEIGHTS,
stage='test',
split='test',
task=cfg.TASK)
test_loader = DataLoader(test_dataset, batch_size=cfg.TRAIN.BATCH_SIZE, shuffle=False)
if cfg.TASK == 'semantic':
num_classes = train_dataset.no_classses
elif cfg.TASK == 'depth':
num_classes = 1
load_model_path = cfg.TRAIN.MODEL_PATH if cfg.TRAIN.FINETUNE or cfg.TRAIN.LOAD_PRETRAIN else None
model = create_model_ggs(num_classes=num_classes, load_model=load_model_path).to(device)
optimizer = torch.optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=cfg.TRAIN.LR, weight_decay=1e-4)
if load_model_path:
model_state_dict = torch.load(load_model_path, map_location='cpu')
optimizer.load_state_dict(model_state_dict['optimizer_state_dict'])
if cfg.TRAIN.FINETUNE:
warmup = False
label_smoothing = 0.
else:
warmup = True
label_smoothing = 0.15
lr_scheduler = create_lr_scheduler(optimizer, len(train_loader), cfg.TRAIN.EPOCHS, warmup=warmup)
if cfg.TASK == 'semantic':
if cfg.TRAIN.FINETUNE:
criterion = LossCR(num_classes=num_classes, feat_dim=256, alpha=1, label_smoothing=label_smoothing)
else:
# criterion = LossCR(num_classes=num_classes, feat_dim=256, alpha=1, label_smoothing=label_smoothing)
criterion = nn.CrossEntropyLoss(ignore_index=cfg.DATA.IGNORE_INDEX, label_smoothing=label_smoothing)
elif cfg.TASK == 'depth':
criterion = nn.MSELoss()
writer = SummaryWriter(log_dir=os.path.join('runs', cfg.NAME))
best_miou = 0.
miou_test = 0.
best_rmse = 1000.
for epoch in tqdm(range(1, cfg.TRAIN.EPOCHS), desc="Epochs"):
if cfg.TASK == 'semantic':
loss_train = train_gen_semantic(model=model,
optimizer=optimizer,
lr_scheduler=lr_scheduler,
loader=train_loader,
criterion=criterion,
gard_clip_norm=cfg.TRAIN.GRAD_CLIP_NORM,
finetune=cfg.TRAIN.FINETUNE,
device=device
)
writer.add_scalar('Loss/avg_train_loss', loss_train, epoch)
loss_test_novel_view, miou_valid_novel_view, acc_novel_view= test_gen_semantic(model=model,
loader=test_loader_nove_view,
criterion=criterion,
num_classes=num_classes,
finetune=cfg.TRAIN.FINETUNE,
device=device)
writer.add_scalar('Loss/avg_test_loss_novel_view', loss_test_novel_view, epoch)
writer.add_scalar('mIoU_valid/test_novel_view', miou_valid_novel_view, epoch)
writer.add_scalar('Accuracy/test_novel_view', acc_novel_view, epoch)
miou_test = miou_valid_novel_view
if not cfg.TRAIN.FINETUNE:
loss_test, miou_valid_test, acc_novel_test= test_gen_semantic(model=model,
loader=test_loader,
criterion=criterion,
num_classes=num_classes,
finetune=cfg.TRAIN.FINETUNE,
device=device)
writer.add_scalar('Loss/avg_test_loss', loss_test, epoch)
writer.add_scalar('mIoU_valid/test', miou_valid_test, epoch)
writer.add_scalar('Accuracy/test', acc_novel_test, epoch)
miou_test = miou_valid_test
elif cfg.TASK == 'depth':
mse_train, rmse_train = train_gen_depth(model=model,
optimizer=optimizer,
lr_scheduler=lr_scheduler,
loader=train_loader,
criterion=criterion,
gard_clip_norm=cfg.TRAIN.GRAD_CLIP_NORM,
device=device
)
writer.add_scalar('RMSE/train', rmse_train, epoch)
writer.add_scalar('MSE/train', mse_train, epoch)
mse_train_novel_view, rmse_train_novel_view = test_gen_depth(model=model,
loader=test_loader_nove_view,
criterion=criterion,
device=device)
writer.add_scalar('RMSE/test_novel_view', rmse_train_novel_view, epoch)
writer.add_scalar('MSE/test_novel_view', mse_train_novel_view, epoch)
if not cfg.TRAIN.FINETUNE:
mse_test, rmse_test = test_gen_depth(model=model,
loader=test_loader,
criterion=criterion,
device=device)
writer.add_scalar('RMSE/test', rmse_test, epoch)
writer.add_scalar('MSE/test', mse_test, epoch)
lr = lr_scheduler.get_last_lr()[0]
writer.add_scalar('Learning Rate', lr, epoch)
# Check if this is the best model based on test mIoU
if cfg.TASK == 'semantic':
if miou_test > best_miou:
best_miou = miou_test
# Save the model and optimizer parameters
checkpoint = {
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}
model_save_path = os.path.join(weights_dir, f'best_model_epoch_{epoch}_miou_{best_miou:.4f}.pth')
torch.save(checkpoint, model_save_path)
print(f"Saved new best model with mIoU: {best_miou:.4f} at epoch {epoch} in {model_save_path}")
elif epoch == cfg.TRAIN.EPOCHS - 1:
# Save the model and optimizer parameters
checkpoint = {
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}
model_save_path = os.path.join(weights_dir, f'last_model_epoch_{epoch}_miou_{miou_test:.4f}.pth')
torch.save(checkpoint, model_save_path)
# Always save the last model of the current epoch
checkpoint = {
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}
model_save_path = os.path.join(weights_dir, f'model_epoch_{epoch}.pth')
torch.save(checkpoint, model_save_path)
elif cfg.TASK == 'depth':
if rmse_test < best_rmse:
best_rmse = rmse_test
# Save the model and optimizer parameters
checkpoint = {
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}
model_save_path = os.path.join(weights_dir, f'best_model_epoch_{epoch}_rmse_{best_rmse:.4f}.pth')
torch.save(checkpoint, model_save_path)
print(f"Saved new best model with RMSE: {best_rmse:.4f} at epoch {epoch} in {model_save_path}")
elif epoch == cfg.TRAIN.EPOCHS - 1:
# Save the model and optimizer parameters
checkpoint = {
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}
model_save_path = os.path.join(weights_dir, f'last_model_epoch_{epoch}_rmse_{rmse_test:.4f}.pth')
torch.save(checkpoint, model_save_path)
# Always save the last model of the current epoch
checkpoint = {
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}
model_save_path = os.path.join(weights_dir, f'model_epoch_{epoch}.pth')
torch.save(checkpoint, model_save_path)
writer.close()
if __name__=='__main__':
main()