'How to implement IoU-like metric to monitor boundary intersection?

How to implement boundary metric in keras? I mean something like IoU but monitoring boundary over union during training?

I found how to define iou metric, how do I modify it so boundary metric can be monitored during training?

from tensorflow.keras import backend as K
def jacard_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (intersection + 1.0) / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + 1.0)

Also I defined BoU metric with predicted vs ground truth photos after the model is trained:

import numpy as np
import cv2
def get_boundary(numpy_img):
  canny_Img = cv2.Canny(numpy_img,100,200)
  contours,_ = cv2.findContours(canny_Img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
  canvas = np.zeros_like(numpy_img)
  boundary = cv2.drawContours(canvas , contours, -1, 255, 1)

  return boundary

def bou(ground_truth, prediction)
  ground_truth_boundary = get_boundary((ground_truth*255).astype(np.uint8))
  prediction_boundary = get_boundary((prediction*255).astype(np.uint8))
  intersection = np.logical_and(ground_truth_boundary, prediction_boundary)
  union = np.logical_or(ground_truth_boundary, prediction_boundary)
  bou_score = np.sum(intersection) / np.sum(union)

  return bou_score

results:

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source