N0vel / weighted-hausdorff-distance-tensorflow-keras-loss

Weighted Hausdorff Distance Loss: use it as point cloud similarity metric based loss for keras and tf. Useful in keypoint detection.
23 stars 5 forks source link

how to simplify the hausdorff distance? #1

Closed jizhang02 closed 5 years ago

jizhang02 commented 5 years ago

Hello, Thank you for sharing your code, which exactly what I want to study recently. However, when I add this file to the U-Net as loss function, there is an error like below, it seems take a very large memory! image So, I wonder if you could simplify this function? For example, my code is as below, but it has an error, too. I think it is because of conflict between tensor and array. But I don't know how to solve it? Could you please help me? Thank you so much! image

N0vel commented 5 years ago

Hello, I guess your image size is 256x256, but do you really have 53227 keypoints on that image or I got it wrong? Target image is a binary mask. 53227 keypoints for 65536 pixels is actually more like a segmentation task, than keypoint detection. Also, when creating a custom loss function, assert it consists only of differentiable functions (that's why I bothered to use "weighted" hausdorff distance). Tensor that caused OOM is 65536x53227 float32 values, so it requires at least 13 gb of memory (professional gpu). Check target, if its segmentation, then use appropriate loss functions. Also, if framework is not important, take a look at the official pytorch implementation, my implementation in TF worked for me, but I may have made a mistake.

jizhang02 commented 5 years ago

Hi, thank you for reply. Yes, my application is to segment a binary image(256*256). I want to use Hausdorff distance as loss function to calculate the distance between the true image and predicted image. Is that feasible? You mean Hausdorff is not differentiable? But why? Thank you for communication.

N0vel commented 5 years ago

The problem is that authors while creating this loss didn't consider its use in segmentation task. You can switch huge tensors to CPU and store them in RAM or on disk, but this will greatly lower training speed. As for differentiability the paper says: Let Y contain the ground truth pixel coordinates, and X be our estimation. Ideally, we would like to use dAH(X, Y ) as the loss function during the training of our Convolutional Neural Network (CNN). We find two impediments when incorporating the Averaged Hausdorff Distance as a loss function. First, CNNs with linear layers implicitly determine the estimated number of points |X| as the size of the last layer. This is a drawback because the actual number of points depends on the content of the image itself. Second, FCNs such as U-Net [48] can indicate the presence of an object center with a higher activation in the output layer, but they do not return the pixel coordinates. In order to learn with backpropagation, the loss function must be differentiable with respect to the network output.

I didn't think about differentiable requirement before, so I made a mistake, trying to implement average hausdorff distance loss at the beginning.

jizhang02 commented 5 years ago

Thank you, you are so kind!

jizhang02 commented 5 years ago

The problem is that authors while creating this loss didn't consider its use in segmentation task. You can switch huge tensors to CPU and store them in RAM or on disk, but this will greatly lower training speed. As for differentiability the paper says: Let Y contain the ground truth pixel coordinates, and X be our estimation. Ideally, we would like to use dAH(X, Y ) as the loss function during the training of our Convolutional Neural Network (CNN). We find two impediments when incorporating the Averaged Hausdorff Distance as a loss function. First, CNNs with linear layers implicitly determine the estimated number of points |X| as the size of the last layer. This is a drawback because the actual number of points depends on the content of the image itself. Second, FCNs such as U-Net [48] can indicate the presence of an object center with a higher activation in the output layer, but they do not return the pixel coordinates. In order to learn with backpropagation, the loss function must be differentiable with respect to the network output.

I didn't think about differentiable requirement before, so I made a mistake, trying to implement average hausdorff distance loss at the beginning.

Hello again, I want to calculate Hausdorff distance as one of metrics during training, is that possible? If can, how to apply the code you shared on the github?

N0vel commented 5 years ago

The problem is that authors while creating this loss didn't consider its use in segmentation task. You can switch huge tensors to CPU and store them in RAM or on disk, but this will greatly lower training speed. As for differentiability the paper says: Let Y contain the ground truth pixel coordinates, and X be our estimation. Ideally, we would like to use dAH(X, Y ) as the loss function during the training of our Convolutional Neural Network (CNN). We find two impediments when incorporating the Averaged Hausdorff Distance as a loss function. First, CNNs with linear layers implicitly determine the estimated number of points |X| as the size of the last layer. This is a drawback because the actual number of points depends on the content of the image itself. Second, FCNs such as U-Net [48] can indicate the presence of an object center with a higher activation in the output layer, but they do not return the pixel coordinates. In order to learn with backpropagation, the loss function must be differentiable with respect to the network output. I didn't think about differentiable requirement before, so I made a mistake, trying to implement average hausdorff distance loss at the beginning.

Hello again, I want to calculate Hausdorff distance as one of metrics during training, is that possible? If can, how to apply the code you shared on the github?

Hello, if you are using Keras you can just copy the code and add hausdorff distance to metrics. Example model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy', hausdorff_distance_function]) Also you should change resized_height = 144
resized_width = 256 to your image resolution. You can check out newer implementations of this loss here on github. https://paperswithcode.com/paper/weighted-hausdorff-distance-a-loss-function

Worshipper600 commented 1 year ago

Hello Novel try to implement hausdorff distance for 3d segmentation task specifically brain tumor segmentation. my images are of shape 128x128x128 with four channel(flair,t1,t1ce,t2) and four number of classes(background,necrotic,edema,enhancing) don't know if your code would work. Any help?