Conv2D.py 2.71 KB
Newer Older
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
1 2 3 4 5
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 17:38 CEST

6
import tensorflow as tf
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
7
from .Layer import Layer
8 9
from bob.learn.tensorflow.initialization import Xavier
from bob.learn.tensorflow.initialization import Constant
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
10 11 12 13 14 15 16 17


class Conv2D(Layer):

    """
    2D Convolution
    """

18
    def __init__(self, name, activation=None,
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
19 20
                 kernel_size=3,
                 filters=8,
21 22
                 weights_initialization=Xavier(),
                 bias_initialization=Constant(),
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
23 24 25 26
                 use_gpu=False,
                 seed=10
                 ):
        """
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
27
        Constructor
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
28 29 30 31 32 33 34 35 36 37

        **Parameters**
        input: Layer input
        activation: Tensor Flow activation
        kernel_size: Size of the convolutional kernel
        filters: Number of filters
        initialization: Initialization type
        use_gpu: Store data in the GPU
        seed: Seed for the Random number generation
        """
38 39 40 41 42 43
        super(Conv2D, self).__init__(name=name,
                                     activation=activation,
                                     weights_initialization=weights_initialization,
                                     bias_initialization=bias_initialization,
                                     use_gpu=use_gpu,
                                     seed=seed)
44 45 46 47 48
        self.kernel_size = kernel_size
        self.filters = filters
        self.W = None
        self.b = None

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
49 50 51 52 53 54 55 56 57
    def create_variables(self, input_layer):
        self.input_layer = input_layer

        # TODO: Do an assert here
        if len(input_layer.get_shape().as_list()) != 4:
            raise ValueError("The input as a convolutional layer must have 4 dimensions, "
                             "but {0} were provided".format(len(input_layer.get_shape().as_list())))
        n_channels = input_layer.get_shape().as_list()[3]

58
        if self.W is None:
59 60 61
            self.W = self.weights_initialization(shape=[self.kernel_size, self.kernel_size, n_channels, self.filters],
                                                 name="w_" + str(self.name))

62
            if self.activation is not None:
63 64
                self.b = self.bias_initialization(shape=[self.filters],
                                                  name="b_" + str(self.name) + "bias")
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
65 66

    def get_graph(self):
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
67

68
        with tf.name_scope(str(self.name)):
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
69
            conv2d = tf.nn.conv2d(self.input_layer, self.W, strides=[1, 1, 1, 1], padding='SAME')
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
70

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
71
            if self.activation is not None:
72
                non_linear_conv2d = self.activation(tf.nn.bias_add(conv2d, self.b))
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
73
                output = non_linear_conv2d
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
74 75
            else:
                output = conv2d
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
76

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
77
            return output