Skip to content

randelastic2d on gpu is broken #1857

@wyli

Description

@wyli

Describe the bug
from transforms_demo_2d tutorial:

# create an elsatic deformation transform
deform = Rand2DElastic(
    prob=1.0,
    spacing=(30, 30),
    magnitude_range=(5, 6),
    rotate_range=(np.pi / 4,),
    scale_range=(0.2, 0.2),
    translate_range=(100, 100),
    padding_mode="zeros",
    device=torch.device("cuda:0"),
)
# transform both image and segmentation using different interpolation mode
deform.set_random_state(seed=123)
new_img = deform(im_data, (224, 224), mode="bilinear")

result:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-44c62f7490ae> in <module>()
     12 # transform both image and segmentation using different interpolation mode
     13 deform.set_random_state(seed=123)
---> 14 new_img = deform(im_data, (224, 224), mode="bilinear")
     15 deform.set_random_state(seed=123)
     16 new_seg = deform(seg_data, (224, 224), mode="nearest")

2 frames
/usr/local/lib/python3.7/dist-packages/monai/transforms/spatial/array.py in __call__(self, img, spatial_size, mode, padding_mode)
   1564                 align_corners=False,
   1565             )
-> 1566             grid = CenterSpatialCrop(roi_size=sp_size)(np.asarray(grid[0]))
   1567         else:
   1568             grid = create_grid(spatial_size=sp_size)

/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     81 
     82     """
---> 83     return array(a, dtype, copy=False, order=order)
     84 
     85 

/usr/local/lib/python3.7/dist-packages/torch/tensor.py in __array__(self, dtype)
    619             return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
    620         if dtype is None:
--> 621             return self.numpy()
    622         else:
    623             return self.numpy().astype(dtype, copy=False)

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions