-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvl_nntukeyloss.m
101 lines (77 loc) · 2.86 KB
/
vl_nntukeyloss.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
91
92
93
94
95
96
97
98
99
100
101
function Y = vl_nntukeyloss(X,c,iter,scbox, dzdy, varargin)
%Robust Optimization for Deep Regression
%V. Belagiannis, C. Rupprecht, G. Carneiro, and N. Navab,
%ICCV 2015, Santiago de Chile.
%Available loss functions: Tukey and L2 loss.
%See the paper for details on the Tukey loss.
%http://campar.in.tum.de/twiki/pub/Chair/DeepReg/deepreg.html
%Contact: Vasilis Belagiannis, [email protected]
opts.loss = 'tukeyloss' ;
opts = vl_argparse(opts,varargin) ;
switch lower(opts.loss)
case {'tukeyloss'}
X = reshape(X(1,1,:,:),[size(c{1,1},1),size(c,2)]);
Y = [c{1,1:size(c,2)}];
%residuals
res=(Y-X);
%Median absolute deviation (MAD)
MAD = 1.4826*mad(res',1)';
%inliers (percentage of inliers)
nonZer = round(100*sum(abs(res(:))<4.685)/numel(res));
%if iter<50 %(as in the paper)
if nonZer<70 %(similar to the above, but more stable)
MAD=MAD*7; %helps the convergence at the first iterations
end
res=bsxfun(@rdivide,res,MAD);
c=4.685;
if isempty(dzdy) %forward
%tukey's beiweight function
yt = (c.^2/6) * (1 - (1-(res./c).^2).^3);
yt(find(abs(res)>c))=(c^2)/6;
Y = sqrt(sum(yt(:)));
else
%derivatives
tu= -1.*res.*((1-(res./c).^2).^2);
Y_(1,1,:,:)= tu.*bsxfun(@lt,abs(res),c); % abs(x) < c
Y = single (Y_ * dzdy);
end
%l2loss
case {'l2loss'}
X = reshape(X(1,1,:,:),[size(c{1,1},1),size(c,2)]);
Y = [c{1,1:size(c,2)}];
res=(Y-X);
n=1;
if isempty(dzdy) %forward
Y = (sum(res(:).^2))/numel(res);
else
Y_(1,1,:,:)= -1.*(Y-X);
Y = single (Y_ * (dzdy / n) );
end
%error layer
case {'mpe'} %mean pixel error
X_orig = X;
X = reshape(X(1,1,:,:),[size(c{1,1},1),size(c,2)]);
Y = [c{1,1:size(c,2)}];
if isempty(dzdy) %forward
%residuals
err=abs(Y-X);
%scale back to pixels
funScale = @(A,B) A.*(B);
err = bsxfun(funScale,err,scbox);
Y=[];
Y = sum(err)./size(X,1);%error per samples
Y = sum(Y);%summed batch error
else %nothing to backprop
Y = zerosLike(X_orig) ;
end
otherwise
error('Unknown parameter ''%s''.', opts.loss) ;
end
% --------------------------------------------------------------------
function y = zerosLike(x)
% --------------------------------------------------------------------
if isa(x,'gpuArray')
y = gpuArray.zeros(size(x),'single') ;
else
y = zeros(size(x),'single') ;
end