qubvel / segmentation_models

Segmentation models with pretrained backbones. Keras and TensorFlow Keras.
MIT License
4.77k stars 1.03k forks source link

Metrics and losses computation #339

Open tlc10 opened 4 years ago

tlc10 commented 4 years ago

Hi,

I having hard time understanding how the different metrics are calculated (iou, f1, precision recall). For the IoU score, what do A and B correspond to in the documentation? Concerning precision and recall, the values TP, FN, FP, TN correspond to pixels of the predicted output? and for the F1 score it would have been the same to calculate it with the classic formula 2TP/(2TP+FN+FP)?

Thank you for your explanations!

JordanMakesMaps commented 4 years ago

A and B represent the two sets, so in this case, the ground-truth and prediction segmentation maps.

True positive, False Negative, False positive, and True negative come from calculating a confusion matrix. They represent what the model predicts for each class category (the pixels, as you said), and what the actual ground-truth is.

The F1 (or Dice) score is calculated in the same way as you have in your comment, but with smooth to deal with dividing by zeros and beta which "is chosen such that recall is considered beta times as important as precision" - Wiki

# calculate score
    tp = backend.sum(gt * pr, axis=axes)
    fp = backend.sum(pr, axis=axes) - tp
    fn = backend.sum(gt, axis=axes) - tp

    score = ((1 + beta ** 2) * tp + smooth) \
            / ((1 + beta ** 2) * tp + beta ** 2 * fn + fp + smooth)
    score = average(score, per_image, class_weights, **kwargs)

    return score