Conv2D.py 2.08 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 44 45 46 47 48 49 50
        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

    def create_variables(self, input):
        self.input = input
        if self.W is None:
            self.W = create_weight_variables([self.kernel_size, self.kernel_size, 1, self.filters],
                                             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
51 52

    def get_graph(self):
53 54
        with tf.name_scope(str(self.name)):
            conv2d = tf.nn.conv2d(self.input, self.W, strides=[1, 1, 1, 1], padding='SAME')
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
55

56 57 58 59 60 61
        if self.activation is not None:
            with tf.name_scope(str(self.name) + 'activation'):
                non_linear_conv2d = tf.nn.tanh(tf.nn.bias_add(conv2d, self.b))
                self.output = non_linear_conv2d
        else:
            self.output = conv2d
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
62

63
        return self.output