Lenet.py 2.44 KB
Newer Older
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 09:39:36 CEST 

"""
Class that creates the lenet architecture
"""

import tensorflow as tf
11 12
from .SequenceNetwork import SequenceNetwork
from ..layers import Conv2D, FullyConnected, MaxPooling
13
import bob.learn.tensorflow
14 15 16
from bob.learn.tensorflow.initialization import Xavier
from bob.learn.tensorflow.initialization import Constant

Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
17

18
class Lenet(SequenceNetwork):
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
19 20 21 22 23 24 25 26 27 28

    def __init__(self,
                 conv1_kernel_size=5,
                 conv1_output=16,

                 conv2_kernel_size=5,
                 conv2_output=32,

                 fc1_output=400,
                 n_classes=10,
29
                 default_feature_layer="fc2",
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
30

31 32
                 seed=10,
                 use_gpu = False):
Tiago de Freitas Pereira's avatar
Scratch  
Tiago de Freitas Pereira committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        """
        Create all the necessary variables for this CNN

        **Parameters**
            conv1_kernel_size=5,
            conv1_output=32,

            conv2_kernel_size=5,
            conv2_output=64,

            fc1_output=400,
            n_classes=10

            seed = 10
        """
48
        super(Lenet, self).__init__(default_feature_layer=default_feature_layer)
49

50
        self.add(Conv2D(name="conv1", kernel_size=conv1_kernel_size,
51 52 53 54 55
                        filters=conv1_output,
                        activation=tf.nn.tanh,
                        weights_initialization=Xavier(seed=seed),
                        bias_initialization=Constant()
                        ))
56
        self.add(MaxPooling(name="pooling1"))
57 58 59 60 61 62
        self.add(Conv2D(name="conv2", kernel_size=conv2_kernel_size,
                        filters=conv2_output,
                        activation=tf.nn.tanh,
                        weights_initialization=Xavier(seed=seed),
                        bias_initialization=Constant()
                        ))
63
        self.add(MaxPooling(name="pooling2"))
64 65 66 67 68 69 70 71 72
        self.add(FullyConnected(name="fc1", output_dim=fc1_output,
                                activation=tf.nn.tanh,
                                weights_initialization=Xavier(seed=seed),
                                bias_initialization=Constant()
                                ))
        self.add(FullyConnected(name="fc2", output_dim=n_classes,
                                activation=None,
                                weights_initialization=Xavier(seed=seed),
                                bias_initialization=Constant()))