-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDropblock.py
294 lines (266 loc) · 11.3 KB
/
Dropblock.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import keras
import keras.backend as K
class DropBlock1D(keras.layers.Layer):
"""See: https://arxiv.org/pdf/1810.12890.pdf"""
def __init__(self,
block_size,
keep_prob,
sync_channels=False,
data_format=None,
**kwargs):
"""Initialize the layer.
:param block_size: Size for each mask block.
:param keep_prob: Probability of keeping the original feature.
:param sync_channels: Whether to use the same dropout for all channels.
:param data_format: 'channels_first' or 'channels_last' (default).
:param kwargs: Arguments for parent class.
"""
super(DropBlock1D, self).__init__(**kwargs)
self.block_size = block_size
self.keep_prob = keep_prob
self.sync_channels = sync_channels
self.data_format = K.normalize_data_format(data_format)
self.input_spec = keras.engine.base_layer.InputSpec(ndim=3)
self.supports_masking = True
def get_config(self):
config = {'block_size': self.block_size,
'keep_prob': self.keep_prob,
'sync_channels': self.sync_channels,
'data_format': self.data_format}
base_config = super(DropBlock1D, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def compute_mask(self, inputs, mask=None):
return mask
def compute_output_shape(self, input_shape):
return input_shape
def _get_gamma(self, feature_dim):
"""Get the number of activation units to drop"""
feature_dim = K.cast(feature_dim, K.floatx())
block_size = K.constant(self.block_size, dtype=K.floatx())
return ((1.0 - self.keep_prob) / block_size) * (feature_dim / (feature_dim - block_size + 1.0))
def _compute_valid_seed_region(self, seq_length):
positions = K.arange(seq_length)
half_block_size = self.block_size // 2
valid_seed_region = K.switch(
K.all(
K.stack(
[
positions >= half_block_size,
positions < seq_length - half_block_size,
],
axis=-1,
),
axis=-1,
),
K.ones((seq_length,)),
K.zeros((seq_length,)),
)
return K.expand_dims(K.expand_dims(valid_seed_region, axis=0), axis=-1)
def _compute_drop_mask(self, shape):
seq_length = shape[1]
mask = K.random_binomial(shape, p=self._get_gamma(seq_length))
mask *= self._compute_valid_seed_region(seq_length)
mask = keras.layers.MaxPool1D(
pool_size=self.block_size,
padding='same',
strides=1,
data_format='channels_last',
)(mask)
return 1.0 - mask
def call(self, inputs, training=None):
def dropped_inputs():
outputs = inputs
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, [0, 2, 1])
shape = K.shape(outputs)
if self.sync_channels:
mask = self._compute_drop_mask([shape[0], shape[1], 1])
else:
mask = self._compute_drop_mask(shape)
outputs = outputs * mask *\
(K.cast(K.prod(shape), dtype=K.floatx()) / K.sum(mask))
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, [0, 2, 1])
return outputs
return K.in_train_phase(dropped_inputs, inputs, training=training)
class DropBlock2D(keras.layers.Layer):
"""See: https://arxiv.org/pdf/1810.12890.pdf"""
def __init__(self,
block_size,
keep_prob,
sync_channels=False,
data_format=None,
**kwargs):
"""Initialize the layer.
:param block_size: Size for each mask block.
:param keep_prob: Probability of keeping the original feature.
:param sync_channels: Whether to use the same dropout for all channels.
:param data_format: 'channels_first' or 'channels_last' (default).
:param kwargs: Arguments for parent class.
"""
super(DropBlock2D, self).__init__(**kwargs)
self.block_size = block_size
self.keep_prob = keep_prob
self.sync_channels = sync_channels
self.data_format = K.normalize_data_format(data_format)
self.input_spec = keras.engine.base_layer.InputSpec(ndim=4)
self.supports_masking = True
def get_config(self):
config = {'block_size': self.block_size,
'keep_prob': self.keep_prob,
'sync_channels': self.sync_channels,
'data_format': self.data_format}
base_config = super(DropBlock2D, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def compute_mask(self, inputs, mask=None):
return mask
def compute_output_shape(self, input_shape):
return input_shape
def _get_gamma(self, height, width):
"""Get the number of activation units to drop"""
height, width = K.cast(height, K.floatx()), K.cast(width, K.floatx())
block_size = K.constant(self.block_size, dtype=K.floatx())
return ((1.0 - self.keep_prob) / (block_size ** 2)) *\
(height * width / ((height - block_size + 1.0) * (width - block_size + 1.0)))
def _compute_valid_seed_region(self, height, width):
positions = K.concatenate([
K.expand_dims(K.tile(K.expand_dims(K.arange(height), axis=1), [1, width]), axis=-1),
K.expand_dims(K.tile(K.expand_dims(K.arange(width), axis=0), [height, 1]), axis=-1),
], axis=-1)
half_block_size = self.block_size // 2
valid_seed_region = K.switch(
K.all(
K.stack(
[
positions[:, :, 0] >= half_block_size,
positions[:, :, 1] >= half_block_size,
positions[:, :, 0] < height - half_block_size,
positions[:, :, 1] < width - half_block_size,
],
axis=-1,
),
axis=-1,
),
K.ones((height, width)),
K.zeros((height, width)),
)
return K.expand_dims(K.expand_dims(valid_seed_region, axis=0), axis=-1)
def _compute_drop_mask(self, shape):
height, width = shape[1], shape[2]
mask = K.random_binomial(shape, p=self._get_gamma(height, width))
mask *= self._compute_valid_seed_region(height, width)
mask = keras.layers.MaxPool2D(
pool_size=(self.block_size, self.block_size),
padding='same',
strides=1,
data_format='channels_last',
)(mask)
return 1.0 - mask
def call(self, inputs, training=None):
def dropped_inputs():
outputs = inputs
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, [0, 2, 3, 1])
shape = K.shape(outputs)
if self.sync_channels:
mask = self._compute_drop_mask([shape[0], shape[1], shape[2], 1])
else:
mask = self._compute_drop_mask(shape)
outputs = outputs * mask *\
(K.cast(K.prod(shape), dtype=K.floatx()) / K.sum(mask))
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, [0, 3, 1, 2])
return outputs
return K.in_train_phase(dropped_inputs, inputs, training=training)
from keras import backend as K
#K.clear_session()
from keras.engine.topology import Layer
from scipy.stats import bernoulli
import copy
import numpy as np
#
# class DropBlock2D(Layer):
# """
# Regularization Technique for Convolutional Layers.
# Pseudocode:
# 1: Input:output activations of a layer (A), block_size, γ, mode
# 2: if mode == Inference then
# 3: return A
# 4: end if
# 5: Randomly sample mask M: Mi,j ∼ Bernoulli(γ)
# 6: For each zero position Mi,j , create a spatial square mask with the center being Mi,j , the width,
# height being block_size and set all the values of M in the square to be zero (see Figure 2).
# 7: Apply the mask: A = A × M
# 8: Normalize the features: A = A × count(M)/count_ones(M)
# # Arguments
# block_size: A Python integer. The size of the block to be dropped.
# gamma: float between 0 and 1. controls how many activation units to drop.
# # References
# - [DropBlock: A regularization method for convolutional networks](
# https://arxiv.org/pdf/1810.12890v1.pdf)
# """
#
# def __init__(self, block_size, keep_prob, **kwargs):
# super(DropBlock2D, self).__init__(**kwargs)
# self.block_size = block_size
# self.keep_prob = keep_prob
#
# def call(self, x, training=None):
#
# '''
# MAKE SURE TO UNCOMMENT BELOW FOR ACTUAL USE
# '''
# # During inference, we do not Drop Blocks. (Similar to DropOut)
# # if training == None:
# # return x
#
# # Calculate Gamma
# #feat_size = int(x.shape[-1])
# feat_size_width = int(x.shape[-3])
# feat_size_height = int(x.shape[-2])
# #gamma = ((1 - self.keep_prob) / (self.block_size ** 2)) * (
# # (feat_size ** 2) / ((feat_size - self.block_size + 1) ** 2))
# gamma = ((1 - self.keep_prob) / (self.block_size ** 2)) * (
# (feat_size_width * feat_size_height) / ((feat_size_width - self.block_size + 1) * (feat_size_height - self.block_size + 1)))
#
# padding = self.block_size // 2
#
# # Randomly sample mask
# sample = bernoulli.rvs(size=(feat_size_width - (padding * 2), feat_size_height - (padding * 2)), p=gamma)
#
# # The above code creates a matrix of zeros and samples ones from the distribution
# # We would like to flip all of these values
# sample = 1 - sample
#
# # Pad the mask with ones
# sample = np.pad(sample, pad_width=padding, mode='constant', constant_values=1)
#
# # For each 0, create spatial square mask of shape (block_size x block_size)
# mask = copy.copy(sample)
# for i in range(feat_size_width):
# for j in range(feat_size_height):
# if sample[i, j] == 0:
# mask[i - padding: i + padding + 1, j - padding: j + padding + 1] = 0
#
# mask = mask.reshape((feat_size_width, feat_size_height, 1))
# #print("mask:", mask.shape)
# # Apply the mask
# x = x * np.repeat(mask, x.shape[-1], 2)
# #print("x:", x.shape)
# # Normalize the features
# count = np.prod(mask.shape)
# count_ones = np.count_nonzero(mask == 1)
# x = x * count / count_ones
#
# return x
#
# def get_config(self):
# config = {'block_size': self.block_size,
# #'gamma': self.gamma,
# #'seed': self.seed
# }
# base_config = super(DropBlock2D, self).get_config()
# return dict(list(base_config.items()) + list(config.items()))
#
# def compute_output_shape(self, input_shape):
# return input_shape