Repository: Project-MONAI/MONAI

Issue:
CuPy <> NumPy
**Is your feature request related to a problem? Please describe.**
There are transformations that rely on cupy ndarray, so it is not working with numpy array or torch tensors.

**Describe the solution you'd like**
Transformers to convert numpy array (and torch tensors) to cupy arrays, and the other way around.



Discussion:

Resolving patch:
diff --git a/monai/transforms/__init__.py b/monai/transforms/__init__.py
--- a/monai/transforms/__init__.py
+++ b/monai/transforms/__init__.py
@@ -286,6 +286,7 @@
     SimulateDelay,
     SplitChannel,
     SqueezeDim,
+    ToCupy,
     ToNumpy,
     ToPIL,
     TorchVision,
@@ -365,6 +366,9 @@
     SqueezeDimd,
     SqueezeDimD,
     SqueezeDimDict,
+    ToCupyd,
+    ToCupyD,
+    ToCupyDict,
     ToNumpyd,
     ToNumpyD,
     ToNumpyDict,
diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py
--- a/monai/transforms/utility/array.py
+++ b/monai/transforms/utility/array.py
@@ -28,6 +28,8 @@
 
 PILImageImage, has_pil = optional_import("PIL.Image", name="Image")
 pil_image_fromarray, _ = optional_import("PIL.Image", name="fromarray")
+cp, has_cp = optional_import("cupy")
+cp_ndarray, _ = optional_import("cupy", name="ndarray")
 
 __all__ = [
     "Identity",
@@ -317,9 +319,25 @@ def __call__(self, img) -> np.ndarray:
         """
         if isinstance(img, torch.Tensor):
             img = img.detach().cpu().numpy()  # type: ignore
+        elif has_cp and isinstance(img, cp_ndarray):
+            img = cp.asnumpy(img)  # type: ignore
         return np.ascontiguousarray(img)
 
 
+class ToCupy(Transform):
+    """
+    Converts the input data to CuPy array, can support list or tuple of numbers, NumPy and PyTorch Tensor.
+    """
+
+    def __call__(self, img):
+        """
+        Apply the transform to `img` and make it contiguous.
+        """
+        if isinstance(img, torch.Tensor):
+            img = img.detach().cpu().numpy()  # type: ignore
+        return cp.ascontiguousarray(cp.asarray(img))
+
+
 class ToPIL(Transform):
     """
     Converts the input image (in the form of NumPy array or PyTorch Tensor) to PIL image
@@ -327,7 +345,7 @@ class ToPIL(Transform):
 
     def __call__(self, img):
         """
-        Apply the transform to `img` and make it contiguous.
+        Apply the transform to `img`.
         """
         if isinstance(img, PILImageImage):
             return img
diff --git a/monai/transforms/utility/dictionary.py b/monai/transforms/utility/dictionary.py
--- a/monai/transforms/utility/dictionary.py
+++ b/monai/transforms/utility/dictionary.py
@@ -44,6 +44,7 @@
     SimulateDelay,
     SplitChannel,
     SqueezeDim,
+    ToCupy,
     ToNumpy,
     ToPIL,
     TorchVision,
@@ -125,6 +126,9 @@
     "SqueezeDimD",
     "SqueezeDimDict",
     "SqueezeDimd",
+    "ToCupyD",
+    "ToCupyDict",
+    "ToCupyd",
     "ToNumpyD",
     "ToNumpyDict",
     "ToNumpyd",
@@ -446,6 +450,28 @@ def __call__(self, data: Mapping[Hashable, Any]) -> Dict[Hashable, Any]:
         return d
 
 
+class ToCupyd(MapTransform):
+    """
+    Dictionary-based wrapper of :py:class:`monai.transforms.ToCupy`.
+    """
+
+    def __init__(self, keys: KeysCollection, allow_missing_keys: bool = False) -> None:
+        """
+        Args:
+            keys: keys of the corresponding items to be transformed.
+                See also: :py:class:`monai.transforms.compose.MapTransform`
+            allow_missing_keys: don't raise exception if key is missing.
+        """
+        super().__init__(keys, allow_missing_keys)
+        self.converter = ToCupy()
+
+    def __call__(self, data: Mapping[Hashable, Any]) -> Dict[Hashable, Any]:
+        d = dict(data)
+        for key in self.key_iterator(d):
+            d[key] = self.converter(d[key])
+        return d
+
+
 class ToPILd(MapTransform):
     """
     Dictionary-based wrapper of :py:class:`monai.transforms.ToNumpy`.
@@ -1066,6 +1092,7 @@ def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.nda
 CastToTypeD = CastToTypeDict = CastToTyped
 ToTensorD = ToTensorDict = ToTensord
 ToNumpyD = ToNumpyDict = ToNumpyd
+ToCupyD = ToCupyDict = ToCupyd
 ToPILD = ToPILDict = ToPILd
 DeleteItemsD = DeleteItemsDict = DeleteItemsd
 SelectItemsD = SelectItemsDict = SelectItemsd


Test patch:
diff --git a/tests/test_to_cupy.py b/tests/test_to_cupy.py
new file mode 100644
--- /dev/null
+++ b/tests/test_to_cupy.py
@@ -0,0 +1,66 @@
+# 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
+from unittest import skipUnless
+
+import numpy as np
+import torch
+
+from monai.transforms import ToCupy
+from monai.utils import optional_import
+
+cp, has_cp = optional_import("cupy")
+
+
+class TestToCupy(unittest.TestCase):
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_cumpy_input(self):
+        test_data = cp.array([[1, 2], [3, 4]])
+        test_data = cp.rot90(test_data)
+        self.assertFalse(test_data.flags["C_CONTIGUOUS"])
+        result = ToCupy()(test_data)
+        self.assertTrue(isinstance(result, cp.ndarray))
+        self.assertTrue(result.flags["C_CONTIGUOUS"])
+        cp.testing.assert_allclose(result, test_data)
+
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_numpy_input(self):
+        test_data = np.array([[1, 2], [3, 4]])
+        test_data = np.rot90(test_data)
+        self.assertFalse(test_data.flags["C_CONTIGUOUS"])
+        result = ToCupy()(test_data)
+        self.assertTrue(isinstance(result, cp.ndarray))
+        self.assertTrue(result.flags["C_CONTIGUOUS"])
+        cp.testing.assert_allclose(result, test_data)
+
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_tensor_input(self):
+        test_data = torch.tensor([[1, 2], [3, 4]])
+        test_data = test_data.rot90()
+        self.assertFalse(test_data.is_contiguous())
+        result = ToCupy()(test_data)
+        self.assertTrue(isinstance(result, cp.ndarray))
+        self.assertTrue(result.flags["C_CONTIGUOUS"])
+        cp.testing.assert_allclose(result, test_data.numpy())
+
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_list_tuple(self):
+        test_data = [[1, 2], [3, 4]]
+        result = ToCupy()(test_data)
+        cp.testing.assert_allclose(result, cp.asarray(test_data))
+        test_data = ((1, 2), (3, 4))
+        result = ToCupy()(test_data)
+        cp.testing.assert_allclose(result, cp.asarray(test_data))
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/tests/test_to_cupyd.py b/tests/test_to_cupyd.py
new file mode 100644
--- /dev/null
+++ b/tests/test_to_cupyd.py
@@ -0,0 +1,66 @@
+# 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
+from unittest import skipUnless
+
+import numpy as np
+import torch
+
+from monai.transforms import ToCupyd
+from monai.utils import optional_import
+
+cp, has_cp = optional_import("cupy")
+
+
+class TestToCupyd(unittest.TestCase):
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_cumpy_input(self):
+        test_data = cp.array([[1, 2], [3, 4]])
+        test_data = cp.rot90(test_data)
+        self.assertFalse(test_data.flags["C_CONTIGUOUS"])
+        result = ToCupyd(keys="img")({"img": test_data})["img"]
+        self.assertTrue(isinstance(result, cp.ndarray))
+        self.assertTrue(result.flags["C_CONTIGUOUS"])
+        cp.testing.assert_allclose(result, test_data)
+
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_numpy_input(self):
+        test_data = np.array([[1, 2], [3, 4]])
+        test_data = np.rot90(test_data)
+        self.assertFalse(test_data.flags["C_CONTIGUOUS"])
+        result = ToCupyd(keys="img")({"img": test_data})["img"]
+        self.assertTrue(isinstance(result, cp.ndarray))
+        self.assertTrue(result.flags["C_CONTIGUOUS"])
+        cp.testing.assert_allclose(result, test_data)
+
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_tensor_input(self):
+        test_data = torch.tensor([[1, 2], [3, 4]])
+        test_data = test_data.rot90()
+        self.assertFalse(test_data.is_contiguous())
+        result = ToCupyd(keys="img")({"img": test_data})["img"]
+        self.assertTrue(isinstance(result, cp.ndarray))
+        self.assertTrue(result.flags["C_CONTIGUOUS"])
+        cp.testing.assert_allclose(result, test_data.numpy())
+
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_list_tuple(self):
+        test_data = [[1, 2], [3, 4]]
+        result = ToCupyd(keys="img")({"img": test_data})["img"]
+        cp.testing.assert_allclose(result, cp.asarray(test_data))
+        test_data = ((1, 2), (3, 4))
+        result = ToCupyd(keys="img")({"img": test_data})["img"]
+        cp.testing.assert_allclose(result, cp.asarray(test_data))
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/tests/test_to_numpy.py b/tests/test_to_numpy.py
--- a/tests/test_to_numpy.py
+++ b/tests/test_to_numpy.py
@@ -10,14 +10,28 @@
 # limitations under the License.
 
 import unittest
+from unittest import skipUnless
 
 import numpy as np
 import torch
 
 from monai.transforms import ToNumpy
+from monai.utils import optional_import
+
+cp, has_cp = optional_import("cupy")
 
 
 class TestToNumpy(unittest.TestCase):
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_cumpy_input(self):
+        test_data = cp.array([[1, 2], [3, 4]])
+        test_data = cp.rot90(test_data)
+        self.assertFalse(test_data.flags["C_CONTIGUOUS"])
+        result = ToNumpy()(test_data)
+        self.assertTrue(isinstance(result, np.ndarray))
+        self.assertTrue(result.flags["C_CONTIGUOUS"])
+        np.testing.assert_allclose(result, test_data.get())
+
     def test_numpy_input(self):
         test_data = np.array([[1, 2], [3, 4]])
         test_data = np.rot90(test_data)
diff --git a/tests/test_to_numpyd.py b/tests/test_to_numpyd.py
--- a/tests/test_to_numpyd.py
+++ b/tests/test_to_numpyd.py
@@ -10,14 +10,28 @@
 # limitations under the License.
 
 import unittest
+from unittest import skipUnless
 
 import numpy as np
 import torch
 
 from monai.transforms import ToNumpyd
+from monai.utils import optional_import
+
+cp, has_cp = optional_import("cupy")
 
 
 class TestToNumpyd(unittest.TestCase):
+    @skipUnless(has_cp, "CuPy is required.")
+    def test_cumpy_input(self):
+        test_data = cp.array([[1, 2], [3, 4]])
+        test_data = cp.rot90(test_data)
+        self.assertFalse(test_data.flags["C_CONTIGUOUS"])
+        result = ToNumpyd(keys="img")({"img": test_data})["img"]
+        self.assertTrue(isinstance(result, np.ndarray))
+        self.assertTrue(result.flags["C_CONTIGUOUS"])
+        np.testing.assert_allclose(result, test_data.get())
+
     def test_numpy_input(self):
         test_data = np.array([[1, 2], [3, 4]])
         test_data = np.rot90(test_data)