Is there any implementation of EMD in pytorch?

falmasri: <p>I’m looking for a differential EMD (earth mover distance) function to measure the distance of a network latent.</p>

tom: <p>I’m not entirely sure I understand your use case enough to be certain that it’s what you’re looking for, but I can offer <a href="https://github.com/t-vi/pytorch-tvmisc/blob/master/wasserstein-distance/Pytorch_Wasserstein.ipynb" rel="nofollow noopener">a notebook implementing entropy-regularized Wasserstein distances</a>.</p>
<p>Best regards</p>
<p>Thomas</p>

falmasri: <p>I saw this work before,<br>
My question is: let’s say my network output is a vector of 10 elements and my ground is also the same. I want my cost function to measure the distance to be the EMD.</p>

tom: <p>What are your probability measures, then, to measure the distance between?<br>
Usually, you have something like a PD of 10 elements just like you (conceptually) would have for classification and then KL divergence / Cross Entropy to the (peaked at target) distribution. That can be straightforwardly replaced by Wasserstein distance as Frogner et al do and (in the regularized case) can be done with my implementation.<br>
Note that the whole thing is somewhat numerically sensitive.</p>
<p>Best regards</p>
<p>Thomas</p>

falmasri: <p>It is histogram measurement, So I want to use EMD to measure the their distance.  I didn’t understand your KL / CE part ?</p>

marcman411: <p>You can try <a href="https://github.com/meder411/PyTorch-EMDLoss" rel="nofollow noopener">this repository</a>. I have been searching for this too, so I spent some time trying to update and generalize a few implementations I’d seen on GitHub. If you have a fix for the overflow issue too, I would be grateful <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=6" title=":wink:" class="emoji" alt=":wink:"></p>

Yuerno: <p>Hey! I came across this while searching for PyTorch EMD implementations, and I was wondering if this would work with input tensors with sizes of around (1, 16k, 3), so basically, batch size of 1, and 16k points  that are represented as x, y, z. If not, would you happen to have any suggestions on how to implement some sort of EMD approximation myself using PyTorch? I’m not particularly concerned about speed yet, just some sort of implementation that can work.</p>

falmasri: <p>None of the suggested methods worked for me yet. If you came a cross something better please post it back here.</p>

agaldran: <p>Hi <a class="mention" href="/u/tom">@tom</a>,</p>
<p>Many thanks for making this implementation available to the community, I appreciate it a lot. I have almost figured out how to use it, but I have a problem, please forgive me if this is a very naive question.</p>
<p>After instantiating a criterion based on your <code>WassersteinLossStab()</code> class, if I try to compute the loss with a batch size of 1 it seems to work pretty consistently, but if I change the batch size to &gt;1, it crashes:</p>
<pre><code class="lang-auto">batch_size = 1 # setting this to 2 breaks the loss
n_samples = 10

x=numpy.arange(n_samples,dtype=numpy.float32)
M=(x[:,numpy.newaxis]-x[numpy.newaxis,:])**2
M/=M.max()

preds = torch.FloatTensor(batch_size,n_samples).uniform_()
preds/=preds.sum(dim=1).unsqueeze(1)
preds = preds.float()

targets = torch.randn(batch_size, n_samples)&gt;0
targets = targets.float()

criterion = WassersteinLossStab(torch.from_numpy(M), lam=0.1)

criterion(preds, targets)
</code></pre>
<p>Do you have any clue on what am I doing wrong here? Thanks!</p>

Kaichun_Mo: <p>Please try my implementation here for 3D point cloud research: <a href="https://github.com/daerduoCarey/PyTorchEMD" rel="nofollow noopener">https://github.com/daerduoCarey/PyTorchEMD</a></p>
<p>I just made a Pytorch wrapper for Haoqiang Fan’s implementation for paper: A Point Set Generation Network for 3D Object Reconstruction from a Single Image. Please cite this paper if you use the code.</p>
<p>Best,<br>
Kaichun</p>

Zhang_Chi: <p>this may help you, which contains solvers based on QPTH and opencv<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/icoz69/DeepEMD/blob/master/Models/models/emd_utils.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/icoz69/DeepEMD/blob/master/Models/models/emd_utils.py" target="_blank" rel="nofollow noopener">icoz69/DeepEMD/blob/master/Models/models/emd_utils.py</a></h4>
<pre><code class="lang-py">import cv2
import torch
import torch.nn.functional as F
from qpth.qp import QPFunction


def emd_inference_qpth(distance_matrix, weight1, weight2, form='QP', l2_strength=0.0001):
    """
    to use the QP solver QPTH to derive EMD (LP problem),
    one can transform the LP problem to QP,
    or omit the QP term by multiplying it with a small value,i.e. l2_strngth.
    :param distance_matrix: nbatch * element_number * element_number
    :param weight1: nbatch  * weight_number
    :param weight2: nbatch  * weight_number
    :return:
    emd distance: nbatch*1
    flow : nbatch * weight_number *weight_number

    """

</code></pre>

  This file has been truncated. <a href="https://github.com/icoz69/DeepEMD/blob/master/Models/models/emd_utils.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


XZLeo: <p>Hi, I found this implementation quite neat. The only problem is it uses C++ on the CPU. So it will slow down the backward propagation.<br>
<a href="https://pythonot.github.io/" rel="noopener nofollow ugc">optimal transport</a></p>