"3rdparty/core-r22.12/src/backend_manager.cc" did not exist on "e38ee081a0495769e25766b894abe19bc8a6209e"
test.py 2.79 KB
Newer Older
liyinhao's avatar
liyinhao committed
1
import mmcv
2
import os
liyinhao's avatar
liyinhao committed
3
import torch
4
from mmcv.image import tensor2imgs
liyinhao's avatar
liyinhao committed
5
6


7
8
9
10
11
def single_gpu_test(model,
                    data_loader,
                    show=False,
                    out_dir=None,
                    show_score_thr=0.3):
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    """Test model with single gpu.

    This method tests model with single gpu and gives the 'show' option.
    By setting ``show=True``, it saves the visualization results under
    ``out_dir``.

    Args:
        model (nn.Module): Model to be tested.
        data_loader (nn.Dataloader): Pytorch data loader.
        show (bool): Whether to save viualization results.
            Default: True.
        out_dir (str): The path to save visualization results.
            Default: None.

    Returns:
        list[dict]: The prediction results.
    """
liyinhao's avatar
liyinhao committed
29
30
31
32
33
34
35
36
37
    model.eval()
    results = []
    dataset = data_loader.dataset
    prog_bar = mmcv.ProgressBar(len(dataset))
    for i, data in enumerate(data_loader):
        with torch.no_grad():
            result = model(return_loss=False, rescale=True, **data)

        if show:
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
            # Visualize the results of MMdetection3D model
            # 'show_results' is MMdetection3D visualization API
            if hasattr(model.module, 'show_results'):
                model.module.show_results(data, result, out_dir)
            # Visualize the results of MMdetection model
            # 'show_result' is MMdetection visualization API
            else:
                batch_size = len(result)
                if batch_size == 1 and isinstance(data['img'][0],
                                                  torch.Tensor):
                    img_tensor = data['img'][0]
                else:
                    img_tensor = data['img'][0].data[0]
                img_metas = data['img_metas'][0].data[0]
                imgs = tensor2imgs(img_tensor, **img_metas[0]['img_norm_cfg'])
                assert len(imgs) == len(img_metas)
liyinhao's avatar
liyinhao committed
54

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                for i, (img, img_meta) in enumerate(zip(imgs, img_metas)):
                    h, w, _ = img_meta['img_shape']
                    img_show = img[:h, :w, :]

                    ori_h, ori_w = img_meta['ori_shape'][:-1]
                    img_show = mmcv.imresize(img_show, (ori_w, ori_h))

                    if out_dir:
                        out_file = os.path.join(out_dir,
                                                img_meta['ori_filename'])
                    else:
                        out_file = None

                    model.module.show_result(
                        img_show,
                        result[i],
                        show=show,
                        out_file=out_file,
                        score_thr=show_score_thr)
74
        results.extend(result)
liyinhao's avatar
liyinhao committed
75

76
        batch_size = len(result)
liyinhao's avatar
liyinhao committed
77
78
79
        for _ in range(batch_size):
            prog_bar.update()
    return results