mmcv/tests/test_image/test_image_misc.py
Cao Yuhang d5cbf7eed1
Migrate op (#392)
* migrate op

* migrate unittest

* update build no torch

* add back use_torch_vision for roi align

* fix type and unit test

* ignore test logging when no torch

* fix no torch ci test

* skip test registry

* remove coverage report when no torch

* fix mac ci order

* install latest pillow when no torch

* mv convws to brisk
2020-07-08 17:29:15 +08:00

53 lines
1.4 KiB
Python

# Copyright (c) Open-MMLab. All rights reserved.
import numpy as np
import pytest
from numpy.testing import assert_array_equal
import mmcv
try:
import torch
except ImportError:
torch = None
@pytest.mark.skipif(torch is None, reason='requires torch library')
def test_tensor2imgs():
# test tensor obj
with pytest.raises(AssertionError):
tensor = np.random.rand(2, 3, 3)
mmcv.tensor2imgs(tensor)
# test tensor ndim
with pytest.raises(AssertionError):
tensor = torch.randn(2, 3, 3)
mmcv.tensor2imgs(tensor)
# test mean length
with pytest.raises(AssertionError):
tensor = torch.randn(2, 3, 5, 5)
mmcv.tensor2imgs(tensor, mean=(1, ))
# test std length
with pytest.raises(AssertionError):
tensor = torch.randn(2, 3, 5, 5)
mmcv.tensor2imgs(tensor, std=(1, ))
# test rgb=True
tensor = torch.randn(2, 3, 5, 5)
gts = [
t.cpu().numpy().transpose(1, 2, 0).astype(np.uint8)
for t in tensor.flip(1)
]
outputs = mmcv.tensor2imgs(tensor, to_rgb=True)
for gt, output in zip(gts, outputs):
assert_array_equal(gt, output)
# test rgb=False
tensor = torch.randn(2, 3, 5, 5)
gts = [t.cpu().numpy().transpose(1, 2, 0).astype(np.uint8) for t in tensor]
outputs = mmcv.tensor2imgs(tensor, to_rgb=False)
for gt, output in zip(gts, outputs):
assert_array_equal(gt, output)