-
Notifications
You must be signed in to change notification settings - Fork 1
/
layers.py
67 lines (56 loc) · 2.22 KB
/
layers.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
"""prepare some layers and their default parameters for configs/*.py"""
import keras
from keras.layers import *
from keras import backend as K
from keras.regularizers import l1_l2, l2
leaky_rectify_alpha = 0.01
kernel_init_gain = 1.0
bias_init_value = 0.05
regular_factor_l1 = 0.
regular_factor_l2 = 5e-4 # weight_decay
def conv_params(filters, **kwargs):
"""default Conv2d arguments"""
args = {
'filters': filters,
'kernel_size': (3, 3),
'padding': 'same',
'activation': lambda x: keras.activations.relu(x, leaky_rectify_alpha),
'use_bias': True,
'kernel_initializer': keras.initializers.Orthogonal(gain=kernel_init_gain),
'bias_initializer': keras.initializers.constant(bias_init_value),
'kernel_regularizer': keras.regularizers.l2(regular_factor_l2),
'bias_regularizer': keras.regularizers.l2(regular_factor_l2),
}
args.update(kwargs)
return args
def pool_params(**kwargs):
"""default MaxPool2d/RMSPoolLayer arguments"""
args = {
'pool_size': 3,
'strides': (2, 2),
}
args.update(kwargs)
return args
def dense_params(num_units, **kwargs):
"""default dense layer arguments"""
args = {
'units': num_units,
'activation': lambda x: keras.activations.relu(x, leaky_rectify_alpha),
'kernel_initializer': keras.initializers.Orthogonal(gain=kernel_init_gain),
'bias_initializer': keras.initializers.constant(bias_init_value),
'kernel_regularizer': keras.regularizers.l2(regular_factor_l2),
'bias_regularizer': keras.regularizers.l2(regular_factor_l2),
}
args.update(kwargs)
return args
class RMSPoolLayer(keras.layers.pooling._Pooling2D):
"""Use RMS(Root Mean Squared) as pooling function.
origin version from https://github.com/benanne/kaggle-ndsb/blob/master/tmp_dnn.py
"""
def __init__(self, *args, **kwargs):
super(RMSPoolLayer, self).__init__(*args, **kwargs)
def _pooling_function(self, inputs, pool_size, strides,
padding, data_format):
output = K.pool2d(K.square(inputs), pool_size, strides,
padding, data_format, pool_mode='avg')
return K.sqrt(output + K.epsilon())