Commit c2290e77 authored by Guillaume HEUSCH's avatar Guillaume HEUSCH

Merge branch 'mlp_algorithm' into 'master'

MLPAlgorithm PAD algorithm V1 version

See merge request !4
parents 9ad938ef c94e1d3a
Pipeline #26699 passed with stages
in 8 minutes and 4 seconds
This diff is collapsed.
......@@ -40,7 +40,7 @@ class MultiNetPatchExtractor(Extractor, object):
config_file: str
Relative name of the config file.
The path should be relative to ``config_group``,
for example: "autoencoder/net1_batl_3_layers_partial.py".
for example: "test_data/multi_net_patch_extractor_test_config.py".
This file **must** contain at least the following definitions:
......@@ -59,7 +59,7 @@ class MultiNetPatchExtractor(Extractor, object):
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".
For example: "bob.ip.pytorch_extractor".
Both ``config_file`` and ``config_group`` are used to access the
configuration module.
......
......@@ -2,6 +2,7 @@ from .CNN8 import CNN8Extractor
from .CasiaNet import CasiaNetExtractor
from .LightCNN9 import LightCNN9Extractor
from .MultiNetPatchExtractor import MultiNetPatchExtractor
from .MLPAlgorithm import MLPAlgorithm
# gets sphinx autodoc done right - don't remove it
def __appropriate__(*args):
......@@ -23,6 +24,7 @@ __appropriate__(
CasiaNetExtractor,
LightCNN9Extractor,
MultiNetPatchExtractor,
MLPAlgorithm,
)
# gets sphinx autodoc done right - don't remove it
......
#!/usr/bin/env python
# encoding: utf-8
import pkg_resources
import numpy
numpy.random.seed(10)
import os
def test_cnn8():
......@@ -65,7 +62,7 @@ def test_multi_net_patch_extractor():
# =========================================================================
# test the extractor:
CONFIG_FILE = "test_data/net1_test_config.py" # config containing an instance of Composed Transform and a Network class to be used in feature extractor
CONFIG_FILE = "test_data/multi_net_patch_extractor_test_config.py" # config containing an instance of Composed Transform and a Network class to be used in feature extractor
CONFIG_GROUP = "bob.ip.pytorch_extractor"
# use specific/unique model for each patch. Models pre-trained on CelebA and fine-tuned (3 layers) on BATL:
......@@ -98,4 +95,35 @@ def test_multi_net_patch_extractor():
# plt.show()
def test_mlp_algorithm():
"""
Test the MLPAlgorithm PAD algorithm class.
"""
from bob.ip.pytorch_extractor import MLPAlgorithm
# =========================================================================
# prepare the test data / feature vector:
features = numpy.random.randn(2, 1296)
# =========================================================================
# test the extractor:
CONFIG_FILE = "test_data/mlp_algo_test_config.py" # config containing an instance of Composed Transform and a Network class to be used in feature extractor
CONFIG_GROUP = "bob.ip.pytorch_extractor"
MODEL_FILE = None
algorithm = MLPAlgorithm(config_file = CONFIG_FILE,
config_group = CONFIG_GROUP,
model_file = MODEL_FILE,
url = None,
archive_extension = '.tar.gz',
frame_level_scores_flag = True)
# pass through encoder only, compute latent vector:
score = algorithm.project(features)
# test:
assert score.shape == (2,)
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
@author: Olegs Nikisins
"""
#==============================================================================
# Import here:
import torch
#==============================================================================
# Define parameters here:
"""
Transformations to be applied to the input 1D numpy arrays (feature vectors).
Only conversion to Tensor and unsqueezing is needed to match the input of
TwoLayerMLP network
"""
def transform(x):
"""
Convert input to Tensor and unsqueeze to match the input of
TwoLayerMLP network.
Arguments
---------
x : numpy array
1D numpy array / feature vector.
Return
------
x_transform : Tensor
Torch tensor, transformed ``x`` to be used as MLP input.
"""
return torch.Tensor(x).unsqueeze(0)
"""
Define the network to be trained as a class, named ``Network``.
Note: Do not change the name of the below class, always import as ``Network``.
"""
from bob.learn.pytorch.architectures import TwoLayerMLP as Network
"""
kwargs to be used for ``Network`` initialization. The name must be ``network_kwargs``.
"""
network_kwargs = {}
network_kwargs['in_features'] = 1296
network_kwargs['n_hidden_relu'] = 10
network_kwargs['apply_sigmoid'] = False # don't use sigmoid to make the scores more even
......@@ -8,10 +8,6 @@
from torchvision import transforms
#from bob.pad.face.database import CELEBAPadDatabase
from torch import nn
#==============================================================================
# Define parameters here:
......
......@@ -26,6 +26,7 @@ requirements:
- bob.core
- bob.bio.base
- bob.learn.pytorch
- bob.pad.base
run:
- python
- setuptools
......
......@@ -6,3 +6,5 @@ bob.bio.base
bob.learn.pytorch
torch >= 0.4.0
torchvision >= 0.2.0
bob.pad.base
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