Conv2D.py 2.39 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 53 54
    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]

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

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

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

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

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