<gh_stars>10-100
import keras
import keras.backend as K
import tensorflow as tf

def recall_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    recall = true_positives / (possible_positives + K.epsilon())
    return recall

def precision_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision

def f1_m(y_true, y_pred):
    precision = precision_m(y_true, y_pred)
    recall = recall_m(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))

# Broken
# https://ai-pool.com/d/keras_iou_implementation
def mean_iou(y_true, y_pred):
    y_pred = K.cast(K.greater(y_pred, .25), dtype='float32') # .5 is the threshold
    inter = K.sum(K.sum(K.sum(y_true * y_pred, axis=3), axis=2), axis=1)
    union = K.sum(K.sum(K.sum(y_true + y_pred, axis=3), axis=2), axis=1) - inter
    return K.mean((inter + K.epsilon()) / (union + K.epsilon()))
import keras
import keras.backend as K
import tensorflow as tf

def recall_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    recall = true_positives / (possible_positives + K.epsilon())
    return recall

def precision_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision

def f1_m(y_true, y_pred):
    precision = precision_m(y_true, y_pred)
    recall = recall_m(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))

# Broken
# https://ai-pool.com/d/keras_iou_implementation
def mean_iou(y_true, y_pred):
    y_pred = K.cast(K.greater(y_pred, .25), dtype='float32') # .5 is the threshold
    inter = K.sum(K.sum(K.sum(y_true * y_pred, axis=3), axis=2), axis=1)
    union = K.sum(K.sum(K.sum(y_true + y_pred, axis=3), axis=2), axis=1) - inter
    return K.mean((inter + K.epsilon()) / (union + K.epsilon()))
# Broken # https://ai-pool.com/d/keras_iou_implementation # .5 is the threshold