# Average Hausdorff loss implementation?

**URL:** <https://discuss.pytorch.org/t/average-hausdorff-loss-implementation/40551>\
**Category:** Uncategorized\
**Created:** [March 21, 2019, 7:33pm UTC](https://discuss.pytorch.org/t/average-hausdorff-loss-implementation/40551 "2019-03-21T19:33:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![che85](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/che85/32/11493_2.png) [@che85](https://discuss.pytorch.org/u/che85)\
**Post date:** [March 21, 2019, 7:33pm UTC](https://discuss.pytorch.org/t/average-hausdorff-loss-implementation/40551/1 "2019-03-21T19:33:19Z")

</div>

Hi there,

does anyone of you know an open source implementation of the Average Hausdorff distance? I would like to try using it for some 3D volumetric data but don’t quite know how to get started with writing a loss function in pytorch.

I am helpful for any kind of input.

Thanks a lot.  
Christian

---

<div class="post-metadata">

**Author:** ![yyifan](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/a9a28c/32.png) [@yyifan](https://discuss.pytorch.org/u/yyifan)\
**Post date:** [January 15, 2020, 10:45am UTC](https://discuss.pytorch.org/t/average-hausdorff-loss-implementation/40551/2 "2020-01-15T10:45:29Z")

</div>

Hi Christian,

The open source code of a CVPR 2019 paper (Locating Objects Without Bounding Boxes) may be helpful. Here is the [url](https://github.com/javiribera/locating-objects-without-bboxes/blob/master/object-locator/losses.py).

Not sure whether this is still helpful you right now.

---

<div class="post-metadata">

**Author:** ![che85](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/che85/32/11493_2.png) [@che85](https://discuss.pytorch.org/u/che85)\
**Post date:** [January 23, 2020, 6:15pm UTC](https://discuss.pytorch.org/t/average-hausdorff-loss-implementation/40551/3 "2020-01-23T18:15:59Z")

</div>

Thanks for this reference. It seem that it’s not applied to 3D. Am I correct?

---

<div class="post-metadata">

**Author:** ![Priyank\_Pathak](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/priyank_pathak/32/19974_2.png) [@Priyank\_Pathak](https://discuss.pytorch.org/u/Priyank_Pathak)\
**Post date:** [October 17, 2025, 5:15am UTC](https://discuss.pytorch.org/t/average-hausdorff-loss-implementation/40551/4 "2025-10-17T05:15:12Z")

</div>

5 years late, but here is a more generic implementation

```auto
X1.shape == (B, N, D)
X2.shape == (M, D)

X2 = X2.unsqueeze(0).expand(B, M, D) # (B, M, D)
dist = torch.cdist(X1, X2_exp) # B, N, M 

dh_x_y = dist.min(1)[0].mean(-1) # B, M --> B 
dh_y_x = dist.min(2)[0].mean(-1) # B, N --> B 
dist = dh_x_y + dh_y_x

```
