Chopra.py 3.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 09:39:36 CEST 

"""

"""

import tensorflow as tf
from .SequenceNetwork import SequenceNetwork
from ..layers import Conv2D, FullyConnected, MaxPooling
import bob.learn.tensorflow
from bob.learn.tensorflow.initialization import Xavier
from bob.learn.tensorflow.initialization import Constant


class Chopra(SequenceNetwork):
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    """Class that creates the architecture presented in the paper:

    Chopra, Sumit, Raia Hadsell, and Yann LeCun. "Learning a similarity metric discriminatively, with application to
    face verification." 2005 IEEE Computer Society Conference on Computer Vision and Pattern Recognition (CVPR'05). Vol. 1. IEEE, 2005.

    This is modifield version of the original architecture.
    It is inspired on https://gitlab.idiap.ch/bob/xfacereclib.cnn/blob/master/lua/network.lua

    -- C1 : Convolutional, kernel = 7x7 pixels, 15 feature maps

    -- M2 : MaxPooling, 2x2

    -- HT : Hard Hyperbolic Tangent

    -- C3 : Convolutional, kernel = 6x6 pixels, 45 feature maps

    -- M4 : MaxPooling, 4x3

    -- HT : Hard Hyperbolic Tangent

    -- R  : Reshaping layer HT 5x5 => 25 (45 times; once for each feature map)

    -- L5 : Linear 25 => 250
42

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    **Parameters**

        conv1_kernel_size:

        conv1_output:

        pooling1_size:

        conv2_kernel_size:

        conv2_output:

        pooling2_size

        fc1_output:

        seed:
    """
62 63 64 65
    def __init__(self,
                 conv1_kernel_size=7,
                 conv1_output=15,

66 67 68
                 pooling1_size=[1, 2, 2, 1],


69 70 71
                 conv2_kernel_size=6,
                 conv2_output=45,

72
                 pooling2_size=[1, 4, 3, 1],
73

74 75
                 fc1_output=250,
                 default_feature_layer="fc1",
76 77

                 seed=10,
78 79
                 use_gpu=False,
                 batch_norm=False):
80 81

        super(Chopra, self).__init__(default_feature_layer=default_feature_layer,
82
                                     use_gpu=use_gpu)
83 84 85

        self.add(Conv2D(name="conv1", kernel_size=conv1_kernel_size,
                        filters=conv1_output,
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
86
                        activation=None,
87
                        weights_initialization=Xavier(seed=seed, use_gpu=self.use_gpu),
88 89
                        bias_initialization=Constant(use_gpu=self.use_gpu),
                        batch_norm=batch_norm
90
                        ))
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
91
        self.add(MaxPooling(name="pooling1", shape=pooling1_size, activation=tf.nn.tanh, batch_norm=False))
92 93 94

        self.add(Conv2D(name="conv2", kernel_size=conv2_kernel_size,
                        filters=conv2_output,
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
95
                        activation=None,
96
                        weights_initialization=Xavier(seed=seed,  use_gpu=self.use_gpu),
97 98
                        bias_initialization=Constant(use_gpu=self.use_gpu),
                        batch_norm=batch_norm))
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
99
        self.add(MaxPooling(name="pooling2", shape=pooling2_size, activation=tf.nn.tanh, batch_norm=False))
100

101
        self.add(FullyConnected(name="fc1", output_dim=fc1_output,
102 103
                                activation=None,
                                weights_initialization=Xavier(seed=seed, use_gpu=self.use_gpu),
104
                                bias_initialization=Constant(use_gpu=self.use_gpu), batch_norm=False))