-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoG_New.m
79 lines (66 loc) · 3.29 KB
/
BoG_New.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
%% BoW
classes = {'Benign', 'InSitu' , 'Invasive', 'Normal'}; % Should be sorted alphapetically to match test data automatic labeling by folder
trainingSplit=0.9;
numberOfClusters = 500; % The number of clusters representing the number of features in the bag of features. Default can be 500.
ratioOfStrongFeatures = 0.8; % Default can be 0.8
%% Load Image Datastore and make them of equal size (size of class with lowest number of images)
imgSets = [];
for i = 1:length(classes)
imgSets = [ imgSets, imageSet(fullfile('Fold5/Train', classes{i})) ];
end
% Balance the data count between of all classes
minClassCount = min([imgSets.Count]);
imgSets = partition(imgSets, minClassCount, 'sequential');
%% Prepare Training and Validation Image Sets
[trainingSets, validationSets] = partition(imgSets, trainingSplit, 'sequential');
%% Visulizing Training and Validation Data (Press any key to procceed to next figure)
% mkdir('Graphs//'); %Directory for Storing Results
%% Forming Bag of Features
% Extracts SURF features from all training images &
% reducing the number of features through quantization of feature space using K-means clustering
bagTrain = bagOfFeatures(trainingSets, 'StrongestFeatures', ratioOfStrongFeatures, 'VocabularySize', numberOfClusters);
%
%% Visualize feature vector of the first image in the train set
figure;
imgTrain = read(imgSets(1), 1); % First image of first class as an example
featureVector = encode(bagTrain, imgTrain);
% Plot the histogram of visual word occurrences
figure;
bar(featureVector);
title('Gorsel Kelime Olusumlari');
xlabel('Gorsel Kelime Indeksi');
ylabel('Olusma Frekansi');
saveas(gcf,['Graphs//FeatureVectorTrain5.png']);
%% Train SVM Classifier
opts = templateSVM('KernelFunction', 'rbf');
% opts = templateSVM('KernelFunction', 'linear');
% opts = templateSVM('KernelFunction', 'knn');
% classifier = trainImageCategoryClassifier(trainingSets, bag, 'LearnerOptions', opts);
classifier = trainImageCategoryClassifier(trainingSets, bagTrain, 'LearnerOptions', opts);
%% Evaluate the classifier on training then validation data
confMatrix_train = evaluate(classifier, trainingSets);
confMatrix_val = evaluate(classifier, validationSets);
train_accuracy = mean(diag(confMatrix_train));
validation_accuracy = mean(diag(confMatrix_val));
display(['The training accuracy is ' train_accuracy '.']);
display(['The validation accuracy is ' validation_accuracy '.']);
%% Deployment (test the system on newly unseen images)
imgSetTest = [];
for i = 1:length(classes)
imgSetTest = [ imgSetTest, imageSet(fullfile('Fold5/Test20', classes{i})) ];
end
bagTest = bagOfFeatures(imgSetTest, 'StrongestFeatures', ratioOfStrongFeatures, 'VocabularySize', numberOfClusters);
confMatrix_test = evaluate(classifier, imgSetTest);
test_accuracy = mean(diag(confMatrix_test));
display(['The test accuracy is ' test_accuracy '.']);
%% Visualize feature vector of the first image in the test set
figure;
imgTest = read(imgSetTest(1), 1); % First image of first class as an example
featureVector = encode(bagTest, imgTest);
% Plot the histogram of visual word occurrences
figure;
bar(featureVector);
title('Gorsel Kelime Olusumlari');
xlabel('Gorsel Kelime Indeksi');
ylabel('Olusma Frekansi');
saveas(gcf,['Graphs//FeatureVectorTest5.png']);