Chamfer Loss in tensorflow

173 views
Skip to first unread message

dimple a shajahan ed16d009

unread,
Jun 27, 2020, 6:07:12 AM6/27/20
to TPU Users
Hi All,

Is there an implementation for Chamfer loss or earth movers distance in tensorflow. Can anyone help me?

Paige Bailey

unread,
Jun 27, 2020, 11:24:05 AM6/27/20
to dimple a shajahan ed16d009, add...@tensorflow.org, TPU Users
You can file a feature request to have Chamfer Loss implemented in TF Addons ( +ad...@tensorflow.org ); in the meantime, it looks like someone on StackOverflow has shared a TensorFlow implementation:

def distance_matrix(array1, array2):
    """
    arguments: 
        array1: the array, size: (num_point, num_feature)
        array2: the samples, size: (num_point, num_feature)
    returns:
        distances: each entry is the distance from a sample to array1
            , it's size: (num_point, num_point)
    """
    num_point, num_features = array1.shape
    expanded_array1 = tf.tile(array1, (num_point, 1))
    expanded_array2 = tf.reshape(
            tf.tile(tf.expand_dims(array2, 1), 
                    (1, num_point, 1)),
            (-1, num_features))
    distances = tf.norm(expanded_array1-expanded_array2, axis=1)
    distances = tf.reshape(distances, (num_point, num_point))
    return distances

def av_dist(array1, array2):
    """
    arguments:
        array1, array2: both size: (num_points, num_feature)
    returns:
        distances: size: (1,)
    """
    distances = distance_matrix(array1, array2)
    distances = tf.reduce_min(distances, axis=1)
    distances = tf.reduce_mean(distances)
    return distances

def av_dist_sum(arrays):
    """
    arguments:
        arrays: array1, array2
    returns:
        sum of av_dist(array1, array2) and av_dist(array2, array1)
    """
    array1, array2 = arrays
    av_dist1 = av_dist(array1, array2)
    av_dist2 = av_dist(array2, array1)
    return av_dist1+av_dist2

def chamfer_distance_tf(array1, array2):
    batch_size, num_point, num_features = array1.shape
    dist = tf.reduce_mean(
               tf.map_fn(av_dist_sum, elems=(array1, array2), dtype=tf.float64)
           )
    return dist

On Sat, Jun 27, 2020 at 3:07 AM dimple a shajahan ed16d009 <ed16...@smail.iitm.ac.in> wrote:
Hi All,

Is there an implementation for Chamfer loss or earth movers distance in tensorflow. Can anyone help me?

--
You received this message because you are subscribed to the Google Groups "TPU Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tpu-users+...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tpu-users/e867367b-e162-4e4e-adc6-5f281df9a4dbn%40tensorflow.org.


--

Paige Bailey   

Product Manager (TensorFlow)

@DynamicWebPaige

webp...@google.com


 

Reply all
Reply to author
Forward
0 new messages