-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathengine.py
307 lines (229 loc) · 12.3 KB
/
engine.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
# ------------------------------------------------------------------------
# Yuanwen Yue
# ETH Zurich
# ------------------------------------------------------------------------
import copy
import json
import math
import os
import sys
import time
from typing import Iterable
import numpy as np
import random
import MinkowskiEngine as ME
import wandb
import torch
from utils.seg import mean_iou, mean_iou_scene, cal_click_loss_weights, extend_clicks, get_simulated_clicks
import utils.misc as utils
from evaluation.evaluator_MO import EvaluatorMO
def train_one_epoch(model: torch.nn.Module, criterion: torch.nn.Module,
data_loader: Iterable, optimizer: torch.optim.Optimizer,
device: torch.device, epoch: int, train_total_iter: int, max_norm: float = 0):
model.train()
criterion.train()
metric_logger = utils.MetricLogger(delimiter=" ")
header = 'Epoch: [{}]'.format(epoch)
print_freq = 10
accum_iter = 20
for i, batched_inputs in enumerate(metric_logger.log_every(data_loader, print_freq, header)):
coords, raw_coords, feats, labels, _, _, click_idx, scene_name, num_obj = batched_inputs
coords = coords.to(device)
labels = [l.to(device) for l in labels]
labels_new = []
raw_coords = raw_coords.to(device)
feats = feats.to(device)
batch_idx = coords[:,0]
data = ME.SparseTensor(
coordinates=coords,
features=feats,
device=device
)
pcd_features, aux, coordinates, pos_encodings_pcd = model.forward_backbone(data, raw_coordinates=raw_coords)
######### 1. random sample obj number and obj index #########
for idx in range(batch_idx.max()+1):
sample_mask = batch_idx == idx
sample_labels = labels[idx]
sample_raw_coords = raw_coords[sample_mask]
valid_obj_idxs = torch.unique(sample_labels)
valid_obj_idxs = valid_obj_idxs[valid_obj_idxs!=-1]
max_num_obj = len(valid_obj_idxs)
num_obj = np.random.randint(1, min(10, max_num_obj)+1)
obj_idxs = valid_obj_idxs[torch.randperm(max_num_obj)[:num_obj]]
sample_labels_new = torch.zeros(sample_labels.shape[0], device=device)
for i, obj_id in enumerate(obj_idxs):
obj_mask = sample_labels == obj_id
sample_labels_new[obj_mask] = i+1
click_idx[idx][str(i+1)] = []
click_idx[idx]['0'] = []
labels_new.append(sample_labels_new)
click_time_idx = copy.deepcopy(click_idx)
######### 2. pre interactive sampling #########
current_num_iter = 0
num_forward_iters = random.randint(0, 19)
with torch.no_grad():
model.eval()
eval_model = model
while current_num_iter <= num_forward_iters:
if current_num_iter == 0:
pred = [torch.zeros(l.shape).to(device) for l in labels]
else:
outputs = eval_model.forward_mask(pcd_features, aux, coordinates, pos_encodings_pcd,
click_idx=click_idx, click_time_idx=click_time_idx)
pred_logits = outputs['pred_masks']
pred = [p.argmax(-1) for p in pred_logits]
for idx in range(batch_idx.max()+1):
sample_mask = batch_idx == idx
sample_pred = pred[idx]
if current_num_iter != 0:
# update prediction with sparse gt
for obj_id, cids in click_idx[idx].items():
sample_pred[cids] = int(obj_id)
sample_labels = labels_new[idx]
sample_raw_coords = raw_coords[sample_mask]
new_clicks, new_clicks_num, new_click_pos, new_click_time = get_simulated_clicks(sample_pred, sample_labels, sample_raw_coords, current_num_iter, training=True)
### add new clicks ###
if new_clicks is not None:
click_idx[idx], click_time_idx[idx] = extend_clicks(click_idx[idx], click_time_idx[idx], new_clicks, new_click_time)
current_num_iter += 1
######### 3. real forward pass with loss back propagation #########
model.train()
outputs = model.forward_mask(pcd_features, aux, coordinates, pos_encodings_pcd,
click_idx=click_idx, click_time_idx=click_time_idx)
# loss
click_weights = cal_click_loss_weights(coords[:,0], raw_coords, torch.cat(labels_new), click_idx)
loss_dict = criterion(outputs, labels_new, click_weights)
weight_dict = criterion.weight_dict
losses = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)
loss_dict_reduced = utils.reduce_dict(loss_dict)
loss_dict_reduced_unscaled = {f'{k}_unscaled': v
for k, v in loss_dict_reduced.items()}
loss_dict_reduced_scaled = {k: v * weight_dict[k]
for k, v in loss_dict_reduced.items() if k in weight_dict}
losses_reduced_scaled = sum(loss_dict_reduced_scaled.values())
loss_value = losses_reduced_scaled.item()
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
print(loss_dict_reduced)
sys.exit(1)
train_total_iter+=1
optimizer.zero_grad()
losses.backward()
if max_norm > 0:
grad_total_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
else:
grad_total_norm = utils.get_total_grad_norm(model.parameters(), max_norm)
optimizer.step()
with torch.no_grad():
pred_logits = outputs['pred_masks']
pred = [p.argmax(-1) for p in pred_logits]
metric_logger.update(mIoU=mean_iou(pred, labels_new))
metric_logger.update(grad_norm=grad_total_norm)
metric_logger.update(loss=loss_value, **loss_dict_reduced_scaled, **loss_dict_reduced_unscaled)
metric_logger.update(lr=optimizer.param_groups[0]["lr"])
if ((i + 1) % 100 == 0):
wandb.log({
"train/loss": metric_logger.meters['loss'].avg,
"train/loss_bce": metric_logger.meters['loss_bce'].avg,
"train/loss_dice": metric_logger.meters['loss_dice'].avg,
"train/mIoU": metric_logger.meters['mIoU'].avg,
"train/total_iter": train_total_iter
})
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}, train_total_iter
@torch.no_grad()
def evaluate(model, criterion, data_loader, args, epoch, device):
model.eval()
criterion.eval()
metric_logger = utils.MetricLogger(delimiter=" ")
header = 'Test:'
instance_counter = 0
results_file = os.path.join(args.valResults_dir, 'val_results_epoch_' + str(epoch) + '.csv')
f = open(results_file, 'w')
for batched_inputs in metric_logger.log_every(data_loader, 10, header):
coords, raw_coords, feats, labels, labels_full, inverse_map, click_idx, scene_name, num_obj = batched_inputs
coords = coords.to(device)
raw_coords = raw_coords.to(device)
labels = [l.to(device) for l in labels]
labels_full = [l.to(device) for l in labels_full]
data = ME.SparseTensor(
coordinates=coords,
features=feats,
device=device
)
###### interactive evaluation ######
batch_idx = coords[:,0]
batch_size = batch_idx.max()+1
# click ids set null
for click_idx_sample in click_idx:
for obj_id, _ in click_idx_sample.items():
click_idx_sample[obj_id] = []
click_time_idx = copy.deepcopy(click_idx)
current_num_clicks = 0
# pre-compute backbone features only once
pcd_features, aux, coordinates, pos_encodings_pcd = model.forward_backbone(data, raw_coordinates=raw_coords)
max_num_clicks = num_obj[0] * args.max_num_clicks
while current_num_clicks <= max_num_clicks:
if current_num_clicks == 0:
pred = [torch.zeros(l.shape).to(device) for l in labels]
else:
outputs = model.forward_mask(pcd_features, aux, coordinates, pos_encodings_pcd,
click_idx=click_idx, click_time_idx=click_time_idx)
pred_logits = outputs['pred_masks']
pred = [p.argmax(-1) for p in pred_logits]
if current_num_clicks != 0:
click_weights = cal_click_loss_weights(batch_idx, raw_coords, torch.cat(labels), click_idx)
loss_dict = criterion(outputs, labels, click_weights)
weight_dict = criterion.weight_dict
losses = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)
loss_dict_reduced = utils.reduce_dict(loss_dict)
loss_dict_reduced_scaled = {k: v * weight_dict[k]
for k, v in loss_dict_reduced.items() if k in weight_dict}
loss_dict_reduced_unscaled = {f'{k}_unscaled': v
for k, v in loss_dict_reduced.items()}
updated_pred = []
for idx in range(batch_idx.max()+1):
sample_mask = batch_idx == idx
sample_pred = pred[idx]
sample_mask = sample_mask.to(feats.device) # Move sample_mask to the same device as feats
sample_feats = feats[sample_mask]
if current_num_clicks != 0:
# update prediction with sparse gt
for obj_id, cids in click_idx[idx].items():
sample_pred[cids] = int(obj_id)
updated_pred.append(sample_pred)
sample_labels = labels[idx]
sample_raw_coords = raw_coords[sample_mask]
sample_pred_full = sample_pred[inverse_map[idx]]
sample_labels_full = labels_full[idx]
sample_iou, _ = mean_iou_scene(sample_pred_full, sample_labels_full)
line = str(instance_counter+idx) + ' ' + scene_name[idx].replace('scene','') + ' ' + str(num_obj[idx]) + ' ' + str(current_num_clicks/num_obj[idx]) + ' ' + str(
sample_iou.cpu().numpy()) + '\n'
f.write(line)
print(scene_name[idx], 'Object: ', num_obj[idx], 'num clicks: ', current_num_clicks/num_obj[idx], 'IOU: ', sample_iou.item())
new_clicks, new_clicks_num, new_click_pos, new_click_time = get_simulated_clicks(sample_pred, sample_labels, sample_raw_coords, current_num_clicks, training=False)
### add new clicks ###
if new_clicks is not None:
click_idx[idx], click_time_idx[idx] = extend_clicks(click_idx[idx], click_time_idx[idx], new_clicks, new_click_time)
if current_num_clicks != 0:
metric_logger.update(mIoU=mean_iou(updated_pred, labels))
metric_logger.update(loss=sum(loss_dict_reduced_scaled.values()),
**loss_dict_reduced_scaled,
**loss_dict_reduced_unscaled)
if current_num_clicks == 0:
new_clicks_num = num_obj[idx]
else:
new_clicks_num = 1
current_num_clicks += new_clicks_num
instance_counter += len(num_obj)
f.close()
evaluator = EvaluatorMO(args.val_list, results_file, [0.5,0.65,0.8,0.85,0.9])
results_dict = evaluator.eval_results()
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
stats = {k: meter.global_avg for k, meter in metric_logger.meters.items()}
stats.update(results_dict)
return stats