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 LearnVariance(LearnAlg,AbsFeaType,AbsFeaDate,WeiBatchNumber,logScale,SkyExclude,LearnNear,DepthDirectory) |
---|
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 | |
---|
55 | |
---|
56 | l = 1; |
---|
57 | for i = batchRow(WeiBatchNumber):min(batchRow(WeiBatchNumber)+WeiBatchSize-1,NuRow) |
---|
58 | %for i = 34:35 |
---|
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 | DepthVectorRobust = []; |
---|
69 | fid = fopen(statusFilename, 'w+'); |
---|
70 | fprintf(fid, 'Currently on row number %i\n', i); |
---|
71 | fclose(fid); %file opening and closing has to be inside the loop, otherwise the file will not appear over afs |
---|
72 | for j = 1:NuBatch |
---|
73 | tic |
---|
74 | load([ScratchDataFolder '/data/feature_Abs_' AbsFeaType int2str(j) '_' AbsFeaDate '.mat']); % 'f' |
---|
75 | %toc |
---|
76 | %for k = trainIndex{j} |
---|
77 | for k = 1:size(f,2)%batchSize |
---|
78 | |
---|
79 | %================== |
---|
80 | % load picsinfo just for the horizontal value |
---|
81 | PicsinfoName = strrep(filename{(j-1)*batchSize+k},'img','picsinfo'); |
---|
82 | temp = dir([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']); |
---|
83 | if size(temp,1) == 0 |
---|
84 | a = a_default; |
---|
85 | b = b_default; |
---|
86 | Ox = Ox_default; |
---|
87 | Oy = Oy_default; |
---|
88 | Horizon = Horizon_default; |
---|
89 | else |
---|
90 | load([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']); |
---|
91 | end |
---|
92 | %================== |
---|
93 | %tic; |
---|
94 | % generate the range of the row for the same thi (weight value) |
---|
95 | RowskyBottom = ceil(NuRow/2); |
---|
96 | PatchSkyBottom = ceil(VertYNuDepth*(1-Horizon)); |
---|
97 | if i <= RowskyBottom |
---|
98 | PatchRowRatio = PatchSkyBottom/RowskyBottom; |
---|
99 | RowTop = ceil((i-1)*PatchRowRatio+1); |
---|
100 | RowBottom = ceil(i*PatchRowRatio); |
---|
101 | else |
---|
102 | PatchRowRatio = (VertYNuDepth-PatchSkyBottom)/(NuRow-RowskyBottom); |
---|
103 | RowTop = ceil((i-RowskyBottom-1)*PatchRowRatio+1)+PatchSkyBottom; |
---|
104 | RowBottom = ceil((i-RowskyBottom)*PatchRowRatio)+PatchSkyBottom; |
---|
105 | end |
---|
106 | ColumnLeft = 1; |
---|
107 | ColumnRight = HoriXNuDepth; |
---|
108 | |
---|
109 | newFea = genFeaVector(f{k},FeatureSuperpixel{(j-1)*batchSize+k},... |
---|
110 | [RowTop:RowBottom],[ColumnLeft:ColumnRight],(j-1)*batchSize+k,LearnNear); |
---|
111 | if SkyExclude == 1 |
---|
112 | maskSkyPics = maskSky{(j-1)*batchSize+k}; |
---|
113 | newFea = newFea(:,~maskSkyPics(RowTop:RowBottom,ColumnLeft:ColumnRight)); |
---|
114 | end |
---|
115 | |
---|
116 | if size(newFea,2)~=0 |
---|
117 | % load laserdepth |
---|
118 | depthfile = strrep(filename{(j-1)*batchSize+k},'img','depth_sph_corr'); |
---|
119 | load([ScratchDataFolder '/Gridlaserdata/' depthfile '.mat']); |
---|
120 | newDepthLaser = genDepthVector(Position3DGrid(:,:,4),... |
---|
121 | RowTop,RowBottom,ColumnLeft,ColumnRight,(j-1)*batchSize+k); |
---|
122 | newDepthLaser = newDepthLaser(~maskSkyPics(RowTop:RowBottom,ColumnLeft:ColumnRight),1); |
---|
123 | % load learned depth |
---|
124 | depthfile = strrep(filename{i},'img','depth_learned'); |
---|
125 | load([DepthDirectory '/' depthfile '.mat']); |
---|
126 | newDepthLearn = genDepthVector(depthMap,... |
---|
127 | RowTop,RowBottom,ColumnLeft,ColumnRight,(j-1)*batchSize+k); |
---|
128 | newDepthLearn = newDepthLearn(~maskSkyPics(RowTop:RowBottom,ColumnLeft:ColumnRight),1); |
---|
129 | |
---|
130 | newFraDepth = abs( (newDepthLaser - newDepthLearn )./ newDepthLaser)+1; |
---|
131 | newFraDepthRobust = (newDepthLaser - newDepthLearn )./ newDepthLaser; |
---|
132 | % with 1 offset to keep log of newFraDepth positive |
---|
133 | % get rid of Inf in newFraDepth |
---|
134 | MaskInf = isinf(newFraDepth); |
---|
135 | %sum(MaskInf) |
---|
136 | newFraDepth = newFraDepth(~MaskInf); |
---|
137 | % FeaWei = [FeaWei ones(1,size(newFea,2))/size(newFea,2)]; |
---|
138 | newFea = newFea(:,~MaskInf); |
---|
139 | FeaVector =[ FeaVector newFea]; |
---|
140 | DepthVector = [DepthVector; newFraDepth]; |
---|
141 | DepthVectorRobust = [DepthVectorRobust; newFraDepthRobust]; |
---|
142 | end |
---|
143 | %DepthVector = [DepthVector genDepthVector(DepthTrueProj{(j-1)*batchSize+k},i,(j-1)*batchSize+k)]; |
---|
144 | %l = l + 1; |
---|
145 | %toc; |
---|
146 | end |
---|
147 | clear f newFea Position3DGrid; |
---|
148 | toc |
---|
149 | end |
---|
150 | |
---|
151 | clear maskg maskSky FeatureSuperpixel maskSkyPics; |
---|
152 | %FeaVector = FeaVector(:,1:round(end*2/3)); |
---|
153 | %DepthVector = DepthVector(1:round(end*2/3),:); |
---|
154 | % learning part |
---|
155 | %X{i} = [ones(size(FeaVector,2),1) FeaVector'] % add offset feature to complete the feature set |
---|
156 | if logScale == 1 |
---|
157 | target{l} = log(DepthVector); |
---|
158 | targetRobust{l} = log(DepthVectorRobust); |
---|
159 | else |
---|
160 | target{l} = DepthVector;%log(DepthVector); |
---|
161 | targetRobust{l} = DepthVectorRobust;%log(DepthVector); |
---|
162 | end |
---|
163 | clear DepthVector; |
---|
164 | % whos; |
---|
165 | % pack; |
---|
166 | % whos; |
---|
167 | % pause; |
---|
168 | % full feature learninga |
---|
169 | % [thi{l},stats] = robustfit(FeaVector',target{l},'huber'); |
---|
170 | Tsize = size(target{l},1) |
---|
171 | Fsize = size(FeaVector,1)+1 |
---|
172 | % A = [-[ones(Tsize,1) FeaVector'] [ones(Tsize,1) FeaVector'] -speye(Tsize) speye(Tsize) sparse(Tsize,Tsize);... |
---|
173 | % -[ones(Tsize,1) FeaVector'] [ones(Tsize,1) FeaVector'] +speye(Tsize) sparse(Tsize,Tsize) -speye(Tsize)]; |
---|
174 | % bb = [-target{i};-target{i}]; |
---|
175 | % cc = [sparse(Fsize*2,1); ones(Tsize,1); sparse(Tsize*2,1)]; |
---|
176 | if strcmp(LearnAlg,'L1norm') |
---|
177 | % ================== yalmip |
---|
178 | opt = sdpsettings('solver','sedumi'); |
---|
179 | thit = sdpvar(Fsize,1); |
---|
180 | F = set(thit >= 0); |
---|
181 | solvesdp(F,norm([ones(Tsize,1) FeaVector']*thit - target{l},1 ), opt); |
---|
182 | thit = double(thit); |
---|
183 | thi{l} = thit; |
---|
184 | % base line learning |
---|
185 | X_base = ones(size(FeaVector,2),1); |
---|
186 | thit_base = sdpvar(1,1); |
---|
187 | F = set(thit_base >= 0); |
---|
188 | solvesdp(F,norm(X_base*thit - target{l} ,1), opt); |
---|
189 | thit_base = double(thit_base); |
---|
190 | thi_base{l} = thit_base; |
---|
191 | % ======================== |
---|
192 | % error measure |
---|
193 | error{l} = ( abs( (target{l} - [ones(size(FeaVector,2),1) FeaVector']*thi{l})) ); |
---|
194 | error_base{l} = ( abs( (target{l} - X_base*thi_base{l})) ); |
---|
195 | learnRatio{l} = sum(error{l})/sum(error_base{l}); |
---|
196 | elseif strcmp(LearnAlg,'robustfit') |
---|
197 | tic; |
---|
198 | [thi{l},stats] = robustfit(FeaVector',targetRobust{l},'huber'); |
---|
199 | toc; |
---|
200 | % pause; |
---|
201 | % base line learning |
---|
202 | X_base = ones(size(FeaVector,2),1); |
---|
203 | [thi_temp,stats] = robustfit(X_base,targetRobust{l},'huber'); |
---|
204 | thi_base{l} = thi_temp(1); |
---|
205 | % error measure |
---|
206 | error{l} = ( abs( (targetRobust{l} - [ones(size(FeaVector,2),1) FeaVector']*thi{l})) ); |
---|
207 | error_base{l} = ( abs( (targetRobust{l} - X_base*thi_base{l})) ); |
---|
208 | learnRatio{l} = sum(error{l})/sum(error_base{l}); |
---|
209 | elseif strcmp(LearnAlg,'L2norm') |
---|
210 | % ================== yalmip |
---|
211 | opt = sdpsettings('solver','sedumi'); |
---|
212 | thit = sdpvar(Fsize,1); |
---|
213 | F = set(thit >= 0); |
---|
214 | solvesdp(F,norm([ones(Tsize,1) FeaVector']*thit - target{l} ), opt); |
---|
215 | thit = double(thit); |
---|
216 | thi{l} = thit; |
---|
217 | % base line learning |
---|
218 | X_base = ones(size(FeaVector,2),1); |
---|
219 | thit_base = sdpvar(1,1); |
---|
220 | F = set(thit_base >= 0); |
---|
221 | solvesdp(F,norm(X_base*thit_base - target{l} ), opt); |
---|
222 | thit_base = double(thit_base); |
---|
223 | thi_base{l} = thit_base; |
---|
224 | % ======================== |
---|
225 | % thi{l} = [ones(Tsize,1) FeaVector']\target{l}; |
---|
226 | % base line learning |
---|
227 | % X_base = ones(size(FeaVector,2),1); |
---|
228 | % thi_base{l} = X_base\target{l}; |
---|
229 | % error measure |
---|
230 | error{l} = ( ( (target{l} - [ones(size(FeaVector,2),1) FeaVector']*thi{l})).^2 ); |
---|
231 | error_base{l} = ( ( (target{l} - X_base*thi_base{l})).^2 ); |
---|
232 | learnRatio{l} = sqrt(sum(error{l})/sum(error_base{l})); |
---|
233 | end |
---|
234 | |
---|
235 | |
---|
236 | l = l +1; |
---|
237 | % change variable name |
---|
238 | nu = thi; |
---|
239 | nu_base = thi_base; |
---|
240 | |
---|
241 | DateStamp = date; |
---|
242 | save([ScratchDataFolder '/../learned_parameter/Variance/Var_' ImgFolder '_' LearnAlg ... |
---|
243 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
244 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
245 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
246 | 'nu','nu_base','error','error_base','learnRatio'); |
---|
247 | end |
---|
248 | |
---|
249 | DateStamp = date; |
---|
250 | save([ScratchDataFolder '/../learned_parameter/Variance/Var_' ImgFolder '_' LearnAlg ... |
---|
251 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
252 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
253 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
254 | 'nu','nu_base','error','error_base','learnRatio'); |
---|
255 | %if logScale == 1 |
---|
256 | % if SkyExclude == 1 |
---|
257 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_Nonsky_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
258 | % else |
---|
259 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
260 | % end |
---|
261 | %else |
---|
262 | % if SkyExclude == 1 |
---|
263 | % 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'); |
---|
264 | % else |
---|
265 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '_linear.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
266 | % end |
---|
267 | %end |
---|