Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions monai/transforms/utility/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,12 @@ def __call__(self, img) -> np.ndarray:
Apply the transform to `img` and make it contiguous.
"""
if isinstance(img, torch.Tensor):
img = img.detach().cpu().numpy() # type: ignore
img = img.detach().cpu().numpy()
elif has_cp and isinstance(img, cp_ndarray):
img = cp.asnumpy(img) # type: ignore
return np.ascontiguousarray(img)
img = cp.asnumpy(img)

array: np.ndarray = np.asarray(img)
return np.ascontiguousarray(array) if array.ndim > 0 else array


class ToCupy(Transform):
Expand All @@ -380,7 +382,7 @@ 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
img = img.detach().cpu().numpy()
return cp.ascontiguousarray(cp.asarray(img))


Expand Down
7 changes: 7 additions & 0 deletions tests/test_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def test_list_tuple(self):
result = ToNumpy()(test_data)
np.testing.assert_allclose(result, np.asarray(test_data))

def test_single_value(self):
for test_data in [5, np.array(5), torch.tensor(5)]:
result = ToNumpy()(test_data)
self.assertTrue(isinstance(result, np.ndarray))
np.testing.assert_allclose(result, np.asarray(test_data))
self.assertEqual(result.ndim, 0)


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