apache / mxnet

Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more
https://mxnet.apache.org
Apache License 2.0
20.78k stars 6.79k forks source link

from Pytorch model to ONNX model to MXNet model #15307

Open smartwell opened 5 years ago

smartwell commented 5 years ago

i try transform pytorch model to mxnet, here i just test an example, conv,bn,LRule

import mxnet as mx
import torch
import torch.nn as t_nn
import numpy as np

randx = np.random.randint(0, 1, (1, 3, 512, 512))

# creat pytorch model and save to onnx model
class Baseblock(t_nn.Module):
    def __init__(self, inc, outc, k=3, s=2, p=1, ):
        super(Baseblock, self).__init__()
        self.cnnblock_1 = t_nn.Sequential(
            t_nn.Conv2d(inc, outc, kernel_size=k, stride=s, padding=p),
            t_nn.BatchNorm2d(outc),
            t_nn.LeakyReLU(inplace=True),
        )
    def forward(self, x,):
        cnnout = self.cnnblock_1(x)
        return cnnout

input_ = torch.Tensor(randx)
t_net = Baseblock(3, 16)
t_net(input_)
torch.onnx.export(t_net, input_, 'test.proto', verbose=True)

# mxnet load model
sym, arg, aux = mx.contrib.onnx.onnx2mx.import_model.import_model("test.proto")

mx.visualization.print_summary(sym)

data_names = ['0']
ctx = mx.cpu()
shape = [1, 3, 512, 512]
mod = mx.mod.Module(symbol=sym, data_names=data_names, context=ctx, label_names=None)
mod.bind(for_training=False,  data_shapes=[('0', tuple(shape))], label_shapes=None)
mod.set_params(arg_params=arg, aux_params=aux, allow_missing=True, allow_extra=True)

input_nd = mx.ndarray.array(randx)
cc = mod.predict(input_nd)

yes, you can run it successfully. However, when you visual this net, you will see like this:

Layer (type)            Output Shape      Param #     Previous Layer                  
0(null)                                                                                        
pad0(Pad)                                                                              
convolution0(Convolution)                                                 
batchnorm0(BatchNorm)                                       
leakyrelu0(LeakyReLU)                        
Total params: 16

OP : Pad cames where? even though i debug i can't find this OP. why i discuss this details, because, TVM doesn't have this OP

mxnet-label-bot commented 5 years ago

Hey, this is the MXNet Label Bot. Thank you for submitting the issue! I will try and suggest some labels so that the appropriate MXNet community members can help resolve it. Here are my recommended labels: ONNX, Feature, Bug

smartwell commented 5 years ago

ok, l solve this problem

smartwell commented 5 years ago

i think this file has problem mxnet/contrib/onnx/onnx2mx/_op_translations.py [373 - 377]

    deconv_op = symbol.Deconvolution(inputs[0], inputs[1], bias,
                                     kernel=kernel, stride=stride, pad=padding[0:2], dilate=dilations,
                                     num_filter=num_filter, num_group=num_group, no_bias=no_bias)

    return deconv_op, new_attrs, inputs

at the same time,i change [342 -346]

conv_op = symbol.Convolution(inputs[0], inputs[1], bias,
                                 kernel=kernel, stride=stride,pad=padding[0:2], dilate=dilations,
                                 num_filter=num_filter, num_group=num_group, no_bias=no_bias)

return conv_op, new_attrs, inputs

Leaky_relu should add new_attrs = translation_utils._add_extra_attributes(new_attrs, {'act_type': 'leaky'})

leleamol commented 5 years ago

@smartwell Thanks for filing the issue as well pointing out the potential fix.

I would recommend creating a PR so that experts can review it and add the fix.

@mxnet-label-bot add [ONNX, Pending Requester Info]