-
Notifications
You must be signed in to change notification settings - Fork 3
/
knn.py
200 lines (184 loc) · 7.93 KB
/
knn.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import argparse
import numpy as np
import os
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import itertools
# import seaborn as sns
# sns.set_style('darkgrid')
# sns.set_palette('muted')
# sns.set_context("notebook", font_scale=1.5,
# rc={"lines.linewidth": 2.5})
# RS = 123
def main():
parser = argparse.ArgumentParser()
parser.add_argument('model', type=str)
parser.add_argument('init_checkpoint', type=str)
parser.add_argument('TUT', type=int)
parser.add_argument('set', type=str)
parsed_args = parser.parse_args()
model = parsed_args.model
init_checkpoint = parsed_args.init_checkpoint
TUT = parsed_args.TUT
datasetTesting = parsed_args.set
s = init_checkpoint.split('/')[-1]
namecheckpoint = (s.split('_')[1]).split('.ckpt')[0]
dataset = 'training'
datasetValidation = 'validation'
name = '{}_{}'.format(model, dataset)
nameTest = '{}_{}'.format(model, datasetTesting)
nameValidation = '{}_{}'.format(model, datasetValidation)
if TUT:
name = 'TUT/' + name
nameTest = 'TUT/' + nameTest
nameValidation = 'TUT/' + nameValidation
data_dir = str.join('/', init_checkpoint.split('/')[:-1] + [name]) + '_' + namecheckpoint
data_dirTest = str.join('/', init_checkpoint.split('/')[:-1] + [nameTest]) + '_' + namecheckpoint
data_dirValidation = str.join('/', init_checkpoint.split('/')[:-1] + [nameValidation]) + '_' + namecheckpoint
featurestraining = []
labelstraining = []
featuresvalidation = []
labelsvalidation = []
featurestest = []
labelstest = []
n_neighbors = [7, 9, 11, 13, 15]
num_frames = 12
sample_length = 5
if os.path.isfile('{}/{}_{}_labels.npy'.format(data_dir, model, dataset)) \
and os.path.isfile('{}/{}_{}_data.npy'.format(data_dir, model, dataset)):
featurestraining = np.load('{}/{}_{}_data.npy'.format(data_dir, model, dataset))
labelstraining = np.load('{}/{}_{}_labels.npy'.format(data_dir, model, dataset))
print(labelstraining.shape[0])
if os.path.isfile('{}/{}_{}_labels.npy'.format(data_dirTest, model, datasetTesting)) \
and os.path.isfile('{}/{}_{}_data.npy'.format(data_dirTest, model, datasetTesting)):
featurestest = np.load('{}/{}_{}_data.npy'.format(data_dirTest, model, datasetTesting))
labelstest = np.load('{}/{}_{}_labels.npy'.format(data_dirTest, model, datasetTesting))
print(labelstest.shape[0])
if os.path.isfile('{}/{}_{}_labels.npy'.format(data_dirValidation, model, datasetValidation)) \
and os.path.isfile('{}/{}_{}_data.npy'.format(data_dirValidation, model, datasetValidation)):
featuresvalidation = np.load('{}/{}_{}_data.npy'.format(data_dirValidation, model, datasetValidation))
labelsvalidation = np.load('{}/{}_{}_labels.npy'.format(data_dirValidation, model, datasetValidation))
print(labelsvalidation.shape[0])
labelstest = np.argmax(labelstest, axis=1)
labelstraining = np.argmax(labelstraining, axis=1)
labelsvalidation = np.argmax(labelsvalidation, axis=1)
labelstest2 = labelstest
# plot TSNE
# x = TSNE(n_components=2, random_state=0, verbose=1, perplexity=40, n_iter=300).fit_transform(featurestest)
# num_classes = len(np.unique(labelstest2))
# palette = np.array(sns.color_palette("hls", num_classes))
# f = plt.figure(figsize=(8, 8))
# ax = plt.subplot(aspect='equal')
# sc = ax.scatter(x[:, 0], x[:, 1], lw=0, s=40, c=palette[labelstest2.astype(np.int)])
# plt.xlim(-25, 25)
# plt.ylim(-25, 25)
# ax.axis('off')
# ax.axis('tight')
#
# # add the labels for each digit corresponding to the label
# txts = []
#
# for i in range(num_classes):
#
# # Position of each label at median of data points.
# xtext, ytext = np.median(x[labelstest2 == i, :], axis=0)
# txt = ax.text(xtext, ytext, str(i), fontsize=24)
# txts.append(txt)
#
# # plt.figure(2)
# # plt.scatter(x[:, 0], x[:, 1], c=palette[labelstest2.astype(np.int)], s=3)
# plt.figure()
bestk = 0
bestacc = 0.0
filev = open('{}_{}_{}_knn.txt'.format(data_dirValidation, model, datasetValidation), 'w')
for k in n_neighbors:
clf = KNeighborsClassifier(n_neighbors=k)
# Train the classifier
clf.fit(featurestraining, labelstraining)
y_pred = clf.predict(featuresvalidation)
counter = 0
percentage2 = labelsvalidation.shape[0]
for i in range(percentage2):
if y_pred[i] == labelsvalidation[i]:
counter += 1
perc = counter / float(percentage2)
print('Accuracy={} k={}\n'.format(perc, k))
filev.write('Accuracy={} k={}\n'.format(perc, k))
if perc > bestacc:
bestk = k
bestacc = perc
print('Best k={}\n'.format(bestk))
filev.write('Best k={}\n'.format(bestk))
filev.close()
file = open('{}_{}_{}_knn.txt'.format(data_dirTest, model, datasetTesting), 'w')
for k in n_neighbors:
clf = KNeighborsClassifier(n_neighbors=k)
# Train the classifier
clf.fit(featurestraining, labelstraining)
y_pred = clf.predict(featurestest)
counter = 0
percentage2 = labelstest.shape[0]
for i in range(percentage2):
if y_pred[i] == labelstest[i]:
counter += 1
perc = counter / float(percentage2)
print('Accuracy={} k={}\n'.format(perc, k))
file.write('Accuracy={} k={}\n'.format(perc, k))
file.close()
filefinal = open('{}_{}_{}_knn_value.txt'.format(data_dirTest, model, datasetTesting), 'w')
clf = KNeighborsClassifier(n_neighbors=bestk)
# Train the classifier
clf.fit(featurestraining, labelstraining)
y_pred = clf.predict(featurestest)
counter = 0
percentage2 = labelstest.shape[0]
for i in range(percentage2):
if y_pred[i] == labelstest[i]:
counter += 1
perc = counter / float(percentage2)
print('Accuracy={} k={}\n'.format(perc, bestk))
filefinal.write('Accuracy={} k={}\n'.format(perc, bestk))
filefinal.close()
#plot confusion matrix
# cm = confusion_matrix(labelstest, y_pred)
# classes = ['Clapping', 'Snapping fingers', 'Speaking', 'Whistling', 'Playing kendama', 'Clicking', 'Typing', \
# 'Knocking', 'Hammering', 'Peanut breaking', 'Paper ripping', 'Plastic crumpling', 'Paper shaking',
# 'Stick dropping']
# plot_confusion_matrix(cm, classes)
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=90)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
plt.show()
if __name__ == '__main__':
main()
# /data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/training.txt
# DualCamHybridNet
# /data/vsanguineti/checkpoints2/embeddingAcousticScalar2MapFarDifferentDot0.00001savemodel/model_400.ckpt