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 LearnFracationalDepth(LearnAlg,AbsFeaType,AbsFeaDate,WeiBatchNumber,logScale,SkyExclude,LearnNear) |
---|
40 | % % This function learned the distance |
---|
41 | |
---|
42 | global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory... |
---|
43 | ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default... |
---|
44 | Horizon_default filename batchSize NuRow_default WeiBatchSize; |
---|
45 | |
---|
46 | statusFilename = [ClusterExecutionDirectory '/matlabExecutionStatus_depth.txt']; |
---|
47 | % parameters setting |
---|
48 | NuPics = size(filename,2); |
---|
49 | NuBatch = ceil(NuPics/batchSize); |
---|
50 | NuRow = NuRow_default; |
---|
51 | %Horizon = Horizon_default; |
---|
52 | %skyBottom = floor(NuRow/2); |
---|
53 | batchRow = 1:WeiBatchSize:NuRow; |
---|
54 | GassuianRegularization = true; |
---|
55 | RegularWei = 0.01; |
---|
56 | |
---|
57 | l = 1; |
---|
58 | for i = batchRow(WeiBatchNumber):min(batchRow(WeiBatchNumber)+WeiBatchSize-1,NuRow) |
---|
59 | %i=RowNumber; |
---|
60 | % constructing features for each batch of rows from batch featuresa |
---|
61 | load([ScratchDataFolder '/data/FeatureSuperpixel.mat']); % load the feature relate to position and shape of superpixel |
---|
62 | % load estimated sky |
---|
63 | load([ScratchDataFolder '/data/MaskGSky.mat']); % maskg is the estimated ground maskSky is the estimated sky |
---|
64 | l |
---|
65 | FeaVector = []; |
---|
66 | %FeaWei = []; |
---|
67 | DepthVector = []; |
---|
68 | fid = fopen(statusFilename, 'w+'); |
---|
69 | fprintf(fid, 'Currently on row number %i\n', i); |
---|
70 | fclose(fid); %file opening and closing has to be inside the loop, otherwise the file will not appear over afs |
---|
71 | for j = 1:NuBatch |
---|
72 | tic |
---|
73 | load([ScratchDataFolder '/data/feature_Abs_' AbsFeaType int2str(j) '_' AbsFeaDate '.mat']); % 'f' |
---|
74 | %toc |
---|
75 | %for k = trainIndex{j} |
---|
76 | for k = 1:size(f,2)%batchSize |
---|
77 | |
---|
78 | %================== |
---|
79 | % load picsinfo just for the horizontal value |
---|
80 | PicsinfoName = strrep(filename{(j-1)*batchSize+k},'img','picsinfo'); |
---|
81 | temp = dir([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']); |
---|
82 | if size(temp,1) == 0 |
---|
83 | a = a_default; |
---|
84 | b = b_default; |
---|
85 | Ox = Ox_default; |
---|
86 | Oy = Oy_default; |
---|
87 | Horizon = Horizon_default; |
---|
88 | else |
---|
89 | load([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']); |
---|
90 | end |
---|
91 | %================== |
---|
92 | %tic; |
---|
93 | % generate the range of the row for the same thi (weight value) |
---|
94 | RowskyBottom = ceil(NuRow/2); |
---|
95 | PatchSkyBottom = ceil(VertYNuDepth*(1-Horizon)); |
---|
96 | if i <= RowskyBottom |
---|
97 | PatchRowRatio = PatchSkyBottom/RowskyBottom; |
---|
98 | RowTop = ceil((i-1)*PatchRowRatio+1); |
---|
99 | RowBottom = ceil(i*PatchRowRatio); |
---|
100 | else |
---|
101 | PatchRowRatio = (VertYNuDepth-PatchSkyBottom)/(NuRow-RowskyBottom); |
---|
102 | RowTop = ceil((i-RowskyBottom-1)*PatchRowRatio+1)+PatchSkyBottom; |
---|
103 | RowBottom = ceil((i-RowskyBottom)*PatchRowRatio)+PatchSkyBottom; |
---|
104 | end |
---|
105 | ColumnLeft = 1; |
---|
106 | ColumnRight = HoriXNuDepth; |
---|
107 | |
---|
108 | newFea = genFeaVector(f{k},FeatureSuperpixel{(j-1)*batchSize+k},... |
---|
109 | [RowTop:RowBottom],[ColumnLeft:ColumnRight],(j-1)*batchSize+k,LearnNear); |
---|
110 | if SkyExclude == 1 |
---|
111 | maskSkyPics = maskSky{(j-1)*batchSize+k}; |
---|
112 | newFea = newFea(:,~maskSkyPics(RowTop:RowBottom,ColumnLeft:ColumnRight)); |
---|
113 | end |
---|
114 | |
---|
115 | if size(newFea,2)~=0 |
---|
116 | % FeaWei = [FeaWei ones(1,size(newFea,2))/size(newFea,2)]; |
---|
117 | FeaVector =[ FeaVector newFea]; |
---|
118 | depthfile = strrep(filename{(j-1)*batchSize+k},'img','depth_sph_corr'); |
---|
119 | load([ScratchDataFolder '/Gridlaserdata/' depthfile '.mat']); |
---|
120 | newDepth = genDepthVector(Position3DGrid(:,:,4),... |
---|
121 | RowTop,RowBottom,ColumnLeft,ColumnRight,(j-1)*batchSize+k); |
---|
122 | newDepth = newDepth(~maskSkyPics(RowTop:RowBottom,ColumnLeft:ColumnRight),1); |
---|
123 | DepthVector = [DepthVector; newDepth]; |
---|
124 | end |
---|
125 | %DepthVector = [DepthVector genDepthVector(DepthTrueProj{(j-1)*batchSize+k},i,(j-1)*batchSize+k)]; |
---|
126 | %l = l + 1; |
---|
127 | %toc; |
---|
128 | end |
---|
129 | clear f newFea Position3DGrid; |
---|
130 | toc |
---|
131 | end |
---|
132 | |
---|
133 | clear maskg maskSky FeatureSuperpixel maskSkyPics; |
---|
134 | %FeaVector = FeaVector(:,1:round(end*2/3)); |
---|
135 | %DepthVector = DepthVector(1:round(end*2/3),:); |
---|
136 | % learning part |
---|
137 | %X{i} = [ones(size(FeaVector,2),1) FeaVector'] % add offset feature to complete the feature set |
---|
138 | if logScale == 1 |
---|
139 | target{l} = log(DepthVector); |
---|
140 | else |
---|
141 | target{l} = DepthVector;%log(DepthVector); |
---|
142 | end |
---|
143 | clear DepthVector; |
---|
144 | % full feature learninga |
---|
145 | % [thi{l},stats] = robustfit(FeaVector',target{l},'huber'); |
---|
146 | Tsize = size(target{l},1) |
---|
147 | Fsize = size(FeaVector,1)+1 |
---|
148 | whos; |
---|
149 | pack; |
---|
150 | whos; |
---|
151 | MaxEig = max(svd(FeaVector)); |
---|
152 | save([ ScratchDataFolder '/data/MaxEigFraDepthL.mat' ],'MaxEig','RegularWei','FeaVector'); |
---|
153 | return; |
---|
154 | if strcmp(LearnAlg,'L1norm') |
---|
155 | tic |
---|
156 | opt = sdpsettings('solver','sedumi'); |
---|
157 | thit = sdpvar(Fsize,1); |
---|
158 | solvesdp([],norm( ([ones(Tsize,1) FeaVector']./repmat(target{l},[1 Fsize]))*thit - ones(Tsize,1) , 1), opt); |
---|
159 | thi{l} = thit; |
---|
160 | % base line learning |
---|
161 | X_base = ones( Tsize,1); |
---|
162 | tic |
---|
163 | cvx_begin |
---|
164 | cvx_quiet(true); |
---|
165 | variable thit(1); |
---|
166 | minimize (norm( (X_base./target{l})*thit - ones(Tsize,1),1)); |
---|
167 | cvx_end |
---|
168 | thi_base{l} = thit; |
---|
169 | toc |
---|
170 | % error measure |
---|
171 | error{l} = abs( ([ones(Tsize,1) FeaVector']./repmat(target{l},[1 Fsize]))*thi{l} - ones(Tsize,1)); |
---|
172 | error_base{l} = abs( (X_base./target{l})*thi_base{l} - ones(Tsize,1)); |
---|
173 | learnRatio{l} = sum(error{l})/sum(error_base{l}); |
---|
174 | elseif strcmp(LearnAlg,'robustfit') |
---|
175 | if GassuianRegularization |
---|
176 | RegM = diag(ones(Fsize,1)); |
---|
177 | size(RegM) |
---|
178 | [thi{l},stats] = robustfit([ ([ones(Tsize,1) FeaVector']./repmat(target{l},[1 Fsize])) ;RegM*RegularWei*MaxEig], [ones(Tsize,1); zeros(Fsize,1) ], 'huber', 1.345,'off'); |
---|
179 | else |
---|
180 | [thi{l},stats] = robustfit(([ones(Tsize,1) FeaVector']./repmat(target{l},[1 Fsize])), ones(Tsize,1), 'huber', 1.345,'off'); |
---|
181 | end |
---|
182 | % base line learning |
---|
183 | X_base = ones(Tsize,1); |
---|
184 | [thi_temp,stats] = robustfit((X_base./target{l}), ones(Tsize,1), 'huber', 1.345,'off'); |
---|
185 | thi_base{l} = thi_temp(1); |
---|
186 | % error measure |
---|
187 | error{l} = abs( ([ones(Tsize,1) FeaVector']./repmat(target{l},[1 Fsize]))*thi{l} - ones(Tsize,1)); |
---|
188 | error_base{l} = abs( (X_base./target{l})*thi_base{l} - ones(Tsize,1)); |
---|
189 | learnRatio{l} = sum(error{l})/sum(error_base{l}); |
---|
190 | elseif strcmp(LearnAlg,'L2norm') |
---|
191 | thi{l} = ([ones(Tsize,1) FeaVector']./repmat(target{l},[1 Fsize]))\ones(Tsize,1); |
---|
192 | % base line learning |
---|
193 | X_base = ones( Tsize,1); |
---|
194 | thi_base{l} = (X_base./target{l})\ ones(Tsize,1); |
---|
195 | % error measure |
---|
196 | error{l} = abs( ([ones(Tsize,1) FeaVector']./repmat(target{l},[1 Fsize]))*thi{l} - ones(Tsize,1)); |
---|
197 | error_base{l} = abs( (X_base./target{l})*thi_base{l} - ones(Tsize,1)); |
---|
198 | learnRatio{l} = sum(error{l})/sum(error_base{l}); |
---|
199 | end |
---|
200 | |
---|
201 | |
---|
202 | l = l +1; |
---|
203 | DateStamp = date; |
---|
204 | if GassuianRegularization |
---|
205 | save([ScratchDataFolder '/../learned_parameter/FractionalRegDepth_' ImgFolder '_' LearnAlg ... |
---|
206 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
207 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
208 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
209 | 'thi','thi_base','error','error_base','learnRatio'); |
---|
210 | else |
---|
211 | save([ScratchDataFolder '/../learned_parameter/FractionalDepth_' ImgFolder '_' LearnAlg ... |
---|
212 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
213 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
214 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
215 | 'thi','thi_base','error','error_base','learnRatio'); |
---|
216 | end |
---|
217 | end |
---|
218 | |
---|
219 | DateStamp = date; |
---|
220 | if GassuianRegularization |
---|
221 | save([ScratchDataFolder '/../learned_parameter/FractionalRegDepth_' ImgFolder '_' LearnAlg ... |
---|
222 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
223 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
224 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
225 | 'thi','thi_base','error','error_base','learnRatio'); |
---|
226 | else |
---|
227 | save([ScratchDataFolder '/../learned_parameter/FractionalDepth_' ImgFolder '_' LearnAlg ... |
---|
228 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
229 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
230 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
231 | 'thi','thi_base','error','error_base','learnRatio'); |
---|
232 | |
---|
233 | end |
---|
234 | %if logScale == 1 |
---|
235 | % if SkyExclude == 1 |
---|
236 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_Nonsky_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
237 | % else |
---|
238 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
239 | % end |
---|
240 | %else |
---|
241 | % if SkyExclude == 1 |
---|
242 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_Nonsky_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '_linear.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
243 | % else |
---|
244 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '_linear.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
245 | % end |
---|
246 | %end |
---|