-
Notifications
You must be signed in to change notification settings - Fork 0
/
GEPUFC.asv
269 lines (227 loc) · 8.12 KB
/
GEPUFC.asv
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
%%
%
% BASIC TEXTURE DESCRIPTOR START
%
% Loads and partitions data into K folds of test/train data and applies
% a basic global descriptor. This method does not use a Bag of Words
% model.
%
% Descriptor:
% GCLM Energy + Variance
% GLCM Contrast + Variance
% Pixel Difference + Variance
% Edge Count + Variance
%
%%
SetupVariables;
%% CHOOSE DATASET
DATA_VIDEO_CHOSENSET = DATA_VIDEO_UMNSCENE3;
VideoList = FN_PopulateStandardList(DATA_VIDEO_CHOSENSET.dir,2)
PCA = true;
DATA = DATA_VIDEO_CHOSENSET.name; % {Cardiff-Original | ViFData | Hockey}
BASEOFFSET = [1 1; 0 1; 1 0; 1 -1; -1 1;-1 -1; 0 -1; -1 0]; % 8 Directions
BASEOFFSET = [0 1; 1 0; 0 -1; -1 0]; % 4 Directions
SYMMETRY = false;
LEVELS = 32;
IMRESIZE = 0.5;
PYRAMID = [1 1];
PYRSIZE = size(PYRAMID);
RANGE = [1 2 3];
WINDOWSKIP = 1; % Window between sample extraction
WINDOWSIZE = 48; % Length of temporal window for descriptor extraction
FRAMERESIZE = IMRESIZE;
FOLD = max([VideoList{:,5}]);
% Determine Output Folder Name
FolderExtension = ['o',sprintf('%d',reshape(BASEOFFSET,1,numel(BASEOFFSET))),...
'l',sprintf('%d',LEVELS),...
'i',num2str(IMRESIZE),...
'p',num2str(reshape(PYRAMID,1,numel(PYRAMID))),...
'r',num2str(RANGE),...
's',sprintf('%d',SYMMETRY)];
FolderExtension(FolderExtension == ' ') = '';
FolderExtension(FolderExtension == '.') = '_';
FolderLocation = fullfile('ALLDATAMEX',DATA_VIDEO_CHOSENSET.name,...
['WS',num2str(WINDOWSKIP),...
'W',num2str(WINDOWSIZE),...
'F',num2str(FOLD),...
FolderExtension]);
OUTPUT = FolderLocation;
% Create the Output Folder
if ~exist(OUTPUT ,'dir')
mkdir(OUTPUT);
end
%Determine the number of FOLDS as dictated by the video list
% Variable Declaration
VideoListExtended = {}; totalTime = 0;
SourceVideoCount = size(VideoList);
Descriptors = []; DescriptorsTags = []; DescriptorGroup = [];
for i = 1 : SourceVideoCount(1)
tic;
% Select the current Item Being Tested
VideoListItem = VideoList(i,:);
if SourceVideoCount(2) >5 % Does the data use a custom window skip value?
if ~isempty(VideoList{i,6})
WINDOWSKIP = VideoList{i,6};
end
end
% Peform feature extraction
ExtractedVideoFeatures = RD_TextureEdgeMeasure( VideoListItem,WINDOWSIZE,...
WINDOWSKIP,PYRAMID,RANGE, FRAMERESIZE,DATA_VIDEO_CHOSENSET,...
SYMMETRY,LEVELS,BASEOFFSET);
% Formate the entire Scene, Each Row is a different Window/Scene
ExtractedVideoFeatures = cell2mat(ExtractedVideoFeatures);
ExtractedSceneCount = size(ExtractedVideoFeatures);
ExtractedSceneCount = ExtractedSceneCount(1); % Scene count is vertical
% Append Number of Samples Taken from the sample
VideoListExtended = [VideoListExtended;VideoList(1,:),ExtractedSceneCount];
if ExtractedSceneCount ~= 0 && ~isempty(ExtractedVideoFeatures);
% Add features to a global list
Descriptors = [Descriptors;ExtractedVideoFeatures];
% assign class tags to each feature
clear Tags
[Tags{1:ExtractedSceneCount}] = deal(VideoList{i,1});
DescriptorsTags = [DescriptorsTags;Tags'];
% Assign the feature a group within the K-folds
clear Tags
[Tags{1:ExtractedSceneCount}] = deal(VideoList{i,5});
DescriptorGroup = [DescriptorGroup;Tags'];
end
% Output Process Time
currentTime = toc; totalTime = totalTime + currentTime;
disp(strcat(num2str(currentTime),'(',num2str(totalTime),')'));
end
%% LIBSVM DATA
LIBClassificationPerf = cell(1,FOLD);
LIBFinalDecision = cell(1,FOLD);
LIBAccuracy = cell(1,FOLD);
LIBProbability = cell(1,FOLD);
LIBActualAnswer = cell(1,FOLD);
LIBVocab = cell(1,FOLD);
LIBTrainingModel = cell(1,FOLD);
LIBROC = cell(FOLD,3);
%% LINEAR SVM DATA
LINClassificationPerf = cell(1,FOLD);
LINFinalDecision = cell(1,FOLD);
LINAccuracy = cell(1,FOLD);
LINProbability = cell(1,FOLD);
LINActualAnswer = cell(1,FOLD);
LINVocab = cell(1,FOLD);
LINTrainingModel = cell(1,FOLD);
LINROC = cell(FOLD,3);
%% TREE DATA
TREEClassificationPerf = cell(1,FOLD);
TREEFinalDecision = cell(1,FOLD);
TREEAccuracy = cell(1,FOLD);
TREEProbability = cell(1,FOLD);
TREEActualAnswer = cell(1,FOLD);
TREEVocab = cell(1,FOLD);
TREETrainingModel = cell(1,FOLD);
TREEROC = cell(FOLD,3);
DescriptorsCopy = Descriptors;
% Perform Dimension Reduction
Descriptors = mat2cell(Descriptors);
if PCA
ElementsToKeepMin = 5;%;
%% Perform PCA on DATA
pyrDataSize = size(Descriptors);
%Fill in missing data
%Create Mat vectors
yMatVect = zeros(length(Descriptors),1);
for m = 1:length(Descriptors)
subSize = size(Descriptors{m});
yMatVect(m) = subSize(1);
end
numericFlatData = cell2mat(Descriptors);
%Perform Reduction
[~,PC, e] = princomp(numericFlatData);
esum = sum(e);
eperc = esum * 0.90;
% Keep 95% of eigen data
ElementsToKeep = 0;
for i = 1: length(e)
if sum(e(1:i)) >= eperc
ElementsToKeep = i;
break;
end
end
if ElementsToKeep < ElementsToKeepMin
ElementsToKeep = ElementsToKeepMin;
end
% Reconstruct Data
numericFlatData = PC(:,1:ElementsToKeep);
Descriptors = mat2cell(numericFlatData,yMatVect,ElementsToKeep);
end
GLCMNonPCADescriptors = DescriptorsCopy;
GLCMDescriptors = Descriptors;
GLCMTags = DescriptorsTags;
GLCMFlowList = VideoList;
GLCMGroup = DescriptorGroup;
subName = [DATA,'GLCM'];
save(strcat(OUTPUT,'/TestOutput','.mat'),...
'GLCMDescriptors',...
'GLCMTags',...
'GLCMGroup',...
'GLCMFlowList',...
'GLCMNonPCADescriptors',...
'-v7.3');
[G GN] = grp2idx(GLCMTags); % Reduce character tags to numeric grouping
for k = 1: max(cell2mat(GLCMGroup)) %Number of Folds
%disp(['Starting Test ',num2str(k)]);
% Split data into two groups (Fight.NotFight) based on DescriptorGroup
% number
% testData = find(str2num([DescriptorGroup{:}]')== k);
testData = find([GLCMGroup{:}]'== k);
TESTIDX = false(length(GLCMGroup),1);
TESTIDX(testData) = true;
TRAINIDX = ~TESTIDX;
% Save group assignments into a
DataSplit{k,1} = k;
DataSplit{k,2} = TRAINIDX;
DataSplit{k,3} = TESTIDX;
DataSplit{k,4} = G;
DataSplit{k,5} = GN;
%% TEST USING NON-LINEAR SVM
% FinalDescriptor = cell2mat(GLCMDescriptors);
FinalDescriptor = GLCMNonPCADescriptors;
%[CPerf,finalDecision,Answer,accuracy,prob_estimates,trainingModel,subROC ]...
% = ML_TwoClassLibSVM(FinalDescriptor ,TESTIDX,TRAINIDX,G,GN );
%LIBFinalDecision{k} = finalDecision;
%LIBAccuracy{k} = accuracy;
%LIBProbability{k} = prob_estimates;
%LIBActualAnswer{k} = Answer;
%LIBTrainingModel{k} = trainingModel;
%LIBClassificationPerf{k} = CPerf;
%LIBROC(k,:) = subROC;
%% TEST USING LINEAR SVM
[CPerf,finalDecision,Answer,accuracy,prob_estimates,trainingModel,subROC ]...
= ML_TwoClassLibLinearSVM(FinalDescriptor ,TESTIDX,TRAINIDX,G,GN );
LINFinalDecision{k} = finalDecision;
LINAccuracy{k} = accuracy;
LINProbability{k} = prob_estimates;
LINActualAnswer{k} = Answer;
LINTrainingModel{k} = trainingModel;
LINClassificationPerf{k} = CPerf;
LINROC(k,:) = subROC;
%% TEST RANDOM FOREST
[ r,finalDecision,Answer,accuracy,prob_estimates,svmMo ]...
= ML_TwoClassForest(FinalDescriptor ,TESTIDX,TRAINIDX,G,GN );
TREEFinalDecision{k} = finalDecision;
TREEAccuracy{k} = accuracy;
TREEProbability{k} = prob_estimates;
TREEActualAnswer{k} = Answer;
TREETrainingModel{k} = svmMo{:};
TREEClassificationPerf{k} = r;
end
FightIndex = 1;
TreeProb = cell2mat(reshape(TREEProbability,FOLD,1));
TreeProb = TreeProb(:,1);
[X,Y,T,AUC] = perfcurve( cell2mat(reshape(TREEActualAnswer,FOLD,1)) , TreeProb,FightIndex );
figure,plot(X,Y);
xlabel('False positive rate');
ylabel('True positive rate');
title(strcat('AUC: ',num2str(AUC)));
[X,Y,T,AUC] = perfcurve( cell2mat(reshape(LINActualAnswer,FOLD,1)) , cell2mat(reshape(LINProbability,FOLD,1)) ,FightIndex );
figure,plot(X,Y);
xlabel('False positive rate');
ylabel('True positive rate');
title(strcat('AUC: ',num2str(AUC)));