Commit ab7cb609 authored by anjith2006's avatar anjith2006 Committed by Anjith GEORGE
Browse files

Mods

parent cc42de3f
......@@ -8,8 +8,6 @@ import pickle
import logging
from bob.pad.base.utils import convert_frame_cont_to_array, convert_list_of_frame_cont_to_array
#==============================================================================
class ScikitClassifier(Algorithm):
"""
......@@ -98,15 +96,11 @@ class ScikitClassifier(Algorithm):
self.one_class = one_class
if self.one_class:
assert('score_samples' in dir(clf))
else:
assert('predict_proba' in dir(clf))
#==========================================================================
def _normalize(self, features, train=False):
"""
The features in the input 2D array are normalized.
......@@ -133,11 +127,10 @@ class ScikitClassifier(Algorithm):
features_norm = self.scaler.transform(features)
else:
features_norm=features.copy()
features_norm = features.copy()
return features_norm
#==========================================================================
def norm_train_data(self, real, attack):
"""
Mean-std normalization of input data arrays. The mean and std normalizers
......@@ -170,7 +163,7 @@ class ScikitClassifier(Algorithm):
else:
all_data=np.vstack([real, attack])
all_data = np.vstack([real, attack])
_ = self._normalize(all_data, train=True)
......@@ -180,7 +173,6 @@ class ScikitClassifier(Algorithm):
return real_norm, attack_norm
#==========================================================================
def train_clf(self, real, attack):
"""
Train Scikit classifier given real and attack classes. Prior to training
......@@ -198,7 +190,7 @@ class ScikitClassifier(Algorithm):
"""
if self.one_class:
assert(self.norm_on_bonafide==True)
assert(self.norm_on_bonafide == True)
real, attack = self.norm_train_data(real, attack)
# real and attack - are now mean-std normalized
......@@ -207,9 +199,9 @@ class ScikitClassifier(Algorithm):
if self.one_class:
X=real.copy()
X = real.copy()
Y=np.ones(len(real))
Y = np.ones(len(real))
self.clf.fit(X)
......@@ -222,7 +214,6 @@ class ScikitClassifier(Algorithm):
return True
#==========================================================================
def save_clf_and_mean_std(self, projector_file):
"""
Saves the Scikit Classifier and scaling parameters to '.obj' files.
......@@ -238,16 +229,15 @@ class ScikitClassifier(Algorithm):
"""
# Saving scikit classifier
projector_file_n = projector_file[:-5]+'_skmodel.obj'
projector_file_n = projector_file[:-5] + '_skmodel.obj'
with open(projector_file_n, 'wb') as fp:
pickle.dump(self.clf, fp)
# Saving the scaler
scaler_file_n = projector_file[:-5]+'_scaler.obj'
scaler_file_n = projector_file[:-5] + '_scaler.obj'
with open(scaler_file_n, 'wb') as fp:
pickle.dump(self.scaler, fp)
#==========================================================================
def subsample_train_videos(self, training_features, step):
"""
Uniformly select subset of frmae containes from the input list
......@@ -273,7 +263,6 @@ class ScikitClassifier(Algorithm):
return training_features_subset
#==========================================================================
def train_projector(self, training_features, projector_file):
"""
Train Scikit Classifier for feature projection and save them to files.
......@@ -326,7 +315,6 @@ class ScikitClassifier(Algorithm):
# Save the Scikit Classifier and normalizers:
self.save_clf_and_mean_std(projector_file)
#==========================================================================
def load_clf_and_mean_std(self, projector_file):
"""
Loads the machine, features mean and std from the hdf5 file.
......@@ -341,20 +329,18 @@ class ScikitClassifier(Algorithm):
"""
projector_file_n = projector_file[:-5]+'_skmodel.obj'
projector_file_n = projector_file[:-5] + '_skmodel.obj'
# Load the params of the machine:
with open(projector_file_n, 'rb') as fp:
self.clf = pickle.load(fp)
scaler_file_n = projector_file[:-5]+'_scaler.obj'
scaler_file_n = projector_file[:-5] + '_scaler.obj'
# Load parameters of the scaler:
with open(scaler_file_n, 'rb') as fp:
self.scaler = pickle.load(fp)
#==========================================================================
def load_projector(self, projector_file):
"""
The absolute name of the file is specified in projector_file string.
......@@ -371,9 +357,6 @@ class ScikitClassifier(Algorithm):
self.load_clf_and_mean_std(projector_file)
#==========================================================================
def project(self, feature):
"""
This function computes a vector of scores for each sample in the input
......@@ -415,15 +398,14 @@ class ScikitClassifier(Algorithm):
features_array = feature.copy()
features_array_norm = self._normalize(features_array, train =False)
features_array_norm = self._normalize(features_array, train=False)
if self.one_class:
scores= self.clf.score_samples(features_array_norm)
scores = self.clf.score_samples(features_array_norm)
else:
scores = self.clf.predict_proba(features_array_norm)[:, 1]
return scores
#==========================================================================
def score(self, toscore):
"""
Returns a probability of a sample being a real class.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment