-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ops.py
223 lines (185 loc) · 8.32 KB
/
Ops.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
217
218
219
220
221
222
223
import numpy as np
import theano
import theano.tensor as T
from theano.tensor.signal.pool import pool_2d as pool2d, pool_3d as pool3d
from theano.tensor import shared_randomstreams
def bn(inpt, scale=1.0, shift=0.0, layer_name='', init_params=None):
if init_params is None:
gamma = theano.shared(np.asarray(scale * np.ones_like(
inpt), dtype=theano.config.floatX), name='gamma_bn_' + layer_name)
beta = theano.shared(np.asarray(shift * np.ones_like(
inpt), dtype=theano.config.floatX), name='beta_bn_' + layer_name)
else:
gamma = init_params[0]
beta = init_params[1]
mean = T.mean(inpt)
std = T.std(inpt)
return T.nnet.batch_normalization(inputs=inpt, gamma=gamma, beta=beta, mean=mean, std=std), [gamma, beta]
def conv_1d(inpt, filter_shapes, stride=1, layer_name='', mode='valid', init_params=None):
stride = (1, stride)
output_channel = filter_shapes[0]
input_channel = filter_shapes[1]
rows = 1
columns = filter_shapes[2]
if init_params is None:
filter_shape = (output_channel, input_channel, rows, columns)
receptive_field_size = rows * columns
w = theano.shared(np.asarray(
np.random.normal(
loc=0, scale=np.sqrt(2. / ((input_channel + output_channel) * receptive_field_size)),
size=filter_shape),
dtype=theano.config.floatX), name='w_conv1d_' + layer_name, borrow=True)
b = theano.shared(
np.asarray(
np.random.normal(
loc=0.0, scale=1.0, size=(
filter_shape[0],)), dtype=theano.config.floatX),
name='b_conv1d_' + layer_name, borrow=True)
else:
w = init_params[0]
b = init_params[1]
inpt = inpt.dimshuffle(0, 1, 'x', 2)
return (T.nnet.conv2d(input=inpt, filters=w, border_mode=mode, subsample=stride) + b.dimshuffle('x', 0, 'x', 'x'))[:, :, 0, :], [w, b]
def conv_3d(inpt, filter_shapes, stride=(1, 1, 1), layer_name='', mode='valid', init_params=None):
output_channel = filter_shapes[0]
input_channel = filter_shapes[1]
depth = filter_shapes[2]
rows = filter_shapes[3]
columns = filter_shapes[4]
if init_params is None:
filter_shape = (output_channel, input_channel, depth, rows, columns)
receptive_field_size = depth * rows * columns
w = theano.shared(np.asarray(
np.random.normal(
loc=0, scale=np.sqrt(2. / ((input_channel + output_channel) * receptive_field_size)),
size=filter_shape),
dtype=theano.config.floatX), name='w_conv3d_' + layer_name, borrow=True)
b = theano.shared(
np.asarray(
np.random.normal(
loc=0.0, scale=1.0, size=(
filter_shape[0],)), dtype=theano.config.floatX),
name='b_conv3d_' + layer_name, borrow=True)
else:
w = init_params[0]
b = init_params[1]
return T.nnet.conv3d(inpt, w, border_mode=mode, subsample=stride) + b.dimshuffle('x', 0, 'x', 'x', 'x'), [w, b]
def conv_2d_transpose(inpt, filter_shapes, stride=(1, 1), layer_name='', mode='valid', init_params=None):
output_channel = filter_shapes[0]
input_channel = filter_shapes[1]
rows = filter_shapes[2]
columns = filter_shapes[3]
if init_params is None:
filter_shape = (output_channel, input_channel, rows, columns)
output_shape = (output_channel, input_channel, rows + 2, columns + 2)
receptive_field_size = rows * columns
w = theano.shared(np.asarray(
np.random.normal(
loc=0, scale=np.sqrt(2. / ((input_channel + output_channel) * receptive_field_size)),
size=filter_shape),
dtype=theano.config.floatX), name='w_conv2d_' + layer_name, borrow=True)
b = theano.shared(
np.asarray(
np.random.normal(
loc=0.0, scale=1.0, size=(
filter_shape[0],)), dtype=theano.config.floatX),
name='b_conv2d_' + layer_name, borrow=True)
else:
w = init_params[0]
b = init_params[1]
return T.nnet.conv2d_transpose(input=inpt, filters=w, output_shape=())
def conv_2d(inpt, filter_shapes, stride=(1, 1), layer_name='', mode='valid', init_params=None):
output_channel = filter_shapes[0]
input_channel = filter_shapes[1]
rows = filter_shapes[2]
columns = filter_shapes[3]
if init_params is None:
filter_shape = (output_channel, input_channel, rows, columns)
receptive_field_size = rows * columns
w = theano.shared(np.asarray(
np.random.normal(
loc=0, scale=np.sqrt(2. / ((input_channel + output_channel) * receptive_field_size)),
size=filter_shape),
dtype=theano.config.floatX), name='w_conv2d_' + layer_name, borrow=True)
b = theano.shared(
np.asarray(
np.random.normal(
loc=0.0, scale=1.0, size=(
filter_shape[0],)), dtype=theano.config.floatX),
name='b_conv2d_' + layer_name, borrow=True)
else:
w = init_params[0]
b = init_params[1]
return T.nnet.conv2d(input=inpt, filters=w, border_mode=mode, subsample=stride) + b.dimshuffle('x', 0, 'x', 'x'), [w, b]
def upsample_3d(inpt, ds):
first_dim = T.extra_ops.repeat(inpt, ds, 4)
sec_dim = T.extra_ops.repeat(first_dim, ds, 3)
return T.extra_ops.repeat(sec_dim, ds, 2)
def upsample_2d(inpt, ds):
one_dim = T.extra_ops.repeat(inpt, ds, 3)
return T.extra_ops.repeat(one_dim, ds, 2), []
def flatten(inpt, ndim=2):
return T.flatten(inpt, ndim), []
def dense(inpt, nb_in, nb_out, layer_name='', init_params=None):
if init_params is None:
w = theano.shared(
np.asarray(
np.random.normal(
loc=0, scale=np.sqrt(1. / nb_out), size=[nb_in, nb_out]),
dtype=theano.config.floatX),
name='w_dense_' + layer_name, borrow=True)
b = theano.shared(np.asarray(np.random.normal(
loc=0.0, scale=1.0, size=[nb_out]),
dtype=theano.config.floatX),
name='b_dense_' + layer_name, borrow=True)
else:
w = init_params[0]
b = init_params[1]
return T.dot(inpt, w) + b, [w, b]
def pool_2d(input, ws, ignore_border=False, mode='max'):
return pool2d(input=input, ws=ws, ignore_border=ignore_border, mode=mode), []
def pool_3d(input, ws, ignore_border=False, mode='max'):
return pool3d(input=input, ws=ws, ignore_border=ignore_border, mode=mode)
def dropout(inpt, prob=0.25):
rng = shared_randomstreams.RandomStreams(
np.random.RandomState(0).randint(int(9e+5)))
mask = rng.binomial(
n=1, p=1 - prob, size=inpt.shape, dtype=theano.config.floatX)
return T.mul(inpt, mask), []
def scale(inpt, scale=1.0, shift=0.0, layer_name='', init_params=None):
"""Elemwise multiplication by gamma, add beta.
Perhaps works when initialized as scale=1 and shift=0
"""
if init_params is None:
gamma = scale * T.ones_like(inpt)
beta = shift * T.ones_like(inpt)
else:
gamma = init_params[0]
beta = init_params[1]
return T.mul(inpt, gamma) + beta, [gamma, beta]
def zero_pad_3d(inpt, padding=(1, 1, 1)):
input_shape = inpt.shape
output_shape = (input_shape[0],
input_shape[1],
input_shape[2] + 2 * padding[0],
input_shape[3] + 2 * padding[1],
input_shape[4] + 2 * padding[2])
output = T.zeros(output_shape)
indices = (slice(None),
slice(None),
slice(padding[0], input_shape[2] + padding[0]),
slice(padding[1], input_shape[3] + padding[1]),
slice(padding[2], input_shape[4] + padding[2]))
return T.set_subtensor(output[indices], inpt)
def zero_pad_2d(inpt, padding=(1, 1)):
input_shape = inpt.shape
output_shape = (input_shape[0],
input_shape[1],
input_shape[2] + 2 * padding[0],
input_shape[3] + 2 * padding[1])
output = T.zeros(output_shape)
indices = (slice(None),
slice(None),
slice(padding[0], input_shape[2] + padding[0]),
slice(padding[1], input_shape[3] + padding[1]))
return T.set_subtensor(output[indices], inpt)