Trying to convert .onnx model to .pb model using onnx-tf library - python

I am trying to convert .onnx model to .pb format by using onnx-tf package, however, after invoking onnx_tf.backend.prepare function, the python kernel crashes. I am using the code below:
import onnx
from onnx_tf.backend import prepare
import tensorflow
onnx_model = onnx.load(<path-to-model>) # load onnx model
tf_rep = prepare(onnx_model) # <------ That's where python crashes
tf_rep.export_graph(<output-path>) # export the model
I have double-checked package version dependencies, as wrong dependencies caused different errors while loading the .onnx model, and those are as follows:
onnx==1.7.0
onnx-tf==1.6.0
tensorflow==2.2.0
tensorflow-addons==0.10.0
torch==1.6.0+cu101
However, the dependencies seem to be correct, according to Github pages.

There were large changes between ONNX 1.6 and 1.7 that caused many dependency issues. Try with Onnx 1.6, which may be the source of your issue. Where did you find 1.7 mentioned as the required version?
Additionally, you mention the Python kernel crashing? Are you running in a Jupyter notebook?
I've found this package to be a lot more unstable in Juypter notebook, and not print out all of the relevant errors. If you could test your system in a standalone Python script you may get more information.

Importing onnx and onnx-tf related libraries before tf and pytorch should help. The problem is probably caused by a version clash since both onnx and onnx-tf use tf.

Can you install onnx-tf of this commit 7d8fa7d88fab469253d75e5e11cf9cdcb90104c4
Between order of imports and this commit the issue was solved for me

Related

Detectron 2 to Onnx

I want to convert a model into onnx
But when I import caffe2_export for exporting model to onnx I get this error
from detectron2.export import caffe2_export
ERROR
import onnx.optimizer
ModuleNotFoundError: No module named 'onnx.optimizer'
It is because onnxoptimizer has been removed since onnx version 1.9.
If you really do need to use it, you can install onnx version 1.8.
pip install onnx==1.8.1
NOTE: I do not recommend this method.
If you really want to export a model into an onnx format, I'd recommend you to use export_model.py.

No module named 'tensorflow.keras'

I am trying to play around with a custom object detection model that builds of a pretrained model. All I want is for my model is to detect a specific logo in a picture. The problem is, the guide that I am following is having problems with the libraries.
import tensorflow as tf
from imageai.Detection.Custom import DetectionModelTrainer
trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="/content/drive/MyDrive/Logo_Model2/")
trainer.setTrainConfig(object_names_array=["logo"], batch_size=4, num_experiments=122, train_from_pretrained_model="/content/drive/MyDrive/pretrained-yolov3.h5")
trainer.trainModel()
My error is coming when I import imageai.Detection.Custom import DetectionModelTrainer. I am doing this on google colab and I checked the versions and they seem to be all up to date.
ModuleNotFoundError: No module named 'tensorflow.keras'
Any ideas? I have looked around stack for similar problems yet I haven't been able to resolve my issue. It doesn't seem to be a tensorflow problem since I am able to build my models just fine until I add imageai.
Seems to be an issue with the latest tensorflow==2.8.0. git issue
For now, you can revert back to the older version of tensorflow
pip install tensorflow==2.7
And upgrade imageAI :
pip install imageai --upgrade

Python TensorFlow v2, locating contrib.training

I am using some code based on Tensorflow 1.x and I am using the latest version of TensorFlow (2.8).
I have run tf_upgrade_v2 (following these migration instructions) on the python file to generate a new compatible file.
However, this doesn't work with the line of code; from TensorFlow.contrib import training as contrib_training.
Getting the error ModuleNotFoundError: No module named 'tensorflow.contrib'
I understand that contrib has been deprecated, so the modules have been moved around. But I haven't been able to find where this training module has moved to or if it has been deleted. I have looked for information first on this document on the status of tf.contrib and after this spreadsheet on TF2.0 Symbols map and haven't found any information on the fate of this training module;
I have also tried looking around TensforFlow/tf.compat.v1 but with no success.
Of course uninstalling this version of TensorFlow and instead installing an earlier version is an option. But not an option that I am overly keen on.
I feel like I may be missing something fairly obvious here...
Thanks in advance!!
The reason behind this error - ModuleNotFoundError: No module named 'tensorflow.contrib' is, tf.contrib has been deprected to use in Tensorflow 2.x. You can use Tensorflow Addons or Tensorflow Slim in the pace of tf.contrib api.
Please check this link for more details.

which python and tensorflow version is used to train DeepLab v3+ using tensorflow api?

I have been getting multiple errors which are due to conflicts in the TensorFlow version installed in my system and the version used to write the code in Tensorflow API.
I am using python 3.6.7 and Tensorflow 2.0 to get started with the code https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/installation.md
But I am getting several errors :
flags = tf.app.flags
AttributeError: module 'tensorflow' has no attribute 'app.
As I am using 2.0 , I replaced tf.app.flags with tf.compat.v1.flags.
from tensorflow.contrib import slim as contrib_slim
ModuleNotFoundError: No module named 'tensorflow.contrib'
I am not able to solve the second one.
Can I get help to know which python and tensorflow version should be used to run DeepLab v3+?
You should use the Tensorflow version 1.x to run the DeepLabV3+ model because it uses a session to run and also the slim library which is based on the TensorFlow 1.x. And so your two problems can be solved as:
Do not need to replace tf.app.flags with tf.compat.v1.flags.
To run DeepLabV3+ model, you need to put deeplab and slim folder in a folder (deeplab_slim),
and export them by running following export commands from this parent folder (deeplab_slim):
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/deeplab

Unable to train dataset for RCNN due to attribute error?

I am currently following the tutorial by EdjeElectronics: https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10#1-install-anaconda-cuda-and-cudnn and I am in the step no:6. Run the Training. I had certain errors before but I cleared them so I have generated the TFrecords and I am stuck here.image
If there are any files that I need to attach for your convenience pls let me know.
The contrib attribute has moved out of Tesnsorflow version 2.
To use version 1, replace the 'import tensorflow as tf' line as follows:
#import tensorflow as tf
import tensorflow.compat.v1 as tf #using v1 of tf
Actually, when looking at this link - https://www.tensorflow.org/guide/migrate, there is a line -
It is still possible to run 1.X code, unmodified (except for contrib), in TensorFlow 2.0
The link goes to page - https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md which explains what happened to each contrib module.
You can try to migrate code to Tensorflow 2 or whatever version you are using.
Another alternative is to uninstall your Tensorflow installation and install Tensorflow with version 1.x.

Categories

Resources