-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
322 lines (274 loc) · 12.2 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""
Train and eval functions used in main.py
"""
import math
import os
import sys
from typing import Iterable
from functools import partial
from util.utils import slprint
import mlx.core as mx
import mlx.nn as nn
import mlx.optimizers as optim
import util.misc as utils
from datasets.coco_eval import CocoEvaluator
def train_one_epoch(model: nn.Module, criterion,
data_loader: Iterable, optimizer: optim.Optimizer, epoch: int, max_norm: float = 0,
wo_class_error=False, args=None, logger=None, print_freq=10, print_loss_dict_freq=100,
compile_forward=False, compile_backward=False):
model.train()
state = [model.state, optimizer.state, mx.random.state]
mx.eval(state)
def forward_pass(array_dict, targets, need_tgt_for_training=False):
if need_tgt_for_training:
outputs = model(array_dict, targets)
else:
outputs = model(array_dict)
return outputs
def loss_fn(array_dict, targets, need_tgt_for_training=False, return_outputs=False):
outputs = forward_pass(array_dict, targets, need_tgt_for_training)
mx.eval(outputs)
loss_dict = criterion.forward(outputs, targets)
weight_dict = criterion.weight_dict
loss = sum(loss_dict[k] * weight_dict[k]
for k in loss_dict.keys() if k in weight_dict)
if return_outputs:
return loss, loss_dict, outputs
return loss, loss_dict
def step(array_dict, targets, need_tgt_for_training=False, return_outputs=False):
train_step_fn = nn.value_and_grad(model, loss_fn)
(loss_value, loss_dict), grads = train_step_fn(
samples, targets, need_tgt_for_training, return_outputs=False)
grads, total_norm = optim.clip_grad_norm(grads, max_norm=max_norm)
optimizer.update(model, grads)
return loss_value, loss_dict
if compile_forward and not compile_backward:
if logger is not None:
logger.info("Compiling forward pass")
forward_pass = mx.compile(
forward_pass, inputs=state, outputs=state)
elif compile_forward and compile_backward:
if logger is not None:
logger.info("Compiling forward and backward passes")
step = mx.compile(step, inputs=state, outputs=state)
try:
need_tgt_for_training = args.use_dn
except:
need_tgt_for_training = False
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', utils.SmoothedValue(
window_size=1, fmt='{value:.6f}'))
if not wo_class_error:
metric_logger.add_meter('class_error', utils.SmoothedValue(
window_size=1, fmt='{value:.2f}'))
header = 'Epoch: [{}]'.format(epoch)
_cnt = 0
for samples, targets in metric_logger.log_every(data_loader, print_freq, print_loss_dict_freq, header, logger=logger):
loss_value, loss_dict = step(
samples, targets, need_tgt_for_training, return_outputs=False)
mx.eval(state)
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
sys.exit(1)
metric_logger.update(loss=loss_value, **loss_dict)
if 'class_error' in loss_dict:
metric_logger.update(class_error=loss_dict['class_error'])
metric_logger.update(lr=optimizer.learning_rate)
_cnt += 1
if args.debug:
if _cnt % 15 == 0:
print("BREAK!"*5)
break
# gather the stats from all processes
print("Averaged stats:", metric_logger)
resstat = {k: meter.global_avg for k,
meter in metric_logger.meters.items() if meter.count > 0}
return resstat
def evaluate(model, criterion, postprocessors, data_loader,
base_ds, output_dir, wo_class_error=False, args=None,
logger=None, print_freq=10, print_loss_dict_freq=100, max_iterations=None,
compile_forward=False, compile_loss_computation=False):
model.eval()
state = [model.state, mx.random.state]
mx.eval(state)
def forward_pass(array_dict, targets, need_tgt_for_training=False):
if need_tgt_for_training:
outputs = model(array_dict, targets)
else:
outputs = model(array_dict)
return outputs
def loss_fn(array_dict, targets, need_tgt_for_training=False, return_outputs=False):
outputs = forward_pass(array_dict, targets, need_tgt_for_training)
mx.eval(outputs)
try:
loss_dict = criterion.forward(outputs, targets)
weight_dict = criterion.weight_dict
loss = sum(loss_dict[k] * weight_dict[k]
for k in loss_dict.keys() if k in weight_dict)
except:
logger.error("Error in loss computation")
loss = 0.0
loss_dict = {}
if return_outputs:
return loss, loss_dict, outputs
return loss, loss_dict
if compile_forward and not compile_loss_computation:
if logger is not None:
logger.info("Compiling forward pass")
forward_pass = mx.compile(
forward_pass, inputs=state, outputs=state)
elif compile_forward and compile_loss_computation:
if logger is not None:
logger.info("Compiling forward pass and loss computation")
loss_fn = mx.compile(loss_fn, inputs=state, outputs=state)
need_tgt_for_training = False
metric_logger = utils.MetricLogger(delimiter=" ")
if not wo_class_error:
metric_logger.add_meter('class_error', utils.SmoothedValue(
window_size=1, fmt='{value:.2f}'))
header = 'Evaluate:'
all_iou_types = ['bbox']
iou_types = []
for k in postprocessors.keys():
if k in all_iou_types:
iou_types.append(k)
useCats = True
try:
useCats = args.useCats
except:
useCats = True
if not useCats:
print("useCats: {} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!".format(useCats))
coco_evaluator = CocoEvaluator(base_ds, iou_types, useCats=useCats)
# coco_evaluator.coco_eval[iou_types[0]].params.iouThrs = [0, 0.1, 0.5, 0.75]
_cnt = 0
output_state_dict = {} # for debug only
for samples, targets in metric_logger.log_every(data_loader, print_freq, print_loss_dict_freq, header, logger=logger):
loss_value, loss_dict, outputs = loss_fn(
samples, targets, need_tgt_for_training, return_outputs=True)
weight_dict = criterion.weight_dict
mx.eval(model)
metric_logger.update(loss=loss_value, **loss_dict)
if 'class_error' in loss_dict:
metric_logger.update(class_error=loss_dict['class_error'])
orig_target_sizes = mx.stack([t["orig_size"] for t in targets], axis=0)
results = postprocessors['bbox'](outputs, orig_target_sizes)
# [scores: [100], labels: [100], boxes: [100, 4]] x B
res = {target['image_id'].item(): output for target,
output in zip(targets, results)}
if coco_evaluator is not None:
coco_evaluator.update(res)
if args.save_results:
res_score = outputs['res_score']
res_label = outputs['res_label']
res_bbox = outputs['res_bbox']
res_idx = outputs['res_idx']
for i, (tgt, res, outbbox) in enumerate(zip(targets, results, outputs['pred_boxes'])):
"""
pred vars:
K: number of bbox pred
score: array(K),
label: list(len: K),
bbox: array(K, 4)
idx: list(len: K)
tgt: dict.
"""
# compare gt and res (after postprocess)
gt_bbox = tgt['boxes']
gt_label = tgt['labels']
gt_info = mx.concatenate((gt_bbox, gt_label.unsqueeze(-1)), 1)
# img_h, img_w = tgt['orig_size'].unbind()
# scale_fct = mx.stack([img_w, img_h, img_w, img_h], axis=0)
# _res_bbox = res['boxes'] / scale_fct
_res_bbox = outbbox
_res_prob = res['scores']
_res_label = res['labels']
res_info = mx.concatenate(
(_res_bbox, _res_prob[..., None], _res_label[..., None]), 1)
# import ipdb;ipdb.set_trace()
if 'gt_info' not in output_state_dict:
output_state_dict['gt_info'] = []
output_state_dict['gt_info'].append(gt_info)
if 'res_info' not in output_state_dict:
output_state_dict['res_info'] = []
output_state_dict['res_info'].append(res_info)
# # for debug only
# import random
# if random.random() > 0.7:
# print("Now let's break")
# break
_cnt += 1
if max_iterations is not None and _cnt > max_iterations:
logger.info(
"Breaking after {} iterations - check max_eval_iterations flag in config file.".format(str(max_iterations)))
break
if args.debug:
if _cnt % 15 == 0:
print("BREAK!"*5)
break
if args.save_results:
import os.path as osp
# output_state_dict['gt_info'] = mx.concatenate(output_state_dict['gt_info'])
# output_state_dict['res_info'] = mx.concatenate(output_state_dict['res_info'])
savepath = osp.join(
args.output_dir, 'results.pkl')
print("Saving res to {}".format(savepath))
import pickle
with open(savepath, 'wb') as f:
pickle.dump(output_state_dict, f)
print("Averaged stats:", metric_logger)
if coco_evaluator is not None:
coco_evaluator.synchronize_between_processes()
# accumulate predictions from all images
if coco_evaluator is not None:
coco_evaluator.accumulate()
coco_evaluator.summarize()
stats = {k: meter.global_avg for k,
meter in metric_logger.meters.items() if meter.count > 0}
if coco_evaluator is not None:
if 'bbox' in postprocessors.keys():
stats['coco_eval_bbox'] = coco_evaluator.coco_eval['bbox'].stats.tolist()
return stats, coco_evaluator
def test(model, criterion, postprocessors, data_loader, base_ds, device, output_dir, wo_class_error=False, args=None, logger=None):
model.eval()
metric_logger = utils.MetricLogger(delimiter=" ")
# if not wo_class_error:
# metric_logger.add_meter('class_error', utils.SmoothedValue(window_size=1, fmt='{value:.2f}'))
header = 'Test:'
iou_types = tuple(k for k in ('segm', 'bbox')
if k in postprocessors.keys())
# coco_evaluator = CocoEvaluator(base_ds, iou_types)
# coco_evaluator.coco_eval[iou_types[0]].params.iouThrs = [0, 0.1, 0.5, 0.75]
final_res = []
for samples, targets in metric_logger.log_every(data_loader, 10, header, logger=logger):
outputs = model(samples)
mx.eval(model)
metric_logger.update(loss=sum(loss_dict.values()),
**loss_dict)
if 'class_error' in loss_dict:
metric_logger.update(class_error=loss_dict['class_error'])
orig_target_sizes = mx.stack([t["orig_size"] for t in targets], axis=0)
results = postprocessors['bbox'](
outputs, orig_target_sizes, not_to_xyxy=True)
# [scores: [100], labels: [100], boxes: [100, 4]] x B
res = {target['image_id'].item(): output for target,
output in zip(targets, results)}
for image_id, outputs in res.items():
_scores = outputs['scores'].tolist()
_labels = outputs['labels'].tolist()
_boxes = outputs['boxes'].tolist()
for s, l, b in zip(_scores, _labels, _boxes):
assert isinstance(l, int)
itemdict = {
"image_id": int(image_id),
"category_id": l,
"bbox": b,
"score": s,
}
final_res.append(itemdict)
if args.output_dir:
import json
with open(args.output_dir + f'/results{args.rank}.json', 'w') as f:
json.dump(final_res, f)
return final_res