text: import numpy as np
  import tensorflow as tf
  
  
  # Score the model and do a threshold optimization by the best IoU.
  # src: https://www.kaggle.com/aglotero/another-iou-metric
  """
   Competition IOU Metric #1
  """
  def iou_metric(y_true_in, y_pred_in, print_table=False):
      labels = y_true_in
      y_pred = y_pred_in
  
      temp1 = np.histogram2d(labels.flatten(), y_pred.flatten(), bins=([0, 0.5, 1], [0, 0.5, 1]))
      intersection = temp1[0]
  
      # Compute areas (needed for finding the union between all objects)
      area_true = np.histogram(labels, bins=[0, 0.5, 1])[0]
      area_pred = np.histogram(y_pred, bins=[0, 0.5, 1])[0]
      area_true = np.expand_dims(area_true, -1)
      area_pred = np.expand_dims(area_pred, 0)
  
      # Compute union
      union = area_true + area_pred - intersection
  
      # Exclude background from the analysis
      intersection = intersection[1:, 1:]
      intersection[intersection == 0] = 1e-9
  
      union = union[1:, 1:]
      union[union == 0] = 1e-9
  
      # Compute the intersection over union
      iou = intersection / union
  
      # Precision helper function
      def precision_at(threshold, iou):
          matches = iou > threshold
          true_positives = np.sum(matches, axis=1) == 1  # Correct objects
          false_positives = np.sum(matches, axis=0) == 0  # Missed objects
          false_negatives = np.sum(matches, axis=1) == 0  # Extra objects
          tp, fp, fn = np.sum(true_positives), np.sum(false_positives), np.sum(false_negatives)
          return tp, fp, fn
  
      # Loop over IoU thresholds
      prec = []
      if print_table:
          print("Thresh\tTP\tFP\tFN\tPrec.")
      for t in np.arange(0.5, 1.0, 0.05):
          tp, fp, fn = precision_at(t, iou)
          if (tp + fp + fn) > 0:
              p = tp / (tp + fp + fn)
          else:
              p = 0
          if print_table:
              print("{:1.3f}\t{}\t{}\t{}\t{:1.3f}".format(t, tp, fp, fn, p))
          prec.append(p)
  
      if print_table:
          print("AP\t-\t-\t-\t{:1.3f}".format(np.mean(prec)))
      return np.mean(prec)
  
  def iou_metric_batch(y_true_in, y_pred_in):
      y_pred_in = y_pred_in > 0.5  # added by sgx 20180728
      batch_size = y_true_in.shape[0]
      metric = []
      for batch in range(batch_size):
          # Choose which metric to use
          value = iou_metric(y_true_in[batch], y_pred_in[batch])
          metric.append(value)
      return np.mean(metric)
  
  def challenge_iou_metric(label, pred):
      metric_value = tf.py_func(iou_metric_batch, [label, pred], tf.float64)
      return metric_value
  
  
  """
   Predict Validation Result
  """
  def predict_result(model,x_test,img_size_target): # predict both orginal and reflect x
      x_test_reflect =  np.array([np.fliplr(x) for x in x_test])
      preds_test1 = model.predict(x_test).reshape(-1, img_size_target, img_size_target)
      preds_test2_refect = model.predict(x_test_reflect).reshape(-1, img_size_target, img_size_target)
      preds_test2 = np.array([ np.fliplr(x) for x in preds_test2_refect] )
      preds_avg = (preds_test1 +preds_test2)/2
      return preds_avg