-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·168 lines (124 loc) · 4.35 KB
/
Copy pathmain.py
File metadata and controls
executable file
·168 lines (124 loc) · 4.35 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
# -*- coding: iso-8859-15 -*-
import sys
import cPickle
import warnings
import numpy as np
from matplotlib import pyplot as plt
from skimage import io as sio
from skimage.feature import daisy
sys.path.append('moduli/')
from dataset import Dataset
from bovw import extract_features
from bovw import describe_dataset
from sklearn.cluster import MiniBatchKMeans as KMeans
from sklearn.preprocessing import Normalizer
dataset_dir = 'dataset/'
warnings.filterwarnings("ignore")
#Caricamento
with open(dataset_dir + "features.pkl") as f:
data = cPickle.load(f)
X_training = data["X_training"]
y_training = data["y_training"]
X_test = data["X_test"]
y_test = data["y_test"]
paths_training = data["paths_training"]
paths_test = data["paths_test"]
#print data
def display_img_and_representation(x, y, pathimage, y_etichetta):
print y[y_etichetta]
img = sio.imread(pathimage)
plt.figure(figsize=(12,4))
plt.subplot(1,2,1)
plt.imshow(img)
plt.subplot(1,2,2)
plt.plot(x)
plt.show()
from sklearn.neighbors import KDTree
tree = KDTree(X_training)
def query_image(tree, paths_training, index_training, paths_test, index_test, x_test):
query_im = sio.imread(paths_test[index_test])
closest_im = sio.imread(paths_training[index_training])
plt.figure(figsize=(12,4))
plt.subplot(1,2,1)
plt.imshow(query_im)
plt.title("Query image {0}".format(paths_test[index_test]))
plt.subplot(1,2,2)
plt.imshow(closest_im)
plt.title("Closest Image {0}".format(paths_training[index_training]))
plt.show()
index_test = np.random.randint(len(X_test))
query_feature = X_test[index_test]
distance, index = tree.query(query_feature)
print distance[0,0]
index_training = index[0,0]
print index_training
def nearestimage_and_representation(paths_training, index_training, X_training, y_training, paths_test, index_test, X_test, y_test, distance, accuracy, matrix):
plt.figure()
query_image = sio.imread(paths_test[index_test])
plt.subplot2grid((2,3), (0, 0))
if(y_test[index_test] == 0):
typeofclass = "Food"
else:
typeofclass = "Not-Food"
plt.title("Query image {0}".format(paths_test[index_test]) )
plt.imshow(query_image)
plt.subplot2grid((2,3), (0, 1))
plt.title("Class {0}".format(typeofclass) )
plt.plot(X_test[index_test])
closest_im = sio.imread(paths_training[index_training])
plt.subplot2grid((2,3), (1, 0))
if(y_training[index_training] == 0):
typeofclass = "Food"
else:
typeofclass = "Not-Food"
plt.title("Closest Im {0}".format(paths_training[index_training]) )
plt.imshow(closest_im)
plt.subplot2grid((2,3), (1, 1))
plt.title("Class {0}".format(typeofclass) )
plt.plot(X_training[index_training])
plt.subplot2grid((2,3), (0, 2), rowspan=2)
plt.title("Distance {0} - Accuracy {1}%\nConfusion Matrix\n {2}".format(distance, accuracy, matrix))
plt.plot(X_test[index_test], color='red')
plt.plot(X_training[index_training], color='blue')
plt.show()
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.metrics import accuracy_score, confusion_matrix
nn1 = KNN(1)
nn1.fit(X_training, y_training)
predicted_labels = nn1.predict(X_test)
a = accuracy_score(y_test, predicted_labels)
M = confusion_matrix(y_test, predicted_labels)
'''
print "\n1‐NN, accuracy: %0.2f, Confusion Matrix:\n" %a
print "Accuracy: %0.2f" %nn1.score(X_test, y_test)
print M
'''
nearestimage_and_representation(paths_training, index_training, X_training, y_training, paths_test, index_test, X_test, y_test, round(distance[0,0],2), round(a,2), M)
'''
print y_training.shape
print y_test.shape
'''
nn1 = KNN(1)
nn1.fit(X_training, y_training)
predicted_labels = nn1.predict(X_test)
a = accuracy_score(y_test, predicted_labels)
M = confusion_matrix(y_test, predicted_labels)
print "\n1‐NN, accuracy: %0.2f, Confusion Matrix:\n" %a
print "Accuracy: %0.2f" %nn1.score(X_test, y_test)
print M
nn5 = KNN(5)
nn5.fit(X_training, y_training)
predicted_labels = nn5.predict(X_test)
a = accuracy_score(y_test, predicted_labels)
M = confusion_matrix(y_test, predicted_labels)
print "\n5‐NN, accuracy: %0.2f, Confusion Matrix:\n" %a
print "Accuracy: %0.2f" %nn5.score(X_test, y_test)
print M
nn51 = KNN(51)
nn51.fit(X_training, y_training)
predicted_labels = nn51.predict(X_test)
a = accuracy_score(y_test, predicted_labels)
M = confusion_matrix(y_test, predicted_labels)
print "\n51‐NN, accuracy: %0.2f, Confusion Matrix:\n" %a
print "Accuracy: %0.2f" %nn51.score(X_test, y_test)
print M