qubvel / segmentation_models

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

How to implement more metrics in model.compile? #566

Closed nora-dimitrova closed 9 months ago

nora-dimitrova commented 1 year ago

I want to evaluate segmentation results with Dice, Jaccard, True positive - False positive and True positive - False negative ratio. I saw the metrics script and how to implement each of them individually but not all of them. How can I get them together, also keeping mse?

..... model = sm.Unet(BACKBONE, encoder_weights = "imagenet") model.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ["mse"]) #mse mean squared error .....

Baakchsu commented 10 months ago

Hey, @nor4eto99! You can define the metrics separately and then use them all together during model.compile().

For example, if you wanted to use Dice and Jaccard index along with MSE, you could try something like this:

` import tensorflow as tf import segmentation_models as sm

mse = tf.keras.metrics.MeanSquaredError() jaccard = sm.metrics.IOUScore() dice = sm.metrics.FScore() model = sm.Unet('inceptionresnetv2', encoder_weights="imagenet") model.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = [mse,dice,jaccard])`

tshr-d-dragon commented 9 months ago

Hi @nora-dimitrova,

You can try following code snippet:

from segmentation_models.metrics import iou_score, f1_score, precision, recall
from segmentation_models.losses import bce_jaccard_loss, dice_loss

model = sm.Unet(BACKBONE, encoder_weights = "imagenet")

Loss = bce_jaccard_loss + dice_loss
Metrics = [precision, recall, f1_score, iou_score]

model.compile(optimizer = "adam", loss = Loss, metrics = Metrics)

Close the issue if this solves your problem!!!:)