Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.ip.pytorch_extractor
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
Model registry
Operate
Environments
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.ip.pytorch_extractor
Merge requests
!4
MLPAlgorithm PAD algorithm V1 version
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
MLPAlgorithm PAD algorithm V1 version
mlp_algorithm
into
master
Overview
16
Commits
9
Pipelines
8
Changes
1
All threads resolved!
Hide all comments
Merged
Olegs NIKISINS
requested to merge
mlp_algorithm
into
master
6 years ago
Overview
16
Commits
9
Pipelines
8
Changes
1
All threads resolved!
Hide all comments
Expand
This is an MLP based PAD algorithm.
Edited
6 years ago
by
Olegs NIKISINS
0
0
Merge request reports
Compare
master
version 7
c51586ca
6 years ago
version 6
150166c7
6 years ago
version 5
55016a19
6 years ago
version 4
306e2efe
6 years ago
version 3
dba77525
6 years ago
version 2
405c8e83
6 years ago
version 1
48d8a345
6 years ago
master (base)
and
version 1
latest version
c94e1d3a
9 commits,
6 years ago
version 7
c51586ca
8 commits,
6 years ago
version 6
150166c7
7 commits,
6 years ago
version 5
55016a19
6 commits,
6 years ago
version 4
306e2efe
5 commits,
6 years ago
version 3
dba77525
4 commits,
6 years ago
version 2
405c8e83
3 commits,
6 years ago
version 1
48d8a345
1 commit,
6 years ago
1 file
+
216
−
0
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
bob/ip/pytorch_extractor/MLPAlgorithm.py
0 → 100644
+
216
−
0
Options
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 10 11:30:34 2018
@author: Olegs Nikisins
"""
# =============================================================================
# Import what is needed here:
from
bob.pad.base.algorithm
import
Algorithm
from
bob.bio.video.utils
import
FrameContainer
import
numpy
as
np
from
bob.ip.pytorch_extractor.utils
import
transform_and_net_forward
# =============================================================================
# Main body :
class
MLPAlgorithm
(
Algorithm
):
"""
This class is designed to apply a function from numpy to the input array
producing a single score.
Attributes
-----------
config_file: str
Relative name of the config file.
The path should be relative to ``config_group``,
for example:
"
!!!!!!!!!!!!!!!!! UPDATE !!!!!!!!!!!!!!!!!
"
.
!!!!!!!!!!!!!!!!! UPDATE BELOW !!!!!!!!!!!!!!!!!
This file **must** contain at least the following definitions:
Function namely ``transform``, which is a Compose transformation of
torchvision package, to be applied to the input samples.
A ``Network`` class, defining your network architecture. Note, if your
class is named differently, import it as ``Network``, for example:
``from bob.learn.pytorch.architectures import MyNetwork as Network``
Optional: ``network_kwargs`` to be used for ``Network`` initialization.
For example, if you want to use the latent embeddings of the autoencoder
class, set the kwargs accodingly. Note: in current extractor the
``forward()`` method of the ``Network`` is used for feature extraction.
config_group: str
Group/package name containing the configuration file. Usually all
configs should be stored in this folder/place.
For example:
"
bob.learn.pytorch.config
"
.
Both ``config_file`` and ``config_group`` are used to access the
configuration module.
model_file : str
A paths to the model file to be used for network initialization.
The network structure is defined in the config file.
url : str
URL to download the pretrained models from.
If models are not available in the locations specified in the
``model_file`` list, the system will try to download them from
``url``. The downloaded models **will be placed to the locations**
specified in ``model_file`` list.
For example, the pretrained model for the MLP pre-trained on
!!!!!!!!!!!!!!!!! UPDATE HERE !!!!!!!!!!!!!!!!!
Default: None
archive_extension : str
Extension of the archived files to download from above ``urls``.
Default:
'
.tar.gz
'
frame_level_scores_flag : bool
Return scores for each frame individually if True. Otherwise, return a
single score per video. Default: ``True``.
"""
def
__init__
(
self
,
config_file
,
config_group
,
model_file
,
url
=
None
,
archive_extension
=
'
.tar.gz
'
,
frame_level_scores_flag
=
True
):
super
(
MLPAlgorithm
,
self
).
__init__
(
config_file
=
config_file
,
config_group
=
config_group
,
model_file
=
model_file
,
url
=
url
,
archive_extension
=
archive_extension
,
frame_level_scores_flag
=
frame_level_scores_flag
)
self
.
config_file
=
config_file
self
.
config_group
=
config_group
self
.
model_file
=
model_file
self
.
url
=
url
self
.
archive_extension
=
archive_extension
self
.
frame_level_scores_flag
=
frame_level_scores_flag
# =========================================================================
def
project
(
self
,
feature
):
"""
This function computes a vector of scores, one score for each sample
in the input array of features. The scores are computed applying
user defined function.
Set ``performs_projection = True`` in the constructor to enable this
function. It is assured that the :py:meth:`load_projector` was
**called before** the ``project`` function is executed.
**Parameters:**
``feature`` : FrameContainer or :py:class:`numpy.ndarray`
Two types of inputs are accepted.
A Frame Container conteining the features of an individual frmaes,
see ``bob.bio.video.utils.FrameContainer``.
Or a ND feature array of the size (N_samples x dim1 x dim2 x ...).
**Returns:**
``scores`` : 1D :py:class:`numpy.ndarray`
Vector of scores. Scores for the real class are expected to be
higher, than the scores of the negative / attack class.
"""
# 1. Convert input array to numpy array if necessary.
if
isinstance
(
feature
,
FrameContainer
):
# if FrameContainer convert to 3D numpy array
feature
=
feature
.
as_array
()
# kwargs for the transform_and_net_forward function:
function_kwargs
=
{}
function_kwargs
[
"
config_file
"
]
=
self
.
config_file
function_kwargs
[
"
config_group
"
]
=
self
.
config_group
function_kwargs
[
"
model_file
"
]
=
self
.
model_file
function_kwargs
[
"
color_input_flag
"
]
=
False
scores
=
transform_and_net_forward
(
feature
=
feature
,
**
self
.
kwargs
)
return
scores
# =========================================================================
def
score
(
self
,
toscore
):
"""
Returns a probability of a sample being a real class.
**Parameters:**
``toscore`` : 1D :py:class:`numpy.ndarray`
Vector with scores for each frame/sample defining the probability
of the frame being a sample of the real class.
**Returns:**
``score`` : [:py:class:`float`]
If ``frame_level_scores_flag = False`` a single score is returned.
One score per video. This score is placed into a list, because
the ``score`` must be an iterable.
Score is a probability of a sample being a real class.
If ``frame_level_scores_flag = True`` a list of scores is returned.
One score per frame/sample.
"""
if
self
.
frame_level_scores_flag
:
score
=
list
(
toscore
)
else
:
score
=
[
np
.
mean
(
toscore
)]
# compute a single score per video
return
score
# =========================================================================
def
score_for_multiple_projections
(
self
,
toscore
):
"""
Returns a list of scores computed by the score method of this class.
**Parameters:**
``toscore`` : 1D :py:class:`numpy.ndarray`
Vector with scores for each frame/sample defining the probability
of the frame being a sample of the real class.
**Returns:**
``list_of_scores`` : [:py:class:`float`]
A list containing the scores.
"""
scores
=
self
.
score
(
toscore
)
# returns float score or 1D array of scores
if
isinstance
(
scores
,
np
.
float
):
# if a single score
list_of_scores
=
[
scores
]
else
:
list_of_scores
=
list
(
scores
)
return
list_of_scores
Loading