__init__.py 1.31 KB
Newer Older
1
from collections import namedtuple
Shaoshuai Shi's avatar
Shaoshuai Shi committed
2
3
4

import numpy as np
import torch
shijianjian's avatar
shijianjian committed
5
from kornia.utils.image import image_to_tensor
Shaoshuai Shi's avatar
Shaoshuai Shi committed
6

7
8
9
10
11
12
13
14
15
16
from .detectors import build_detector


def build_network(model_cfg, num_class, dataset):
    model = build_detector(
        model_cfg=model_cfg, num_class=num_class, dataset=dataset
    )
    return model


17
18
19
20
def load_data_to_gpu(batch_dict):
    for key, val in batch_dict.items():
        if not isinstance(val, np.ndarray):
            continue
21
        elif key in ['frame_id', 'metadata', 'calib']:
22
            continue
23
        elif key in ['images']:
shijianjian's avatar
shijianjian committed
24
            batch_dict[key] = image_to_tensor(val).float().cuda().contiguous()
25
26
27
28
        elif key in ['image_shape']:
            batch_dict[key] = torch.from_numpy(val).int().cuda()
        else:
            batch_dict[key] = torch.from_numpy(val).float().cuda()
29
30


31
32
33
34
def model_fn_decorator():
    ModelReturn = namedtuple('ModelReturn', ['loss', 'tb_dict', 'disp_dict'])

    def model_func(model, batch_dict):
35
        load_data_to_gpu(batch_dict)
36
37
38
39
40
41
42
43
44
45
46
        ret_dict, tb_dict, disp_dict = model(batch_dict)

        loss = ret_dict['loss'].mean()
        if hasattr(model, 'update_global_step'):
            model.update_global_step()
        else:
            model.module.update_global_step()

        return ModelReturn(loss, tb_dict, disp_dict)

    return model_func