onnx-tensorflow icon indicating copy to clipboard operation
onnx-tensorflow copied to clipboard

Issue: No module named 'onnx_tf'

Open codulers opened this issue 5 years ago • 18 comments

**I'm trying to convert my PyTorch model into Tensorflow. I have successfully convert my PyTorch model into .onnx format using opset=11. But When I try to convert .onnx to tensorflow it gives me an Error:No module named 'onnx_tf' **

  • Python version: 3 on Google CoLab
  • ONNX version: I have install using master branch. Using bellow comman I have install ONNX !git clone https://github.com/onnx/onnx.git

but version is not showing when I print -> print(onnx.tf.version). Saying that 'has no attribute version'

  • ONNX-TF version: I have install using master branch. Using bellow comman I have install ONNX !git clone https://github.com/onnx/onnx-tensorflow.git

  • Tensorflow version: 1.15.0

I have .onnx file which is created using bellow function notice 'opset = 11' torch.onnx.export(model_pytorch, dummy_input, "filePath", verbose=False,opset_version=11)

And because of opset 11 I must need to install onnx using master repository as @chinhuang007 suggest me in this issue https://github.com/onnx/onnx-tensorflow/issues/516

---------------------------------- Error Statement ----------------------------------------- ModuleNotFoundError Traceback (most recent call last) in () ----> 1 from onnx_tf.backend import prepare 2 3 # Convert ONNX to Tensorflow 4 #print("Hello World") 5 # load onnx model and convert into tensorflow format

ModuleNotFoundError: No module named 'onnx_tf'


I'm not willing to install onnx using pip command because pip install is still at the opset 10 level. And my .onnx file is created using opset 11.

Notobook of my code. PTorch_to_Tensorflow Convertion.zip

Thanks is advance

codulers avatar Jan 25 '20 09:01 codulers

I have the same errors as well, while the pip install -e . shows

Obtaining file:///content/onnx-tensorflow
Requirement already satisfied: onnx>=1.5.0 in /usr/local/lib/python3.6/dist-packages (from onnx-tf==1.5.0) (1.6.0)
Requirement already satisfied: PyYAML in /usr/local/lib/python3.6/dist-packages (from onnx-tf==1.5.0) (3.13)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from onnx>=1.5.0->onnx-tf==1.5.0) (1.12.0)
Requirement already satisfied: typing-extensions>=3.6.2.1 in /usr/local/lib/python3.6/dist-packages (from onnx>=1.5.0->onnx-tf==1.5.0) (3.6.6)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from onnx>=1.5.0->onnx-tf==1.5.0) (1.17.5)
Requirement already satisfied: protobuf in /usr/local/lib/python3.6/dist-packages (from onnx>=1.5.0->onnx-tf==1.5.0) (3.10.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf->onnx>=1.5.0->onnx-tf==1.5.0) (42.0.2)
Installing collected packages: onnx-tf
  Found existing installation: onnx-tf 1.5.0
    Uninstalling onnx-tf-1.5.0:
      Successfully uninstalled onnx-tf-1.5.0
  Running setup.py develop for onnx-tf
Successfully installed onnx-tf

But when I try to import it, it shows

      1 import onnx
----> 2 import onnx_tf

ModuleNotFoundError: No module named 'onnx_tf'

gitathrun avatar Jan 27 '20 12:01 gitathrun

I am not sure is this the solution but it does solve the problem for me, in Google Colab.

You will need to install tensorflow-addons first, along with the tf, then git clone and install it.

!pip install tensorflow-addons
.......

Successfully installed google-auth-1.11.0 tensorboard-2.1.0 tensorflow-2.1.0 tensorflow-addons-0.7.1 tensorflow-estimator-2.1.0

Check version

import tensorflow as tf
import tensorflow_addons as tfa

print(tf.__version__)
print(tfa.__version__)

>>>2.1.0
>>>0.7.1

then git clone and install

!git clone https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow && pip install -e .

Installing collected packages: onnx-tf
  Found existing installation: onnx-tf 1.5.0
    Can't uninstall 'onnx-tf'. No files were found to uninstall.
  Running setup.py develop for onnx-tf
Successfully installed onnx-tf

Final test import

from onnx_tf.backend import prepare


/content/onnx-tensorflow/onnx_tf/common/__init__.py:89: UserWarning: onnx_tf.common.get_outputs_names is deprecated. It will be removed in future release. Use TensorflowGraph.get_outputs_names instead.
  warnings.warn(message)

It works at this point.

I also try to install onn-tf with my custom docker environment, the solution is to append the build directory after pip install -e . to the sys.path, but before doing this, you have to make sure the tensorflow-addons is properly installed. You probabaly need to upgrade your tensorflow to 2.1 in order to install tensorflow-addons properly...

import sys
sys.path

sys.path.append('your onn-tf build path")

gitathrun avatar Jan 27 '20 16:01 gitathrun

Please retry with the latest from master branch. Make sure you have tf 2.x, addons, and onnx can be either master or pip install.

chinhuang007 avatar Feb 06 '20 20:02 chinhuang007

@Ehtasha Have you tried it lately? Please use source build from the tf-1.x branch if you have tensorflow 1.x, and do source build from the master branch for tensorflow 2.x. You also need tensorflow-addons as described here, https://github.com/onnx/onnx-tensorflow#installation. Please report if any errors. I'd like to close this issue if working for you.

chinhuang007 avatar Mar 25 '20 19:03 chinhuang007

It's still not working. On google colab, I get an error (onnx_tf not found) when I run the following:

!pip install tensorflow-addons
import tensorflow as tf
import tensorflow_addons as tfa

print(tf.__version__)
print(tfa.__version__)
!git clone https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow && pip install -e .

from onnx_tf.backend import prepare

elmahyai avatar Apr 22 '20 05:04 elmahyai

Can the onnx converter for tensorflow be used with tensorflow 1.14.x ?

karanbirchahal-bc avatar Jul 08 '20 20:07 karanbirchahal-bc

Ah never mind, it works

karanbirchahal-bc avatar Jul 09 '20 16:07 karanbirchahal-bc

This doesn't seem to work on Colab. The package gets installed but importing it fails for some reason.

import tensorflow as tf
tf.__version__
'2.2.0'

!pip install tensorflow-addons
!git clone https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow && pip install -e .

Requirement already satisfied: tensorflow-addons in /usr/local/lib/python3.6/dist-packages (0.8.3)
Requirement already satisfied: typeguard in /usr/local/lib/python3.6/dist-packages (from tensorflow-addons) (2.7.1)
Cloning into 'onnx-tensorflow'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 5004 (delta 0), reused 0 (delta 0), pack-reused 4996
Receiving objects: 100% (5004/5004), 1.33 MiB | 1.81 MiB/s, done.
Resolving deltas: 100% (3823/3823), done.
Obtaining file:///content/onnx-tensorflow
Collecting onnx>=1.6.0
  Downloading https://files.pythonhosted.org/packages/36/ee/bc7bc88fc8449266add978627e90c363069211584b937fd867b0ccc59f09/onnx-1.7.0-cp36-cp36m-manylinux1_x86_64.whl (7.4MB)
     |████████████████████████████████| 7.4MB 2.5MB/s 
Requirement already satisfied: PyYAML in /usr/local/lib/python3.6/dist-packages (from onnx-tf==1.6.0) (3.13)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from onnx>=1.6.0->onnx-tf==1.6.0) (1.18.5)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from onnx>=1.6.0->onnx-tf==1.6.0) (1.15.0)
Requirement already satisfied: protobuf in /usr/local/lib/python3.6/dist-packages (from onnx>=1.6.0->onnx-tf==1.6.0) (3.12.2)
Requirement already satisfied: typing-extensions>=3.6.2.1 in /usr/local/lib/python3.6/dist-packages (from onnx>=1.6.0->onnx-tf==1.6.0) (3.7.4.2)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf->onnx>=1.6.0->onnx-tf==1.6.0) (49.1.0)
Installing collected packages: onnx, onnx-tf
  Running setup.py develop for onnx-tf
Successfully installed onnx-1.7.0 onnx-tf

from onnx_tf.backend import prepare
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-6913ab77a9da> in <module>()
----> 1 from onnx_tf.backend import prepare

ModuleNotFoundError: No module named 'onnx_tf'

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

sasikiran avatar Jul 30 '20 06:07 sasikiran

Update: Restarting the kernel and importing the package fixed it.

sasikiran avatar Jul 30 '20 07:07 sasikiran

Still not working on google colab

dheerajmpai avatar Oct 14 '20 10:10 dheerajmpai

This worked for me

%%bash
pip install onnx tensorflow-addons
git clone https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow && pip install -e .

and then

sys.path.append("./onnx-tensorflow")

naraB avatar Feb 03 '21 10:02 naraB

sys.path.append("./onnx-tensorflow")

/bin/bash: -c: line 0: syntax error near unexpected token `"./onnx-tensorflow"'
/bin/bash: -c: line 0: `sys.path.append("./onnx-tensorflow")'

AlexandrParkhomenko avatar Feb 24 '21 17:02 AlexandrParkhomenko

sys.path.append("./onnx-tensorflow")

/bin/bash: -c: line 0: syntax error near unexpected token `"./onnx-tensorflow"'
/bin/bash: -c: line 0: `sys.path.append("./onnx-tensorflow")'

@AlexandrParkhomenko Try to run import sys beforehand.

naraB avatar Mar 03 '21 08:03 naraB

sys.path.append("./onnx-tensorflow")

/bin/bash: -c: line 0: syntax error near unexpected token `"./onnx-tensorflow"'
/bin/bash: -c: line 0: `sys.path.append("./onnx-tensorflow")'

@AlexandrParkhomenko Try to run import sys beforehand.

Thanks

AlexandrParkhomenko avatar Mar 03 '21 08:03 AlexandrParkhomenko

pip install onnx-tf

i think this will fix your error .try it

ashikprakash21 avatar Aug 29 '21 15:08 ashikprakash21

Thanks! Successfully installed onnx in archlinux. Issue can be closed.

AlexandrParkhomenko avatar Aug 29 '21 18:08 AlexandrParkhomenko

Just Restart the Kernel after you clone the git repo & install the requirements.txt file . then

from onnx_tf.backend import prepare

It Worked for me in colab pro :)

AbhishekMaity001 avatar Sep 21 '21 05:09 AbhishekMaity001