-
Notifications
You must be signed in to change notification settings - Fork 7
/
losses.py
183 lines (133 loc) · 6.07 KB
/
losses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import tensorflow as tf
from tensorflow.keras import backend as K
def iou_coef(y_true, y_pred, smooth=1):
"""
Intersection-over-Union coefficient from 0 to 1.
Can be used to evaluate image segmentation problems where the output of the
model are images or segmentation maps.
`y_true` and `y_pred` must have shape (num_images, height, width, classes)
See: https://towardsdatascience.com/metrics-to-evaluate-your-semantic-segmentation-model-6bcb99639aa2
"""
intersection = K.sum(K.abs(y_true * y_pred), axis=[1, 2, 3])
union = K.sum(y_true, [1, 2, 3]) + K.sum(y_pred, [1, 2, 3]) - intersection
iou = K.mean((intersection + smooth) / (union + smooth), axis=0)
return iou
def dice_coef(y_true, y_pred, smooth=1):
"""Dice coefficient is the `2 * overlapped_area / total_area`
Can be used to evaluate image segmentation problems where the output of the
model are images or segmentation maps.
`y_true` and `y_pred` must have shape (num_images, height, width, classes)
See: https://towardsdatascience.com/metrics-to-evaluate-your-semantic-segmentation-model-6bcb99639aa2
"""
intersection = K.sum(y_true * y_pred, axis=[1, 2, 3])
union = K.sum(y_true, axis=[1, 2, 3]) + K.sum(y_pred, axis=[1, 2, 3])
dice = K.mean((2.0 * intersection + smooth) / (union + smooth), axis=0)
return dice
def dice_loss(y_true, y_pred):
"""Loss for image segmentation problems. It is usually combined with binary
cross entropy for robustness.
See: https://lars76.github.io/neural-networks/object-detection/losses-for-segmentation/
"""
numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=(1, 2, 3))
denominator = tf.reduce_sum(y_true + y_pred, axis=(1, 2, 3))
return tf.reshape(1 - numerator / denominator, (-1, 1, 1))
def r2_score(y_true, y_pred):
"""R-squared score from 0 to 1. Can be used for evaluating regression problem."""
SS_res = K.sum(K.square(y_true - y_pred))
SS_tot = K.sum(K.square(y_true - K.mean(y_true)))
return 1 - SS_res / (SS_tot + K.epsilon())
# region Euclidean Distance
def euclidean_distance_squared(
y_true, y_pred, axis=-1, keepdims=False, force_positive=True
):
"""Compute the distance squared along the specified axis.
# Example
If each input has shape (10, 3), and axis=-1, the output will have shape (10,).
Each element in the output represent euclidean distance ** 2.
"""
sum_square = K.sum(K.square(y_true - y_pred), axis=axis, keepdims=keepdims)
if force_positive:
sum_square = K.maximum(sum_square, K.epsilon())
return sum_square
def euclidean_distance(y_true, y_pred, axis=-1, keepdims=False, force_positive=True):
"""Compute the distance along the specified axis.
# Example
If each input has shape (10, 3), and axis=-1, the output will have shape (10,).
Each element in the output represent euclidean distance.
"""
return K.sqrt(
euclidean_distance_squared(
y_true, y_pred, axis=axis, keepdims=keepdims, force_positive=force_positive
)
)
def mean_euclidean_distance_squared(
y_true, y_pred, axis=-1, keepdims=False, force_positive=True
):
return K.mean(
euclidean_distance_squared(
y_true, y_pred, axis=axis, keepdims=keepdims, force_positive=force_positive
)
)
class mean_euclidean_distance_squared_metric:
"""Return keras metric that computes mean euclidean distance squared along specified axis."""
name = "mean_euclidean_distance_squared"
__name__ = "mean_euclidean_distance_squared"
def __init__(self, axis):
self.axis = axis
def __call__(self, y_true, y_pred):
return mean_euclidean_distance_squared(
y_true, y_pred, axis=self.axis, keepdims=False, force_positive=True
)
def mean_euclidean_distance(
y_true, y_pred, axis=-1, keepdims=False, force_positive=True
):
return K.mean(
euclidean_distance(
y_true, y_pred, axis=axis, keepdims=keepdims, force_positive=force_positive
)
)
class mean_euclidean_distance_metric:
"""Return keras metric that computes mean euclidean distance along specified axis."""
name = "mean_euclidean_distance"
__name__ = "mean_euclidean_distance"
def __init__(self, axis):
self.axis = axis
def __call__(self, y_true, y_pred):
return mean_euclidean_distance(
y_true, y_pred, axis=self.axis, keepdims=False, force_positive=True
)
# endregion
# region Quaternion Distance and Angle
def get_quat_distance(q1, q2, K):
"""Get quaternion distance from 0 to 1. The input must be normalized."""
return 1 - K.sum(q1 * q2, axis=-1) ** 2
def mean_quat_distance(y_true, y_pred):
"""Quaternion distance from 0 to 1 loss/metric for keras.
Make sure that the quaternion are normalized."""
return K.mean(get_quat_distance(y_true, y_pred, K))
def get_quat_angle(q1, q2):
"""Get quaternion angle from 0 to pi. The input must be normalized."""
import numpy as np
eps = np.finfo(float).eps
return np.arccos(np.clip(2 * np.sum(q1 * q2, axis=-1) ** 2 - 1, -1 + eps, 1 - eps))
def get_quat_angle_tf(q1, q2):
"""Get quaternion angle from 0 to pi. The input must be normalized."""
acos_input = 2 * K.sum(q1 * q2, axis=-1) ** 2 - 1
acos_input = K.clip(
acos_input, -1 + K.epsilon(), 1 - K.epsilon()
) # prevent acos from returning nan
return tf.acos(acos_input)
def mean_quat_angle(y_true, y_pred):
"""Quaternion angle loss/metric for keras in radians.
Make sure that the quaternion are normalized. Otherwise you might get nan."""
return K.mean(get_quat_angle_tf(y_true, y_pred))
def mean_quat_angle_deg(y_true, y_pred):
"""Quaternion angle loss/metric for keras in degrees.
Make sure that the quaternion are normalized. Otherwise you might get nan."""
import numpy as np
return mean_quat_angle(y_true, y_pred) * 180 / np.pi
def mean_sqr_quat_angle(y_true, y_pred):
"""Get mean square quaternion angle.
Make sure that the quaternion are normalized."""
return K.mean(get_quat_angle_tf(y_true, y_pred) ** 2)
# endregion