Bob 2.0-based training of the binary Logistic Regression model

This algorithm is a legacy one. The API has changed since its implementation. New versions and forks will need to be updated.

Algorithms have at least one input and one output. All algorithm endpoints are organized in groups. Groups are used by the platform to indicate which inputs and outputs are synchronized together. The first group is automatically synchronized with the channel defined by the block in which the algorithm is deployed.

Group: main

Endpoint Name Data Format Nature
features system/array_1d_floats/1 Input
class system/text/1 Input
classifier tutorial/linear_machine/1 Output
xxxxxxxxxx
86
 
1
import bob
2
import bob.learn.linear
3
import numpy
4
import math 
5
6
def calc_mean(c0, c1=[]):
7
    """ Calculates the mean of the data."""
8
    if c1 != []:
9
        return (numpy.mean(c0, 0) + numpy.mean(c1, 0)) / 2.
10
    else:
11
        return numpy.mean(c0, 0)
12
13
def calc_std(c0, c1=[]):
14
    """ Calculates the variance of the data."""
15
    if c1 == []:
16
        return numpy.std(c0, 0)
17
    prop = float(len(c0)) / float(len(c1))
18
    if prop < 1:
19
        p0 = int(math.ceil(1/prop))
20
        p1 = 1
21
    else:
22
        p0 = 1
23
        p1 = int(math.ceil(prop))
24
    return numpy.std(numpy.vstack(p0*[c0] + p1*[c1]), 0)
25
26
"""
27
@param c0
28
@param c1
29
@param nonStdZero if the std was zero, convert to one. This will avoid a zero division
30
"""
31
def calc_mean_std(c0, c1=[], nonStdZero=False):
32
    """ Calculates both the mean of the data. """
33
    mi = calc_mean(c0,c1)
34
    std = calc_std(c0,c1)
35
    if(nonStdZero):
36
        std[std==0] = 1
37
38
    return mi, std
39
40
def zeromean_unitvar_norm(data, mean, std):
41
    """ Normalized the data with zero mean and unit variance. Mean and variance are in numpy.ndarray format"""
42
    return numpy.divide(data-mean,std)
43
44
45
class Algorithm:
46
47
    def __init__(self):
48
        self.positives = []
49
        self.negatives = []
50
51
52
    def process(self, inputs, outputs):
53
54
        # accumulates the input data in different
55
        # containers for hit or miss
56
        feature_vector = inputs["features"].data.value
57
        if inputs["class"].data.text == 'real':
58
            self.positives.append(feature_vector)
59
        else:
60
            self.negatives.append(feature_vector)
61
62
        if not(inputs.hasMoreData()):
63
            # trains the LDA projection
64
            self.positives = numpy.vstack(self.positives)
65
            self.negatives = numpy.vstack(self.negatives)
66
67
            mean, std = calc_mean_std(self.positives, self.negatives, nonStdZero=True)
68
            self.positives = zeromean_unitvar_norm(self.positives, mean, std)
69
            self.negatives = zeromean_unitvar_norm(self.negatives, mean, std)
70
71
            trainer = bob.learn.linear.CGLogRegTrainer()
72
73
            machine = trainer.train(self.negatives, self.positives)
74
            if mean is not None and std is not None:
75
                machine.input_subtract = mean
76
                machine.input_divide = std
77
78
            # outputs data
79
            outputs["classifier"].write({
80
                'input_subtract': machine.input_subtract,
81
                'input_divide':   machine.input_divide,
82
                'weights':        machine.weights,
83
                'biases':         machine.biases,
84
            })
85
86
        return True

The code for this algorithm in Python
The ruler at 80 columns indicate suggested POSIX line breaks (for readability).
The editor will automatically enlarge to accomodate the entirety of your input
Use keyboard shortcuts for search/replace and faster editing. For example, use Ctrl-F (PC) or Cmd-F (Mac) to search through this box

This algorithm will run a Logistic Regression model [LR] for a binary classification problem using features as inputs.

The inputs take feature vectors as input and a text flag indicating if the data is a hit (it should be 'real') or a miss.

[LR]https://en.wikipedia.org/wiki/Logistic_regression

Experiments

Updated Name Databases/Protocols Analyzers
pkorshunov/pkorshunov/isv-asv-pad-fusion-complete/1/asv_isv-pad_lbp_hist_ratios_lr-fusion_lr-pa_aligned avspoof/2@physicalaccess_verification,avspoof/2@physicalaccess_verify_train,avspoof/2@physicalaccess_verify_train_spoof,avspoof/2@physicalaccess_antispoofing,avspoof/2@physicalaccess_verification_spoof pkorshunov/spoof-score-fusion-roc_hist/1
pkorshunov/pkorshunov/speech-pad-simple/1/speech-pad_lbp_hist_ratios_lr-pa_aligned avspoof/2@physicalaccess_antispoofing pkorshunov/simple_antispoofing_analyzer/4
pkorshunov/pkorshunov/speech-antispoofing-baseline/1/btas2016-baseline-pa avspoof/1@physicalaccess_antispoofing pkorshunov/simple_antispoofing_analyzer/2
Created with Raphaël 2.1.2[compare]pkorshunov/logistic-regression/12016Mar8

This table shows the number of times this algorithm has been successfully run using the given environment. Note this does not provide sufficient information to evaluate if the algorithm will run when submitted to different conditions.

Terms of Service | Contact Information | BEAT platform version 2.2.1b0 | © Idiap Research Institute - 2013-2025