-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackup_tf_fet_rpd.m
112 lines (93 loc) · 3.37 KB
/
backup_tf_fet_rpd.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
102
103
104
105
106
107
108
109
110
111
112
classdef tf_fet_rpd < tf_i
%TF_RPD Random Pixel Difference, using similarity tform
% Detailed explanation goes here
properties
r; % [1] radius
M; % [1] #pixel difference pairs per point
d1; % [2, M, L] random points in canonical coordinate (<=r)
d2;
pMean; % [2,L]
ind1; % index
ind2;
is_bprop_in2; % true: bprop for in 2 (the image I); false: don't
end
methods
function ob = tf_fet_rpd(pMean_)
ob.r = 0.1;
ob.M = 2;
ob.pMean = pMean_;
ob.is_bprop_in2 = false;
end
function ob = fprop(ob)
%%% in
p = ob.i(1).a; % in 1: p [2,L,N]
II = ob.i(2).a; % in 2: II [W,H,3,N]
I = squeeze( II(:,:,1,:) ); % I [W,H,N]
%%% do it: generate the features
if ( isempty(ob.d1) ) % initialize if necessary
L = size(p,2);
ob = init_param(ob, L);
end
% get the index to the random pixels
ob.ind1 = get_yxind_posetform(size(I), p, ob.pMean, ob.d1);
f1 = double( I(ob.ind1) ); % [MLN]
ob.ind2 = get_yxind_posetform(size(I), p, ob.pMean, ob.d2);
f2 = double( I(ob.ind2) ); % [MLN]
% the values
[~,L,N] = size(p);
X = reshape(f1-f2, [ob.M, L, N]);
%%% out 1: X [M, L, N]
ob.o.a = X;
end
function ob = bprop(ob)
%%% out and in
dX = ob.o.d; % out .d: dX [M,L,N]
p = ob.i(1).a; % in 1.a: p [2, L, N]
II = ob.i(2).a; % in 2.a : II [W,H,3,N]
Gx = squeeze( II(:,:,2,:) ); % [W,H,N]
Gy = squeeze( II(:,:,3,:) ); % [W,H,N]
%%% bprop for p: in1.d
[~,L,N] = size(p);
f1x = double( Gx(ob.ind1) ); % [MLN]
f1x = reshape(f1x, [1, ob.M,L,N]); % [1, M,L,N]
f1y = double( Gy(ob.ind1) ); % [MLN]
f1y = reshape(f1y, [1, ob.M,L,N]); % [1, M,L,N]
GG1 = cat(1, f1x,f1y); % [2,M,L,N]
%obj.ind2 = get_yxind_posetform(size(I), p, obj.pMean, obj.d2); % [MLN]
f2x = double( Gx(ob.ind2) ); % [MLN]
f2x = reshape(f2x, [1, ob.M,L,N]); % [1, M,L,N]
f2y = double( Gy(ob.ind2) ); % [MLN]
f2y = reshape(f2y, [1, ob.M,L,N]); % [1, M,L,N]
GG2 = cat(1, f2x,f2y); % [2,M,L,N]
% delta
dXdX = reshape(dX,[1,ob.M,L,N]); % [1,M,L,N]
dXdX = cat(1, dXdX,dXdX); % [2,M,L,N]
% times
tmp = (GG1-GG2) .* dXdX; % [2,M,L,N]
% in 1.d: dp [2,L,N]
ob.i(1).d = squeeze( sum(tmp,2) ); % squeeze( [2,1,L,N] )
%%% whether bprop for I? (typically doesn't need it when training)
ob.i(2).d = zeros( size( ob.i(2).a ) ); % [W,H,3,N]
if (~ob.is_bprop_in2), return; end
%%% bprop for I: in2.d
tmp1 = zeros( size(Gx) ); % [W,H,N]
tmp1(ob.ind1) = dX(:); % [W,H,N], with MLN non-zero elements
tmp2 = zeros( size(tmp1) ); % [W,H,N]
tmp2(ob.ind2) = dX(:); % [W,H,N], with MLN non-zero elements
tmp = tmp1 - tmp2; % [W,H,N]
% write it
ob.i(2).d(:,:,1,:) = tmp; % leave the other 2 channels
end % bprop
end % methods
%%% helpers
methods
function obj = init_param(obj, L)
% L: [1] #points
% set the random difference coordinates
obj.d1 = rand_pnts_unit_circle(obj.M * L) * obj.r;
obj.d1 = reshape(obj.d1, [2,obj.M,L]);
obj.d2 = rand_pnts_unit_circle(obj.M * L) * obj.r;
obj.d2 = reshape(obj.d2, [2,obj.M,L]);
end % init_param
end
end