Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.fusion.base
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bob
bob.fusion.base
Commits
f643a2c2
Commit
f643a2c2
authored
8 years ago
by
Amir Mohammadi
Browse files
Options
Downloads
Patches
Plain Diff
allow for preprocessors to use label for training
parent
a2c7603f
No related branches found
No related tags found
1 merge request
!2
First Alpha release
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bob/fusion/base/algorithm/Algorithm.py
+121
-121
121 additions, 121 deletions
bob/fusion/base/algorithm/Algorithm.py
with
121 additions
and
121 deletions
bob/fusion/base/algorithm/Algorithm.py
+
121
−
121
View file @
f643a2c2
...
...
@@ -11,126 +11,126 @@ logger = bob.core.log.setup("bob.fusion.base")
class
Algorithm
(
object
):
"""
A class to be used in score fusion
"""
def
__init__
(
self
,
preprocessors
=
None
,
classifier
=
None
,
*
args
,
**
kwargs
):
"""
preprocessors: A list of preprocessors that follow the API of
:py:meth:`sklearn.preprocessing.StandardScaler`. Especially `fit_transform`
and `transform` must be implemented.
classifier: An instance of a class that implements `fit(X[, y])` and
`decision_function(X)` like:
:py:meth:`sklearn.linear_model.LogisticRegression`
kwargs : ``key=value`` pairs
A list of keyword arguments to be written in the
:py:meth:`__str__` function.
"""
A class to be used in score fusion
"""
def
__init__
(
self
,
preprocessors
=
None
,
classifier
=
None
,
*
args
,
**
kwargs
):
"""
preprocessors: A list of preprocessors that follow the API of
:py:meth:`sklearn.preprocessing.StandardScaler`. Especially `fit_transform`
and `transform` must be implemented.
classifier: An instance of a class that implements `fit(X[, y])` and
`decision_function(X)` like:
:py:meth:`sklearn.linear_model.LogisticRegression`
kwargs : ``key=value`` pairs
A list of keyword arguments to be written in the
:py:meth:`__str__` function.
"""
super
(
Algorithm
,
self
).
__init__
()
self
.
classifier
=
classifier
self
.
preprocessors
=
preprocessors
self
.
_kwargs
=
kwargs
self
.
_kwargs
[
'
preprocessors
'
]
=
preprocessors
if
classifier
is
not
self
:
self
.
_kwargs
[
'
classifier
'
]
=
classifier
def
train_preprocessors
(
self
,
X
):
"""
Train preprocessors in order.
X: numpy.ndarray with the shape of (n_samples, n_systems).
"""
if
self
.
preprocessors
is
not
None
:
for
preprocessor
in
self
.
preprocessors
:
X
=
preprocessor
.
fit_transform
(
X
)
def
preprocess
(
self
,
scores
):
"""
scores: numpy.ndarray with the shape of (n_samples, n_systems).
returns the transformed scores.
"""
if
self
.
preprocessors
is
not
None
:
for
preprocessor
in
self
.
preprocessors
:
scores
=
preprocessor
.
transform
(
scores
)
return
scores
def
train
(
self
,
train_neg
,
train_pos
,
devel_neg
=
None
,
devel_pos
=
None
):
"""
If you use development data for training you need to override this
method.
train_neg: numpy.ndarray
Negatives training data should be numpy.ndarray with the shape of
(n_samples, n_systems).
train_pos: numpy.ndarray
Positives training data should be numpy.ndarray with the shape of
(n_samples, n_systems).
devel_neg, devel_pos: numpy.ndarray
Same as ``train`` but used for development (validation).
"""
train_scores
=
np
.
vstack
((
train_neg
,
train_pos
))
neg_len
=
train_neg
.
shape
[
0
]
y
=
np
.
zeros
((
train_scores
.
shape
[
0
],),
dtype
=
'
bool
'
)
y
[
neg_len
:]
=
True
self
.
classifier
.
fit
(
train_scores
,
y
)
def
fuse
(
self
,
scores
):
"""
scores: numpy.ndarray
A numpy.ndarray with the shape of (n_samples, n_systems).
**Returns:**
fused_score: numpy.ndarray
The fused scores in shape of (n_samples,).
"""
return
self
.
classifier
.
decision_function
(
scores
)
def
__str__
(
self
):
"""
Return all parameters of this class (and its derived class) in string.
**Returns:**
info: str
A string containing the full information of all parameters of this
(and the derived) class.
"""
return
"
%s(%s)
"
%
(
str
(
self
.
__class__
),
"
,
"
.
join
(
[
"
%s=%s
"
%
(
key
,
value
)
for
key
,
value
in
self
.
_kwargs
.
items
()
if
value
is
not
None
]))
def
save
(
self
,
model_file
):
"""
Save the instance of the algorithm.
model_file: str
A path to save the file. Please note that file objects
are not accepted. The filename MUST end with
"
.pkl
"
.
Also, an algorithm may save itself in multiple files with different
extensions such as model_file and model_file[:-3]+
'
hdf5
'
.
"""
# support for bob machines
if
hasattr
(
self
,
"
custom_save
"
):
self
.
custom_save
(
model_file
)
else
:
with
open
(
model_file
,
"
wb
"
)
as
f
:
pickle
.
dump
(
type
(
self
),
f
)
pickle
.
dump
(
self
,
f
)
def
load
(
self
,
model_file
):
"""
Load the algorithm the same way it was saved.
A new instance will be returned.
**Returns:**
loaded_algorithm: Algorithm
A new instance of the loaded algorithm.
"""
with
open
(
model_file
,
"
rb
"
)
as
f
:
algo_class
=
pickle
.
load
(
f
)
algo
=
algo_class
()
if
not
hasattr
(
algo
,
'
custom_save
'
):
return
pickle
.
load
(
f
)
return
algo
.
load
(
model_file
)
super
(
Algorithm
,
self
).
__init__
()
self
.
classifier
=
classifier
self
.
preprocessors
=
preprocessors
self
.
_kwargs
=
kwargs
self
.
_kwargs
[
'
preprocessors
'
]
=
preprocessors
if
classifier
is
not
self
:
self
.
_kwargs
[
'
classifier
'
]
=
classifier
def
train_preprocessors
(
self
,
X
,
y
=
None
):
"""
Train preprocessors in order.
X: numpy.ndarray with the shape of (n_samples, n_systems).
"""
if
self
.
preprocessors
is
not
None
:
for
preprocessor
in
self
.
preprocessors
:
X
=
preprocessor
.
fit_transform
(
X
,
y
)
def
preprocess
(
self
,
scores
):
"""
scores: numpy.ndarray with the shape of (n_samples, n_systems).
returns the transformed scores.
"""
if
self
.
preprocessors
is
not
None
:
for
preprocessor
in
self
.
preprocessors
:
scores
=
preprocessor
.
transform
(
scores
)
return
scores
def
train
(
self
,
train_neg
,
train_pos
,
devel_neg
=
None
,
devel_pos
=
None
):
"""
If you use development data for training you need to override this
method.
train_neg: numpy.ndarray
Negatives training data should be numpy.ndarray with the shape of
(n_samples, n_systems).
train_pos: numpy.ndarray
Positives training data should be numpy.ndarray with the shape of
(n_samples, n_systems).
devel_neg, devel_pos: numpy.ndarray
Same as ``train`` but used for development (validation).
"""
train_scores
=
np
.
vstack
((
train_neg
,
train_pos
))
neg_len
=
train_neg
.
shape
[
0
]
y
=
np
.
zeros
((
train_scores
.
shape
[
0
],),
dtype
=
'
bool
'
)
y
[
neg_len
:]
=
True
self
.
classifier
.
fit
(
train_scores
,
y
)
def
fuse
(
self
,
scores
):
"""
scores: numpy.ndarray
A numpy.ndarray with the shape of (n_samples, n_systems).
**Returns:**
fused_score: numpy.ndarray
The fused scores in shape of (n_samples,).
"""
return
self
.
classifier
.
decision_function
(
scores
)
def
__str__
(
self
):
"""
Return all parameters of this class (and its derived class) in string.
**Returns:**
info: str
A string containing the full information of all parameters of this
(and the derived) class.
"""
return
"
%s(%s)
"
%
(
str
(
self
.
__class__
),
"
,
"
.
join
(
[
"
%s=%s
"
%
(
key
,
value
)
for
key
,
value
in
self
.
_kwargs
.
items
()
if
value
is
not
None
]))
def
save
(
self
,
model_file
):
"""
Save the instance of the algorithm.
model_file: str
A path to save the file. Please note that file objects
are not accepted. The filename MUST end with
"
.pkl
"
.
Also, an algorithm may save itself in multiple files with different
extensions such as model_file and model_file[:-3]+
'
hdf5
'
.
"""
# support for bob machines
if
hasattr
(
self
,
"
custom_save
"
):
self
.
custom_save
(
model_file
)
else
:
with
open
(
model_file
,
"
wb
"
)
as
f
:
pickle
.
dump
(
type
(
self
),
f
)
pickle
.
dump
(
self
,
f
)
def
load
(
self
,
model_file
):
"""
Load the algorithm the same way it was saved.
A new instance will be returned.
**Returns:**
loaded_algorithm: Algorithm
A new instance of the loaded algorithm.
"""
with
open
(
model_file
,
"
rb
"
)
as
f
:
algo_class
=
pickle
.
load
(
f
)
algo
=
algo_class
()
if
not
hasattr
(
algo
,
'
custom_save
'
):
return
pickle
.
load
(
f
)
return
algo
.
load
(
model_file
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment