gmalivenko / onnx2keras

Convert ONNX model graph to Keras model format.
MIT License
195 stars 116 forks source link

Mobilenetv2 conversion from pytorch doesnt work with chnage_ordering=True #41

Closed jkparuchuri closed 4 years ago

jkparuchuri commented 4 years ago

@nerox8664 Getting exception 'list' object has no attribute 'shape'

At https://github.com/nerox8664/onnx2keras/blob/master/onnx2keras/converter.py#L220

Any suggestion how to fix ?

gmalivenko commented 4 years ago

Hello @jkparuchuri.

Please, provide pytorch and torchvision versions. And code snippet how you initialize and try to convert the model.

jkparuchuri commented 4 years ago

@nerox8664 torch - 1.3.1 ; torchvision 0.4.2

import numpy as np
import torch
from torchvision import models
from torch.autograd import Variable
import tensorflow as tf
import onnx
from onnx2keras import onnx_to_keras

model = models.mobilenet_v2(pretrained= True)
for m in model.modules():
    m.training = False

input_np = np.array(np.random.uniform(0, 1, (224, 224, 3)), dtype=np.float32)
input_var = Variable(torch.FloatTensor(np.expand_dims(np.rollaxis(input_np,2), axis=0)))
output = model(input_var)
torch.onnx.export(model, input_var, model_name + ".onnx", verbose=False, input_names=['input'],output_names=['output'])

tf.keras.backend.set_image_data_format('channels_first')
onnx_model = onnx.load(model_name + ".onnx")
k_model = onnx_to_keras(onnx_model, ['input'], change_ordering=True)
gmalivenko commented 4 years ago

I updated converter, please upgrade and try again.

The problem with using reduce_mean with multiple dimensions at once.

jkparuchuri commented 4 years ago

@nerox8664 Thank you. Issue resolved now.

Other minor issue , why is the converter replacing 'same' padding to zeropadding + 'valid'. Adding extra zeropadding block before every conv layer ??

jkparuchuri commented 4 years ago

@nerox8664 I tested new version with your check_torch_keras_error util, the mobilenet error is too high due to conversion. Its is something like 1e^-3 (1000 times higher) compared to something like 1e^-6 for resnet18 etc. May be there is some bug in the the newly added code to fix issue with mobilenet. Any feedback ??

gmalivenko commented 4 years ago

I get 10^-5 error on your model using random input and pretrained weights.