MIC-DKFZ / medicaldetectiontoolkit

The Medical Detection Toolkit contains 2D + 3D implementations of prevalent object detectors such as Mask R-CNN, Retina Net, Retina U-Net, as well as a training and inference framework focused on dealing with medical images.
Apache License 2.0
1.3k stars 297 forks source link

Not working: CropAndResizeFunction3D #23

Closed JimmyCai91 closed 5 years ago

JimmyCai91 commented 5 years ago

Hi,

I am using pytorch 0.4.0 with python 3.6.7. I got error when I test the function CropAndResizeFunction with the following code:

import torch from cuda_functions.roi_align_3D.roi_align.crop_and_resize import CropAndResizeFunction as ra3D import numpy as np

roi_layer = ra3D(2,2,2,0)

subv = np.ones((1, 1, 100, 100, 100)).astype(np.float32) # [B,C,Y,X,Z] subv[:,:,:50,:50,:] = 0.0 box = np.array([ [ 20.0/subv.shape[2], 20.0/subv.shape[3], 30.0/subv.shape[2], 30.0/subv.shape[3], 0.00/subv.shape[4], 100./subv.shape[4] ]]) # Y1, X1, Y2, X2, Z1, Z2 batch_ix = np.array([0])

im_blob = torch.from_numpy(subv).cuda() im_box = torch.from_numpy(box).cuda() ind = torch.from_numpy(batch_ix).int().cuda()

roi_cuda = roi_layer.forward(im_blob, im_box, ind)

print(roi_cuda)

The output is tensor([[[[[1., 1.], [1., 1.]], [[1., 1.], [1., 1.]]]]], device='cuda:0'

However, I think the output should be all zeros. Am I use the function correctly?

pfjaeger commented 5 years ago

interesting, when running your code I get an error, which requires to change the input to floats:

im_blob = torch.from_numpy(subv).float().cuda() im_box = torch.from_numpy(box).float().cuda()

when I run the code then I receive all zeros, as expected. Can you try the code with floats please? also what GPU model and CUDA version are you using?

JimmyCai91 commented 5 years ago

Hi,

Thanks for this. Yes, I will use float(). I am using GeForce GTX 1080 titan XP with CUDA 8.0.

The code will output different if I instead change the input matrix to float64. Here is the example,

subv = np.ones((1, 1, 100, 100, 100)).astype(np.float) # [B,C,Y,X,Z] subv[:,:,:40,:40,:] = 0.0 box = np.array([ [ 20.0/subv.shape[2], 20.0/subv.shape[3], 60.0/subv.shape[2], 60.0/subv.shape[3], 0.00/subv.shape[4], 100./subv.shape[4] ]]) # Y1, X1, Y2, X2, Z1, Z2 batch_ix = np.array([0])

im_blob = torch.from_numpy(subv).cuda() im_box = torch.from_numpy(box).cuda() ind = torch.from_numpy(batch_ix).int().cuda()

Then, the output is

tensor([[[[[ 1.0000, 1.0000], [ 1.0000, 1.0000]],

      [[ 0.0000,  0.0000],
       [ 0.0000,  0.0000]]]]], dtype=torch.float64, device='cuda:0') 

However, if I use float()

subv = np.ones((1, 1, 100, 100, 100)).astype(np.float32) # [B,C,Y,X,Z] subv[:,:,:40,:40,:] = 0.0 box = np.array([ [ 20.0/subv.shape[2], 20.0/subv.shape[3], 60.0/subv.shape[2], 60.0/subv.shape[3], 0.00/subv.shape[4], 100./subv.shape[4] ]]) # Y1, X1, Y2, X2, Z1, Z2 batch_ix = np.array([0])

im_blob = torch.from_numpy(subv).float().cuda() im_box = torch.from_numpy(box).float().cuda() ind = torch.from_numpy(batch_ix).int().cuda()

The output is

tensor([[[[[ 0., 0.], [ 1., 1.]],

      [[ 1.,  1.],
       [ 1.,  1.]]]]], device='cuda:0') 

Fortunately, the latter is what I expected.

pfjaeger commented 5 years ago

I am glad this works for you. Also did you see the section in the readme pointing out the need for re-compilation of cuda functions for different GPU models? I am not quite sure what happens if you run the TitanXP compile code on your GTX1080.

JimmyCai91 commented 5 years ago

Yes, I recompiled the code with

cd cuda_functions/roi_align_xD/roi_align/src/cuda/ nvcc -c -o crop_and_resize_kernel.cu.o crop_and_resize_kernel.cu -x cu -Xcompiler -fPIC -arch=compute_61 cd ../../ python build.py cd ../../