-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathML_GenerateModel_RF.m
185 lines (146 loc) · 6.48 KB
/
ML_GenerateModel_RF.m
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This file constructs the model to be used for prediction by using the labelled images generated by LabelObjects_BATCH.m
% The SVM model can then be used by ML_PredictWithModel.m and ML_PredictWithModel_BATCH.m
% 2016-07-07 Romain Laine [email protected]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialization
clear all;
close all;
clc
% User defined parameters -------------------------------------------------
% Cross-validation
Cross_validation = 1;
Fold_for_CV = 10; % number of fold for cross-validation
Calculate_posterior_prob = 1;
N_sample_for_probability = 10;
% Model parameters
MaxNumSplits = 6000;
NumLearningCycles = 60;
Learners_names_selected = {'Area' 'AxisLengthRatio' 'Eccentricity' 'Solidity' 'Perim2Area_ratio' 'MeanIntensity' 'Std_pixel_values'...
'Hu_IM1' 'Hu_IM4' 'Hu_IM5' 'Hu_IM6' 'Phi_4'...
'AN1653' 'AN2859' 'AN4011' 'AN832' 'AN3486' 'AN2689'...
'BoF22' 'BoF58' 'BoF95' 'BoF110' 'BoF177' 'BoF178'};
% Other options
Remove_unknown = 0;
Save_confusion_matrix = 1;
Save_model = 1;
Default_path = 'C:\Users\rfl30\DATA raw\SIM data\';
%% ------------------------------------------------------------------------
% Read in virus data with learners and classifiers
Model_parameters{1} = MaxNumSplits;
Model_parameters{2} = NumLearningCycles;
[Filename, Filepath] = uigetfile('*.xlsx','Choose an annotated descriptor file...',Default_path);
tic
disp('------------------------------');
disp('Reading data from spreadsheet...');
disp(fullfile(Filepath, Filename));
[Learners_values, annotation, ~] = xlsread(fullfile(Filepath, Filename),1);
toc
% Rename variables with the data and information
Learners_names = annotation(1,1:end-1);
annotation = annotation(2:end,end);
Learners_values = Learners_values(:,1:end);
n_learners = size(Learners_values,2);
disp(['Number of learners: ', num2str(n_learners)]);
disp(['Learners selected: ', Learners_names_selected]);
n_learners_selected = length(Learners_names_selected);
% Get the ordered list of selected learners
Learners_names_selected_ordered = Learners_names;
Learners_names_selected_ordered(~ismember(Learners_names, Learners_names_selected)) = [];
if n_learners_selected ~= length(Learners_names_selected_ordered)
disp('Learners list in .xls file not matching those selected.');
return;
end
%%
if Remove_unknown == 1
% Remove all unknown data points
inds = ~strcmp(annotation,'Unknown');
Learners_values = Learners_values(inds,:);
annotation = annotation(inds);
end
% Get the list of classes
Class_list = unique(annotation);
n_classes = length(Class_list);
disp(['Number of classes: ',num2str(n_classes)]);
disp('Class list:');
disp(Class_list);
% Class_list{1} = 'Filamentous';
% Class_list{2} = 'Small filamentous';
% Class_list{3} = 'Large spherical';
% Class_list{4} = 'Small spherical';
% Class_list{5} = 'Rod';
% Class_list{6} = 'Unknown';
n_examples = size(annotation,1);
disp(['Number of examples: ', num2str(n_examples)]);
% Select the learners
Learners_values_selected = Learners_values(:,ismember(Learners_names, Learners_names_selected_ordered));
trainedClassifier = GenerateModel_RandomForest(Learners_values_selected, annotation, ...
Model_parameters, [], Class_list, Learners_names_selected_ordered);
%% ------------------------------------------------------------------------
% Choose a name for the model
if Save_model == 1
RF_model_name_default = 'RF_Model';
answer = inputdlg('Choose the file name for the model','Model name:',1,{RF_model_name_default});
disp('------------------------------');
save(fullfile(Filepath,[answer{1},'.mat']), 'trainedClassifier');
disp('RF model saved as:');
disp(fullfile(Filepath,[answer{1},'.mat']));
end
%%
if Cross_validation == 1
%% Perform cross-validation
tic
disp('------------------------------');
disp('Performing cross-validation...');
partitionedModel = crossval(trainedClassifier.ClassificationEnsemble, 'KFold', Fold_for_CV);
% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
disp(['Accuracy: ',num2str(100*validationAccuracy,'%.1f'),' %']);
% Compute validation predictions and scores
validationPredictions = kfoldPredict(partitionedModel);
[C,order] = confusionmat(categorical(annotation), validationPredictions);
toc
% Convert the integer label vector to a class-identifier matrix.
[~,grp] = ismember(validationPredictions, Class_list);
oofLabelMat = zeros(n_classes, n_examples);
idxLinear = sub2ind([n_classes n_examples],grp,(1:n_examples)');
oofLabelMat(idxLinear) = 1; % Flags the row corresponding to the class
YMat = zeros(n_classes, n_examples);
[~,grp] = ismember(annotation, Class_list);
idxLinearY = sub2ind([n_classes n_examples],grp,(1:n_examples)');
YMat(idxLinearY) = 1;
%%
% Class_list2 = cell(6,1);
% Class_list2{1} = 'LF';
% Class_list2{2} = 'SF';
% Class_list2{3} = 'LS';
% Class_list2{4} = 'SS';
% Class_list2{5} = 'RD';
% Class_list2{6} = 'UK';
h_ConfMat = figure('Color','white','name','Confusion matrix');
hPC = plotconfusion(YMat,oofLabelMat);
h = gca;
h.XTickLabel = [Class_list; {''}];
h.YTickLabel = [Class_list; {''}];
%%
if Save_confusion_matrix == 1
% Save the confusion matrix
conf_matrix.matrix = C;
conf_matrix.order = order;
disp('------------------------------');
disp('Saving confusion matrix...');
% save(fullfile(Filepath,[answer{1},'_conf_matrix.mat']), 'conf_matrix');
saveas(h_ConfMat, fullfile(Filepath,[answer{1},'_conf_matrix image.png']), 'png');
end
%%
if Calculate_posterior_prob == 1
[label, Posterior] = resubPredict(trainedClassifier.ClassificationEnsemble);
idx = randsample(size(Learners_values,1),N_sample_for_probability,0);
% SVMModel.ClassNames
Probability_table = table(annotation(idx),label(idx),Posterior(idx,:),...
'VariableNames',{'TrueLabel','PredLabel','Posterior'});
disp(Probability_table);
end
end
disp('-----------------------------');
disp('All done.');