Conv2D.py 1.56 KB
Newer Older
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/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

import tensoflow as tf
from bob.learn.tensorflow.util import *
from .Layer import Layer


class Conv2D(Layer):

    """
    2D Convolution
    """

    def __init__(self, input, activation=None,
                 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

        **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
        """
        super(Conv2D, self).__init__(input, initialization='xavier', use_gpu=False, seed=10)
        self.activation = activation

        self.W = create_weight_variables([kernel_size, kernel_size, 1, filters],
                                         seed=seed, name="conv", use_gpu=use_gpu)

        if activation is not None:
            self.b = create_bias_variables([filters], name="bias", use_gpu=self.use_gpu)

    def get_graph(self):
        with tf.name_scope('conv'):
            conv = tf.nn.conv2d(self.input, self.W, strides=[1, 1, 1, 1], padding='SAME')

        with tf.name_scope('activation'):
            non_linearity = tf.nn.tanh(tf.nn.bias_add(conv, self.b))

        return non_linearity