LightCNN29.py 5.46 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
25
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/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 

import tensorflow as tf
from bob.learn.tensorflow.layers import maxout

class LightCNN29(object):
    """Creates the graph for the Light CNN-9 in 

       Wu, Xiang, et al. "A light CNN for deep face representation with noisy labels." arXiv preprint arXiv:1511.02683 (2015).
    """
    def __init__(self,
                 seed=10,
                 n_classes=10,
                 device="/cpu:0",
                 batch_norm=False):

            self.seed = seed
            self.device = device
            self.batch_norm = batch_norm
            self.n_classes = n_classes

    def __call__(self, inputs, reuse=False):
        slim = tf.contrib.slim

        #with tf.device(self.device):

        initializer = tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32, seed=self.seed)
                    
        graph = slim.conv2d(inputs, 96, [5, 5], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv1',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=48,
                       name='Maxout1')

        graph = slim.max_pool2d(graph, [2, 2], stride=2, padding="SAME", scope='Pool1')

        ####

        graph = slim.conv2d(graph, 96, [1, 1], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv2a',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=48,
                       name='Maxout2a')

        graph = slim.conv2d(graph, 192, [3, 3], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv2',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=96,
                       name='Maxout2')

        graph = slim.max_pool2d(graph, [2, 2], stride=2, padding="SAME", scope='Pool2')

        #####

        graph = slim.conv2d(graph, 192, [1, 1], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv3a',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=96,
                       name='Maxout3a')

        graph = slim.conv2d(graph, 384, [3, 3], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv3',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=192,
                       name='Maxout3')

        graph = slim.max_pool2d(graph, [2, 2], stride=2, padding="SAME", scope='Pool3')

        #####

        graph = slim.conv2d(graph, 384, [1, 1], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv4a',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=192,
                       name='Maxout4a')

        graph = slim.conv2d(graph, 256, [3, 3], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv4',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=128,
                       name='Maxout4')

        #####

        graph = slim.conv2d(graph, 256, [1, 1], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv5a',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=128,
                       name='Maxout5a')

        graph = slim.conv2d(graph, 256, [3, 3], activation_fn=tf.nn.relu,
                            stride=1,
                            weights_initializer=initializer,
                            scope='Conv5',
                            reuse=reuse)

        graph = maxout(graph,
                       num_units=128,
                       name='Maxout5')

        graph = slim.max_pool2d(graph, [2, 2], stride=2, padding="SAME", scope='Pool4')

        graph = slim.flatten(graph, scope='flatten1')

        #graph = slim.dropout(graph, keep_prob=0.3, scope='dropout1')

        graph = slim.fully_connected(graph, 512,
                                     weights_initializer=initializer,
                                     activation_fn=tf.nn.relu,
                                     scope='fc1',
                                     reuse=reuse)
        graph = maxout(graph,
                       num_units=256,
                       name='Maxoutfc1')
        
        graph = slim.dropout(graph, keep_prob=0.3, scope='dropout1')

        graph = slim.fully_connected(graph, self.n_classes,
                                     weights_initializer=initializer,
                                     activation_fn=None,
                                     scope='fc2',
                                     reuse=reuse)

        return graph