gmalivenko/onnx2keras

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 3, 60, 60), found shape=(5, 60, 60, 3)

seyeeet opened this issue · 0 comments

Here is a simple steps to get tensorflow (keras) model from onnx and it throws this error: ValueError: Input 0 is incompatible with layer model: expected shape=(None, 3, 60, 60), found shape=(5, 60, 60, 3),

!echo $CONDA_PREFIX
import os
os.environ["CUDA_VISIBLE_DEVICES"]="-1"

import onnx
from onnx2keras import onnx_to_keras
from onnx_tf.backend import prepare
import tensorflow as tf

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

# random model
model = torchvision.models.resnet50(pretrained=True)
model.eval()
torch.save(model, 'model.pth')
# Loads torch model
torch_model = torch.load("model.pth")
dummy = torch.randn(5, 3, 60, 60)


# save at onnx
torch.onnx.export(torch_model,               # model being run
                  dummy,                         # model input (or a tuple for multiple inputs)
                  "resnet.onnx",             # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=13,          # the ONNX version to export the model to
                  do_constant_folding=False,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output']  )



# Load ONNX model and convert to TensorFlow format
model_onnx = onnx.load('resnet.onnx')
tf_rep = prepare(model_onnx)
# Export model as .pb file
tf_rep.export_graph('models/model_simple.pb')

# Keras
k_model = onnx_to_keras(model_onnx, ['input'])


k_model(dummy.permute(0,2,3,1).numpy())

error:

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 3, 60, 60), found shape=(5, 60, 60, 3)

even if I use k_model(dummy.numpy()) I still get the error that
UnimplementedError: The Conv2D op currently only supports the NHWC tensor format on the CPU. The op was given the format: NCHW [Op:Conv2D]