Skip to content

U-Net loss function [cross_entropy] quickly cause gradient-vanishing in high order #1

@windsor718

Description

@windsor718

Issue

In the segmentation map, we have much less levee pixels, causing imbalanced learning. We may address this issue by class weight, but we are more interested in the geometrical similarity between predicted images and ground truth, which should create a clearer gradient.

Solution

Use distance between two matrices. Each matrix has shape (N, 2), where N is number of samples and 2-dimension contains the coordinates of pixels labelled as 1 (Useful reference). Actual implementation in tensorflow would be like this.

def pairwise_dist (A, B):  
  """
  Computes pairwise distances between each element of A and B.
  Args:
    A,    [m,d] matrix
    B,    [n,d] matrix
  Returns:
    D,    [m,n] matrix of pairwise distances
  """
  with tf.variable_scope('pairwise_dist'):
    # squared norms of each row in A and B
    na = tf.reduce_sum(tf.square(A), 1)
    nb = tf.reduce_sum(tf.square(B), 1)
    
    # na as a row and nb as a co"lumn vectors
    na = tf.reshape(na, [-1, 1])
    nb = tf.reshape(nb, [1, -1])

    # return pairwise euclidead difference matrix
    D = tf.sqrt(tf.maximum(na - 2*tf.matmul(A, B, False, True) + nb, 0.0))
  return D

You could use tf.where to extract the indices (=coordinates) of images.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions