//  [python]
def mrcnn_coord_bins_loss_graph(target_masks, target_coord, target_class_ids, pred_coord):
    """Mask L2 loss for the coordinates head.

    target_masks: [batch, num_rois, height, width].
        A float32 tensor of values 0 or 1. Uses zero padding to fill array.
    target_coord: [batch, num_rois, height, width]. Might be for x, y or z channel.
        A float32 tensor of values in the range of [0, 1]. Uses zero padding to fill array.
    target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded.
    pred_coord: [batch, proposals, height, width, num_classes, num_bins] float32 tensor with values from 0 to 1.
    """

    # Reshape for simplicity. Merge first two dimensions into one.

    #num_bins = 32
    num_bins = tf.shape(pred_coord)[-1]


    target_class_ids = K.reshape(target_class_ids, (-1,))
    mask_shape = tf.shape(target_masks)
    target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3]))

    target_coord = K.reshape(target_coord, (-1, mask_shape[2], mask_shape[3]))
    coord_shape = tf.shape(target_coord)


    #target_coord_bins = target_coord*(num_bins-1)


    target_coord_bins = target_coord * tf.cast(num_bins, tf.float32) - 0.000001
    target_coord_bins = tf.floor(target_coord_bins)
    target_coord_bins = tf.cast(target_coord_bins, dtype=tf.int32)
    target_coord_bins_flatten = K.flatten(target_coord_bins)

    target_coord_one_hot = tf.one_hot(target_coord_bins_flatten, num_bins)
    target_coord_one_hot = K.reshape(target_coord_one_hot, (coord_shape[0], coord_shape[1], coord_shape[2], num_bins))

    pred_shape = tf.shape(pred_coord)
    pred_coord = K.reshape(pred_coord, (-1, pred_shape[2], pred_shape[3], pred_shape[4], pred_shape[5]))
    # Permute predicted masks to [N, num_classes, height, width, bins]
    pred_coord = tf.transpose(pred_coord, [0, 3, 1, 2, 4])
    #pred_coord = tf.Print(pred_coord, [tf.shape(pred_coord)[-1]], message='pred_coord')


    # Only positive ROIs contribute to the loss. And only
    # the class specific mask of each ROI.
    positive_ix = tf.where(target_class_ids > 0)[:, 0]
    positive_class_ids = tf.cast(tf.gather(target_class_ids, positive_ix), tf.int64)
    indices = tf.stack([positive_ix, positive_class_ids], axis=1)

    # Gather the coordinate maps and masks (predicted and true) that contribute to loss
    # true coord map:[N', height, width, bins]
    y_true = tf.gather(target_coord_one_hot, positive_ix)
    # masks: [N', height, width]
    mask = tf.gather(target_masks, positive_ix)
    mask = tf.cast(mask, dtype=tf.bool)
    y_true_in_mask = tf.boolean_mask(y_true, mask)

    #y_true_in_mask = tf.Print(y_true_in_mask, [tf.shape(y_true_in_mask)], message='y_true_in_mask')

    # num_of_pixels = tf.reduce_sum(y_mask, axis=[1, 2])
    # num_of_pixels = tf.Print(num_of_pixels, [num_of_pixels, tf.shape(num_of_pixels)], message='number_of_pixels_for_each_roi')

    # predicted coord map:[N', height, width, bins]
    y_pred = tf.gather_nd(pred_coord, indices)
    y_pred_in_mask = tf.boolean_mask(y_pred, mask)

    #y_pred_in_mask = tf.Print(y_pred_in_mask, [tf.shape(y_pred_in_mask)], message='y_pred_in_mask')

    coord_loss_in_mask = K.categorical_crossentropy(y_true_in_mask, y_pred_in_mask)
    mean_loss =  K.mean(coord_loss_in_mask)

    loss = K.switch(tf.size(y_true) > 0, mean_loss, tf.constant(0.0))
    # loss = K.mean(loss)
    loss = K.reshape(loss, [1, 1])
    return loss