-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogcoshfocaltverskyPixelClassificationLayer.m
60 lines (47 loc) · 1.96 KB
/
logcoshfocaltverskyPixelClassificationLayer.m
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
classdef logcoshfocaltverskyPixelClassificationLayer < nnet.layer.ClassificationLayer
% This layer implements the logcoshfocalTversky loss function for training semantic
% segmentation networks.
%
properties(Constant)
% Small constant to prevent division by zero.
Epsilon = 1e-8;
end
properties
% Default weighting coefficients for False Positives and False
% Negatives
Alpha = 0.3;
Beta = 0.7;
Gamma= 4/3;
end
methods
function layer = logcoshfocaltverskyPixelClassificationLayer(name, alpha, beta, gamma)
% layer = tverskyPixelClassificationLayer(name) creates a
% Tversky pixel classification layer with the specified name.
% Set layer name.
layer.Name = name;
% Set layer properties.
layer.Alpha = alpha;
layer.Beta = beta;
layer.Gamma = gamma;
% Set layer description.
layer.Description = 'logcoshfocalTversky loss';
end
function loss = forwardLoss(layer, Y, T)
% loss = forwardLoss(layer, Y, T) returns the logcoshfocalTversky loss
% between the predictions Y and the training targets T.
Ycnot = 1-Y;
Tcnot = 1-T;
TP = sum(sum(Y.*T,1),2);
FP = sum(sum(Y.*Tcnot,1),2);
FN = sum(sum(Ycnot.*T,1),2);
numer = TP + layer.Epsilon;
denom = TP + layer.Alpha*FP + layer.Beta*FN + layer.Epsilon;
% Compute logcoshfocalTversky index
lossTIc = log(cosh((1 - numer./denom).^(1/layer.Gamma)));
lossTI = sum(lossTIc,3);
% Return average logcoshfocalTversky index loss.
N = size(Y,4);
loss = sum(lossTI)/N;
end
end
end