-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist_gesture.py
More file actions
240 lines (173 loc) · 6.9 KB
/
Copy pathmnist_gesture.py
File metadata and controls
240 lines (173 loc) · 6.9 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
# Kaustav Vats(2016048)
import cv2
import numpy as np
import csv
from tqdm import tqdm
import matplotlib.pyplot as plt
from skimage.feature import hog
from sklearn.cluster import KMeans
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
import pickle
# Reading Data --------------------
PATH = "./Data/MNIST/"
def ReadData():
train = []
train_labels = []
test = []
test_labels = []
with open(PATH + 'sign_mnist_train.csv', 'r') as csvFile:
reader = csv.reader(csvFile)
count = 0
for row in reader:
if count == 0:
count += 1
continue
train_labels.append(row.pop(0))
train.append(row)
csvFile.close()
with open(PATH + 'sign_mnist_test.csv', 'r') as csvFile:
reader = csv.reader(csvFile)
count = 0
for row in reader:
if count == 0:
count += 1
continue
test_labels.append(row.pop(0))
test.append(row)
csvFile.close()
train = np.asarray(train, dtype=np.uint8)
train_labels = np.asarray(train_labels)
test = np.asarray(test, dtype=np.uint8)
test_labels = np.asarray(test_labels)
print("Train shape: {}".format(train.shape))
print("Train Labels shape: {}".format(train_labels.shape))
print("Test shape: {}".format(test.shape))
print("Test Labels shape: {}".format(test_labels.shape))
print("Classes: {}".format(np.unique(train_labels).shape))
return train, train_labels, test, test_labels
# X_Train, Y_Train, X_Test, Y_Test = ReadData()
# np.save(PATH + "X_Train.npy", X_Train)
# np.save(PATH + "Y_Train.npy", Y_Train)
# np.save(PATH + "X_Test.npy", X_Test)
# np.save(PATH + "Y_Test.npy", Y_Test)
X_Train = np.load(PATH + "X_Train.npy")
Y_Train = np.load(PATH + "Y_Train.npy")
X_Test = np.load(PATH + "X_Test.npy")
Y_Test = np.load(PATH + "Y_Test.npy")
print("[+] Data Reading done")
# data Visualization -------------------------
def Visualize(x_data, n_images=5):
for i in range(n_images):
image = np.reshape(x_data[i], (28, 28))
cv2.imwrite(PATH + "temp_" + str(i) + ".png", image)
# Visualize(X_Train)
# Preprocessing ---------------------------------
print("[+] PreProcessing...")
print("[+] PreProcessing Done")
# Features Extraction ---------------------------
print("[+] Features Extraction...")
# Sift Features
def SiftFeatures(x_data, y_data):
sift = cv2.xfeatures2d.SIFT_create()
features = []
labels = []
for i in range(x_data.shape[0]):
image = np.reshape(x_data[i], (28, 28))
_, des = sift.detectAndCompute(image, None)
if des is None:
continue
for d in des:
features.append(d)
labels.append(y_data[i])
features = np.asarray(features)
labels = np.asarray(labels)
print("Features shape: {}".format(features.shape))
print("Labels shape: {}".format(labels.shape))
return features, labels
def Hog(x_data, y_data, n_images=5):
features = []
for i in range(x_data.shape[0]):
image = np.reshape(x_data[i], (28, 28))
fd, hog_image = hog(image, orientations=12, pixels_per_cell=(8, 8), cells_per_block=(2, 2), block_norm="L2-Hys", visualize=True)
if i < n_images:
cv2.imwrite(PATH + "hog_" + str(i) + ".png", hog_image)
features.append(fd)
features = np.asarray(features)
print("Features shape: {}".format(features.shape))
return features, y_data
# NX_Train, NY_Train = SiftFeatures(X_Train, Y_Train)
# NX_Train, NY_Train = Hog(X_Train, Y_Train)
# NX_Test, NY_Test = Hog(X_Test, Y_Test)
# np.save(PATH + "NX_Train.npy", NX_Train)
# np.save(PATH + "NY_Train.npy", NY_Train)
# np.save(PATH + "NX_Test.npy", NX_Test)
# np.save(PATH + "NY_Test.npy", NY_Test)
NX_Train = np.load(PATH + "NX_Train.npy")
NY_Train = np.load(PATH + "NY_Train.npy")
NX_Test = np.load(PATH + "NX_Test.npy")
NY_Test = np.load(PATH + "NY_Test.npy")
print("[+] Features Extraction Done")
# Unsupervised Learning ----------------------------------------
# print("[+] Unsupervised Learning[KMeans]...")
# kmeans = KMeans(n_clusters=100, n_jobs=-1)
# kmeans.fit(NX_Train)
# pickle.dump(kmeans, open(PATH + "Kmean.sav", 'wb'))
# # Creating Bag of Visual Words (Creating Vocaboulary)
# def Bovw(kmeans, x_data, y_data):
# sift = cv2.xfeatures2d.SIFT_create()
# features = []
# labels = []
# for i in range(x_data.shape[0]):
# image = np.reshape(x_data[i], (28, 28))
# histogram = np.zeros(len(kmeans.cluster_centers_))
# kp, des = sift.detectAndCompute(image, None)
# if des is None:
# continue
# nkp = np.size(kp)
# labels.append(y_data[i])
# for d in des:
# idx = kmeans.predict(np.reshape(d, (1, d.shape[0])))
# histogram[idx] += 1 / nkp
# features.append(histogram)
# features = np.asarray(features)
# labels = np.asarray(labels)
# print("Features shape: {}".format(features.shape))
# print("Labels shape: {}".format(labels.shape))
# return features, labels
# NX_Train, NY_Train = Bovw(kmeans, X_Train, Y_Train)
# NX_Test, NY_Test = Bovw(kmeans, X_Test, Y_Test)
# # np.save(PATH + "NX_Train.npy", NX_Train)
# # np.save(PATH + "NY_Train.npy", NY_Train)
# # np.save(PATH + "X_Test.npy", X_Train)
# # np.save(PATH + "Y_Test.npy", Y_Train)
# # NX_Train = np.load(PATH + "NX_Train.npy")
# # NY_Train = np.load(PATH + "NY_Train.npy")
# # X_Test = np.load(PATH + "X_Test.npy")
# # Y_Test = np.load(PATH + "Y_Test.npy")
# print("[+] Unsupervised Learning[KMeans] Done")
# Analysis -----------------------------------------------------
print("Train data shape: {}".format(NX_Train.shape))
print("Train data labels shape: {}".format(NY_Train.shape))
print("Test data shape: {}".format(NX_Test.shape))
print("Test data labels shape: {}".format(NY_Test.shape))
# TODO: [1] PCA, [2] LDA, [3] PCA then LDA, [4] LDA then PCA
# Training Classifier ------------------------------------------
print("[+] Training SVM ...")
clf = svm.SVC(gamma='auto', kernel="rbf")
clf.fit(NX_Train, NY_Train)
print("[+] Training SVM done")
print("[+] Testing SVM...")
Accuracy = clf.score(NX_Train, NY_Train)
print("[SVM]Accuracy[Train]:", Accuracy*100)
Accuracy = clf.score(NX_Test, NY_Test)
print("[SVM]Accuracy[Test]:", Accuracy*100)
print("[+] Testing SVM Done")
print("[+] Random Forest Classifier...")
clf = RandomForestClassifier(n_estimators=1000, random_state=0, n_jobs=-1)
clf.fit(NX_Train, NY_Train)
Accuracy = clf.score(NX_Train, NY_Train)
print("[RFC]Accuracy[Train]:", Accuracy*100)
Accuracy = clf.score(NX_Test, NY_Test)
print("[RFC]Accuracy[Test]:", Accuracy*100)
print("[+] Random Forest Classifier done")