Coverage for /home/ubuntu/flatiron/python/flatiron/tf/metric.py: 30%
33 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-08 21:55 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-08 21:55 +0000
1from typing import Any, Union # noqa F401
2import numpy
4import tensorflow as tf
6import flatiron.tf.tools as fi_tftools
8Arraylike = Union[numpy.ndarray, tf.Tensor]
9# ------------------------------------------------------------------------------
12def get(config):
13 # type: (dict) -> Any
14 '''
15 Get function from this module.
17 Args:
18 config (dict): Optimizer config.
20 Returns:
21 function: Module function.
22 '''
23 return fi_tftools.get(config, __name__, 'keras.api.metrics')
24# ------------------------------------------------------------------------------
27def intersection_over_union(y_true, y_pred, smooth=1.0):
28 # type: (Arraylike, Arraylike, float) -> tf.Tensor
29 r'''
30 Intersection over union metric.
32 See: https://medium.com/analytics-vidhya/iou-intersection-over-union-705a39e7acef
34 Equation:
36 .. math::
37 :nowrap:
39 \begin{alignat*}{3}
40 \definecolor{blue2}{rgb}{0.58, 0.71, 0.9}
41 \definecolor{cyan2}{rgb}{0.71, 0.93, 0.95}
42 \definecolor{green2}{rgb}{0.63, 0.82, 0.48}
43 \definecolor{light1}{rgb}{0.64, 0.64, 0.64}
44 \definecolor{red2}{rgb}{0.87, 0.58, 0.56}
46 \color{cyan2} IOU (y, \hat{y}, S) && = \frac{I + S}{U + S}
47 \end{alignat*}
49 Terms:
51 .. math::
52 :nowrap:
54 \begin{alignat*}{3}
55 intersection & \rightarrow \color{red2}
56 I(y, \hat{y}) && = \sum{(y_i * \hat{y_i})}
57 \\
58 union & \rightarrow \color{green2}
59 U(y, \hat{y}) && = \sum{(y_i + \hat{y_i})} - I(y_i, \hat{y_i})
60 \\
61 \text{smoothing factor} & \rightarrow \color{blue2} S
62 \\
63 \text{expansion} & \rightarrow
64 \color{cyan2} IOU(y, \hat{y}, S) && =
65 \frac{
66 \color{red2} \sum{(y_i * \hat{y_i})}
67 \color{white} + \color{blue2} S
68 }{
69 \color{green2} \sum{(y_i + \hat{y_i})} - \sum{(y_i * \hat{y_i})}
70 \color{white} + \color{blue2} S
71 }
72 \end{alignat*}
74 Args:
75 y_true (NDArray or Tensor): True labels.
76 y_pred (NDArray or Tensor): Predicted labels.
77 smooth (float, optional): Smoothing factor. Default: 1.0
79 Returns:
80 tf.Tensor: IOU metric.
81 '''
82 y_true = tf.cast(y_true, dtype='float16')
83 y_pred = tf.cast(y_pred, dtype='float16')
84 yt = tf.reshape(y_true, [-1])
85 yp = tf.reshape(y_pred, [-1])
86 i = tf.reduce_sum(yt * yp)
87 u = tf.reduce_sum(yt) + tf.reduce_sum(yp) - i
88 iou = (i + smooth) / (u + smooth)
89 return iou
92def jaccard(y_true, y_pred):
93 # type: (Arraylike, Arraylike) -> tf.Tensor
94 r'''
95 Jaccard metric.
97 See: https://en.wikipedia.org/wiki/Jaccard_index
99 Equation:
101 .. math::
102 :nowrap:
104 \begin{alignat*}{3}
105 \definecolor{blue2}{rgb}{0.58, 0.71, 0.9}
106 \definecolor{cyan2}{rgb}{0.71, 0.93, 0.95}
107 \definecolor{green2}{rgb}{0.63, 0.82, 0.48}
108 \definecolor{light1}{rgb}{0.64, 0.64, 0.64}
109 \definecolor{red2}{rgb}{0.87, 0.58, 0.56}
111 \color{cyan2} Jacc(y, \hat{y}) && =
112 \frac{1}{N} \sum_{i=0}^{N} \frac{I + 1}{U + 1}
113 \end{alignat*}
115 Terms:
117 .. math::
118 :nowrap:
120 \begin{alignat*}{3}
121 intersection & \rightarrow \color{red2}
122 I(y, \hat{y}) && = \sum{(y_i * \hat{y_i})}
123 \\
124 union & \rightarrow \color{green2}
125 U(y, \hat{y}) && = \sum{(y_i + \hat{y_i})} - I(y_i, \hat{y_i})
126 \\
127 \text{expansion} & \rightarrow
128 \color{cyan2} Jacc(y, \hat{y}) && =
129 \frac{1}{N} \sum_{i=0}^{N}
130 \frac{
131 \color{red2} \sum{(y_i * \hat{y_i})}
132 \color{white} + 1
133 }{
134 \color{green2} \sum{(y_i + \hat{y_i})} - \sum{(y_i * \hat{y_i})}
135 \color{white} + 1
136 }
137 \end{alignat*}
139 Args:
140 y_true (NDArray or Tensor): True labels.
141 y_pred (NDArray or Tensor): Predicted labels.
143 Returns:
144 tf.Tensor: Jaccard metric.
145 '''
146 y_true = tf.cast(y_true, dtype='float16')
147 y_pred = tf.cast(y_pred, dtype='float16')
148 i = tf.reduce_sum(y_true * y_pred)
149 u = tf.reduce_sum(y_true + y_pred) - i
150 jacc = (i + 1.0) / (u + 1.0)
151 jacc = tf.reduce_mean(jacc)
152 return jacc
155def dice(y_true, y_pred, smooth=1.0):
156 # type: (Arraylike, Arraylike, float) -> tf.Tensor
157 r'''
158 Dice metric.
160 See: https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
162 Equation:
164 .. math::
165 :nowrap:
167 \begin{alignat*}{3}
168 \definecolor{blue2}{rgb}{0.58, 0.71, 0.9}
169 \definecolor{cyan2}{rgb}{0.71, 0.93, 0.95}
170 \definecolor{green2}{rgb}{0.63, 0.82, 0.48}
171 \definecolor{light1}{rgb}{0.64, 0.64, 0.64}
172 \definecolor{red2}{rgb}{0.87, 0.58, 0.56}
174 \color{cyan2} Dice(y, \hat{y}) && = \frac{2 * I + S}{U + S}
175 \end{alignat*}
177 Terms:
179 .. math::
180 :nowrap:
182 \begin{alignat*}{3}
183 intersection & \rightarrow \color{red2}
184 I(y, \hat{y}) && = \sum{(y_i * \hat{y_i})}
185 \\
186 \text{union} & \rightarrow \color{green2}
187 U(y, \hat{y}) && = \sum{(y_i + \hat{y_i})}
188 \\
189 \text{smoothing factor} & \rightarrow \color{blue2} S
190 \\
191 \text{expansion} & \rightarrow
192 \color{cyan2} Dice(y, \hat{y}, S) && =
193 \frac{
194 \color{white} 2 * \color{red2} \sum{(y_i * \hat{y_i})}
195 \color{white} + \color{blue2} S
196 }{
197 \color{green2} \sum{(y_i + \hat{y_i})}
198 \color{white} + \color{blue2} S
199 }
200 \end{alignat*}
202 Args:
203 y_true (NDArray or Tensor): True labels.
204 y_pred (NDArray or Tensor): Predicted labels.
205 smooth (float, optional): Smoothing factor. Default: 1.0
207 Returns:
208 tf.Tensor: Dice metric.
209 '''
210 y_true = tf.cast(y_true, dtype='float16')
211 y_pred = tf.cast(y_pred, dtype='float16')
212 yt = tf.reshape(y_true, [-1])
213 yp = tf.reshape(y_pred, [-1])
214 i = tf.reduce_sum(yt * yp)
215 u = tf.reduce_sum(yt) + tf.reduce_sum(yp)
216 dice = (2.0 * i + smooth) / (u + smooth)
217 return dice