-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model_GUI.m
More file actions
300 lines (253 loc) · 12.5 KB
/
Copy pathtrain_model_GUI.m
File metadata and controls
300 lines (253 loc) · 12.5 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
classdef train_model_GUI < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
TrainModel matlab.ui.Figure
LoadManmadeTrainingSetButton matlab.ui.control.Button
LoadNaturalTrainingSetButton matlab.ui.control.Button
TrainKNNClassifierButton matlab.ui.control.Button
NumberofNeighboursEditFieldLabel matlab.ui.control.Label
NumberofNeighboursEditField matlab.ui.control.NumericEditField
LoadManmadeTestSetButton matlab.ui.control.Button
DistanceDropDownLabel matlab.ui.control.Label
DistanceDropDown matlab.ui.control.DropDown
ErrorMessage matlab.ui.control.Label
LoadNaturalTestSetButton matlab.ui.control.Button
ClassifyImageButton matlab.ui.control.Button
end
properties (Access = public)
mTestSet;
mTestSetLoaded = false;
nTestSet;
nTestSetLoaded = false;
numNeighbours = 20;
distance = 'cosine';
imageWidth = 180; % width of the image
imageHeight = 180; % height of the image
manmadeImageSet % manmade training set
manmadeImageSetLoaded = false; % stores whether or not a image set has been loaded
naturalImageSet % natural training set
naturalImageSetLoaded = false; % stores whether or not a image set has been loaded
canTrain = false;
end
methods (Access = public)
function ValidateInputs(app)
if app.numNeighbours <= 0
app.canTrain = false;
app.ErrorMessage.FontColor = [1 0 0];
app.ErrorMessage.Text = sprintf('The number of neighbours \nmust be larger than 0');
elseif app.imageHeight <= 0 || app.imageWidth <= 0
app.canTrain = false;
app.ErrorMessage.FontColor = [1 0 0];
app.ErrorMessage.Text = sprintf('Image resolution must be \nlarger than 0.\nThe recommended size is 180.');
elseif (app.manmadeImageSetLoaded == false)
app.canTrain = false;
app.ErrorMessage.FontColor = [1 0 0];
app.ErrorMessage.Text = sprintf('Please load the manmade \ntraining set');
elseif (app.naturalImageSetLoaded == false)
app.canTrain = false;
app.ErrorMessage.FontColor = [1 0 0];
app.ErrorMessage.Text = sprintf('Please load the natural \ntraining set');
elseif (app.mTestSetLoaded == false)
app.canTrain = false;
app.ErrorMessage.FontColor = [1 0 0];
app.ErrorMessage.Text = sprintf('Please load the manmade \ntest set');
elseif (app.nTestSetLoaded == false)
app.canTrain = false;
app.ErrorMessage.FontColor = [1 0 0];
app.ErrorMessage.Text = sprintf('Please load the natural \ntest set');
else
app.canTrain = true;
app.ErrorMessage.FontColor = [0 0 0];
app.ErrorMessage.Text = "Ready to train model";
end
end
end
methods (Access = private)
% Button pushed function: LoadManmadeTrainingSetButton
function LoadManmadeTrainingSetButtonPushed(app, event)
app.manmadeImageSetLoaded = false;
ValidateInputs(app);
imgSetPath = uigetdir+"\";
app.manmadeImageSet = filesRead(imgSetPath);
if(isempty(app.manmadeImageSet))
app.manmadeImageSetLoaded = false;
else
app.manmadeImageSetLoaded = true;
end
ValidateInputs(app)
end
% Button pushed function: LoadNaturalTrainingSetButton
function LoadNaturalTrainingSetButtonPushed(app, event)
app.naturalImageSetLoaded = false;
ValidateInputs(app);
imgSetPath = uigetdir+"\";
app.naturalImageSet = filesRead(imgSetPath);
if(isempty(app.naturalImageSet))
app.naturalImageSetLoaded = false;
else
app.naturalImageSetLoaded = true;
end
ValidateInputs(app);
end
% Button pushed function: LoadManmadeTestSetButton
function LoadManmadeTestSetButtonPushed(app, event)
app.mTestSetLoaded = false;
ValidateInputs(app);
imgSetPath = uigetdir+"\";
app.mTestSet = filesRead(imgSetPath);
if(isempty(app.mTestSet))
app.mTestSetLoaded = false;
else
app.mTestSetLoaded = true;
end
ValidateInputs(app);
end
% Value changed function: DistanceDropDown
function DistanceDropDownValueChanged(app, event)
app.distance = app.DistanceDropDown.Value;
end
% Callback function
function ImageWidthEditFieldValueChanged(app, event)
app.imageWidth = app.ImageWidthEditField.Value;
ValidateInputs(app);
end
% Callback function
function ImageHeightEditFieldValueChanged(app, event)
app.imageHeight = app.ImageHeightEditField.Value;
ValidateInputs(app);
end
% Button pushed function: TrainKNNClassifierButton
function TrainKNNClassifierButtonPushed(app, event)
if (app.canTrain == true)
% get path to save model to
[file, path] = uiputfile('.mat');
savePath = fullfile(path, file);
ml_train(app.imageWidth, app.imageHeight, app.numNeighbours, app.distance, app.manmadeImageSet, app.naturalImageSet, app.mTestSet, app.nTestSet, savePath);
end
end
% Button pushed function: LoadNaturalTestSetButton
function LoadNaturalTestSetButtonPushed(app, event)
app.nTestSetLoaded = false;
ValidateInputs(app);
imgSetPath = uigetdir+"\";
app.nTestSet = filesRead(imgSetPath);
if(isempty(app.nTestSet))
app.nTestSetLoaded = false;
else
app.nTestSetLoaded = true;
end
ValidateInputs(app);
end
% Value changed function: NumberofNeighboursEditField
function NumberofNeighboursEditFieldValueChanged(app, event)
app.numNeighbours = app.NumberofNeighboursEditField.Value;
ValidateInputs(app);
end
% Button pushed function: ClassifyImageButton
function ClassifyImageButtonPushed(app, event)
[file, path] = uigetfile('', 'Load model object');
model = loadCompactModel(fullfile(path, file));
[file, path] = uigetfile('', 'Choose image to classify');
img = imread(fullfile(path, file));
if(isempty(img))
msgbox('Failed to load image');
else
img = imresize(img, [180 180]);
i{1} = img;
t = getAllFeatures(i);
label = predict(model, t);
if (label == 1)
msgbox('This image has been classified as manmade');
elseif (label == 0)
msgbox('This image has been classified as natural');
else
msgbox('Something went horribly wrong');
end
end
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create TrainModel
app.TrainModel = uifigure;
app.TrainModel.Position = [100 100 194 323];
app.TrainModel.Name = 'Train KNN Model';
% Create LoadManmadeTrainingSetButton
app.LoadManmadeTrainingSetButton = uibutton(app.TrainModel, 'push');
app.LoadManmadeTrainingSetButton.ButtonPushedFcn = createCallbackFcn(app, @LoadManmadeTrainingSetButtonPushed, true);
app.LoadManmadeTrainingSetButton.Position = [13 287 171 22];
app.LoadManmadeTrainingSetButton.Text = 'Load Manmade Training Set';
% Create LoadNaturalTrainingSetButton
app.LoadNaturalTrainingSetButton = uibutton(app.TrainModel, 'push');
app.LoadNaturalTrainingSetButton.ButtonPushedFcn = createCallbackFcn(app, @LoadNaturalTrainingSetButtonPushed, true);
app.LoadNaturalTrainingSetButton.Position = [13 255 171 22];
app.LoadNaturalTrainingSetButton.Text = 'Load Natural Training Set';
% Create TrainKNNClassifierButton
app.TrainKNNClassifierButton = uibutton(app.TrainModel, 'push');
app.TrainKNNClassifierButton.ButtonPushedFcn = createCallbackFcn(app, @TrainKNNClassifierButtonPushed, true);
app.TrainKNNClassifierButton.Position = [12 41 171 22];
app.TrainKNNClassifierButton.Text = 'Train KNN Classifier';
% Create NumberofNeighboursEditFieldLabel
app.NumberofNeighboursEditFieldLabel = uilabel(app.TrainModel);
app.NumberofNeighboursEditFieldLabel.HorizontalAlignment = 'right';
app.NumberofNeighboursEditFieldLabel.Position = [13 158 126 22];
app.NumberofNeighboursEditFieldLabel.Text = 'Number of Neighbours';
% Create NumberofNeighboursEditField
app.NumberofNeighboursEditField = uieditfield(app.TrainModel, 'numeric');
app.NumberofNeighboursEditField.ValueChangedFcn = createCallbackFcn(app, @NumberofNeighboursEditFieldValueChanged, true);
app.NumberofNeighboursEditField.Position = [147 158 37 22];
app.NumberofNeighboursEditField.Value = 20;
% Create LoadManmadeTestSetButton
app.LoadManmadeTestSetButton = uibutton(app.TrainModel, 'push');
app.LoadManmadeTestSetButton.ButtonPushedFcn = createCallbackFcn(app, @LoadManmadeTestSetButtonPushed, true);
app.LoadManmadeTestSetButton.Position = [13 223 171 22];
app.LoadManmadeTestSetButton.Text = 'Load Manmade Test Set';
% Create DistanceDropDownLabel
app.DistanceDropDownLabel = uilabel(app.TrainModel);
app.DistanceDropDownLabel.HorizontalAlignment = 'right';
app.DistanceDropDownLabel.Position = [13 126 52 22];
app.DistanceDropDownLabel.Text = 'Distance';
% Create DistanceDropDown
app.DistanceDropDown = uidropdown(app.TrainModel);
app.DistanceDropDown.Items = {'cityblock', 'chebychev', 'correlation', 'cosine', 'euclidean', 'hamming', 'jaccard', 'mahalanobis', 'minkowski', 'seuclidean', 'spearman'};
app.DistanceDropDown.ValueChangedFcn = createCallbackFcn(app, @DistanceDropDownValueChanged, true);
app.DistanceDropDown.Position = [82 126 102 22];
app.DistanceDropDown.Value = 'cosine';
% Create ErrorMessage
app.ErrorMessage = uilabel(app.TrainModel);
app.ErrorMessage.VerticalAlignment = 'top';
app.ErrorMessage.FontColor = [1 0 0];
app.ErrorMessage.Position = [13 75 170 40];
app.ErrorMessage.Text = '';
% Create LoadNaturalTestSetButton
app.LoadNaturalTestSetButton = uibutton(app.TrainModel, 'push');
app.LoadNaturalTestSetButton.ButtonPushedFcn = createCallbackFcn(app, @LoadNaturalTestSetButtonPushed, true);
app.LoadNaturalTestSetButton.Position = [13 191 171 22];
app.LoadNaturalTestSetButton.Text = 'Load Natural Test Set';
% Create ClassifyImageButton
app.ClassifyImageButton = uibutton(app.TrainModel, 'push');
app.ClassifyImageButton.ButtonPushedFcn = createCallbackFcn(app, @ClassifyImageButtonPushed, true);
app.ClassifyImageButton.Position = [13 12 170 22];
app.ClassifyImageButton.Text = 'Classify Image';
end
end
methods (Access = public)
% Construct app
function app = train_model_GUI
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.TrainModel)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.TrainModel)
end
end
end