[37] | 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 LearnDepth(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 | |
---|
| 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 | 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 | if SkyExclude == 1 |
---|
| 123 | newDepth = newDepth(~maskSkyPics(RowTop:RowBottom,ColumnLeft:ColumnRight),1); |
---|
| 124 | end |
---|
| 125 | DepthVector = [DepthVector; newDepth]; |
---|
| 126 | end |
---|
| 127 | %DepthVector = [DepthVector genDepthVector(DepthTrueProj{(j-1)*batchSize+k},i,(j-1)*batchSize+k)]; |
---|
| 128 | %l = l + 1; |
---|
| 129 | %toc; |
---|
| 130 | end |
---|
| 131 | clear f newFea Position3DGrid; |
---|
| 132 | toc |
---|
| 133 | end |
---|
| 134 | |
---|
| 135 | clear maskg maskSky FeatureSuperpixel maskSkyPics; |
---|
| 136 | %FeaVector = FeaVector(:,1:round(end*2/3)); |
---|
| 137 | %DepthVector = DepthVector(1:round(end*2/3),:); |
---|
| 138 | % learning part |
---|
| 139 | %X{i} = [ones(size(FeaVector,2),1) FeaVector'] % add offset feature to complete the feature set |
---|
| 140 | if logScale == 1 |
---|
| 141 | target{l} = log(DepthVector); |
---|
| 142 | else |
---|
| 143 | target{l} = DepthVector;%log(DepthVector); |
---|
| 144 | end |
---|
| 145 | clear DepthVector; |
---|
| 146 | % whos; |
---|
| 147 | % pack; |
---|
| 148 | % whos; |
---|
| 149 | % pause; |
---|
| 150 | % full feature learninga |
---|
| 151 | % [thi{l},stats] = robustfit(FeaVector',target{l},'huber'); |
---|
| 152 | Tsize = size(target{l},1) |
---|
| 153 | Fsize = size(FeaVector,1)+1 |
---|
| 154 | % A = [-[ones(Tsize,1) FeaVector'] [ones(Tsize,1) FeaVector'] -speye(Tsize) speye(Tsize) sparse(Tsize,Tsize);... |
---|
| 155 | % -[ones(Tsize,1) FeaVector'] [ones(Tsize,1) FeaVector'] +speye(Tsize) sparse(Tsize,Tsize) -speye(Tsize)]; |
---|
| 156 | % bb = [-target{i};-target{i}]; |
---|
| 157 | % cc = [sparse(Fsize*2,1); ones(Tsize,1); sparse(Tsize*2,1)]; |
---|
| 158 | if strcmp(LearnAlg,'L1norm') |
---|
| 159 | tic |
---|
| 160 | cvx_begin |
---|
| 161 | cvx_quiet(false); |
---|
| 162 | % variable thit(Fsize,1); |
---|
| 163 | % variable k(Tsize,1); |
---|
| 164 | % variable alpha(Tsize,1); |
---|
| 165 | % variable beta(Tsize,1); |
---|
| 166 | % minimize(ones(1,Tsize)*k); |
---|
| 167 | % A*[thit; k; alpha; beta]==B; |
---|
| 168 | % alpha>=0; |
---|
| 169 | % beta<=0; |
---|
| 170 | [x] = sedumi([-[ones(Tsize,1) FeaVector'] [ones(Tsize,1) FeaVector'] -speye(Tsize) speye(Tsize) sparse(Tsize,Tsize);... |
---|
| 171 | -[ones(Tsize,1) FeaVector'] [ones(Tsize,1) FeaVector'] +speye(Tsize) sparse(Tsize,Tsize) -speye(Tsize)],... |
---|
| 172 | [-target{i};-target{i}],[sparse(Fsize*2,1); ones(Tsize,1); sparse(Tsize*2,1)]); |
---|
| 173 | cvx_end |
---|
| 174 | thi{l} = x(1:Fsize,1)-x((Fsize+1):2*Fsize,1); |
---|
| 175 | % base line learning |
---|
| 176 | X_base = ones(size(FeaVector,2),1); |
---|
| 177 | tic |
---|
| 178 | cvx_begin |
---|
| 179 | cvx_quiet(true); |
---|
| 180 | variable thit(1); |
---|
| 181 | minimize (norm((target{l} - X_base*thit),1)); |
---|
| 182 | cvx_end |
---|
| 183 | thi_base{l} = thit; |
---|
| 184 | toc |
---|
| 185 | % error measure |
---|
| 186 | error{l} = ( abs( (target{l} - [ones(size(FeaVector,2),1) FeaVector']*thi{l})) ); |
---|
| 187 | error_base{l} = ( abs( (target{l} - X_base*thi_base{l})) ); |
---|
| 188 | learnRatio{l} = sum(error{l})/sum(error_base{l}); |
---|
| 189 | elseif strcmp(LearnAlg,'robustfit') |
---|
| 190 | tic; |
---|
| 191 | [thi{l},stats] = robustfit(FeaVector',target{l},'huber'); |
---|
| 192 | toc; |
---|
| 193 | % pause; |
---|
| 194 | % base line learning |
---|
| 195 | X_base = ones(size(FeaVector,2),1); |
---|
| 196 | [thi_temp,stats] = robustfit(X_base,target{l},'huber'); |
---|
| 197 | thi_base{l} = thi_temp(1); |
---|
| 198 | % error measure |
---|
| 199 | error{l} = ( abs( (target{l} - [ones(size(FeaVector,2),1) FeaVector']*thi{l})) ); |
---|
| 200 | error_base{l} = ( abs( (target{l} - X_base*thi_base{l})) ); |
---|
| 201 | learnRatio{l} = sum(error{l})/sum(error_base{l}); |
---|
| 202 | elseif strcmp(LearnAlg,'L2norm') |
---|
| 203 | thi{l} = [ones(Tsize,1) FeaVector']\target{l}; |
---|
| 204 | % base line learning |
---|
| 205 | X_base = ones(size(FeaVector,2),1); |
---|
| 206 | thi_base{l} = X_base\target{l}; |
---|
| 207 | % error measure |
---|
| 208 | error{l} = ( ( (target{l} - [ones(size(FeaVector,2),1) FeaVector']*thi{l})).^2 ); |
---|
| 209 | error_base{l} = ( ( (target{l} - X_base*thi_base{l})).^2 ); |
---|
| 210 | learnRatio{l} = sqrt(sum(error{l})/sum(error_base{l})); |
---|
| 211 | end |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | l = l +1; |
---|
| 215 | DateStamp = date; |
---|
| 216 | save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg ... |
---|
| 217 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
| 218 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
| 219 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
| 220 | 'thi','thi_base','error','error_base','learnRatio'); |
---|
| 221 | end |
---|
| 222 | |
---|
| 223 | DateStamp = date; |
---|
| 224 | save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg ... |
---|
| 225 | '_Nonsky' num2str(SkyExclude) '_Log' num2str(logScale) ... |
---|
| 226 | '_Near' num2str(LearnNear) '_WeiBatNu' num2str(WeiBatchNumber) ... |
---|
| 227 | '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],... |
---|
| 228 | 'thi','thi_base','error','error_base','learnRatio'); |
---|
| 229 | %if logScale == 1 |
---|
| 230 | % if SkyExclude == 1 |
---|
| 231 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_Nonsky_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
| 232 | % else |
---|
| 233 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
| 234 | % end |
---|
| 235 | %else |
---|
| 236 | % if SkyExclude == 1 |
---|
| 237 | % 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'); |
---|
| 238 | % else |
---|
| 239 | % save([ScratchDataFolder '/../learned_parameter/Depth_' ImgFolder '_' LearnAlg '_WeiBatNu' num2str(WeiBatchNumber) '_' AbsFeaType '_AbsFeaDate' AbsFeaDate '_LearnDate' DateStamp '_linear.mat'],'thi','thi_base','error','error_base','learnRatio'); |
---|
| 240 | % end |
---|
| 241 | %end |
---|