Spaces:
Sleeping
Sleeping
Add utils file
Browse files
utils.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
from sklearn.base import BaseEstimator, TransformerMixin
|
4 |
+
|
5 |
+
def visual_words(X, bovw):
|
6 |
+
|
7 |
+
# X = cv2.cvtColor(X, cv2.COLOR_RGB2GRAY)
|
8 |
+
|
9 |
+
N = len(X) # Number of images
|
10 |
+
K = bovw.n_clusters # Number of visual words
|
11 |
+
|
12 |
+
# SIFT object
|
13 |
+
sift = cv2.SIFT_create()
|
14 |
+
|
15 |
+
# Feature vector histogram: new and better representation of the images
|
16 |
+
feature_vector = np.zeros((N, K))
|
17 |
+
visial_word_pos = 0 # Position of the visual word
|
18 |
+
|
19 |
+
# For each image
|
20 |
+
for i in range(N):
|
21 |
+
|
22 |
+
# Extract the keypoints descriptors of the current image
|
23 |
+
_, curr_des = sift.detectAndCompute(X[i], None)
|
24 |
+
|
25 |
+
# Define the feature vector of the current image
|
26 |
+
feature_vector_curr = np.zeros(bovw.n_clusters, dtype=np.float32)
|
27 |
+
|
28 |
+
# Uses the BoVW to predict the visual words of each keypoint descriptors of the current image
|
29 |
+
word_vector = bovw.predict(np.asarray(curr_des, dtype=float))
|
30 |
+
|
31 |
+
# For each unique visual word
|
32 |
+
for word in np.unique(word_vector):
|
33 |
+
res = list(word_vector).count(word) # Count the number of word in word_vector
|
34 |
+
feature_vector_curr[word] = res # Increments histogram for that word
|
35 |
+
|
36 |
+
# Normalizes the current histogram
|
37 |
+
cv2.normalize(feature_vector_curr, feature_vector_curr, norm_type=cv2.NORM_L2)
|
38 |
+
|
39 |
+
feature_vector[visial_word_pos] = feature_vector_curr # Assined the current histogram to the feature vector
|
40 |
+
visial_word_pos += 1 # Increments the position of the visual word
|
41 |
+
|
42 |
+
return feature_vector
|
43 |
+
|
44 |
+
class ELMClassifier(BaseEstimator, TransformerMixin):
|
45 |
+
|
46 |
+
def __init__(self, L, random_state=None):
|
47 |
+
|
48 |
+
self.L = L # number of hidden neurons
|
49 |
+
self.random_state = random_state # random state
|
50 |
+
|
51 |
+
def fit(self, X, y=None):
|
52 |
+
|
53 |
+
M = np.size(X, axis=0) # Number of examples
|
54 |
+
N = np.size(X, axis=1) # Number of features
|
55 |
+
|
56 |
+
np.random.seed(seed=self.random_state) # set random seed
|
57 |
+
|
58 |
+
self.w1 = np.random.uniform(low=-1, high=1, size=(self.L, N+1)) # Weights with bias
|
59 |
+
|
60 |
+
bias = np.ones(M).reshape(-1, 1) # Bias definition
|
61 |
+
Xa = np.concatenate((bias, X), axis=1) # Input with bias
|
62 |
+
|
63 |
+
S = Xa.dot(self.w1.T) # Weighted sum of hidden layer
|
64 |
+
H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L
|
65 |
+
|
66 |
+
bias = np.ones(M).reshape(-1, 1) # Bias definition
|
67 |
+
Ha = np.concatenate((bias, H), axis=1) # Activation function with bias
|
68 |
+
|
69 |
+
# One-hot encoding
|
70 |
+
n_classes = len(np.unique(y))
|
71 |
+
y = np.eye(n_classes)[y]
|
72 |
+
|
73 |
+
self.w2 = (np.linalg.pinv(Ha).dot(y)).T # w2' = pinv(Ha)*D
|
74 |
+
|
75 |
+
return self
|
76 |
+
|
77 |
+
def predict(self, X):
|
78 |
+
|
79 |
+
M = np.size(X, axis=0) # Number of examples
|
80 |
+
N = np.size(X, axis=1) # Number of features
|
81 |
+
|
82 |
+
bias = np.ones(M).reshape(-1, 1) # Bias definition
|
83 |
+
Xa = np.concatenate((bias, X), axis=1) # Input with bias
|
84 |
+
|
85 |
+
S = Xa.dot(self.w1.T) # Weighted sum of hidden layer
|
86 |
+
H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L
|
87 |
+
|
88 |
+
bias = np.ones(M).reshape(-1, 1) # Bias definition
|
89 |
+
Ha = np.concatenate((bias, H), axis=1) # Activation function with bias
|
90 |
+
|
91 |
+
y_pred = Ha.dot(self.w2.T) # Predictions
|
92 |
+
|
93 |
+
# Revert one-hot encoding
|
94 |
+
y_pred = np.argmax(y_pred, axis=1) # axis=1 means that we want to find the index of the maximum value in each row
|
95 |
+
|
96 |
+
return y_pred
|
97 |
+
|
98 |
+
def predict_proba(self, X):
|
99 |
+
|
100 |
+
M = np.size(X, axis=0) # Number of examples
|
101 |
+
N = np.size(X, axis=1) # Number of features
|
102 |
+
|
103 |
+
bias = np.ones(M).reshape(-1, 1) # Bias definition
|
104 |
+
Xa = np.concatenate((bias, X), axis=1) # Input with bias
|
105 |
+
|
106 |
+
S = Xa.dot(self.w1.T) # Weighted sum of hidden layer
|
107 |
+
H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L
|
108 |
+
|
109 |
+
bias = np.ones(M).reshape(-1, 1) # Bias definition
|
110 |
+
Ha = np.concatenate((bias, H), axis=1) # Activation function with bias
|
111 |
+
|
112 |
+
y_pred = Ha.dot(self.w2.T) # Predictions
|
113 |
+
|
114 |
+
return y_pred
|