This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (122 loc) · 3.93 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
import numpy as np
import cv2
import os
import pywt
from matplotlib import pyplot as plt
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import confusion_matrix
import seaborn as sn
import pickle
# testing cv2.imread() is working correctly
try:
img1 = cv2.imread('test_images/hog1.jpeg')
except NameError or ImportError:
print("OpenCV has failed")
else:
print("OpenCV test: successful")
def w2d(img, mode='haar', level=1):
imArray = img
# Datatype conversions
# convert to grayscale
imArray = cv2.cvtColor(imArray, cv2.COLOR_RGB2GRAY)
# convert to float
imArray = np.float32(imArray)
imArray /= 255
# compute coefficients
coeffs = pywt.wavedec2(imArray, mode, level=level)
# Process Coefficients
coeffs_H = list(coeffs)
coeffs_H[0] *= 0
# reconstruction
imArray_H = pywt.waverec2(coeffs_H, mode)
imArray_H *= 255
imArray_H = np.uint8(imArray_H)
return imArray_H
def categorise_with_pic_names(dir):
allpics = (os.walk(dir))
for pic in allpics:
return pic[2]
category = {
"dog": categorise_with_pic_names('picdata/dog/'),
"frog": categorise_with_pic_names('picdata/frog/'),
"hog": categorise_with_pic_names('picdata/hog/')
}
def scaling_transforming(img, marker):
scaledraw = cv2.resize(img, (32, 32))
img_trans = w2d(img, 'db1', 5)
scaledtrans = cv2.resize(img_trans, (32, 32))
combined = np.vstack((scaledraw.reshape(32 * 32 * 3, 1), scaledtrans.reshape(32 * 32, 1)))
x.append(combined)
y.append(marker)
x = []
y = []
for i in category["dog"]:
img = cv2.imread('picdata/dog/' + i)
scaling_transforming(img, 0)
print("Dog images cleaned")
for i in category["frog"]:
img = cv2.imread('picdata/frog/' + i)
scaling_transforming(img, 1)
print("Frog images cleaned")
for i in category["hog"]:
img = cv2.imread('picdata/hog/' + i)
scaling_transforming(img, 2)
print("Hog images cleaned")
x = np.array(x).reshape(len(x), 4096).astype(float)
x_train, x_test, y_train, y_test = train_test_split(x, y)
model_params = {
'svm': {
'model': svm.SVC(gamma='auto', probability=True),
'params': {
'svc__C': [1, 10, 100, 1000],
'svc__kernel': ['rbf', 'linear']
}
},
'random_forest': {
'model': RandomForestClassifier(),
'params': {
'randomforestclassifier__n_estimators': [1, 5, 10]
}
},
'logistic_regression': {
'model': LogisticRegression(solver='liblinear', multi_class='auto'),
'params': {
'logisticregression__C': [1, 5, 10]
}
}
}
scores = []
for algo, mp in model_params.items():
pipe = make_pipeline(StandardScaler(), mp['model'])
clf = GridSearchCV(pipe, mp['params'], cv=5, return_train_score=False)
clf.fit(x_train, y_train)
scores.append({
'model': algo,
'best_score': clf.best_score_,
'best_params': clf.best_params_
})
print("Algorithm + parameter sweep results:")
for results in scores:
print(results)
# training the best algorithm (manually selected from above results)
pipe = Pipeline([('scaler', StandardScaler()), ('svc', SVC(kernel='linear', C=1))])
pipe.fit(x_train, y_train)
print("\n\nClassification report and confusion matrix for best combination:")
print(classification_report(y_test, pipe.predict(x_test)))
cm = confusion_matrix(y_test, pipe.predict(x_test))
plt.figure(figsize=(10, 7))
sn.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')
plt.show()
with open('dogfroghog.pickle', 'wb') as f:
pickle.dump(pipe, f)