-
Notifications
You must be signed in to change notification settings - Fork 5
/
CTrainerNet.lua
executable file
·158 lines (108 loc) · 4.93 KB
/
CTrainerNet.lua
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
local trainerNet = {}
function trainerNet.getMilContrastive(disp_max, width, th_sup, th_occ, loss_margin, embed_net, head_net)
local Net = nn.Sequential()
local comNet = nn.ConcatTable()
Net:add(comNet);
local refPosNet = nn.Sequential()
local refNegNet = nn.Sequential()
local negPosNet = nn.Sequential()
comNet:add(refPosNet)
comNet:add(refNegNet)
comNet:add(negPosNet)
local refPosSelNet = nn.ConcatTable() -- input selectors for each distance net
local refNegSelNet = nn.ConcatTable()
local negPosSelNet = nn.ConcatTable()
refPosNet:add(refPosSelNet)
refNegNet:add(refNegSelNet)
negPosNet:add(negPosSelNet)
refPosSelNet:add(nn.SelectTable(1))
refPosSelNet:add(nn.SelectTable(2))
refNegSelNet:add(nn.SelectTable(1))
refNegSelNet:add(nn.SelectTable(3))
negPosSelNet:add(nn.SelectTable(3))
negPosSelNet:add(nn.SelectTable(2))
local refPosMetricNet = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max)
local refNegMetricNet = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max)
local negPosMetricNet = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max)
refPosNet:add(refPosMetricNet);
refNegNet:add(refNegMetricNet);
negPosNet:add(negPosMetricNet);
Net:add(nn.milContrastive(th_sup, th_occ, disp_max));
local milFwdCst = nn.MarginRankingCriterion(loss_margin);
local milBwdCst = nn.MarginRankingCriterion(loss_margin);
local contrastiveFwdCst = nn.MarginRankingCriterion(loss_margin);
local contrastiveBwdCst = nn.MarginRankingCriterion(loss_margin);
local criterion = nn.ParallelCriterion():add(milFwdCst,1):add(milBwdCst,1):add(contrastiveFwdCst,1):add(contrastiveFwdCst,1)
return Net, criterion
end
function trainerNet.getPipeline(disp_max, width, th_sup, loss_margin, embed_net, head_net)
--[[
Input: {{ref, pos}, matchInRow_pipe}, where ref, neg are tensor 1 x (2*hpatch + 1) x width
]]--
local Net = nn.Sequential()
local comNet = nn.ParallelTable()
Net:add( comNet )
local metricNet = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max) ;
comNet:add( metricNet )
comNet:add( nn.Identity() )
--Net:add( metricNet )
Net:add( nn.pipeline(th_sup) )
local fwdCst = nn.MarginRankingCriterion(loss_margin);
local bwdCst = nn.MarginRankingCriterion(loss_margin);
local criterion = nn.ParallelCriterion():add(fwdCst,1):add(bwdCst,1)
return Net, criterion
end
function trainerNet.getMil(disp_max, width, loss_margin, embed_net, head_net)
--[[
Input: {ref, pos, neg}, where ref, neg, pos are tensor 1 x (2*hpatch + 1) x width
]]--
local Net = nn.Sequential()
local comNet = nn.ConcatTable()
Net:add(comNet);
local refPosNet = nn.Sequential()
local refNegNet = nn.Sequential()
local negPosNet = nn.Sequential()
comNet:add(refPosNet)
comNet:add(refNegNet)
comNet:add(negPosNet)
local refPosSelNet = nn.ConcatTable() -- input selectors for each distance net
local refNegSelNet = nn.ConcatTable()
local negPosSelNet = nn.ConcatTable()
refPosNet:add(refPosSelNet)
refNegNet:add(refNegSelNet)
negPosNet:add(negPosSelNet)
refPosSelNet:add(nn.SelectTable(1))
refPosSelNet:add(nn.SelectTable(2))
refNegSelNet:add(nn.SelectTable(1))
refNegSelNet:add(nn.SelectTable(3))
negPosSelNet:add(nn.SelectTable(3))
negPosSelNet:add(nn.SelectTable(2))
local refPosMetricNet = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max)
local refNegMetricNet = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max)
local negPosMetricNet = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max)
refPosNet:add(refPosMetricNet);
refNegNet:add(refNegMetricNet);
negPosNet:add(negPosMetricNet);
Net:add(nn.mil(disp_max));
local milFwdCst = nn.MarginRankingCriterion(loss_margin);
local milBwdCst = nn.MarginRankingCriterion(loss_margin);
local criterion = nn.ParallelCriterion():add(milFwdCst,1):add(milBwdCst,1)
return Net, criterion
end
function trainerNet.getContrastive(disp_max, width, th_sup, loss_margin, embed_net, head_net)
local Net = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max)
Net:add(nn.contrastive(th_sup, disp_max))
local contrastiveFwdCst = nn.MarginRankingCriterion(loss_margin);
local contrastiveBwdCst = nn.MarginRankingCriterion(loss_margin);
local criterion = nn.ParallelCriterion():add(contrastiveFwdCst, 1):add(contrastiveBwdCst, 1)
return Net, criterion
end
function trainerNet.getContrastiveDP(disp_max, width, th_sup, th_occ, loss_margin, embed_net, head_net)
local Net = cnnMetric.setupSiamese(embed_net, head_net, width, disp_max)
Net:add(nn.contrastiveDP(th_sup, th_occ))
local contrastiveFwdCst = nn.MarginRankingCriterion(loss_margin);
local contrastiveBwdCst = nn.MarginRankingCriterion(loss_margin);
local criterion = nn.ParallelCriterion():add(contrastiveFwdCst,1):add(contrastiveBwdCst,1)
return Net, criterion
end
return trainerNet