-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (149 loc) · 6.79 KB
/
main.py
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
# Author: Vedant Singhania
# Machine Learning | Spring 2020
# University of Colorado Denver
# Final Project
import h5py
import numpy as np
import os
import glob
import cv2
import warnings
from matplotlib import pyplot
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.preprocessing import MinMaxScaler
import sklearn.metrics as metrics
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn import svm
import joblib
import matplotlib.pyplot as plt
from feature_extraction import HSV_histogram
from imblearn.over_sampling import SMOTE
fixed_size = tuple((500, 500))
warnings.filterwarnings('ignore')
smote = SMOTE('minority')
seed = 9
train_path = "dataset/train"
test_path = "dataset/images/test"
scoring = "accuracy"
# get the training labels
train_labels = ['Green', 'Midripe', 'Overripe', 'Yellowish_Green']
# create all the machine learning models
models = []
models.append(('LR', LogisticRegression(random_state=seed)))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier(random_state=seed)))
models.append(('RF', RandomForestClassifier(n_estimators=100, random_state=seed)))
models.append(('NB', GaussianNB()))
models.append(('SVM', svm.SVC(random_state=seed, kernel='linear', C=10, probability=True)))
#models.append(('SVM', svm.SVC(random_state=seed, kernel='sigmoid')))
#models.append(('SVM', svm.SVC(random_state=seed, kernel='rbf')))
#models.append(('SVM', svm.SVC(random_state=seed, kernel='poly', degree=8)))
#models.append(('SVM', svm.SVC(C=100, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
# decision_function_shape='ovr', degree=3, gamma=0.01, kernel='rbf',
# max_iter=-1, probability=False, random_state=None, shrinking=True,
# tol=0.001, verbose=False)))
# models.append(('SVM', svm.SVC(C=10, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
# decision_function_shape='ovr', degree=3, gamma=1, kernel='linear',
# max_iter=-1, probability=False, random_state=None, shrinking=True,
# tol=0.001, verbose=False)))
# variables to hold the results and names
results = []
names = []
# import the feature vector and trained labels
hsv_features_h5 = h5py.File('output/data.h5', 'r')
hsv_labels_h5 = h5py.File('output/labels.h5', 'r')
global_features_string = hsv_features_h5['dataset_1']
global_labels_string = hsv_labels_h5['dataset_1']
hsv_features = np.array(global_features_string)
hsv_labels = np.array(global_labels_string)
hsv_features_h5.close()
hsv_labels_h5.close()
print("****Starting Training...****")
# split the training and testing data
(trainDataGlobal, testDataGlobal, trainLabelsGlobal, testLabelsGlobal) = train_test_split(np.array(hsv_features),
np.array(hsv_labels),
test_size=0.1,
random_state=seed)
print("Split training and testing data...")
print("Training data: ".format(trainDataGlobal.shape))
print("Testing data: ".format(testDataGlobal.shape))
print("Training labels: ".format(trainLabelsGlobal.shape))
print("Testing labels : ".format(testLabelsGlobal.shape))
# 10-fold cross validation
for name, model in models:
kfold = KFold(n_splits=10, random_state=seed)
cv_results = cross_val_score(model, trainDataGlobal, trainLabelsGlobal, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
# boxplot algorithm comparison
fig = pyplot.figure()
fig.suptitle('Machine Learning algorithm comparison')
ax = fig.add_subplot(111)
pyplot.boxplot(results)
ax.set_xticklabels(names)
pyplot.show()
print("****Starting testing....****")
######################################################
# Testing Section
######################################################
clf = svm.SVC(random_state=seed, kernel='linear', C=10, probability=True)
# clf = svm.SVC(random_state=seed, kernel='poly', degree=8)
#clf = svm.SVC(random_state=seed, kernel='sigmoid')
#clf = svm.SVC(random_state=seed, kernel='rbf')
# clf = svm.SVC(C=100, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
# decision_function_shape='ovr', degree=3, gamma=0.01, kernel='rbf',
# max_iter=-1, probability=False, random_state=None, shrinking=True,
# tol=0.001, verbose=False)
# clf = svm.SVC(C=10, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
# decision_function_shape='ovr', degree=3, gamma=1, kernel='linear',
# max_iter=-1, probability=False, random_state=None, shrinking=True,
# tol=0.001, verbose=False)
# SMOTE dataset
X_sm, y_sm = smote.fit_sample(trainDataGlobal, trainLabelsGlobal)
print(X_sm.shape, y_sm.shape)
# model fitting
#clf.fit(trainDataGlobal, trainLabelsGlobal)
clf.fit(X_sm, y_sm)
disp = metrics.plot_confusion_matrix(clf, testDataGlobal, testLabelsGlobal, display_labels=train_labels, cmap=plt.cm.Blues, normalize='true')
print(disp.confusion_matrix)
plt.show()
y_preds = clf.predict(testDataGlobal)
print('\n')
print(metrics.classification_report(testLabelsGlobal, y_preds, target_names=train_labels, output_dict=False))
print('\n')
print(metrics.classification_report(testLabelsGlobal, y_preds, target_names=train_labels, output_dict=True))
# loop through the test images
for file in glob.glob(test_path + "/*.jpg"):
# read the image
image = cv2.imread(file)
# resize the image
image = cv2.resize(image, fixed_size)
####################################
# Feature extraction
####################################
fv_histogram = HSV_histogram(image)
###################################
# Concatenate global features
###################################
global_feature = fv_histogram
# scale the extracted HSV features
scaler = MinMaxScaler(feature_range=(0, 1))
rescaled_feature = scaler.fit_transform(global_feature.reshape(-1, 1))
# make predictions
prediction = clf.predict(rescaled_feature.reshape(1, -1))[0]
# show predicted label on image
cv2.putText(image, train_labels[prediction], (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 255), 3)
# display the output image
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
image = cv2.bilateralFilter(image, 7, sigmaSpace=75, sigmaColor=75)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2HSV))
plt.show()