Commit 74a65610 authored by Manuel Günther's avatar Manuel Günther

Added bic_intra_extra_pairs auxiliary function; updated documentation

parent adac40ef
......@@ -10,6 +10,7 @@ matrix:
- python: 3.2
env:
- NUMPYSPEC===1.7.1
- SPHINXSPEC===1.2
- python: 3.3
env:
- NUMPYSPEC===1.8.0
......@@ -19,7 +20,7 @@ before_install:
- sudo apt-get install -qq --force-yes libboost-all-dev libblitz1-dev libhdf5-serial-dev libatlas-dev libatlas-base-dev liblapack-dev texlive-latex-recommended texlive-latex-extra texlive-fonts-recommended
- if [ -n "${NUMPYSPEC}" ]; then sudo apt-get install -qq gfortran; fi
- if [ -n "${NUMPYSPEC}" ]; then pip install --find-links http://wheels.astropy.org/ --find-links http://wheels2.astropy.org/ --use-wheel numpy$NUMPYSPEC; fi
- pip install --find-links http://wheels.astropy.org/ --find-links http://wheels2.astropy.org/ --use-wheel sphinx nose==1.3.0 jinja2==2.6 cpp-coveralls pygments==1.6
- pip install --find-links http://wheels.astropy.org/ --find-links http://wheels2.astropy.org/ --use-wheel sphinx$SPHINXSPEC nose==1.3.0 jinja2==2.6 cpp-coveralls pygments==1.6
- pip install coveralls
install:
- python bootstrap-buildout.py
......
......@@ -12,6 +12,8 @@ from . import version
from .version import module as __version__
from .version import api as __api_version__
from .auxiliary import *
def get_config():
"""Returns a string containing the configuration information.
"""
......
"""Auxiliary functions to be used for preparing the training data."""
def bic_intra_extra_pairs(training_data):
"""bic_intra_extra_pairs(training_data) -> intra_pairs, extra_pairs
Computes intra-class and extra-class pairs from given training data.
The ``training_data`` should be aligned in a list of sub-lists, where each sub-list contains the data of one class.
This function will return two lists of tuples of data, where the first list contains tuples of the same class, while the second list contains tuples of different classes.
These tuples can be used to compute difference vectors, which then can be fed into the :py:meth:`bob.learn.linear.BICTrainer.train` method.
Note that in general many more ``extra_pairs?`` than ``intra_pairs`` are returned.
**Keyword parameters**
training_data : [[array_like]]
The training data, where the data for each class are enclosed in one list.
**Return values**
intra_pairs : [(array_like, array_like)]
A list of tuples of data, where both data belong to the same class, where each data element is a reference to one element of the given ``training_data``.
extra_pairs : [(array_like, array_like)]
A list of tuples of data, where both data belong to different classes, where each data element is a reference to one element of the given ``training_data``.
"""
# generate intra-class pairs
intra_pairs = []
for clazz in range(len(training_data)):
for c1 in range(len(training_data[clazz])-1):
for c2 in range (c1+1, len(training_data[clazz])):
intra_pairs.append((training_data[clazz][c1], training_data[clazz][c2]))
# generate extra-class pairs
extra_pairs = []
for clazz1 in range(len(training_data)-1):
for c1 in range(len(training_data[clazz1])):
for clazz2 in range(clazz1+1, len(training_data)):
if clazz1 != clazz2:
for c2 in range(len(training_data[clazz2])):
extra_pairs.append((training_data[clazz1][c1], training_data[clazz2][c2]))
# return a tuple of pairs
return (intra_pairs, extra_pairs)
......@@ -47,8 +47,8 @@ static auto BICMachine_doc = bob::extension::ClassDoc(
.add_prototype("bic", "")
.add_prototype("hdf5", "")
.add_parameter("use_DFFS", "bool", "[default: ``False``] Use the *Distance From Feature Space* measure as described in [Teixeira2003]_")
.add_parameter("bic", ":py:class:`bob.learn.linear.BICMachine`", "Another BICMachine to copy")
.add_parameter("hdf5", ":py:class:`bob.io.base.HD5File`", "An HDF5 file open for reading to load the Gabor jet from")
.add_parameter("bic", ":py:class:`bob.learn.linear.BICMachine`", "Another machine to copy")
.add_parameter("hdf5", ":py:class:`bob.io.base.HDF5File`", "An HDF5 file open for reading")
);
static int PyBobLearnLinearBICMachine_init(PyBobLearnLinearBICMachineObject* self, PyObject* args, PyObject* kwargs) {
......@@ -376,20 +376,19 @@ static auto train_doc = bob::extension::FunctionDoc(
"train",
"Trains the given machine to classify intrapersonal (image) difference vectors vs. extrapersonal ones",
"The given difference vectors might be the result of any (image) comparison function, e.g., the pixel difference of two images. "
"In any case, all distance vectors must have the same length",
"In any case, all distance vectors must have the same length.",
true
)
.add_prototype("intra_differences, extra_differences, machine")
.add_prototype("intra_differences, extra_differences", "machine")
.add_prototype("intra_differences, extra_differences, [machine]", "machine")
.add_parameter("intra_differences", "array_like (float, 2D)", "The input vectors, which are the result of intrapersonal (facial image) comparisons, in shape ``(#features, length)``")
.add_parameter("extra_differences", "array_like (float, 2D)", "The input vectors, which are the result of extrapersonal (facial image) comparisons, in shape ``(#features, length)``")
.add_parameter("machine", ":py:class:`bob.lear.linear.BICMachine`", "The machine to be trained")
.add_return("machine", ":py:class:`bob.lear.linear.BICMachine`", "A newly generated and trained BIC machine, where the `bob.lear.linear.BICMachine.use_DFFS` flag is set to ``False``")
.add_parameter("machine", ":py:class:`bob.learn.linear.BICMachine`", "The machine to be trained")
.add_return("machine", ":py:class:`bob.learn.linear.BICMachine`", "A newly generated and trained BIC machine, where the `bob.lear.linear.BICMachine.use_DFFS` flag is set to ``False``")
;
static PyObject* PyBobLearnLinearBICTrainer_train(PyBobLearnLinearBICTrainerObject* self, PyObject* args, PyObject* kwargs) {
try{
static char* kwlist[] = {c("intra"), c("extra"), c("machine"), 0};
char** kwlist = train_doc.kwlist();
PyBlitzArrayObject* intra,* extra;
PyBobLearnLinearBICMachineObject* machine = 0;
......@@ -505,4 +504,3 @@ bool init_BobLearnLinearBIC(PyObject* module)
PyModule_AddObject(module, "BICMachine", (PyObject*)&PyBobLearnLinearBICMachine_Type) >= 0 &&
PyModule_AddObject(module, "BICTrainer", (PyObject*)&PyBobLearnLinearBICTrainer_Type) >= 0;
}
......@@ -104,6 +104,31 @@ def test_BIC():
# But, in fact the machines should be identical.
assert machine.is_similar_to(machine2, 1e-10, 1e-15)
def test_bic_split():
# Tests the auxiliary function bic_intra_extra_pairs
data = [[1,2,3],[4,5,6],[7,8,9]]
intra_pairs, extra_pairs = bob.learn.linear.bic_intra_extra_pairs(data)
# check number of pairs
assert len(intra_pairs) == 9
assert len(extra_pairs) == 27
# check exact intra pairs
for c in data:
for v1 in c:
for v2 in c:
if v1 != v2:
# check that exactly one of the two possible pairs are inside
assert ((v1,v2) in intra_pairs) != ((v2,v1) in intra_pairs)
# check extra_pairs
for c1 in data:
for c2 in data:
if c1 != c2:
for v1 in c1:
for v2 in c2:
# check that exactly one of the two possible pairs is inside
assert ((v1,v2) in extra_pairs) != ((v2,v1) in extra_pairs)
if __name__ == '__main__':
test_IEC()
......@@ -6,9 +6,29 @@
Python API
============
This section includes information for using the pure Python API of
``bob.learn.linear``.
This section includes information for using the pure Python API of ``bob.learn.linear``.
.. automodule:: bob.learn.linear
Classes
-------
.. autosummary::
bob.learn.linear.Machine
bob.learn.linear.PCATrainer
bob.learn.linear.FisherLDATrainer
bob.learn.linear.WCCNTrainer
bob.learn.linear.WhiteningTrainer
bob.learn.linear.CGLogRegTrainer
bob.learn.linear.BICMachine
bob.learn.linear.BICTrainer
Functions
---------
.. autosummary::
bob.learn.linear.get_config
bob.learn.linear.bic_intra_extra_pairs
.. automodule:: bob.learn.linear
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