Skip to content
Snippets Groups Projects
Commit 48d8a345 authored by Olegs NIKISINS's avatar Olegs NIKISINS
Browse files

MLPAlgorithm PAD algorithm V1 version

parent 9ad938ef
No related branches found
No related tags found
1 merge request!4MLPAlgorithm PAD algorithm V1 version
Pipeline #26615 passed
#!/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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment