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 [x,status,history] = L1Barrier_wo_Constrain(Para,method,ptol,pmaxi, VERBOSE) |
---|
40 | %function [x,status,history] = L1Barrier_wo_Constrain(A,b,t_0,method,ptol,pmaxi) |
---|
41 | % |
---|
42 | % Fast L1 - norm Solver |
---|
43 | % |
---|
44 | % L1 - norm Solver Solves problems of the following form: |
---|
45 | % |
---|
46 | % minimize | A*x - b|L1 |
---|
47 | % |
---|
48 | % where variable is x and problem data are A and b. |
---|
49 | % |
---|
50 | % INPUT |
---|
51 | % |
---|
52 | % A : mxn matrix; input data. each column corresponds to each feature |
---|
53 | % b : m vector; class label |
---|
54 | % |
---|
55 | % method : string; search direction method type |
---|
56 | % 'cg' : conjugate gradients method, 'pcg' |
---|
57 | % 'pcg' : preconditioned conjugate gradients method |
---|
58 | % 'exact': exact method (default value) |
---|
59 | % ptol : scalar; pcg relative tolerance. if empty, use adaptive rule. |
---|
60 | % pmaxi : scalar: pcg maximum iteration. if empty, use default value (500). |
---|
61 | % |
---|
62 | % OUTPUT |
---|
63 | % |
---|
64 | % x : n vector; |
---|
65 | % status : scalar; +1: success, -1: maxiter exceeded |
---|
66 | % history : |
---|
67 | % row 1) phi |
---|
68 | % row 2) norm(gradient of phi) |
---|
69 | % row 3) cumulative cg iterations |
---|
70 | % |
---|
71 | % USAGE EXAMPLE |
---|
72 | % |
---|
73 | % [x,status] = l2_logreg(A,b,lambda,'pcg'); |
---|
74 | % |
---|
75 | |
---|
76 | % Written by Kwangmoo Koh <deneb1@stanford.edu> |
---|
77 | % adopted by Min Sun |
---|
78 | |
---|
79 | %------------------------------------------------------------ |
---|
80 | % INITIALIZE |
---|
81 | %------------------------------------------------------------ |
---|
82 | global A D p b; |
---|
83 | |
---|
84 | % LOG BARRIER METHOD |
---|
85 | MAX_LOGB_ITER = 100; |
---|
86 | EPSILON_GAP = 2e-4; |
---|
87 | MU_t = 100; % for t -- log barrier. Changed ASH |
---|
88 | %if(isempty(t_0)) t_0 = 1; end |
---|
89 | t_0 = 1; |
---|
90 | t = t_0; |
---|
91 | |
---|
92 | |
---|
93 | % NEWTON PARAMETERS |
---|
94 | MAX_TNT_ITER = 100; % maximum (truncated) Newton iteration |
---|
95 | ABSTOL = 1e-8; % terminates when the norm of gradient < ABSTOL |
---|
96 | EPSILON = 1e-7; % terminate when lambdasqr_by_2 < EPSILON |
---|
97 | StopNorm = 0; % set to 0 using newton decrement |
---|
98 | |
---|
99 | % LINE SEARCH PARAMETERS |
---|
100 | ALPHA = 0.01; % minimum fraction of decrease in norm(gradient) |
---|
101 | BETA = 0.5; % stepsize decrease factor |
---|
102 | MAX_LS_ITER = 50; % maximum backtracking line search iteration |
---|
103 | Eps = -1e-50; % gap for inequality |
---|
104 | normg_Flag = 1; % if evaluate function set normg_Flag = 1 |
---|
105 | |
---|
106 | [m,n] = size(A); % problem size: m examples, n features |
---|
107 | A2 = A.^2; |
---|
108 | |
---|
109 | |
---|
110 | %if(isempty(pmaxi)) pcgmaxi = 500; else pcgmaxi = pmaxi; end |
---|
111 | %if(isempty(ptol )) pcgtol = 1e-4; else pcgtol = ptol; end |
---|
112 | pcgmaxi = 500; |
---|
113 | pcgtol = 1e-4; |
---|
114 | |
---|
115 | % INITIALIZE |
---|
116 | pobj = Inf; s = inf; pitr = 0 ; pflg = 0 ; prelres = 0; pcgiter = 0; |
---|
117 | history = []; |
---|
118 | status = -1; |
---|
119 | % feasible starting point |
---|
120 | |
---|
121 | |
---|
122 | x = zeros(n,1); dx = zeros(n,1); |
---|
123 | y = max( abs( A*x-b))-Eps- (-1e-2); dy = zeros(m,1); |
---|
124 | |
---|
125 | |
---|
126 | % check is x y feasible start |
---|
127 | % if max(A*(x) - (y)-b)< 0 && max(-A*(x) - (y)+b) < 0 |
---|
128 | % disp('Feasible start'); |
---|
129 | % end |
---|
130 | |
---|
131 | |
---|
132 | %------------------------------------------------------------ |
---|
133 | % LOG BARRIER OUTER LOOP |
---|
134 | %------------------------------------------------------------ |
---|
135 | %for lbiter = 1:MAX_LOGB_ITER |
---|
136 | %while true |
---|
137 | % return; |
---|
138 | %end |
---|
139 | for LogBIter = 1:MAX_LOGB_ITER |
---|
140 | if VERBOSE |
---|
141 | disp(sprintf('%s %15s %10s %10s %10s %s %s %6s %10s %6s',... |
---|
142 | 'iter','primal obj','stepsize','norg(g)','lambdasqr','LSiter', 'LSiter_fea''p_flg','p_res','p_itr')); |
---|
143 | end |
---|
144 | %------------------------------------------------------------ |
---|
145 | % MAIN LOOP |
---|
146 | %------------------------------------------------------------ |
---|
147 | |
---|
148 | status = -1; % initalized to -1; |
---|
149 | % nt_hist = []; |
---|
150 | Ax_b = A*x - b; |
---|
151 | Ax_b_y = Ax_b + y; |
---|
152 | Neg_Ax_b_y = -Ax_b + y; |
---|
153 | g_1 = (1./Neg_Ax_b_y - 1./Ax_b_y); |
---|
154 | g_2 = (t - 1./Ax_b_y - 1./Neg_Ax_b_y); |
---|
155 | gradphi_x = (g_1'*A)'; |
---|
156 | for ntiter = 0:MAX_TNT_ITER |
---|
157 | D_1 = (1./Neg_Ax_b_y).^2; |
---|
158 | D_2 = (1./Ax_b_y).^2; |
---|
159 | D = 2./(y.^2 + Ax_b.^2 ); |
---|
160 | g = g_1 +(D_1-D_2).*(1./(D_1+D_2)).*g_2; |
---|
161 | gradphi_x_eli = (g'*A)'; |
---|
162 | |
---|
163 | %------------------------------------------------------------ |
---|
164 | % CALCULATE NEWTON STEP |
---|
165 | %------------------------------------------------------------ |
---|
166 | % switch lower(method) |
---|
167 | % case 'pcg' |
---|
168 | % p = 1./(A2'*D); |
---|
169 | % if (isempty(ptol)) pcgtol = min(0.1,norm(gradphi_x_eli)); end |
---|
170 | % [dx, pflg, prelres, pitr, presvec] = ... |
---|
171 | % pcg(@AXfunc,-gradphi_x_eli,pcgtol,pcgmaxi,@Mfunc,[],[]); |
---|
172 | % % A,D,[],1./(A2'*D)); |
---|
173 | % if (pitr == 0) pitr = pcgmaxi; end |
---|
174 | % |
---|
175 | % case 'cg' |
---|
176 | % if (isempty(ptol)) pcgtol = min(0.1,norm(gradphi_x_eli)); end |
---|
177 | % [dx, pflg, prelres, pitr, presvec] = ... |
---|
178 | % pcg(@AXfunc,-gradphi_x_eli,pcgtol,pcgmaxi,[],[],[]); |
---|
179 | % % A,D,[],[]); |
---|
180 | % if (pitr == 0) pitr = pcgmaxi; end |
---|
181 | % |
---|
182 | % otherwise % exact method |
---|
183 | % hessphi_x = A'*sparse(1:m,1:m,D)*A; |
---|
184 | hessphi_x = (sparse(1:m,1:m, D)*A)' * A; |
---|
185 | dx = -hessphi_x\gradphi_x_eli; |
---|
186 | % end |
---|
187 | dy = (1./(D_1 + D_2)) .*(-g_2 + (D_1 - D_2).*(A*dx)); |
---|
188 | %pcgiter = pcgiter+pitr; |
---|
189 | |
---|
190 | % function value and normg for back tracking line search or stoping critera |
---|
191 | normg = norm([gradphi_x; g_2]); |
---|
192 | |
---|
193 | phi = sum(y) - sum( log( [Ax_b_y; Neg_Ax_b_y]))/t; |
---|
194 | lambda_sqr = -gradphi_x'*dx - g_2'*dy; |
---|
195 | %------------------------------------------------------------ |
---|
196 | % BACKTRACKING LINE SEARCH |
---|
197 | %------------------------------------------------------------ |
---|
198 | s = 1; |
---|
199 | % if false % debug |
---|
200 | Delta = A*(dx) - (dy); |
---|
201 | Delta_Negdy = -A*(dx) - (dy); |
---|
202 | LSiter = 0; |
---|
203 | while any( (s*Delta - Neg_Ax_b_y) >= Eps) || any(( s*Delta_Negdy - Ax_b_y) >= Eps) |
---|
204 | s = BETA*s; |
---|
205 | LSiter = LSiter + 1; |
---|
206 | end |
---|
207 | for lsiter = 1:MAX_LS_ITER |
---|
208 | new_x = x + s*dx; |
---|
209 | new_y = y + s*dy; |
---|
210 | Ax_b = A*new_x - b; |
---|
211 | Ax_b_y = Ax_b + new_y; |
---|
212 | Neg_Ax_b_y = -Ax_b + new_y; |
---|
213 | |
---|
214 | % if normg_Flag |
---|
215 | g_1 = (1./Neg_Ax_b_y - 1./Ax_b_y); |
---|
216 | g_2 = (t - 1./Ax_b_y - 1./Neg_Ax_b_y); |
---|
217 | gradphi_x = (g_1'*A)'; |
---|
218 | if (norm([gradphi_x; g_2])<=(1-ALPHA*s)*normg) break; end |
---|
219 | s = BETA*s; |
---|
220 | % else |
---|
221 | % % evaluate function value |
---|
222 | % new_phi = sum(y) - sum( log( [Anew_x_b_y; Neg_Anew_x_b_y]))/t; |
---|
223 | % if new_phi <= phi +ALPHA*s*[gradphi_x; g_2]'*[dx; dy] break; end |
---|
224 | % s = BETA*s; |
---|
225 | % end |
---|
226 | end |
---|
227 | % end |
---|
228 | x = new_x; |
---|
229 | % dx; |
---|
230 | % x'; |
---|
231 | y = new_y; |
---|
232 | % if VERBOSE |
---|
233 | % disp(sprintf('%4d %15.6e %10.2e %10.2e %10.2e %4d %3d %6d %10.2e %6d',... |
---|
234 | % ntiter,phi,s,normg, lambda_sqr/2, lsiter, LSiter, pflg,prelres,pitr)); |
---|
235 | % nt_hist = [nt_hist [phi; normg; pcgiter]]; |
---|
236 | % end |
---|
237 | %------------------------------------------------------------ |
---|
238 | % STOPPING CRITERION |
---|
239 | %------------------------------------------------------------ |
---|
240 | if (lsiter == MAX_LS_ITER) disp('MaxLSIter'); break; end |
---|
241 | if StopNorm |
---|
242 | if (normg < ABSTOL) |
---|
243 | status = 1; |
---|
244 | %disp('Absolute normg tolerance reached.'); |
---|
245 | % disp(sprintf('%d/%d',sum(abs((A2'*h)./(2*lambda))<0.5),n)); |
---|
246 | break; |
---|
247 | end |
---|
248 | else |
---|
249 | if (lambda_sqr/2 <= EPSILON) |
---|
250 | status = 1; |
---|
251 | %disp('Absolute Lambda tolerance reached.'); |
---|
252 | break; |
---|
253 | end |
---|
254 | end |
---|
255 | end |
---|
256 | if status == -1 disp('Error status -1'); end |
---|
257 | |
---|
258 | %-------------- decreasing the gap ------------- |
---|
259 | gap = m/t; |
---|
260 | disp(gap); |
---|
261 | %history=[history [length(nt_hist); gap]]; |
---|
262 | if gap< EPSILON_GAP break; end |
---|
263 | t = MU_t*t; |
---|
264 | |
---|
265 | end |
---|
266 | |
---|
267 | return; |
---|
268 | |
---|
269 | |
---|
270 | %------------------------------------------------------------ |
---|
271 | % CALL BACK FUNCTIONS FOR PCG |
---|
272 | %------------------------------------------------------------ |
---|
273 | function y = AXfunc(x) |
---|
274 | global A D; |
---|
275 | y = A'*(D.*(A*x)); |
---|
276 | |
---|
277 | function y = Mfunc(x) |
---|
278 | global p; |
---|
279 | y = x.*p; |
---|