The problem is the distance_transform_edt from scipy ndimage. Which convert it back to a numpy array anyway.
I already remove the torch.no_grad and replace bound = compute_edts_forhdloss(targets.cpu().numpy() > 0.5) by bound = compute_edts_forhdloss(targets > 0.5) which was not necessary, the result is still a numpy array. If I understand correctly even if it is on the target you recommend to not use directly distance_transform_edt directly in forward pass. And find another way (through using distance mask instead of segmentation mask has the 2nd parameter in forward pass or having distance mask has a thIrd parameter to the forward pass).
The reason I was doing it this way was to have to pass only the segmentation mask has a ground truth parameter to the forward pass and to be able to easily combine with a regional loss (like Dice loss) like in the article. So I do not have to manage multiple ground truth for one input. But I guess, you are suggesting me to do it the other way (preprocessing distance mask in the dataset and passing distance mask has a parameter to forward pass anyway).
Thanks for your input.