-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgo_TrainModel_R_gray.m
More file actions
131 lines (95 loc) · 3.72 KB
/
Copy pathgo_TrainModel_R_gray.m
File metadata and controls
131 lines (95 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
% Copyright ©2013. The Regents of the University of California (Regents).
% All Rights Reserved. Permission to use, copy, modify, and distribute
% this software and its documentation for educational, research, and
% not-for-profit purposes, without fee and without a signed licensing
% agreement, is hereby granted, provided that the above copyright notice,
% this paragraph and the following two paragraphs appear in all copies,
% modifications, and distributions. Contact The Office of Technology
% Licensing, UC Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley, CA
% 94720-1620, (510) 643-7201, for commercial licensing opportunities.
%
% Created by Jonathan T Barron and Jitendra Malik, Electrical Engineering
% and Computer Science, University of California, Berkeley.
%
% IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
% SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
% ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
% REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%
% REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY,
% PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO
% PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
clear all;
rand('twister',5489)
randn('state',0)
CONSTANTS;
N_GAUSSIANS = 40;
N_TRAIN = 100000;
in_directory = MIT_LABORATORY_FOLDER;
% MODE = 'test'
% MODE = 'train';
out_file = ['./prior.mat'];
names = MIT_TRAIN;
% names = [MIT_TRAIN, MIT_TEST];
try
load(out_file, 'prior');
fprintf('Loaded existing prior file.\n');
catch
prior = [];
fprintf('No existing prior file, making a new one.\n');
end
HALF_WIDTH = 2;
for name_i = 1:length(names)
name = names{name_i};
fprintf('loading %s\n', name)
A = imread([in_directory, name, '/reflectance.png']);
A = double(A) ./ double(intmax('uint16'));
A = mean(A,3);
background = A == 0;
A = log(max(eps,A));
A(background) = nan;
M_im = medianFilterMat_mask(background, HALF_WIDTH);
As{name_i} = A(~background);
MAs{name_i} = M_im * A(:);
idx = find(~background);
idx1 = randomlySelect(idx, 10000);
idx2 = randomlySelect(idx, 10000);
DAs{name_i} = A(idx1) - A(idx2);
end
As = cellfun(@(x) randomlySelect(x, 30000), As, 'UniformOutput', false);
prior.reflectance.gray.A_train = cat(1,As{:});
prior.reflectance.gray.A_range = [-7, 4];
% lambdas = 2.^[0:1:7];
% robust_costs = 1;%[0,1];
% LLs = [];
% for ri = 1:length(robust_costs)
% for li = 1:length(lambdas)
% for j = 1:length(As)
%
% A_train = cat(1,As{setdiff(1:length(As), j)});
% A_test = As{j};
%
% [junk, LL] = smoothHist1_fit(A_train, prior.reflectance.gray.A_range(1), prior.reflectance.gray.A_range(2), lambdas(li), robust_costs(ri));
%
% s = splat1(A_test, prior.reflectance.gray.A_range(1), prior.reflectance.gray.A_range(2));
% LLs(ri,li,j) = sum(sum(sum(LL .* s.N)));
% end
% end
% end
%
% cost = -sum(LLs,3);
% [ri, li] = find(cost == min(cost(:)));
% lambda = lambdas(li)
% robust_cost = robust_costs(ri)
lambda = 32;
robust_cost = 1;
A_train = cat(1,As{:});
prior.reflectance.gray.A_spline = smoothHist1_fit(A_train, prior.reflectance.gray.A_range(1), prior.reflectance.gray.A_range(2), lambda, robust_cost);
prior.reflectance.gray.A_spline = prior.reflectance.gray.A_spline - min(prior.reflectance.gray.A_spline(:));
MAs = cellfun(@(x) randomlySelect(x, 30000), MAs, 'UniformOutput', false);
MA = cat(1, MAs{:});
prior.reflectance.gray.MA.MA_train = MA;
prior.reflectance.gray.MA.GSM = GSM_fit(MA, 40, 0);
save(out_file, 'prior');