-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathnnUtils.py
216 lines (193 loc) · 8.91 KB
/
nnUtils.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import tensorflow as tf
import math
from tensorflow.python.training import moving_averages
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.framework import ops
def binarize(x):
"""
Clip and binarize tensor using the straight through estimator (STE) for the gradient.
"""
g = tf.get_default_graph()
with ops.name_scope("Binarized") as name:
#x=tf.clip_by_value(x,-1,1)
with g.gradient_override_map({"Sign": "Identity"}):
return tf.sign(x)
def BinarizedSpatialConvolution(nOutputPlane, kW, kH, dW=1, dH=1,
padding='VALID', bias=True, reuse=False, name='BinarizedSpatialConvolution'):
def b_conv2d(x, is_training=True):
nInputPlane = x.get_shape().as_list()[3]
with tf.variable_scope(name, None,[x], reuse=reuse):
w = tf.get_variable('weight', [kH, kW, nInputPlane, nOutputPlane],
initializer=tf.contrib.layers.xavier_initializer_conv2d())
w = tf.clip_by_value(w,-1,1)
bin_w = binarize(w)
bin_x = binarize(x)
'''
Note that we use binarized version of the input and the weights. Since the binarized function uses STE
we calculate the gradients using bin_x and bin_w but we update w (the full precition version).
'''
out = tf.nn.conv2d(bin_x, bin_w, strides=[1, dH, dW, 1], padding=padding)
if bias:
b = tf.get_variable('bias', [nOutputPlane],initializer=tf.zeros_initializer)
out = tf.nn.bias_add(out, b)
return out
return b_conv2d
def BinarizedWeightOnlySpatialConvolution(nOutputPlane, kW, kH, dW=1, dH=1,
padding='VALID', bias=True, reuse=False, name='BinarizedWeightOnlySpatialConvolution'):
'''
This function is used only at the first layer of the model as we dont want to binarized the RGB images
'''
def bc_conv2d(x, is_training=True):
nInputPlane = x.get_shape().as_list()[3]
with tf.variable_scope(name, None, [x], reuse=reuse):
w = tf.get_variable('weight', [kH, kW, nInputPlane, nOutputPlane],
initializer=tf.contrib.layers.xavier_initializer_conv2d())
w = tf.clip_by_value(w,-1,1)
bin_w = binarize(w)
out = tf.nn.conv2d(x, bin_w, strides=[1, dH, dW, 1], padding=padding)
if bias:
b = tf.get_variable('bias', [nOutputPlane],initializer=tf.zeros_initializer)
out = tf.nn.bias_add(out, b)
return out
return bc_conv2d
def SpatialConvolution(nOutputPlane, kW, kH, dW=1, dH=1,
padding='VALID', bias=True, reuse=False, name='SpatialConvolution'):
def conv2d(x, is_training=True):
nInputPlane = x.get_shape().as_list()[3]
with tf.variable_scope(name,None,[x], reuse=reuse):
w = tf.get_variable('weight', [kH, kW, nInputPlane, nOutputPlane],
initializer=tf.contrib.layers.xavier_initializer_conv2d())
out = tf.nn.conv2d(x, w, strides=[1, dH, dW, 1], padding=padding)
if bias:
b = tf.get_variable('bias', [nOutputPlane],initializer=tf.zeros_initializer)
out = tf.nn.bias_add(out, b)
return out
return conv2d
def Affine(nOutputPlane, bias=True, name=None, reuse=False):
def affineLayer(x, is_training=True):
with tf.variable_scope(name,'Affine',[x], reuse=reuse):
reshaped = tf.reshape(x, [x.get_shape().as_list()[0], -1])
nInputPlane = reshaped.get_shape().as_list()[1]
w = tf.get_variable('weight', [nInputPlane, nOutputPlane], initializer=tf.contrib.layers.xavier_initializer())
output = tf.matmul(reshaped, w)
if bias:
b = tf.get_variable('bias', [nOutputPlane],initializer=tf.zeros_initializer)
output = tf.nn.bias_add(output, b)
return output
return affineLayer
def BinarizedAffine(nOutputPlane, bias=True, name=None, reuse=False):
def b_affineLayer(x, is_training=True):
with tf.variable_scope(name,'Affine',[x], reuse=reuse):
'''
Note that we use binarized version of the input (bin_x) and the weights (bin_w). Since the binarized function uses STE
we calculate the gradients using bin_x and bin_w but we update w (the full precition version).
'''
bin_x = binarize(x)
reshaped = tf.reshape(bin_x, [x.get_shape().as_list()[0], -1])
nInputPlane = reshaped.get_shape().as_list()[1]
w = tf.get_variable('weight', [nInputPlane, nOutputPlane], initializer=tf.contrib.layers.xavier_initializer())
w = tf.clip_by_value(w,-1,1)
bin_w = binarize(w)
output = tf.matmul(reshaped, bin_w)
if bias:
b = tf.get_variable('bias', [nOutputPlane],initializer=tf.zeros_initializer)
output = tf.nn.bias_add(output, b)
return output
return b_affineLayer
def BinarizedWeightOnlyAffine(nOutputPlane, bias=True, name=None, reuse=False):
def bwo_affineLayer(x, is_training=True):
with tf.variable_scope(name,'Affine',[x], reuse=reuse):
'''
Note that we use binarized version of the input (bin_x) and the weights (bin_w). Since the binarized function uses STE
we calculate the gradients using bin_x and bin_w but we update w (the full precition version).
'''
reshaped = tf.reshape(x, [x.get_shape().as_list()[0], -1])
nInputPlane = reshaped.get_shape().as_list()[1]
w = tf.get_variable('weight', [nInputPlane, nOutputPlane], initializer=tf.contrib.layers.xavier_initializer())
w = tf.clip_by_value(w,-1,1)
bin_w = binarize(w)
output = tf.matmul(reshaped, bin_w)
if bias:
b = tf.get_variable('bias', [nOutputPlane],initializer=tf.zeros_initializer)
output = tf.nn.bias_add(output, b)
return output
return bwo_affineLayer
def Linear(nInputPlane, nOutputPlane):
return Affine(nInputPlane, nOutputPlane, add_bias=False)
def wrapNN(f,*args,**kwargs):
def layer(x, scope='', is_training=True):
return f(x,*args,**kwargs)
return layer
def Dropout(p, name='Dropout'):
def dropout_layer(x, is_training=True):
with tf.variable_scope(name,None,[x]):
# def drop(): return tf.nn.dropout(x,p)
# def no_drop(): return x
# return tf.cond(is_training, drop, no_drop)
if is_training:
return tf.nn.dropout(x,p)
else:
return x
return dropout_layer
def ReLU(name='ReLU'):
def layer(x, is_training=True):
with tf.variable_scope(name,None,[x]):
return tf.nn.relu(x)
return layer
def HardTanh(name='HardTanh'):
def layer(x, is_training=True):
with tf.variable_scope(name,None,[x]):
return tf.clip_by_value(x,-1,1)
return layer
def View(shape, name='View'):
with tf.variable_scope(name,None,[x], reuse=reuse):
return wrapNN(tf.reshape,shape=shape)
def SpatialMaxPooling(kW, kH=None, dW=None, dH=None, padding='VALID',
name='SpatialMaxPooling'):
kH = kH or kW
dW = dW or kW
dH = dH or kH
def max_pool(x,is_training=True):
with tf.variable_scope(name,None,[x]):
return tf.nn.max_pool(x, ksize=[1, kW, kH, 1], strides=[1, dW, dH, 1], padding=padding)
return max_pool
def SpatialAveragePooling(kW, kH=None, dW=None, dH=None, padding='VALID',
name='SpatialAveragePooling'):
kH = kH or kW
dW = dW or kW
dH = dH or kH
def avg_pool(x,is_training=True):
with tf.variable_scope(name, None, [x]):
return tf.nn.avg_pool(x, ksize=[1, kW, kH, 1], strides=[1, dW, dH, 1], padding=padding)
return avg_pool
def BatchNormalization(*kargs, **kwargs):
return wrapNN(tf.contrib.layers.batch_norm, *kargs, **kwargs)
def Sequential(moduleList):
def model(x, is_training=True):
# Create model
output = x
#with tf.variable_scope(name,None,[x]):
for i,m in enumerate(moduleList):
output = m(output, is_training=is_training)
tf.add_to_collection(tf.GraphKeys.ACTIVATIONS, output)
return output
return model
def Concat(moduleList, dim=3):
def model(x, is_training=True):
# Create model
outputs = []
for i,m in enumerate(moduleList):
name = 'layer_'+str(i)
with tf.variable_scope(name, 'Layer', [x], reuse=reuse):
outputs[i] = m(x, is_training=is_training)
output = tf.concat(dim, outputs)
return output
return model
def Residual(moduleList, name='Residual'):
m = Sequential(moduleList)
def model(x, is_training=True):
# Create model
with tf.variable_scope(name,None,[x]):
output = tf.add(m(x, is_training=is_training), x)
return output
return model