4

I want to convert a pytorch model to keras using onnx based on this medium article:

https://medium.com/analytics-vidhya/pytorch-to-keras-using-onnx-71d98258ad76

I reproduced the same code as this article but when I want to convert the onnx model to keras I face this error:

ValueError: 'onnx::Add_6_reshape/' is not a valid root scope name. A root scope name has to match the following pattern: ^[A-Za-z0-9.][A-Za-z0-9_.\/>-]*$

Anyone knows how can I fix it?

1 Answers1

0

Depends on what model you are trying to convert, but can you try the following:

  1. Install pt2keras: https://github.com/JWLee89/pt2keras
pip install -U pt2keras
  1. Convert the model with the script below (tested)

import tensorflow as tf

from pt2keras import Pt2Keras
from pt2keras import converter

import torch.nn.functional as F
import torch.nn as nn


class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(784, 128)
        self.output = nn.Linear(128, 10)

    def forward(self, x):
        x = self.hidden(x)
        x = F.sigmoid(x)
        x = self.output(x)
        return x


if __name__ == '__main__':
    input_shape = (1, 784)

    # Grab model
    model = Model()

    # Create pt2keras object
    converter = Pt2Keras()

    # convert model
    # model can be both pytorch nn.Module or 
    # string path to .onnx file. E.g. 'model.onnx'
    keras_model: tf.keras.Model = converter.convert(model, input_shape)

    # Save the model
    keras_model.save('output_model.h5')

I am the author of the package and this was mainly done quickly during my free time, but hopefully this works for you.

I tested the code above in my local environment and it works for me.

J. Lee
  • 513
  • 4
  • 15
  • Hello there! I am trying to use your code, but it does not seem to work with conv2d layers. I tried with a supersimple network, with a conv2d layer with 1 filter of dimension 3, followed by a relu layer. I get the following error `Failed to convert model. The following operations are currently unsupported: - ConstantOfShape`. I tried to have a look at your github page, but the example with the relu layer is not helping. – Eddymage Oct 04 '22 at 12:06
  • From the error message, I think it is because the onnx `ConstantOfShape` converter has not been added to the main repository of converters. I have not added the `ConstantOfShape` converter because the current operators so far supported my particular use cases for conversion. Can you send me the simple model architecture? I will then write a converter and update the repo. Alternatively, you can write your own converter by using the `@converter('ConstantOfShape')` decorator. – J. Lee Oct 06 '22 at 07:14
  • Yes, it is the one depicted in [this question](https://stackoverflow.com/q/73950114/13156261) (without the Batch normalization layer). I tried to do as you suggest, but I stopped since I do not know what to write for the conversion, I am not very expert in python programming. – Eddymage Oct 06 '22 at 07:17
  • 1
    Thanks for the link. I will check it out and get back to you – J. Lee Oct 06 '22 at 07:18
  • Okay, for now, if you want to see successful conversion, could you try removing `track_running_stats=False` from the sample code? On my end, the conversion works after removing this part. I will need to dig into this issue during my downtime. – J. Lee Oct 06 '22 at 08:27