
    6biro                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKJ	s  J
r  SSKJr  SSKJr  SS	KJr  SS
KJr   " S S\R*                  5      r\" S5       " S S\5      5       r\" S5       " S S\5      5       r\" S5       " S S\5      5       r\" S5       " S S\5      5       r\" S5       " S S\5      5       rg)zIoU metrics.    )List)Optional)Tuple)UnionN)backend)utils)base_metric)keras_exportc                      ^  \ rS rSrSr      SS\S\\   S\\\\	R                  R                  4      S\\   S\S\S	\4U 4S
 jjjrSS jrS rSrU =r$ )_IoUBase!   a  Computes the confusion matrix for Intersection-Over-Union metrics.

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

For an individual class, the IoU metric is defined as follows:

```
iou = true_positives / (true_positives + false_positives + false_negatives)
```

From IoUs of individual classes, the MeanIoU can be computed as the mean of
the individual IoUs.

To compute IoUs, the predictions are accumulated in a confusion matrix,
weighted by `sample_weight` and the metric is then calculated from it.

If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.

Args:
  num_classes: The possible number of labels the prediction task can have.
    This value must be provided, since a confusion matrix of size
    `(num_classes, num_classes)` will be allocated.
  name: (Optional) string name of the metric instance.
  dtype: (Optional) data type of the metric result.
  ignore_class: Optional integer. The ID of a class to be ignored during
    metric computation. This is useful, for example, in segmentation
    problems featuring a "void" class (commonly -1 or 255) in segmentation
    maps. By default (`ignore_class=None`), all classes are considered.
  sparse_y_true: Whether labels are encoded using integers or
    dense floating point vectors. If `False`, the `tf.argmax` function
    will be used to determine each sample's most likely associated label.
  sparse_y_pred: Whether predictions are encoded using integers or
    dense floating point vectors. If `False`, the `tf.argmax` function
    will be used to determine each sample's most likely associated label.
  axis: (Optional) -1 is the dimension containing the logits.
    Defaults to `-1`.
num_classesnamedtypeignore_classsparse_y_truesparse_y_predaxisc                    > [         TU ]  X#S9  Xl        X@l        XPl        X`l        Xpl        U R                  SX4SS9U l        g )N)r   r   total_confusion_matrixzeros)shapeinitializer)	super__init__r   r   r   r   r   
add_weighttotal_cm)	selfr   r   r   r   r   r   r   	__class__s	           Z/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/metrics/iou_metrics.pyr   _IoUBase.__init__J   sU     	d0&(**	 $, ( 
    c                 
   U R                   (       d  [        R                  " XR                  S9nU R                  (       d  [        R                  " X R                  S9n[        R
                  " XR                  5      n[        R
                  " X R                  5      nUR                  R                  S:  a  [        R                  " US/5      nUR                  R                  S:  a  [        R                  " US/5      nUbR  [        R
                  " X0R                  5      nUR                  R                  S:  a  [        R                  " US/5      nU R                  bP  [        R
                  " U R                  UR                  5      n[        R                  " X5      nX   nX%   nUb  X5   n[        R                  R                  UUU R                  UU R                  S9nU R                   R#                  U5      $ )aK  Accumulates the confusion matrix statistics.

Args:
  y_true: The ground truth values.
  y_pred: The predicted values.
  sample_weight: Optional weighting of each example. Can
    be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
    and must be broadcastable to `y_true`. Defaults to `1`.

Returns:
  Update op.
r      )weightsr   )r   tfargmaxr   r   cast_dtyper   ndimsreshaper   r   	not_equalmathconfusion_matrixr   r   
assign_add)r   y_truey_predsample_weightr   
valid_mask
current_cms          r    update_state_IoUBase.update_stateb   s    !!YYvII6F!!YYvII6F-- <<!ZZ-F<<!ZZ-F$GGM;;?M""((1, "

=2$ ?(774#4#4fllCLf;J'F'F( - 9 WW--!++ . 

 }}''
33r"   c                     [         R                  " U R                  [        R                  " U R
                  U R
                  45      5        g N)r   	set_valuer   npr   r   r   s    r    reset_state_IoUBase.reset_state   s3    MM288T%5%5t7G7G$HI	
r"   )r   r   r   r   r   r   NNNTTr&   r:   )__name__
__module____qualname____firstlineno____doc__intr   strr   r(   dtypesDTypeboolr   r7   r>   __static_attributes____classcell__r   s   @r    r   r   !   s    &V #7;&*""

 sm
 c299??234	

 sm
 
 
 
 
024h
 
r"   r   zkeras.metrics.IoUc                      ^  \ rS rSrSr\R                        SS\S\\	\   \
\S4   4   S\\   S\\\\R                  R                  4      S\\   S	\S
\S\4U 4S jjj5       rS rU 4S jrSrU =r$ )IoU   a  Computes the Intersection-Over-Union metric for specific target classes.

General definition and computation:

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

For an individual class, the IoU metric is defined as follows:

```
iou = true_positives / (true_positives + false_positives + false_negatives)
```

To compute IoUs, the predictions are accumulated in a confusion matrix,
weighted by `sample_weight` and the metric is then calculated from it.

If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.

Note, this class first computes IoUs for all individual classes, then
returns the mean of IoUs for the classes that are specified by
`target_class_ids`. If `target_class_ids` has only one id value, the IoU of
that specific class is returned.

Args:
  num_classes: The possible number of labels the prediction task can have.
    A confusion matrix of dimension = [num_classes, num_classes] will be
    allocated to accumulate predictions from which the metric is calculated.
  target_class_ids: A tuple or list of target class ids for which the metric
    is returned. To compute IoU for a specific class, a list (or tuple) of a
    single id value should be provided.
  name: (Optional) string name of the metric instance.
  dtype: (Optional) data type of the metric result.
  ignore_class: Optional integer. The ID of a class to be ignored during
    metric computation. This is useful, for example, in segmentation
    problems featuring a "void" class (commonly -1 or 255) in segmentation
    maps. By default (`ignore_class=None`), all classes are considered.
  sparse_y_true: Whether labels are encoded using integers or
    dense floating point vectors. If `False`, the `tf.argmax` function
    will be used to determine each sample's most likely associated label.
  sparse_y_pred: Whether predictions are encoded using integers or
    dense floating point vectors. If `False`, the `tf.argmax` function
    will be used to determine each sample's most likely associated label.
  axis: (Optional) -1 is the dimension containing the logits.
    Defaults to `-1`.

Standalone usage:

>>> # cm = [[1, 1],
>>> #        [1, 1]]
>>> # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]
>>> # iou = true_positives / (sum_row + sum_col - true_positives))
>>> # iou = [0.33, 0.33]
>>> m = tf.keras.metrics.IoU(num_classes=2, target_class_ids=[0])
>>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1])
>>> m.result().numpy()
0.33333334

>>> m.reset_state()
>>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1],
...                sample_weight=[0.3, 0.3, 0.3, 0.1])
>>> # cm = [[0.3, 0.3],
>>> #        [0.3, 0.1]]
>>> # sum_row = [0.6, 0.4], sum_col = [0.6, 0.4],
>>> # true_positives = [0.3, 0.1]
>>> # iou = [0.33, 0.14]
>>> m.result().numpy()
0.33333334

Usage with `compile()` API:

```python
model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.IoU(num_classes=2, target_class_ids=[0])])
```
r   target_class_ids.r   r   r   r   r   r   c	           
         > [         T	U ]  UUUUUUUS9  [        U5      U:  a  [        S[        U5       SS SU S35      e[	        U5      U l        g )N)r   r   r   r   r   r   r   zTarget class id z is out of range, which is [r   z, z).)r   r   max
ValueErrorlistrQ   )
r   r   rQ   r   r   r   r   r   r   r   s
            r    r   IoU.__init__   s~     	#%'' 	 	
  K/"3'7#8"9 :3bR) 
 !%%5 6r"   c                 @   [         R                  " [         R                  " U R                  SS9U R                  S9n[         R                  " [         R                  " U R                  SS9U R                  S9n[         R                  " [         R
                  R                  U R                  5      U R                  S9nX-   U-
  n[         R                  " X0R                  5      n[         R                  " X@R                  5      n[         R                  " [         R                  " [         R                  " US5      U R                  S95      n[         R                  R                  X45      n[         R                  R                  [         R                  " USS9U5      $ )z=Compute the intersection-over-union via the confusion matrix.r   r$   )r   r%   mean_iou)r   )r(   r*   
reduce_sumr   r+   linalgtensor_diag_partgatherrQ   r.   r/   divide_no_nan)r   sum_over_rowsum_over_coltrue_positivesdenominatornum_valid_entriesious          r    result
IoU.result
  s!   wwMM$--a0
 wwMM$--a0
 II&&t}}5T[[
 #1NB >3H3HIii-B-BC MMGGBLLa0D
 gg##N@ww$$MM#J/1B
 	
r"   c                 "  > U R                   U R                  U R                  U R                  U R                  U R
                  S.n[        TU ]  5       n[        [        UR                  5       5      [        UR                  5       5      -   5      $ )N)r   rQ   r   r   r   r   )r   rQ   r   r   r   r   r   
get_configdictrU   items)r   configbase_configr   s      r    rg   IoU.get_config)  sv    ++ $ 5 5 --!//!//II
 g(*D**,-V\\^0DDEEr"   )rQ   r@   )rA   rB   rC   rD   rE   dtensor_utilsinject_meshrF   r   r   r   r   rG   r(   rH   rI   rJ   r   rd   rg   rK   rL   rM   s   @r    rO   rO      s    M^ 
 #7;&*""77  S	5c? :;7 sm	7
 c299??2347 sm7 7 7 7 78
>
F 
Fr"   rO   zkeras.metrics.BinaryIoUc                      ^  \ rS rSrSr\R                      S	S\\\	   \
\	S4   4   4U 4S jjj5       rS
U 4S jjrS rSrU =r$ )	BinaryIoUi6  aW	  Computes the Intersection-Over-Union metric for class 0 and/or 1.

General definition and computation:

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

For an individual class, the IoU metric is defined as follows:

```
iou = true_positives / (true_positives + false_positives + false_negatives)
```

To compute IoUs, the predictions are accumulated in a confusion matrix,
weighted by `sample_weight` and the metric is then calculated from it.

If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.

This class can be used to compute IoUs for a binary classification task
where the predictions are provided as logits. First a `threshold` is applied
to the predicted values such that those that are below the `threshold` are
converted to class 0 and those that are above the `threshold` are converted
to class 1.

IoUs for classes 0 and 1 are then computed, the mean of IoUs for the classes
that are specified by `target_class_ids` is returned.

Note: with `threshold=0`, this metric has the same behavior as `IoU`.

Args:
  target_class_ids: A tuple or list of target class ids for which the metric
    is returned. Options are `[0]`, `[1]`, or `[0, 1]`. With `[0]` (or
    `[1]`), the IoU metric for class 0 (or class 1, respectively) is
    returned. With `[0, 1]`, the mean of IoUs for the two classes is
    returned.
  threshold: A threshold that applies to the prediction logits to convert
    them to either predicted class 0 if the logit is below `threshold` or
    predicted class 1 if the logit is above `threshold`.
  name: (Optional) string name of the metric instance.
  dtype: (Optional) data type of the metric result.

Standalone usage:

>>> m = tf.keras.metrics.BinaryIoU(target_class_ids=[0, 1], threshold=0.3)
>>> m.update_state([0, 1, 0, 1], [0.1, 0.2, 0.4, 0.7])
>>> m.result().numpy()
0.33333334

>>> m.reset_state()
>>> m.update_state([0, 1, 0, 1], [0.1, 0.2, 0.4, 0.7],
...                sample_weight=[0.2, 0.3, 0.4, 0.1])
>>> # cm = [[0.2, 0.4],
>>> #        [0.3, 0.1]]
>>> # sum_row = [0.6, 0.4], sum_col = [0.5, 0.5],
>>> # true_positives = [0.2, 0.1]
>>> # iou = [0.222, 0.125]
>>> m.result().numpy()
0.17361112

Usage with `compile()` API:

```python
model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.BinaryIoU(target_class_ids=[0], threshold=0.5)])
```
rQ   .c                 2   > [         TU ]  SUUUS9  X l        g )N   )r   rQ   r   r   )r   r   	threshold)r   rQ   rs   r   r   r   s        r    r   BinaryIoU.__init__~  s*     	-	 	 	
 #r"   c                    > [         R                  " X R                  5      n[         R                  " X R                  :  U R                  5      n[        TU ]  XU5      $ )a  Accumulates the confusion matrix statistics.

Before the confusion matrix is updated, the predicted values are
thresholded to be:
  0 for values that are smaller than the `threshold`
  1 for values that are larger or equal to the `threshold`

Args:
  y_true: The ground truth values.
  y_pred: The predicted values.
  sample_weight: Optional weighting of each example. Can
    be a `Tensor` whose rank is either 0, or the same rank as `y_true`,
    and must be broadcastable to `y_true`. Defaults to `1`.

Returns:
  Update op.
)r(   r*   r+   rs   r   r7   )r   r2   r3   r4   r   s       r    r7   BinaryIoU.update_state  sD    $ ->>14;;?w#FMBBr"   c                 `    U R                   U R                  U R                  U R                  S.$ )N)rQ   rs   r   r   )rQ   rs   r   r+   r=   s    r    rg   BinaryIoU.get_config  s*     $ 5 5II[[	
 	
r"   )rs   ))r   r%   g      ?NNr:   )rA   rB   rC   rD   rE   rm   rn   r   r   rF   r   r   r7   rg   rK   rL   rM   s   @r    rp   rp   6  sa    DL  ?E#S	5c? :;# #C,
 
r"   rp   zkeras.metrics.MeanIoUc                      ^  \ rS rSrSr\R                        SS\S\\	   S\\
\	\R                  R                  4      S\\   S\S\S	\4U 4S
 jjj5       rS rSrU =r$ )MeanIoUi  a	  Computes the mean Intersection-Over-Union metric.

General definition and computation:

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

For an individual class, the IoU metric is defined as follows:

```
iou = true_positives / (true_positives + false_positives + false_negatives)
```

To compute IoUs, the predictions are accumulated in a confusion matrix,
weighted by `sample_weight` and the metric is then calculated from it.

If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.

Note that this class first computes IoUs for all individual classes, then
returns the mean of these values.

Args:
  num_classes: The possible number of labels the prediction task can have.
    This value must be provided, since a confusion matrix of dimension =
    [num_classes, num_classes] will be allocated.
  name: (Optional) string name of the metric instance.
  dtype: (Optional) data type of the metric result.
  ignore_class: Optional integer. The ID of a class to be ignored during
    metric computation. This is useful, for example, in segmentation
    problems featuring a "void" class (commonly -1 or 255) in segmentation
    maps. By default (`ignore_class=None`), all classes are considered.
  sparse_y_true: Whether labels are encoded using integers or
    dense floating point vectors. If `False`, the `tf.argmax` function
    will be used to determine each sample's most likely associated label.
  sparse_y_pred: Whether predictions are encoded using integers or
    dense floating point vectors. If `False`, the `tf.argmax` function
    will be used to determine each sample's most likely associated label.
  axis: (Optional) The dimension containing the logits. Defaults to `-1`.

Standalone usage:

>>> # cm = [[1, 1],
>>> #        [1, 1]]
>>> # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]
>>> # iou = true_positives / (sum_row + sum_col - true_positives))
>>> # result = (1 / (2 + 2 - 1) + 1 / (2 + 2 - 1)) / 2 = 0.33
>>> m = tf.keras.metrics.MeanIoU(num_classes=2)
>>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1])
>>> m.result().numpy()
0.33333334

>>> m.reset_state()
>>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1],
...                sample_weight=[0.3, 0.3, 0.3, 0.1])
>>> m.result().numpy()
0.23809525

Usage with `compile()` API:

```python
model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.MeanIoU(num_classes=2)])
```
r   r   r   r   r   r   r   c                 V   > [        [        U5      5      n[        T	U ]  UUUUUUUUS9  g )N)r   r   rQ   r   r   r   r   r   )rU   ranger   r   )
r   r   r   r   r   r   r   r   rQ   r   s
            r    r   MeanIoU.__init__  s?      k 23#-%'' 	 		
r"   c                     U R                   U R                  U R                  U R                  U R                  U R
                  U R                  S.$ )N)r   r   r   r   r   r   r   )r   r   r+   r   r   r   r   r=   s    r    rg   MeanIoU.get_config
  sE    ++II[[ --!//!//II
 	
r"    r@   )rA   rB   rC   rD   rE   rm   rn   rF   r   rG   r   r(   rH   rI   rJ   r   rg   rK   rL   rM   s   @r    rz   rz     s    BH  #7;&*""

 sm
 c299??234	

 sm
 
 
 
 
,	
 	
r"   rz   zkeras.metrics.OneHotIoUc                      ^  \ rS rSrSr\R                       SS\S\\	\   \
\S4   4   S\\   S\S\4
U 4S	 jjj5       rS
 rSrU =r$ )	OneHotIoUi  a  Computes the Intersection-Over-Union metric for one-hot encoded labels.

General definition and computation:

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

For an individual class, the IoU metric is defined as follows:

```
iou = true_positives / (true_positives + false_positives + false_negatives)
```

To compute IoUs, the predictions are accumulated in a confusion matrix,
weighted by `sample_weight` and the metric is then calculated from it.

If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.

This class can be used to compute IoU for multi-class classification tasks
where the labels are one-hot encoded (the last axis should have one
dimension per class). Note that the predictions should also have the same
shape. To compute the IoU, first the labels and predictions are converted
back into integer format by taking the argmax over the class axis. Then the
same computation steps as for the base `IoU` class apply.

Note, if there is only one channel in the labels and predictions, this class
is the same as class `IoU`. In this case, use `IoU` instead.

Also, make sure that `num_classes` is equal to the number of classes in the
data, to avoid a "labels out of bound" error when the confusion matrix is
computed.

Args:
  num_classes: The possible number of labels the prediction task can have.
    A confusion matrix of shape `(num_classes, num_classes)` will be
    allocated to accumulate predictions from which the metric is calculated.
  target_class_ids: A tuple or list of target class ids for which the metric
    is returned. To compute IoU for a specific class, a list (or tuple) of a
    single id value should be provided.
  name: (Optional) string name of the metric instance.
  dtype: (Optional) data type of the metric result.
  ignore_class: Optional integer. The ID of a class to be ignored during
    metric computation. This is useful, for example, in segmentation
    problems featuring a "void" class (commonly -1 or 255) in segmentation
    maps. By default (`ignore_class=None`), all classes are considered.
  sparse_y_pred: Whether predictions are encoded using natural numbers or
    probability distribution vectors. If `False`, the `tf.argmax` function
    will be used to determine each sample's most likely associated label.
  axis: (Optional) The dimension containing the logits. Defaults to `-1`.

Standalone usage:

>>> y_true = tf.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0], [1, 0, 0]])
>>> y_pred = tf.constant([[0.2, 0.3, 0.5], [0.1, 0.2, 0.7], [0.5, 0.3, 0.1],
...                       [0.1, 0.4, 0.5]])
>>> sample_weight = [0.1, 0.2, 0.3, 0.4]
>>> m = tf.keras.metrics.OneHotIoU(num_classes=3, target_class_ids=[0, 2])
>>> m.update_state(
...     y_true=y_true, y_pred=y_pred, sample_weight=sample_weight)
>>> # cm = [[0, 0, 0.2+0.4],
>>> #       [0.3, 0, 0],
>>> #       [0, 0, 0.1]]
>>> # sum_row = [0.3, 0, 0.7], sum_col = [0.6, 0.3, 0.1]
>>> # true_positives = [0, 0, 0.1]
>>> # single_iou = true_positives / (sum_row + sum_col - true_positives))
>>> # mean_iou = (0 / (0.3 + 0.6 - 0) + 0.1 / (0.7 + 0.1 - 0.1)) / 2
>>> m.result().numpy()
0.071

Usage with `compile()` API:

```python
model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.OneHotIoU(num_classes=3, target_class_id=[1])])
```
r   rQ   .r   r   r   c                 .   > [         TU ]  UUUUUSUUS9  g )NF)r   rQ   r   r   r   r   r   r   r   r   )	r   r   rQ   r   r   r   r   r   r   s	           r    r   OneHotIoU.__init__h  s/     	#-%' 	 		
r"   c                     U R                   U R                  U R                  U R                  U R                  U R
                  U R                  S.$ )N)r   rQ   r   r   r   r   r   )r   rQ   r   r+   r   r   r   r=   s    r    rg   OneHotIoU.get_config~  sE    ++ $ 5 5II[[ --!//II
 	
r"   r   NNNFr&   )rA   rB   rC   rD   rE   rm   rn   rF   r   r   r   r   rJ   r   rg   rK   rL   rM   s   @r    r   r     s    N` 
 &*#

  S	5c? :;
 sm
 
 
 
*	
 	
r"   r   zkeras.metrics.OneHotMeanIoUc                      ^  \ rS rSrSr\R                       SS\S\S\	\
\\R                  R                  4      S\	\   S\S\4U 4S	 jjj5       rS
 rSrU =r$ )OneHotMeanIoUi  a<  Computes mean Intersection-Over-Union metric for one-hot encoded labels.

General definition and computation:

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

For an individual class, the IoU metric is defined as follows:

```
iou = true_positives / (true_positives + false_positives + false_negatives)
```

To compute IoUs, the predictions are accumulated in a confusion matrix,
weighted by `sample_weight` and the metric is then calculated from it.

If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.

This class can be used to compute the mean IoU for multi-class
classification tasks where the labels are one-hot encoded (the last axis
should have one dimension per class). Note that the predictions should also
have the same shape. To compute the mean IoU, first the labels and
predictions are converted back into integer format by taking the argmax over
the class axis. Then the same computation steps as for the base `MeanIoU`
class apply.

Note, if there is only one channel in the labels and predictions, this class
is the same as class `MeanIoU`. In this case, use `MeanIoU` instead.

Also, make sure that `num_classes` is equal to the number of classes in the
data, to avoid a "labels out of bound" error when the confusion matrix is
computed.

Args:
  num_classes: The possible number of labels the prediction task can have.
    A confusion matrix of shape `(num_classes, num_classes)` will be
    allocated to accumulate predictions from which the metric is calculated.
  name: (Optional) string name of the metric instance.
  dtype: (Optional) data type of the metric result.
  ignore_class: Optional integer. The ID of a class to be ignored during
    metric computation. This is useful, for example, in segmentation
    problems featuring a "void" class (commonly -1 or 255) in segmentation
    maps. By default (`ignore_class=None`), all classes are considered.
  sparse_y_pred: Whether predictions are encoded using natural numbers or
    probability distribution vectors. If `False`, the `tf.argmax` function
    will be used to determine each sample's most likely associated label.
  axis: (Optional) The dimension containing the logits. Defaults to `-1`.

Standalone usage:

>>> y_true = tf.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0], [1, 0, 0]])
>>> y_pred = tf.constant([[0.2, 0.3, 0.5], [0.1, 0.2, 0.7], [0.5, 0.3, 0.1],
...                       [0.1, 0.4, 0.5]])
>>> sample_weight = [0.1, 0.2, 0.3, 0.4]
>>> m = tf.keras.metrics.OneHotMeanIoU(num_classes=3)
>>> m.update_state(
...     y_true=y_true, y_pred=y_pred, sample_weight=sample_weight)
>>> # cm = [[0, 0, 0.2+0.4],
>>> #       [0.3, 0, 0],
>>> #       [0, 0, 0.1]]
>>> # sum_row = [0.3, 0, 0.7], sum_col = [0.6, 0.3, 0.1]
>>> # true_positives = [0, 0, 0.1]
>>> # single_iou = true_positives / (sum_row + sum_col - true_positives))
>>> # mean_iou = (0 + 0 + 0.1 / (0.7 + 0.1 - 0.1)) / 3
>>> m.result().numpy()
0.048

Usage with `compile()` API:

```python
model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.OneHotMeanIoU(num_classes=3)])
```
r   r   r   r   r   r   c           
      ,   > [         TU ]  UUUUUSUS9  g )NF)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   s          r    r   OneHotMeanIoU.__init__  s,     	#%' 	 	
r"   c                     U R                   U R                  U R                  U R                  U R                  U R
                  S.$ )N)r   r   r   r   r   r   )r   r   r+   r   r   r   r=   s    r    rg   OneHotMeanIoU.get_config  s<    ++II[[ --!//II
 	
r"   r   r   )rA   rB   rC   rD   rE   rm   rn   rF   rG   r   r   r(   rH   rI   rJ   r   rg   rK   rL   rM   s   @r    r   r     s    L\  7;&*#

 
 c299??234	

 sm
 
 
 
&
 
r"   r   )rE   typingr   r   r   r   numpyr<   tensorflow.compat.v2compatv2r(   tf_keras.srcr   tf_keras.src.dtensorr   rm   tf_keras.src.metricsr	    tensorflow.python.util.tf_exportr
   Metricr   rO   rp   rz   r   r   r   r"   r    <module>r      s          ! !   7 , :x
{!! x
v !"VF( VF #VFr '(s
 s
 )s
l %&e
c e
 'e
P '(p
 p
 )p
f +,k
G k
 -k
r"   