gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
858 stars 143 forks source link

ValueError: 'onnx::Ma/' is not a valid root scope name. #147

Open pranoyr opened 1 year ago

pranoyr commented 1 year ago

Describe the bug I am running the below code snippet and getting this error.

ValueError: 'onnx::Ma/' 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_.\/>-]*$

To Reproduce

import os
os.environ['CUDA_VISIBLE_DEVICES'] = ''
import sys
sys.path.insert(0, '../expression_capture/')
from torchvision import models
from models.resnet18 import Fc

import torch
from pytorch2keras.converter import pytorch_to_keras

model = models.resnet18(pretrained=True).eval()

x = torch.randn(1, 3, 224, 224, requires_grad=False)
k_model = pytorch_to_keras(model, x, [(3, None, None,)], verbose=True, name_policy='short')
k_model.save('keras.h5') 

Environment (please complete the following information):

balisujohn commented 1 year ago

The first thing to try is an old version of onnx, since the pypi version of this package hasnt been updated in a very long time.

balisujohn commented 1 year ago

ok so I encountered this, the fix is to go into your tensorflow source in the file env/lib/python3.9/site-packages/tensorflow/python/framework/ops.py in your virtualenv and kill all the regex checks for node and scope name format. Seemed to work for me lol. As far as I can tell, tensorflow imposed new name restrictions, and you can just remove the restrictions lol(who knows if there could be unintended consequences).

balisujohn commented 1 year ago

Actually, even better, try setting the name_policy to "renumerate" when calling pytorch_to_keras. This fixed that error for me, without requiring such a cursed form of patch to tensorflow.

yisong17tao commented 10 months ago

Actually, even better, try setting the name_policy to "renumerate" when calling pytorch_to_keras. This fixed that error for me, without requiring such a cursed form of patch to tensorflow.

Thank you very much, solved my problem!