Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Add more support for mxnet_to_coreml #14222

Merged
merged 6 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion tools/coreml/converter/_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,41 @@ def convert_activation(net, node, module, builder):
output_name = output_name)


def convert_leakyrelu(net, node, module, builder):
szha marked this conversation as resolved.
Show resolved Hide resolved
"""Convert a leakyrelu layer from mxnet to coreml.

Parameters
----------
network: net
A mxnet network object.

layer: node
Node to convert.

module: module
An module for MXNet

builder: NeuralNetworkBuilder
A neural network builder object.
"""

input_name, output_name = _get_input_output_name(net, node)
szha marked this conversation as resolved.
Show resolved Hide resolved
name = node['name']
inputs = node['inputs']
args, _ = module.get_params()
mx_non_linearity = _get_attrs(node)['act_type']
if mx_non_linearity == 'prelu':
non_linearity = 'PRELU'
params = args[_get_node_name(net, inputs[1][0])].asnumpy()
else:
raise TypeError('Unknown activation type %s' % mx_non_linearity)
builder.add_activation(name = name,
non_linearity = non_linearity,
input_name = input_name,
output_name = output_name,
params = params)


def convert_elementwise_add(net, node, module, builder):
"""Convert an elementwise add layer from mxnet to coreml.

Expand Down Expand Up @@ -335,6 +370,7 @@ def convert_convolution(net, node, module, builder):
border_mode = "valid"

n_filters = int(param['num_filter'])
n_groups = int(param['num_group']) if 'num_group' in param else 1

W = args[_get_node_name(net, inputs[1][0])].asnumpy()
if has_bias:
Expand All @@ -361,7 +397,7 @@ def convert_convolution(net, node, module, builder):
stride_height=stride_height,
stride_width=stride_width,
border_mode=border_mode,
groups=1,
groups=n_groups,
szha marked this conversation as resolved.
Show resolved Hide resolved
W=W,
b=Wb,
has_bias=has_bias,
Expand Down
3 changes: 3 additions & 0 deletions tools/coreml/converter/_mxnet_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@
'elemwise_add' : _layers.convert_elementwise_add,
'Reshape' : _layers.convert_reshape,
'Deconvolution' : _layers.convert_deconvolution,
'LeakyReLU' : _layers.convert_leakyrelu,
}

_MXNET_SKIP_LAYERS = [
'_MulScalar',
'Dropout',
'_minus_scalar',
'_mul_scalar',
]

def _mxnet_remove_batch(input_data):
Expand Down
51 changes: 51 additions & 0 deletions tools/coreml/test/test_mxnet_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ def test_tiny_tanh_activation_random_input(self):
net = mx.sym.Activation(net, name='tanh1', act_type="tanh")
self._test_mxnet_model(net, input_shape=input_shape, mode='random')

def test_tiny_prelu_leakyrelu_random_input(self):
np.random.seed(1988)
input_shape = (1, 10)
net = mx.sym.Variable('data')
net = mx.sym.FullyConnected(data=net, name='fc1', num_hidden=5)
gamma = mx.sym.Variable('gamma')
net = mx.sym.LeakyReLU(net, gamma=gamma, name='prelu1', act_type="prelu")
szha marked this conversation as resolved.
Show resolved Hide resolved
self._test_mxnet_model(net, input_shape=input_shape, mode='random')

def test_really_tiny_conv_random_input(self):
np.random.seed(1988)
input_shape = (1, 1, 10, 10)
Expand Down Expand Up @@ -408,6 +417,26 @@ def test_really_tiny_conv_random_input_multi_filter(self):
)
self._test_mxnet_model(net, input_shape=input_shape, mode='random')

def test_really_tiny_conv_random_input_multi_group(self):
np.random.seed(1988)
input_shape = (1, 16, 10, 10)
num_filter = 16
num_group = 4
kernel = (1, 1)
stride = (1, 1)
pad = (0, 0)
net = mx.sym.Variable('data')
net = mx.symbol.Convolution(
data=net,
num_filter=num_filter,
num_group=num_group,
kernel=kernel,
stride=stride,
pad=pad,
name='conv_1'
)
self._test_mxnet_model(net, input_shape=input_shape, mode='random')

def test_tiny_conv_random_3d_input(self):
np.random.seed(1988)
input_shape = (1, 3, 10, 10)
Expand Down Expand Up @@ -444,17 +473,39 @@ def test_tiny_conv_random_input_multi_filter(self):
)
self._test_mxnet_model(net, input_shape=input_shape, mode='random')

def test_tiny_conv_random_input_multi_group(self):
np.random.seed(1988)
input_shape = (1, 16, 10, 10)
num_filter = 16
num_group = 4
kernel = (5, 5)
stride = (1, 1)
pad = (0, 0)
net = mx.sym.Variable('data')
net = mx.symbol.Convolution(
data=net,
num_filter=num_filter,
num_group=num_group,
kernel=kernel,
stride=stride,
pad=pad,
name='conv_1'
)
self._test_mxnet_model(net, input_shape=input_shape, mode='random')

def test_conv_random(self):
np.random.seed(1988)
input_shape = (1, 3, 10, 10)
num_filter = 64
num_group = 1
szha marked this conversation as resolved.
Show resolved Hide resolved
kernel = (5, 5)
stride = (1, 1)
pad = (0, 0)
net = mx.sym.Variable('data')
net = mx.symbol.Convolution(
data=net,
num_filter=num_filter,
num_group=num_group,
kernel=kernel,
stride=stride,
pad=pad,
Expand Down