Conv2D.py 2.4 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 8 9 10 11 12 13 14 15 16
from bob.learn.tensorflow.util import *
from .Layer import Layer


class Conv2D(Layer):

    """
    2D Convolution
    """

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

        **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
        """
36 37 38 39 40 41 42 43
        super(Conv2D, self).__init__(name, activation=activation, initialization='xavier',
                                     use_gpu=use_gpu, seed=seed)
        self.kernel_size = kernel_size
        self.filters = filters
        self.initialization = initialization
        self.W = None
        self.b = None

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
44 45 46 47 48 49 50 51 52
    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]

53
        if self.W is None:
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
54
            self.W = create_weight_variables([self.kernel_size, self.kernel_size, n_channels, self.filters],
55
                                             seed=self.seed, name="w_" + str(self.name), use_gpu=self.use_gpu)
56
            if self.activation is not None:
57
                self.b = create_bias_variables([self.filters], name="b_" + str(self.name) + "bias", use_gpu=self.use_gpu)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
58 59

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

61
        with tf.name_scope(str(self.name)):
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
62
            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
63

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
64
            if self.activation is not None:
65
                non_linear_conv2d = self.activation(tf.nn.bias_add(conv2d, self.b))
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
66
                output = non_linear_conv2d
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
67 68
            else:
                output = conv2d
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
69

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