-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFb_loss_v4.m
90 lines (75 loc) · 2.89 KB
/
Fb_loss_v4.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
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
classdef Fb_loss_v4 < nnet.layer.ClassificationLayer
% fb sum loss with smoothing
properties
betavalue
% Layer properties go here
end
methods
function layer = Fb_loss_v4(name,beta)
% (Optional) Create a myClassificationLayer
layer.betavalue=beta;
% Set layer name
%if nargin == 1
layer.Name = name;
%end
% Set layer description
layer.Description = 'F beta score cost function layer for semantic segmentation';
end
function loss = forwardLoss(layer, Y, T)
% Return the loss between the predictions Y and the
% training targets T
%
% Inputs:
% layer - Output layer
% Y – Predictions made by network
% T – Training targets
%
% Output:
% loss - Loss between Y and T
% Layer forward loss function goes here
%obversations=size(Y,4)*size(Y,1)*size(Y,2);
b=layer.betavalue;
p0=Y;
p1=-1*Y+1;
g0=T.^2;
g1=1-g0;
p0_g0=p0.*g0;
p0_g1=p0.*g1;
p1_g0=p1.*g0;
sum_p0_g0=sum(sum(sum(sum(p0_g0,3),2),1));
sum_p0_g1=sum(sum(sum(sum(p0_g1,3),2),1));
sum_p1_g0=sum(sum(sum(sum(p1_g0,3),2),1));
fb_numerator=sum_p0_g0*(1+b^2);
fb_denominator=sum_p0_g0*(1+b^2)+sum_p1_g0*(b^2)+sum_p0_g1;
fb_coe=(fb_numerator+1)/(fb_denominator+1);
loss=1-fb_coe;
end
function dLdY = backwardLoss(layer, Y, T)
% Backward propagate the derivative of the loss function
%
% Inputs:
% layer - Output layer
% Y – Predictions made by network
% T – Training targets
%
% Output:
% dLdY - Derivative of the loss with respect to the predictions Y
% Layer backward loss function goes here
%obversations=size(Y,4)*size(Y,1)*size(Y,2);
b=layer.betavalue;
p0=Y;
p1=-1*Y+1;
g0=T.^2;
g1=1-g0;
p0_g0=p0.*g0;
p0_g1=p0.*g1;
p1_g0=p1.*g0;
sum_p0_g0=sum(sum(sum(sum(p0_g0,3),2),1));
sum_p0_g1=sum(sum(sum(sum(p0_g1,3),2),1));
sum_p1_g0=sum(sum(sum(sum(p1_g0,3),2),1));
gradient_numerator=((1+b^2)*((b^2)*sum_p1_g0+sum_p0_g1+1)-1)*T;
gradient_denominator=(sum_p0_g0*(1+b^2)+sum_p1_g0*(b^2)+sum_p0_g1+1).^2;
dLdY=-(gradient_numerator/gradient_denominator);
end
end
end