test.py 1.13 KB
Newer Older
liyinhao's avatar
liyinhao committed
1
2
3
4
5
import mmcv
import torch


def single_gpu_test(model, data_loader, show=False, out_dir=None):
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    """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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    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:
            model.module.show_results(data, result, out_dir)

        results.append(result)

        batch_size = len(data['img_metas'][0].data)
        for _ in range(batch_size):
            prog_bar.update()
    return results