Coverage for /home/ubuntu/flatiron/python/flatiron/tf/loss.py: 47%

19 statements  

« 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 

3 

4import tensorflow as tf 

5 

6import flatiron.tf.tools as fi_tftools 

7 

8Arraylike = Union[numpy.ndarray, tf.Tensor] 

9# ------------------------------------------------------------------------------ 

10 

11 

12def get(config): 

13 # type: (dict) -> Any 

14 ''' 

15 Get function from this module. 

16 

17 Args: 

18 config (dict): Optimizer config. 

19 

20 Returns: 

21 function: Module function. 

22 ''' 

23 return fi_tftools.get(config, __name__, 'keras.api.losses') 

24# ------------------------------------------------------------------------------ 

25 

26 

27def jaccard_loss(y_true, y_pred, smooth=100): 

28 # type: (Arraylike, Arraylike, int) -> tf.Tensor 

29 r''' 

30 Jaccard's loss is usefull for unbalanced datasets. This has been shifted so 

31 it converges on 0 and is smoothed to avoid exploding or disappearing 

32 gradients. 

33 

34 See: https://en.wikipedia.org/wiki/Jaccard_index 

35 

36 Equation: 

37 

38 .. math:: 

39 :nowrap: 

40 

41 \begin{alignat*}{3} 

42 \definecolor{blue2}{rgb}{0.58, 0.71, 0.9} 

43 \definecolor{cyan2}{rgb}{0.71, 0.93, 0.95} 

44 \definecolor{green2}{rgb}{0.63, 0.82, 0.48} 

45 \definecolor{light1}{rgb}{0.64, 0.64, 0.64} 

46 \definecolor{red2}{rgb}{0.87, 0.58, 0.56} 

47 

48 \color{cyan2} L_{jacc}(y, \hat{y}, S) && = 

49 (1 - \frac{I + S}{U - I + S}) S 

50 \end{alignat*} 

51 

52 Terms: 

53 

54 .. math:: 

55 :nowrap: 

56 

57 \begin{alignat*}{3} 

58 intersection & \rightarrow \color{red2} 

59 I(y, \hat{y}) && = \sum{|y_i * \hat{y_i}|} 

60 \\ 

61 union & \rightarrow \color{green2} 

62 U(y, \hat{y}) && = \sum{(|y_i| + |\hat{y_i}|)} 

63 \\ 

64 \text{smoothing factor} & \rightarrow \color{blue2} S && 

65 \\ 

66 \text{expansion} & \rightarrow 

67 \color{cyan2} L_{jacc}(y, \hat{y}, S) && = 

68 (1 - \frac{ 

69 \color{red2} \sum{|y_i * \hat{y_i}|} 

70 \color{white} + \color{blue2} S 

71 }{ 

72 \color{green2} \sum{(|y_i| + |\hat{y_i}|)} 

73 \color{white} - 

74 \color{red2} \sum{|y_i * \hat{y_i}|} 

75 \color{white} + \color{blue2} S 

76 }) \color{blue2} S 

77 \end{alignat*} 

78 

79 Args: 

80 y_true (NDArray or Tensor): Ground truth labels. 

81 y_pred (NDArray or Tensor): Predicted labels. 

82 smooth (int, optional): Smoothing factor. Default: 100. 

83 

84 Returns: 

85 tf.Tensor: Loss function. 

86 ''' 

87 i = tf.reduce_sum(tf.abs(y_true * y_pred), axis=-1) 

88 u = tf.reduce_sum(tf.abs(y_true) + tf.abs(y_pred), axis=-1) 

89 jacc = (i + smooth) / (u - i + smooth) 

90 loss = (1 - jacc) * smooth 

91 return loss 

92 

93 

94def dice_loss(y_true, y_pred, smooth=1): 

95 # type: (Arraylike, Arraylike, int) -> tf.Tensor 

96 r''' 

97 Dice loss function with smoothing factor to prevent exploding or vanishing 

98 gradients. 

99 

100 See: https://cnvrg.io/semantic-segmentation 

101 

102 Equation: 

103 

104 .. math:: 

105 :nowrap: 

106 

107 \begin{alignat*}{3} 

108 \definecolor{blue2}{rgb}{0.58, 0.71, 0.9} 

109 \definecolor{cyan2}{rgb}{0.71, 0.93, 0.95} 

110 \definecolor{green2}{rgb}{0.63, 0.82, 0.48} 

111 \definecolor{light1}{rgb}{0.64, 0.64, 0.64} 

112 \definecolor{red2}{rgb}{0.87, 0.58, 0.56} 

113 

114 \color{cyan2} L_{dice}(y, \hat{y}, S) && = 

115 1 - \frac{2 * I + S}{U + S} 

116 \end{alignat*} 

117 

118 Terms: 

119 

120 .. math:: 

121 :nowrap: 

122 

123 \begin{alignat*}{3} 

124 intersection & \rightarrow \color{red2} 

125 I(y, \hat{y}) && = \sum{|y_i * \hat{y_i}|} 

126 \\ 

127 union & \rightarrow \color{green2} 

128 U(y, \hat{y}) && = \sum{(|y_i| + |\hat{y_i}|)} 

129 \\ 

130 \text{smoothing factor} & \rightarrow \color{blue2} S && 

131 \\ 

132 \text{expansion} & \rightarrow 

133 \color{cyan2} L_{dice}(y, \hat{y}, S) && = 

134 1 - \frac{ 

135 2 * 

136 \color{red2} \sum{|y_i * \hat{y_i}|} 

137 \color{white} + \color{blue2} S 

138 }{ 

139 \color{green2} \sum{(|y_i| + |\hat{y_i}|)} 

140 \color{white} + \color{blue2} S 

141 } 

142 \end{alignat*} 

143 

144 Args: 

145 y_true (NDArray or Tensor): Ground truth labels. 

146 y_pred (NDArray or Tensor): Predicted labels. 

147 smooth (int, optional): Smoothing factor. Default: 1. 

148 

149 Returns: 

150 tf.Tensor: Loss function. 

151 ''' 

152 intersection = tf.reduce_sum(tf.abs(y_true * y_pred), axis=-1) 

153 union = tf.reduce_sum(tf.abs(y_true) + tf.abs(y_pred), axis=-1) 

154 dice = (2.0 * intersection + smooth) / (union + smooth) 

155 loss = 1.0 - dice 

156 return loss