1 | % * This code was used in the following articles:
|
---|
2 | % * [1] Learning 3-D Scene Structure from a Single Still Image,
|
---|
3 | % * Ashutosh Saxena, Min Sun, Andrew Y. Ng,
|
---|
4 | % * In ICCV workshop on 3D Representation for Recognition (3dRR-07), 2007.
|
---|
5 | % * (best paper)
|
---|
6 | % * [2] 3-D Reconstruction from Sparse Views using Monocular Vision,
|
---|
7 | % * Ashutosh Saxena, Min Sun, Andrew Y. Ng,
|
---|
8 | % * In ICCV workshop on Virtual Representations and Modeling
|
---|
9 | % * of Large-scale environments (VRML), 2007.
|
---|
10 | % * [3] 3-D Depth Reconstruction from a Single Still Image,
|
---|
11 | % * Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
|
---|
12 | % * International Journal of Computer Vision (IJCV), Aug 2007.
|
---|
13 | % * [6] Learning Depth from Single Monocular Images,
|
---|
14 | % * Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
|
---|
15 | % * In Neural Information Processing Systems (NIPS) 18, 2005.
|
---|
16 | % *
|
---|
17 | % * These articles are available at:
|
---|
18 | % * http://make3d.stanford.edu/publications
|
---|
19 | % *
|
---|
20 | % * We request that you cite the papers [1], [3] and [6] in any of
|
---|
21 | % * your reports that uses this code.
|
---|
22 | % * Further, if you use the code in image3dstiching/ (multiple image version),
|
---|
23 | % * then please cite [2].
|
---|
24 | % *
|
---|
25 | % * If you use the code in third_party/, then PLEASE CITE and follow the
|
---|
26 | % * LICENSE OF THE CORRESPONDING THIRD PARTY CODE.
|
---|
27 | % *
|
---|
28 | % * Finally, this code is for non-commercial use only. For further
|
---|
29 | % * information and to obtain a copy of the license, see
|
---|
30 | % *
|
---|
31 | % * http://make3d.stanford.edu/publications/code
|
---|
32 | % *
|
---|
33 | % * Also, the software distributed under the License is distributed on an
|
---|
34 | % * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
---|
35 | % * express or implied. See the License for the specific language governing
|
---|
36 | % * permissions and limitations under the License.
|
---|
37 | % *
|
---|
38 | % */
|
---|
39 | function LearnDisCts(LearnAlg,HistFeaType,HistFeaDate,AbsFeaType,AbsFeaDate,WeiBatchNumber,logScale,SkyExclude,LearnNear); |
---|
40 | %WeiBatchNumber,logScale,SkyExclude) |
---|
41 | % % This function learned the distance |
---|
42 | |
---|
43 | global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory... |
---|
44 | ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default... |
---|
45 | Horizon_default filename batchSize NuRow_default WeiBatchSize; |
---|
46 | |
---|
47 | statusFilename = [ClusterExecutionDirectory '/matlabExecutionStatus_ratio.txt']; |
---|
48 | % parameters setting |
---|
49 | NuPics = size(filename,2); |
---|
50 | NuBatch = ceil(NuPics/batchSize); |
---|
51 | NuRow = NuRow_default; |
---|
52 | % =================== can be modified later============== |
---|
53 | SpiatalDisCTSThresh = log(5); |
---|
54 | % ======================================================= |
---|
55 | %Horizon = Horizon_default; |
---|
56 | %skyBottom = floor(NuRow/2); |
---|
57 | batchRow = 1:WeiBatchSize:NuRow; |
---|
58 | |
---|
59 | % constructing features for each batch of rows from batch featuresa |
---|
60 | %load([ScratchDataFolder '/data/FeatureSuperpixel.mat']); % load the feature relate to position and shape of superpixel |
---|
61 | % load estimated sky |
---|
62 | load([ScratchDataFolder '/data/MaskGSky.mat']); % maskg is the estimated ground maskSky is the estimated sky |
---|
63 | % load data |
---|
64 | load([ScratchDataFolder '/data/LowResImgIndexSuperpixelSep.mat']); % LowResImgIndexSuperpixelSep |
---|
65 | load([ScratchDataFolder '/data/DiffLowResImgIndexSuperpixelSep.mat']); % DiffLowResImgIndexSuperpixelSep(medi$large) |
---|
66 | %load([ScratchDataFolder '/data/TextLowResImgIndexSuperpixelSep.mat']); % TextLowResImgIndexSuperpixelSep using Textrea |
---|
67 | |
---|
68 | l = 1; |
---|
69 | for i = batchRow(WeiBatchNumber):min(batchRow(WeiBatchNumber)+WeiBatchSize-1,NuRow) |
---|
70 | %i = NuRow |
---|
71 | l |
---|
72 | FeaVectorH = []; |
---|
73 | FeaVectorV = []; |
---|
74 | % FeaWeiH = []; |
---|
75 | % FeaWeiV = []; |
---|
76 | DepthDisCtsVectorH = []; |
---|
77 | DepthDisCtsVectorV = []; |
---|
78 | fid = fopen(statusFilename, 'w+'); |
---|
79 | fprintf(fid, 'Currently on row number %i\n', i); |
---|
80 | fclose(fid); %file opening and closing has to be inside the loop, otherwise the file will not appear over afs |
---|
81 | for j = 1:NuBatch |
---|
82 | tic |
---|
83 | load([ScratchDataFolder '/data/feature_Abs_' AbsFeaType int2str(j) '_' AbsFeaDate '.mat']); % 'f' |
---|
84 | % load([ScratchDataFolder '/data/feature_Hist_' HistFeaType int2str(j) '_' HistFeaDate '.mat']); % 'f' |
---|
85 | %toc |
---|
86 | %for k = trainIndex{j} |
---|
87 | for k = 1:size(f,2)%batchSize |
---|
88 | |
---|
89 | %================== |
---|
90 | % load picsinfo just for the horizontal value |
---|
91 | PicsinfoName = strrep(filename{(j-1)*batchSize+k},'img','picsinfo'); |
---|
92 | temp = dir([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']); |
---|
93 | if size(temp,1) == 0 |
---|
94 | a = a_default; |
---|
95 | b = b_default; |
---|
96 | Ox = Ox_default; |
---|
97 | Oy = Oy_default; |
---|
98 | Horizon = Horizon_default; |
---|
99 | else |
---|
100 | load([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']); |
---|
101 | end |
---|
102 | |
---|
103 | % load the depths |
---|
104 | depthfile = strrep(filename{(j-1)*batchSize+k},'img','depth_sph_corr'); |
---|
105 | load([ScratchDataFolder '/Gridlaserdata/' depthfile '.mat']); |
---|
106 | %================== |
---|
107 | %tic; |
---|
108 | % generate the range of the row for the same thi (weight value) |
---|
109 | RowskyBottom = ceil(NuRow/2); |
---|
110 | PatchSkyBottom = ceil(VertYNuDepth*(1-Horizon)); |
---|
111 | if i <= RowskyBottom |
---|
112 | PatchRowRatio = PatchSkyBottom/RowskyBottom; |
---|
113 | RowTop = ceil((i-1)*PatchRowRatio+1); |
---|
114 | RowBottom = ceil(i*PatchRowRatio); |
---|
115 | else |
---|
116 | PatchRowRatio = (VertYNuDepth-PatchSkyBottom)/(NuRow-RowskyBottom); |
---|
117 | RowTop = ceil((i-RowskyBottom-1)*PatchRowRatio+1)+PatchSkyBottom; |
---|
118 | RowBottom = ceil((i-RowskyBottom)*PatchRowRatio)+PatchSkyBottom; |
---|
119 | end |
---|
120 | ColumnLeft = 1; |
---|
121 | ColumnRight = HoriXNuDepth; |
---|
122 | |
---|
123 | % generate the DisCts |
---|
124 | [DisCtsH DisCtsV] = DisCtsDetect(Position3DGrid(:,:,4)); |
---|
125 | % pick out the 1st end-1 end column and row |
---|
126 | rangeYV = RowTop:RowBottom; |
---|
127 | rangeYH = ColumnLeft:ColumnRight; |
---|
128 | %rangeYV = setdiff(rangeYV,[1 VertYNuDepth-1 VertYNuDepth]); |
---|
129 | rangeXV = RowTop:RowBottom; |
---|
130 | rangeXH = ColumnLeft:ColumnRight; |
---|
131 | %rangeXH = setdiff(rangeXH,[1 HoriXNuDepth-1 HoriXNuDepth]); |
---|
132 | temp = DisCtsV(rangeYV,rangeYH); |
---|
133 | DepthDisCtsVectorV = [DepthDisCtsVectorV; temp(:)]; |
---|
134 | temp = DisCtsH(rangeXV,rangeXH); |
---|
135 | DepthDisCtsVectorH = [DepthDisCtsVectorH; temp(:)]; |
---|
136 | |
---|
137 | % pick out the features in the right rows |
---|
138 | newFeaH = genFeaVector(f{k},[],... |
---|
139 | rangeXV,rangeXH,(j-1)*batchSize+k,LearnNear,[1]); |
---|
140 | newFeaH = abs(newFeaH - genFeaVector(f{k},[],... |
---|
141 | rangeXV,(rangeXH+1),(j-1)*batchSize+k,LearnNear,[1])); |
---|
142 | newFeaV = genFeaVector(f{k},[],... |
---|
143 | rangeYV,rangeYH,(j-1)*batchSize+k,LearnNear,[1]); |
---|
144 | newFeaV = abs(newFeaV - genFeaVector(f{k},[],... |
---|
145 | (rangeYV+1),rangeYH,(j-1)*batchSize+k,LearnNear,[1])); |
---|
146 | % ================================== |
---|
147 | % setting the estimated Ground Verticl Sky segmentation (generated from CMU's code) |
---|
148 | maskEstGVS = 2*ones(VertYNuDepth,HoriXNuDepth); |
---|
149 | maskEstGVS(maskg{i}) = 1; |
---|
150 | maskEstGVS(maskSky{i}) = 3; |
---|
151 | |
---|
152 | % generate segmentation features |
---|
153 | NuSupType = size(LowResImgIndexSuperpixelSep,2)+size(DiffLowResImgIndexSuperpixelSep,2); |
---|
154 | %+size(TextLowResImgIndexSuperpixelSep,2)*size(TextLowResImgIndexSuperpixelSep,3); |
---|
155 | |
---|
156 | for j = 1:NuSupType % total 21 seg : 3 RGB 6*3= 18 texture filters |
---|
157 | |
---|
158 | % pick the specific segmentation |
---|
159 | if j==1 |
---|
160 | sup = LowResImgIndexSuperpixelSep{i,1}; |
---|
161 | elseif j<=3 |
---|
162 | sup = DiffLowResImgIndexSuperpixelSep{i,j-1}; |
---|
163 | else |
---|
164 | Subm = mod((j-3),6); |
---|
165 | if Subm==0 |
---|
166 | Subm=6; |
---|
167 | end |
---|
168 | sup = TextLowResImgIndexSuperpixelSep{i,Subm,ceil((j-3)/6)}; |
---|
169 | end |
---|
170 | |
---|
171 | % extend the estimated maskGVS to the new segmentation |
---|
172 | NewSupInd = (unique(sup))'; |
---|
173 | NewEstGSup = zeros(VertYNuDepth,HoriXNuDepth); |
---|
174 | NewEstVSup = zeros(VertYNuDepth,HoriXNuDepth); |
---|
175 | NewEstSSup = zeros(VertYNuDepth,HoriXNuDepth); |
---|
176 | for m = NewSupInd |
---|
177 | mask = sup == m; |
---|
178 | if any(maskEstGVS(mask)==1) && any(maskEstGVS(mask)==3) |
---|
179 | GVSInd = analysesupinpatch(maskEstGVS(mask)); |
---|
180 | elseif any(maskEstGVS(mask)==1) |
---|
181 | GVSInd =1; |
---|
182 | elseif any(maskEstGVS(mask)==3) |
---|
183 | GVSInd =3; |
---|
184 | else |
---|
185 | GVSInd =2; |
---|
186 | end |
---|
187 | %GVSInd = analysesupinpatch(maskEstGVS(mask)); |
---|
188 | if GVSInd == 1 |
---|
189 | NewEstGSup(mask) = m; |
---|
190 | NewEstVSup(mask) = 0; |
---|
191 | NewEstSSup(mask) = 0; |
---|
192 | elseif GVSInd == 2 |
---|
193 | NewEstVSup(mask) = m; |
---|
194 | NewEstGSup(mask) = -1; |
---|
195 | NewEstSSup(mask) = -1; |
---|
196 | else |
---|
197 | NewEstSSup(mask) = m; |
---|
198 | NewEstGSup(mask) = -2; |
---|
199 | NewEstVSup(mask) = -2; |
---|
200 | end |
---|
201 | end |
---|
202 | %if j == 2 |
---|
203 | % SpreadFactor = gen_SFactor(LearnedDepth,sup,Rz); |
---|
204 | %end |
---|
205 | %clear LowResImgIndexSuperpixelSep; |
---|
206 | |
---|
207 | % 2nd order smooth |
---|
208 | % [SecXG(j,:) SecYG(j,:)]= gen_2ndSmooth(NewEstGSup); |
---|
209 | % [SecXV(j,:) SecYV(j,:)]= gen_2ndSmooth(NewEstVSup); |
---|
210 | % [SecXS(j,:) SecYS(j,:)]= gen_2ndSmooth(NewEstSSup); |
---|
211 | |
---|
212 | % 1st order smooth |
---|
213 | [FirstYG(j,:) FirstXG(j,:)] = gen_1stSmooth(NewEstGSup); |
---|
214 | [FirstYV(j,:) FirstXV(j,:)] = gen_1stSmooth(NewEstVSup); |
---|
215 | [FirstYS(j,:) FirstXS(j,:)] = gen_1stSmooth(NewEstSSup); |
---|
216 | FirstY = FirstYG + FirstYV + FirstYS; |
---|
217 | FirstX = FirstXG + FirstXV + FirstXS; |
---|
218 | |
---|
219 | %[GPy{j} ] = gen_GravityP_vertical(maskV); |
---|
220 | %[PlanePriorX PlanePriorY]= gen_PlanePrior(LowResImgIndexSuperpixelSep{i,1}); |
---|
221 | end |
---|
222 | |
---|
223 | %size(newFeaH) |
---|
224 | % add on the Hist features |
---|
225 | %SizeHisFea = size(RFVector{k},3); |
---|
226 | %HistFea = genHisFeaVector(reshape(RFVector{k},[],SizeHisFea),... |
---|
227 | % RowTop,RowBottom,ColumnLeft,ColumnRight,(j-1)*batchSize+k); |
---|
228 | %rank(HistFea) |
---|
229 | %newFeaH = [newFeaH; HistFea]; |
---|
230 | %newFeaV = [newFeaV; HistFea]; |
---|
231 | |
---|
232 | % finally storage all the data |
---|
233 | [Ix Iy] = meshgrid(rangeXH,rangeXV); |
---|
234 | Index = sub2ind([VertYNuDepth, HoriXNuDepth], Iy(:), Ix(:)); |
---|
235 | FeaVectorH =[ FeaVectorH [newFeaH; FirstX(:,Index) ]]; |
---|
236 | FeaVectorV =[ FeaVectorV [newFeaV; FirstY(:,Index) ]]; |
---|
237 | end |
---|
238 | % get rid of the sky region |
---|
239 | clear f newFeaH newFeaV DisCtsH DisCtsV Position3DGrid; |
---|
240 | toc |
---|
241 | end |
---|
242 | |
---|
243 | % learning part |
---|
244 | targetH{l} = DepthDisCtsVectorH(:); |
---|
245 | targetV{l} = DepthDisCtsVectorV(:); |
---|
246 | size(FeaVectorH) |
---|
247 | TsizeH = size(targetH{l},1) |
---|
248 | FsizeH = size(FeaVectorH,1)+1 |
---|
249 | TsizeV = size(targetV{l},1) |
---|
250 | FsizeV = size(FeaVectorV,1)+1 |
---|
251 | % calculate the weight to even the bias of much more spatial smooth data |
---|
252 | wetH = ones(TsizeH,1); |
---|
253 | wetH(targetH{l}==1) = sum(targetH{l}==0)./sum(targetH{l}==1); |
---|
254 | wetV = ones(TsizeV,1); |
---|
255 | wetH(targetV{l}==1) = sum(targetV{l}==0)./sum(targetV{l}==1); |
---|
256 | |
---|
257 | |
---|
258 | |
---|
259 | % start learning ======================================== |
---|
260 | if strcmp(LearnAlg,'Logit') |
---|
261 | size([targetH{l} ones(TsizeH,1)]) |
---|
262 | [thiH{l}] = glmfit(FeaVectorH', [targetH{l} ones(TsizeH,1)],'binomial', 'link', 'logit','weights',wetH); |
---|
263 | X_baseH = ones(TsizeH,1); |
---|
264 | [thi_baseH{l}] = glmfit(X_baseH, [targetH{l} ones(TsizeH,1)],'binomial', 'link', 'logit','weights',wetH); |
---|
265 | temp = targetH{l}; |
---|
266 | PfitH = glmval(thiH{l},FeaVectorH' , 'logit')>=0.5; |
---|
267 | FNH{l} = sum(PfitH(logical(temp))==0)./sum(logical(temp)); |
---|
268 | FPH{l} = sum(PfitH(~logical(temp))==1)./sum(~logical(temp)); |
---|
269 | Pfit_baseH = glmval(thi_baseH{l},X_baseH , 'logit') >= 0.5; |
---|
270 | FN_baseH{l} = sum(Pfit_baseH(logical(temp))==0)./sum(logical(temp)); |
---|
271 | FP_baseH{l} = sum(Pfit_baseH(~logical(temp))==1)./sum(~logical(temp)); |
---|
272 | if TsizeV ~=0 |
---|
273 | [thiV{l}] = glmfit(FeaVectorV', [targetV{l} ones(TsizeV,1)],'binomial', 'link', 'logit','weights',wetH); |
---|
274 | X_baseV = ones(TsizeV,1); |
---|
275 | [thi_baseV{l}] = glmfit(X_baseV, [targetV{l} ones(TsizeV,1)],'binomial', 'link', 'logit','weights',wetH); |
---|
276 | temp = targetV{l}; |
---|
277 | PfitV = glmval(thiV{l},FeaVectorV' , 'logit')>=0.5; |
---|
278 | FNV{l} = sum(PfitV(logical(temp))==0)./sum(logical(temp)); |
---|
279 | FPV{l} = sum(PfitV(~logical(temp))==1)./sum(~logical(temp)); |
---|
280 | Pfit_baseV = glmval(thi_baseV{l},X_baseV , 'logit') >= 0.5; |
---|
281 | FN_baseV{l} = sum(Pfit_baseV(logical(temp))==0)./sum(logical(temp)); |
---|
282 | FP_baseV{l} = sum(Pfit_baseV(~logical(temp))==1)./sum(~logical(temp)); |
---|
283 | else |
---|
284 | [thiV{l}] = [thiH{l}]; |
---|
285 | [thi_baseV{l}] = [thi_baseH{l}]; |
---|
286 | targetV{l} = targetH{l}; |
---|
287 | X_baseV = ones(TsizeH,1); |
---|
288 | PfitV = PfitH; |
---|
289 | FPV{l} = FPH{l}; |
---|
290 | FNV{l} = FNH{l}; |
---|
291 | Pfit_baseV = Pfit_baseH; |
---|
292 | FP_baseV{l} = FP_baseH{l}; |
---|
293 | FN_baseV{l} = FN_baseH{l}; |
---|
294 | end |
---|
295 | |
---|
296 | % calculate error |
---|
297 | |
---|
298 | elseif strcmp(LearnAlg,'GDA') |
---|
299 | elseif strcmp(LearnAlg,'SVM') |
---|
300 | end |
---|
301 | % end learning ============================================================================== |
---|
302 | l = l +1; |
---|
303 | DateStamp = date; |
---|
304 | save([ScratchDataFolder '/../learned_parameter/DisCts_' ImgFolder '_' LearnAlg ... |
---|
305 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
306 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
307 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_' ... |
---|
308 | HistFeaType '_HistFeaDate' HistFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
309 | 'thiH','thiV','thi_baseH','thi_baseH','FPH','FPV','FNH','FNV','FP_baseH','FP_baseV','FN_baseH','FN_baseV'); |
---|
310 | end |
---|
311 | |
---|
312 | DateStamp = date; |
---|
313 | save([ScratchDataFolder '/../learned_parameter/DisCts_' ImgFolder '_' LearnAlg ... |
---|
314 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
315 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
316 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_' ... |
---|
317 | HistFeaType '_HistFeaDate' HistFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
318 | 'thiH','thiV','thi_baseH','thi_baseH','FPH','FPV','FNH','FNV','FP_baseH','FP_baseV','FN_baseH','FN_baseV'); |
---|
319 | %if logScale == 1 |
---|
320 | % if SkyExclude == 1 |
---|
321 | % save([ScratchDataFolder '/../learned_parameter/DisCts_' ImgFolder '_' LearnAlg '_Nonsky_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_' HistFeaType '_HistFeaDate' HistFeaDate '_LearnDate' DateStamp '.mat'],'thiH','thiV','thi_baseH','thi_baseH','errorH','errorV','error_baseH','error_baseV','learnRatioH','learnRatioV'); |
---|
322 | % else |
---|
323 | % save([ScratchDataFolder '/../learned_parameter/DisCts_' ImgFolder '_' LearnAlg '_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_' HistFeaType '_HistFeaDate' HistFeaDate '_LearnDate' DateStamp '.mat'],'thiH','thiV','thi_baseH','thi_baseH','errorH','errorV','error_baseH','error_baseV','learnRatioH','learnRatioV'); |
---|
324 | % end |
---|
325 | %else |
---|
326 | % if SkyExclude == 1 |
---|
327 | % save([ScratchDataFolder '/../learned_parameter/DisCts_' ImgFolder '_' LearnAlg '_Nonsky_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_' HistFeaType '_HistFeaDate' HistFeaDate '_LearnDate' DateStamp '_linear.mat'],'thiH','thiV','thi_baseH','thi_baseH','errorH','errorV','error_baseH','error_baseV','learnRatioH','learnRatioV'); |
---|
328 | % else |
---|
329 | % save([ScratchDataFolder '/../learned_parameter/DisCts_' ImgFolder '_' LearnAlg '_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_' HistFeaType '_HistFeaDate' HistFeaDate '_LearnDate' DateStamp '_linear.mat'],'thiH','thiV','thi_baseH','thi_baseH','errorH','errorV','error_baseH','error_baseV','learnRatioH','learnRatioV'); |
---|
330 | % end |
---|
331 | %end |
---|