Commit 8f5d3bd2 by Yuwei Hu Committed by Tianqi Chen

[Keras] fix weight shape in dilated conv (#1715)

parent a6724b6e
...@@ -58,8 +58,10 @@ def _convert_activation(insym, keras_layer, _): ...@@ -58,8 +58,10 @@ def _convert_activation(insym, keras_layer, _):
return _get_elu(insym, alpha) return _get_elu(insym, alpha)
elif act_type == 'selu': elif act_type == 'selu':
# Alpha, Gamma values, obtained from https://arxiv.org/abs/1706.02515 # Alpha, Gamma values, obtained from https://arxiv.org/abs/1706.02515
alpha = keras_layer.alpha if hasattr(keras_layer, "alpha") else 1.6732 alpha = keras_layer.alpha if hasattr(keras_layer, "alpha") \
gamma = keras_layer.gamma if hasattr(keras_layer, "gamma") else 1.0507 else 1.6732632423543772848170429916717
gamma = keras_layer.gamma if hasattr(keras_layer, "gamma") \
else 1.0507009873554804934193349852946
return gamma * _get_elu(insym, alpha) return gamma * _get_elu(insym, alpha)
elif act_type == 'relu6': elif act_type == 'relu6':
return _sym.clip(insym, a_min=0, a_max=6) return _sym.clip(insym, a_min=0, a_max=6)
...@@ -155,8 +157,8 @@ def _convert_convolution(insym, keras_layer, symtab): ...@@ -155,8 +157,8 @@ def _convert_convolution(insym, keras_layer, symtab):
dilation = [keras_layer.dilation_rate[0], keras_layer.dilation_rate[1]] dilation = [keras_layer.dilation_rate[0], keras_layer.dilation_rate[1]]
else: else:
dilation = [keras_layer.dilation_rate, keras_layer.dilation_rate] dilation = [keras_layer.dilation_rate, keras_layer.dilation_rate]
kernel_h = (kernel_h - 1) * dilation[0] + 1 dilated_kernel_h = (kernel_h - 1) * dilation[0] + 1
kernel_w = (kernel_w - 1) * dilation[1] + 1 dilated_kernel_w = (kernel_w - 1) * dilation[1] + 1
stride_h, stride_w = keras_layer.strides stride_h, stride_w = keras_layer.strides
params = {'weight': symtab.new_const(weight), params = {'weight': symtab.new_const(weight),
'kernel_size': [kernel_h, kernel_w], 'kernel_size': [kernel_h, kernel_w],
...@@ -178,8 +180,8 @@ def _convert_convolution(insym, keras_layer, symtab): ...@@ -178,8 +180,8 @@ def _convert_convolution(insym, keras_layer, symtab):
elif keras_layer.padding == 'same': elif keras_layer.padding == 'same':
in_h = keras_layer.input_shape[1] in_h = keras_layer.input_shape[1]
in_w = keras_layer.input_shape[2] in_w = keras_layer.input_shape[2]
pad_t, pad_b = _get_pad_pair(in_h, kernel_h, stride_h) pad_t, pad_b = _get_pad_pair(in_h, dilated_kernel_h, stride_h)
pad_l, pad_r = _get_pad_pair(in_w, kernel_w, stride_w) pad_l, pad_r = _get_pad_pair(in_w, dilated_kernel_w, stride_w)
if pad_t == pad_b and pad_l == pad_r: if pad_t == pad_b and pad_l == pad_r:
params['padding'] = (pad_t, pad_l) params['padding'] = (pad_t, pad_l)
else: else:
......
...@@ -73,10 +73,10 @@ def test_forward_elemwise_add(): ...@@ -73,10 +73,10 @@ def test_forward_elemwise_add():
keras_model = keras.models.Model(data, y) keras_model = keras.models.Model(data, y)
verify_keras_frontend(keras_model) verify_keras_frontend(keras_model)
def test_forward_dense(): def test_forward_dense():
data = keras.layers.Input(shape=(32,32,3)) data = keras.layers.Input(shape=(32,32,1))
x = keras.layers.MaxPooling2D(pool_size=(2,2))(data) x = keras.layers.Flatten()(data)
x = keras.layers.Flatten()(x)
x = keras.layers.Dropout(0.5)(x) x = keras.layers.Dropout(0.5)(x)
x = keras.layers.Dense(10, activation='relu', kernel_initializer='uniform')(x) x = keras.layers.Dense(10, activation='relu', kernel_initializer='uniform')(x)
keras_model = keras.models.Model(data, x) keras_model = keras.models.Model(data, x)
...@@ -84,7 +84,7 @@ def test_forward_dense(): ...@@ -84,7 +84,7 @@ def test_forward_dense():
def test_forward_pool(): def test_forward_pool():
data = keras.layers.Input(shape=(2,2,1)) data = keras.layers.Input(shape=(32,32,1))
# maxpool # maxpool
x = keras.layers.MaxPooling2D((3, 3), strides=(1, 1), padding='same')(data) x = keras.layers.MaxPooling2D((3, 3), strides=(1, 1), padding='same')(data)
keras_model = keras.models.Model(data, x) keras_model = keras.models.Model(data, x)
...@@ -95,22 +95,17 @@ def test_forward_pool(): ...@@ -95,22 +95,17 @@ def test_forward_pool():
verify_keras_frontend(keras_model) verify_keras_frontend(keras_model)
def test_forward_transpose_conv(): def test_forward_conv():
data = keras.layers.Input(shape=(32,32,3)) data = keras.layers.Input(shape=(32,32,3))
x = keras.layers.Conv2D(filters=10, kernel_size=(3,3), strides=(2,2), padding='same')(data) conv_funcs = [keras.layers.Conv2D(filters=10, kernel_size=(3,3),
x = keras.layers.DepthwiseConv2D(kernel_size=(3,3), padding='same')(x) strides=(2,2), padding='same'),
x = keras.layers.Conv2DTranspose(filters=64, kernel_size=(3,3), padding='valid')(x) keras.layers.Conv2D(filters=10, kernel_size=(3,3),
x = keras.layers.GlobalMaxPooling2D()(x) dilation_rate=(2,2), padding='same'),
keras_model = keras.models.Model(data, x) keras.layers.DepthwiseConv2D(kernel_size=(3,3), padding='same'),
verify_keras_frontend(keras_model) keras.layers.Conv2DTranspose(filters=10, kernel_size=(3,3), padding='valid'),
keras.layers.SeparableConv2D(filters=10, kernel_size=(3,3), padding='same')]
for conv_func in conv_funcs:
def test_forward_separable_conv(): x = conv_func(data)
data = keras.layers.Input(shape=(32,32,3))
x = keras.layers.SeparableConv2D(filters=10, kernel_size=(3,3),
padding='same', activation='relu')(data)
x = keras.layers.BatchNormalization(scale=True, center=False,
beta_initializer='uniform', gamma_initializer='uniform')(x)
x = keras.layers.GlobalAveragePooling2D()(x) x = keras.layers.GlobalAveragePooling2D()(x)
keras_model = keras.models.Model(data, x) keras_model = keras.models.Model(data, x)
verify_keras_frontend(keras_model) verify_keras_frontend(keras_model)
...@@ -123,6 +118,7 @@ def test_forward_upsample(): ...@@ -123,6 +118,7 @@ def test_forward_upsample():
keras_model = keras.models.Model(data, x) keras_model = keras.models.Model(data, x)
verify_keras_frontend(keras_model) verify_keras_frontend(keras_model)
def test_forward_reshape(): def test_forward_reshape():
data = keras.layers.Input(shape=(32,32,3)) data = keras.layers.Input(shape=(32,32,3))
x = keras.layers.Reshape(target_shape=(32,32,3))(data) x = keras.layers.Reshape(target_shape=(32,32,3))(data)
...@@ -168,6 +164,7 @@ def test_forward_mobilenet(): ...@@ -168,6 +164,7 @@ def test_forward_mobilenet():
input_shape=(224,224,3), classes=1000) input_shape=(224,224,3), classes=1000)
verify_keras_frontend(keras_model) verify_keras_frontend(keras_model)
def test_forward_activations(): def test_forward_activations():
data = keras.layers.Input(shape=(32,32,3)) data = keras.layers.Input(shape=(32,32,3))
weights = np.random.rand(1, 32, 32, 3) weights = np.random.rand(1, 32, 32, 3)
...@@ -187,10 +184,11 @@ def test_forward_activations(): ...@@ -187,10 +184,11 @@ def test_forward_activations():
keras.layers.Activation('linear')] keras.layers.Activation('linear')]
for act_func in act_funcs: for act_func in act_funcs:
x = act_func(data) x = act_func(data)
x = keras.layers.GlobalMaxPooling2D()(x) x = keras.layers.GlobalAveragePooling2D()(x)
keras_model = keras.models.Model(data, x) keras_model = keras.models.Model(data, x)
verify_keras_frontend(keras_model) verify_keras_frontend(keras_model)
def test_forward_multi_inputs(): def test_forward_multi_inputs():
data1 = keras.layers.Input(shape=(32,32,3)) data1 = keras.layers.Input(shape=(32,32,3))
data2 = keras.layers.Input(shape=(32,32,3)) data2 = keras.layers.Input(shape=(32,32,3))
...@@ -239,8 +237,7 @@ if __name__ == '__main__': ...@@ -239,8 +237,7 @@ if __name__ == '__main__':
test_forward_activations() test_forward_activations()
test_forward_dense() test_forward_dense()
test_forward_pool() test_forward_pool()
test_forward_transpose_conv() test_forward_conv()
test_forward_separable_conv()
test_forward_upsample() test_forward_upsample()
test_forward_reshape() test_forward_reshape()
test_forward_crop() test_forward_crop()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment