Spaces:
Sleeping
Sleeping
import numpy as np | |
import cv2 | |
from sklearn.base import BaseEstimator, TransformerMixin | |
def visual_words(X, bovw): | |
# X = cv2.cvtColor(X, cv2.COLOR_RGB2GRAY) | |
N = len(X) # Number of images | |
K = bovw.n_clusters # Number of visual words | |
# SIFT object | |
sift = cv2.SIFT_create() | |
# Feature vector histogram: new and better representation of the images | |
feature_vector = np.zeros((N, K)) | |
visial_word_pos = 0 # Position of the visual word | |
# For each image | |
for i in range(N): | |
# Extract the keypoints descriptors of the current image | |
_, curr_des = sift.detectAndCompute(X[i], None) | |
# Define the feature vector of the current image | |
feature_vector_curr = np.zeros(bovw.n_clusters, dtype=np.float32) | |
# Uses the BoVW to predict the visual words of each keypoint descriptors of the current image | |
word_vector = bovw.predict(np.asarray(curr_des, dtype=float)) | |
# For each unique visual word | |
for word in np.unique(word_vector): | |
res = list(word_vector).count(word) # Count the number of word in word_vector | |
feature_vector_curr[word] = res # Increments histogram for that word | |
# Normalizes the current histogram | |
cv2.normalize(feature_vector_curr, feature_vector_curr, norm_type=cv2.NORM_L2) | |
feature_vector[visial_word_pos] = feature_vector_curr # Assined the current histogram to the feature vector | |
visial_word_pos += 1 # Increments the position of the visual word | |
return feature_vector | |
class ELMClassifier(BaseEstimator, TransformerMixin): | |
def __init__(self, L, random_state=None): | |
self.L = L # number of hidden neurons | |
self.random_state = random_state # random state | |
def fit(self, X, y=None): | |
M = np.size(X, axis=0) # Number of examples | |
N = np.size(X, axis=1) # Number of features | |
np.random.seed(seed=self.random_state) # set random seed | |
self.w1 = np.random.uniform(low=-1, high=1, size=(self.L, N+1)) # Weights with bias | |
bias = np.ones(M).reshape(-1, 1) # Bias definition | |
Xa = np.concatenate((bias, X), axis=1) # Input with bias | |
S = Xa.dot(self.w1.T) # Weighted sum of hidden layer | |
H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L | |
bias = np.ones(M).reshape(-1, 1) # Bias definition | |
Ha = np.concatenate((bias, H), axis=1) # Activation function with bias | |
# One-hot encoding | |
n_classes = len(np.unique(y)) | |
y = np.eye(n_classes)[y] | |
self.w2 = (np.linalg.pinv(Ha).dot(y)).T # w2' = pinv(Ha)*D | |
return self | |
def predict(self, X): | |
M = np.size(X, axis=0) # Number of examples | |
N = np.size(X, axis=1) # Number of features | |
bias = np.ones(M).reshape(-1, 1) # Bias definition | |
Xa = np.concatenate((bias, X), axis=1) # Input with bias | |
S = Xa.dot(self.w1.T) # Weighted sum of hidden layer | |
H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L | |
bias = np.ones(M).reshape(-1, 1) # Bias definition | |
Ha = np.concatenate((bias, H), axis=1) # Activation function with bias | |
y_pred = Ha.dot(self.w2.T) # Predictions | |
# Revert one-hot encoding | |
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 | |
return y_pred | |
def predict_proba(self, X): | |
M = np.size(X, axis=0) # Number of examples | |
N = np.size(X, axis=1) # Number of features | |
bias = np.ones(M).reshape(-1, 1) # Bias definition | |
Xa = np.concatenate((bias, X), axis=1) # Input with bias | |
S = Xa.dot(self.w1.T) # Weighted sum of hidden layer | |
H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L | |
bias = np.ones(M).reshape(-1, 1) # Bias definition | |
Ha = np.concatenate((bias, H), axis=1) # Activation function with bias | |
y_pred = Ha.dot(self.w2.T) # Predictions | |
return y_pred |