CoolFace
Apppublic

rlrocha/bovw-classification

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
utils.py114 linesDownload Raw Back to root
1import numpy as np2import cv23from sklearn.base import BaseEstimator, TransformerMixin4 5def visual_words(X, bovw):6 7    # X = cv2.cvtColor(X, cv2.COLOR_RGB2GRAY)8 9    N = len(X) # Number of images10    K = bovw.n_clusters # Number of visual words11 12    # SIFT object13    sift = cv2.SIFT_create()14 15    # Feature vector histogram: new and better representation of the images16    feature_vector = np.zeros((N, K))17    visial_word_pos = 0 # Position of the visual word18 19    # For each image20    for i in range(N):21 22        # Extract the keypoints descriptors of the current image23        _, curr_des = sift.detectAndCompute(X[i], None)24 25        # Define the feature vector of the current image26        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 image29        word_vector = bovw.predict(np.asarray(curr_des, dtype=float))30        31        # For each unique visual word32        for word in np.unique(word_vector):33            res = list(word_vector).count(word) # Count the number of word in word_vector34            feature_vector_curr[word] = res # Increments histogram for that word35        36        # Normalizes the current histogram37        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 vector40        visial_word_pos += 1 # Increments the position of the visual word41 42    return feature_vector43 44class ELMClassifier(BaseEstimator, TransformerMixin):45 46    def __init__(self, L, random_state=None):47        48        self.L = L # number of hidden neurons49        self.random_state = random_state # random state50 51    def fit(self, X, y=None):52 53        M = np.size(X, axis=0) # Number of examples54        N = np.size(X, axis=1) # Number of features55 56        np.random.seed(seed=self.random_state) # set random seed57 58        self.w1 = np.random.uniform(low=-1, high=1, size=(self.L, N+1)) # Weights with bias59 60        bias = np.ones(M).reshape(-1, 1) # Bias definition61        Xa = np.concatenate((bias, X), axis=1) # Input with bias62 63        S = Xa.dot(self.w1.T) # Weighted sum of hidden layer64        H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L65 66        bias = np.ones(M).reshape(-1, 1) # Bias definition67        Ha = np.concatenate((bias, H), axis=1) # Activation function with bias68 69        # One-hot encoding70        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)*D74 75        return self76 77    def predict(self, X):78 79        M = np.size(X, axis=0) # Number of examples80        N = np.size(X, axis=1) # Number of features81 82        bias = np.ones(M).reshape(-1, 1) # Bias definition83        Xa = np.concatenate((bias, X), axis=1) # Input with bias84 85        S = Xa.dot(self.w1.T) # Weighted sum of hidden layer86        H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L87 88        bias = np.ones(M).reshape(-1, 1) # Bias definition89        Ha = np.concatenate((bias, H), axis=1) # Activation function with bias90 91        y_pred = Ha.dot(self.w2.T) # Predictions92        93        # Revert one-hot encoding94        y_pred = np.argmax(y_pred, axis=1) # axis=1 means that we want to find the index of the maximum value in each row95 96        return y_pred97 98    def predict_proba(self, X):99 100        M = np.size(X, axis=0) # Number of examples101        N = np.size(X, axis=1) # Number of features102 103        bias = np.ones(M).reshape(-1, 1) # Bias definition104        Xa = np.concatenate((bias, X), axis=1) # Input with bias105 106        S = Xa.dot(self.w1.T) # Weighted sum of hidden layer107        H = np.tanh(S) # Activation function f(x) = tanh(x), dimension M X L108 109        bias = np.ones(M).reshape(-1, 1) # Bias definition110        Ha = np.concatenate((bias, H), axis=1) # Activation function with bias111 112        y_pred = Ha.dot(self.w2.T) # Predictions113 114        return y_pred