question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migrate `IoU` segmentaiton metrics from `keras.metrics` to `keras_cv.metrics`

See original GitHub issue

Intersection-Over-Union is a common evaluation metric for semantic image segmentation.

tf.keras.metrics.OneHotIoU(
    num_classes: int,
    target_class_ids: Union[List[int], Tuple[int, ...]],
    name=None,
    dtype=None,
    ignore_class: Optional[int] = None,
    sparse_y_pred: bool = False,
    axis: int = -1
)

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
DavidLandup0commented, Nov 2, 2022

The issue with the class was that it was expecting exclusively categorical vectors, so when using sparse categorical crossentropy and an output fit for it - there would be a shape mismatch. The common fix was to simply get the argmax and thus convert it into a sparse representation.

However, as @tanzhenyu pointed out in the previous comment:

As of tf 2.10, this is fixed by accepting sparse_y_true and sparse_y_pred@innat can you verify it before I close this?

Not sure if it’s a better API choice to have MeanIoU(arg) or SparseMeanIoU and CategoricalMeanIoU. KerasCV losses have gone with the latter format so far, which also allows for more specialization down the line.

1reaction
LukeWoodcommented, Oct 13, 2022

It’s reported here: tensorflow/tensorflow#32875 Sparse categorical crossentropy doesn’t play well with it, apparently. Most fixes boil down to subclassing it modifying one line that throws the exception:

class MeanIoU(tf.keras.metrics.MeanIoU):
  def __init__(self,
               y_true=None,
               y_pred=None,
               num_classes=None,
               name=None,
               dtype=None):
    super(UpdatedMeanIoU, self).__init__(num_classes = num_classes,name=name, dtype=dtype)

  def update_state(self, y_true, y_pred, sample_weight=None):
    y_pred = tf.math.argmax(y_pred, axis=-1)
    return super().update_state(y_true, y_pred, sample_weight)

Depending on what we want. So if this metrics is particularly for segmentation, we should have it at keras_cv. The Keras version MeanIOU is intended for a wider usage which doesn’t really think about the segmentation map tensor shape. So I’d say in this case, we should do the migration.

Yeah gotcha, that makes sense to me. We also could use a bounding box variant of IOU too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tf.keras.metrics.MeanIoU | TensorFlow v2.11.0
Intersection-Over-Union is a common evaluation metric for semantic image segmentation. For an individual class, the IoU metric is defined as ...
Read more >
Metrics - Keras
Metrics. A metric is a function that is used to judge the performance of your model. Metric functions are ... Image segmentation metrics...
Read more >
tf.keras.metrics.MeanIoU - TensorFlow 2.4 - W3cubDocs
Main aliases. tf.metrics.MeanIoU. Compat aliases for migration ... evaluation metric for semantic image segmentation, which first computes the IOU for each ...
Read more >
Metrics for semantic segmentation - Excursions in data
The Jaccard index is also known as Intersection over Union (IoU) and because of its simple and intuitive expression is widely used in...
Read more >
Intersection over union (IOU) metric for multi-class semantic ...
But I'm not sure is the IOU function the correct implementation or not,. could you clarify this. python · tensorflow · keras ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found