Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
46c34cd
Implement CuCIM wrapper transfrom
bhashemian Sep 12, 2021
fe09a19
Implement CuCIMd and RandCuCIMd
bhashemian Sep 12, 2021
920ea4e
Update init
bhashemian Sep 12, 2021
751f154
Update docs
bhashemian Sep 12, 2021
4a942e2
Order imports
bhashemian Sep 12, 2021
86767ce
Update RandCuCIM and RandCuCIMd to be randomly applied
bhashemian Sep 13, 2021
f45968c
Merge branch 'dev' into cucim-transform
bhashemian Sep 13, 2021
c9b3198
Add optional_import
bhashemian Sep 13, 2021
97eb1d8
Merge branch 'cucim-transform' of github.com:drbeh/MONAI into cucim-t…
bhashemian Sep 13, 2021
64c11ee
Update docs and docstring
bhashemian Sep 13, 2021
d433a22
Update cucim api for monai
bhashemian Sep 13, 2021
f9b690d
Merge branch 'dev' into cucim-transform
bhashemian Sep 13, 2021
2c260b9
Fix docs
bhashemian Sep 13, 2021
4e7c137
Update imports
bhashemian Sep 13, 2021
2500e09
Remove type checking for cupy.ndarray
bhashemian Sep 14, 2021
6b4505a
Merge branch 'dev' into cucim-transform
bhashemian Sep 14, 2021
da45518
Merge branch 'dev' into cucim-transform
bhashemian Sep 14, 2021
1715212
Update cuCIM exposed API
bhashemian Sep 14, 2021
3ca5d88
Update cucim transfom calls
bhashemian Sep 15, 2021
fca930d
fix typo
bhashemian Sep 15, 2021
299e9b7
Merge branch 'dev' into cucim-transform
bhashemian Sep 15, 2021
22a3ec9
Rename prob to apply_prob
bhashemian Sep 15, 2021
b996567
Add unittests for CuCIM tranform
bhashemian Sep 15, 2021
628b139
Add unittests for CuCIMDict transform
bhashemian Sep 15, 2021
e464c13
Add unittests for RandCuCIM transform
bhashemian Sep 16, 2021
41553ea
Add unittests for RandCuCIMDict transform
bhashemian Sep 16, 2021
71d6311
Merge branch 'dev' into cucim-transform
bhashemian Sep 16, 2021
1f6b6e2
Fix docstrigs
bhashemian Sep 16, 2021
a6c9c48
Merge branch 'dev' into cucim-transform
wyli Sep 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unittests for CuCIM tranform
Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com>
  • Loading branch information
bhashemian committed Sep 15, 2021
commit b996567af34c4732b56bfb5bddc7bb1d81f72b3a
296 changes: 296 additions & 0 deletions tests/test_cucim_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
# Copyright 2020 - 2021 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np
from parameterized import parameterized

from monai.transforms import CuCIM
from monai.utils import optional_import, set_determinism
from tests.utils import skip_if_no_cuda

_, has_cut = optional_import("cucim.core.operations.expose.transform")
cp, has_cp = optional_import("cupy")

set_determinism(seed=0)

TEST_CASE_COLOR_JITTER_1 = [
{"name": "color_jitter", "brightness": 0.0, "contrast": 0.0, "saturation": 0.0, "hue": 0.0},
np.array(
[
[
[0.0, 1.0],
[1.0, 2.0],
],
[
[0.0, 1.0],
[1.0, 2.0],
],
[
[0.0, 1.0],
[1.0, 2.0],
],
],
dtype=np.float32,
),
np.array(
[
[
[0.0, 1.0],
[1.0, 2.0],
],
[
[0.0, 1.0],
[1.0, 2.0],
],
[
[0.0, 1.0],
[1.0, 2.0],
],
],
dtype=np.float32,
),
]

TEST_CASE_COLOR_JITTER_2 = [
{"name": "color_jitter", "brightness": 0.0, "contrast": 0.0, "saturation": 0.0, "hue": 0.0},
np.array(
[
[
[0, 1],
[2, 3],
],
[
[0, 1],
[2, 3],
],
[
[0, 1],
[2, 3],
],
],
dtype=np.uint8,
),
np.array(
[
[
[0, 1],
[2, 3],
],
[
[0, 1],
[2, 3],
],
[
[0, 1],
[2, 3],
],
],
dtype=np.uint8,
),
]

TEST_CASE_FLIP_1 = [
{"name": "image_flip", "spatial_axis": -1},
np.array(
[
[
[0.0, 1.0],
[2.0, 3.0],
],
[
[0.0, 1.0],
[2.0, 3.0],
],
[
[0.0, 1.0],
[2.0, 3.0],
],
],
dtype=np.float32,
),
np.array(
[
[
[1.0, 0.0],
[3.0, 2.0],
],
[
[1.0, 0.0],
[3.0, 2.0],
],
[
[1.0, 0.0],
[3.0, 2.0],
],
],
dtype=np.float32,
),
]


TEST_CASE_ROTATE_1 = [
{"name": "image_rotate_90", "k": 1, "spatial_axis": (-2, -1)},
np.array(
[
[[0.0, 1.0], [2.0, 3.0]],
[[0.0, 1.0], [2.0, 3.0]],
[
[0.0, 1.0],
[2.0, 3.0],
],
],
dtype=np.float32,
),
np.array(
[
[
[1.0, 3.0],
[0.0, 2.0],
],
[
[1.0, 3.0],
[0.0, 2.0],
],
[
[1.0, 3.0],
[0.0, 2.0],
],
],
dtype=np.float32,
),
]

TEST_CASE_SCALE_INTENSITY_1 = [
{"name": "scale_intensity_range", "a_min": 0.0, "a_max": 4.0, "b_min": 0.0, "b_max": 1.0, "clip": False},
np.array(
[
[
[0.0, 1.0],
[2.0, 3.0],
],
[
[0.0, 1.0],
[2.0, 3.0],
],
[[0.0, 1.0], [2.0, 3.0]],
],
dtype=np.float32,
),
np.array(
[
[
[0.0, 0.25],
[0.5, 0.75],
],
[
[0.0, 0.25],
[0.5, 0.75],
],
[
[0.0, 0.25],
[0.5, 0.75],
],
],
dtype=np.float32,
),
]

TEST_CASE_ZOOM_1 = [
{"name": "zoom", "zoom_factor": (0.5, 0.5)},
np.mgrid[:3, 1:4].astype(dtype=np.float32),
np.concatenate(
[
np.ones((1, 3, 3), dtype=np.float32) * 1.0,
np.ones((1, 3, 3), dtype=np.float32) * 2.0,
]
),
]


@skip_if_no_cuda
@unittest.skipUnless(has_cp, "CuPy is required.")
@unittest.skipUnless(has_cut, "cuCIM transforms are required.")
class TestCuCIM(unittest.TestCase):
@parameterized.expand(
[
TEST_CASE_COLOR_JITTER_1,
TEST_CASE_COLOR_JITTER_2,
TEST_CASE_FLIP_1,
TEST_CASE_ROTATE_1,
TEST_CASE_SCALE_INTENSITY_1,
TEST_CASE_ZOOM_1,
]
)
def test_tramsforms_numpy_single(self, params, input, expected):
output = CuCIM(**params)(input)
self.assertTrue(output.dtype == expected.dtype)
self.assertTrue(isinstance(output, np.ndarray))
cp.testing.assert_allclose(output, expected)

@parameterized.expand(
[
TEST_CASE_COLOR_JITTER_1,
TEST_CASE_COLOR_JITTER_2,
TEST_CASE_FLIP_1,
TEST_CASE_ROTATE_1,
TEST_CASE_SCALE_INTENSITY_1,
TEST_CASE_ZOOM_1,
]
)
def test_tramsforms_numpy_batch(self, params, input, expected):
input = input[cp.newaxis, ...]
expected = expected[cp.newaxis, ...]
output = CuCIM(**params)(input)
self.assertTrue(output.dtype == expected.dtype)
self.assertTrue(isinstance(output, np.ndarray))
cp.testing.assert_allclose(output, expected)

@parameterized.expand(
[
TEST_CASE_COLOR_JITTER_1,
TEST_CASE_COLOR_JITTER_2,
TEST_CASE_FLIP_1,
TEST_CASE_ROTATE_1,
TEST_CASE_SCALE_INTENSITY_1,
TEST_CASE_ZOOM_1,
]
)
def test_tramsforms_cupy_single(self, params, input, expected):
input = cp.asarray(input)
expected = cp.asarray(expected)
output = CuCIM(**params)(input)
self.assertTrue(output.dtype == expected.dtype)
self.assertTrue(isinstance(output, cp.ndarray))
cp.testing.assert_allclose(output, expected)

@parameterized.expand(
[
TEST_CASE_COLOR_JITTER_1,
TEST_CASE_COLOR_JITTER_2,
TEST_CASE_FLIP_1,
TEST_CASE_ROTATE_1,
TEST_CASE_SCALE_INTENSITY_1,
TEST_CASE_ZOOM_1,
]
)
def test_tramsforms_cupy_batch(self, params, input, expected):
input = cp.asarray(input)[cp.newaxis, ...]
expected = cp.asarray(expected)[cp.newaxis, ...]
output = CuCIM(**params)(input)
self.assertTrue(output.dtype == expected.dtype)
self.assertTrue(isinstance(output, cp.ndarray))
cp.testing.assert_allclose(output, expected)


if __name__ == "__main__":
unittest.main()