An error occurred while exporting the onnx model - pytorch

Here is the model:SMSR
When I try to export the onnx model using torch.onnx.export(self.model, lr, onnx_save_path, verbose=True), the following error occurs:
TyperError: state_dict() takes 1 positional argument but 3 were given
My torch version is: 1.1.0
So, what should I do?
Thank U all!!!

Torch version is quite old, your code would run in version 1.12

Related

Error in loading ONNX model with ONNXRuntime

I'm converting a customized Pytorch model to ONNX. However, when loading it with ONNXRuntime, I've encountered an error as follows:
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeException: [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : Exception during initialization: ...onnxruntime/core/providers/cpu/tensor/transpose.h:46 onnxruntime::TransposeBase::TransposeBase(const onnxruntime::OpKernelInfo &) v >= 0 && static_cast<uint64_t>(v) <= std::numeric_limits<size_t>::max() was false.
I've checked with onnx.checker.check_model() and it's totally fine.
I've also tried to replace transpose() into permute() in forward() function but the error has still remained.
Is anyone familiar with this error?
Environments:
Python 3.7
Pytorch 1.9.0
CUDA 10.2
ONNX 1.10.1
ONNXRuntime 1.8.1
OS Ubuntu 18.04
The perm attribute of node Transpose_52 is [-1, 0, 1] although ONNX Runtime requires that all of them should be positive: onnxruntime/core/providers/cpu/tensor/transpose.h#L46

After downgrading Tensorflow 2.0 to 1.5 results changed and results reproduction is not available

Would you help me to achieve reproducible results with Tensorflow 1.15 without restarting Python kernel. And why the output results in TF 2.0 and TF 1.5 are different with absolutely identical parameters and dataset? Is it possible to achieve identical output?
More details:
I tried to interpret model results in TF 2.0 by:
import shap
background = df3.iloc[np.random.choice(df3.shape[0], 100, replace=False)]
explainer = shap.DeepExplainer(model, background)
I recieved an error:
`get_session` is not available when using TensorFlow 2.0.`get_session` is not available when using TensorFlow 2.0.
According to the SO topic, I tried to setup TF 2.0 compatibility with TF 1 by using in the front of my code:
import tensorflow.compat.v1 as tf
But the error appeared again.
Following advice by many users, I downgraded TF2 to TF 1.15 it solved the problem, and shap module interprets the results but:
1) to make results reproducible now I have to change tf.random.set_seed(7) on tf.random.set_random_seed(7) and restart the Python kernel every time! In TF2 I didn't have to restart the kernel.
2) prediction results has been changed, especially, Economical efficiency (that is, TF1.5. wrongly classifies more important samples than TF2.0).
TF 2:
Accuracy: 94.95%, Economical efficiency = 64%
TF 1:
Accuracy: 94.85%, Economical efficiency = 56%
The code of the model is here
First, results differ from each other not only in TF1 and TF2 versions, but also in TF2.0 and TF2.2 versions. Probably, it depends on diffenent internal parameters in the packages.
Second, TensorFlow2 works with DeepExplainer in the following versions:
import tensorflow
import pandas as pd
import keras
import xgboost
import numpy
import shap
print(tensorflow.__version__)
print(pd.__version__)
print(keras.__version__)
print(xgboost.__version__)
print(numpy.__version__)
print(shap.__version__)
output:
2.2.0
0.24.2
2.3.1
0.90
1.17.5
0.35.0
But you will face some difficulties in updating the libraries.
In Python 3.5, running TF2.2, you will face the error 'DLL load failed: The specified module could not be found'.
It 100% can be solved by installing newer C++ package. See this:https://github.com/tensorflow/tensorflow/issues/22794#issuecomment-573297027
Link to download the package:https://support.microsoft.com/ru-ru/help/2977003/the-latest-supported-visual-c-downloads
In Python 3.7 you will not find the shap 0.35.0 version with whl extention. Only tar.gz extension which gives the error: "Install visual c++ package". But installation doesn't help.
Then download shap 0.35.0 for Python 3.7 here: https://anaconda.org/conda-forge/shap/files. Run Anaconda shell. Type: conda install -c conda-forge C:\shap-0.35.0-py37h3bbf574_0.tar.bz2.

Error: Module 'tensorflow' has no attribute 'gfile' error while running tensorflow object detection api tutorial

I am trying to use the object detection tutorial from tensor flow api. I am using python 3 and tensor flow version 2. But getting the below error.I tried several ways:
File "C:\Aniruddhya\object_detection\object_detection\utils\label_map_util.py", line 137, in load_labelmap
with tf.gfile.GFile(path, 'r') as fid:
AttributeError: module 'tensorflow' has no attribute 'gfile'
can someone help me to run this?
code link: https://drive.google.com/drive/u/3/folders/1XHpnr5rsENzOOSzoWNTvRqhEbLKXaenL
It's not called that in TensorFlow 2. You might be using a TensorFlow 1 tutorial.
Version 1
tf.gfile.GFile
https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/io/gfile/GFile
Version 2
tf.io.gfile.GFile
https://www.tensorflow.org/api_docs/python/tf/io/gfile/GFile
If you have Tensorflow version 2. You can use the next module compatible with the version 1, too.
import tensorflow.compat.v1 as tf
I solved this problem by reinstalling tensor using the previous version: sudo pip3 install tensorflow==1.14.0
You may optionally downgrade to previous version of tensorflow:
!pip install tensorflow==1.12.0
import tensorflow as tf
print(tf.__version__)
otherwise , make if tf.io.gfile and import tf.io

How to use densenet in Keras

I notice densenet has been added to keras (https://github.com/keras-team/keras/tree/master/keras/applications)and I want to apply it in my project but when I tried to import it in jupyter anaconda, I got an error saying:
module 'keras.applications' has no attribute 'densenet'
it seems like densenet has not been incorporated into current version of keras.
Any idea how can I add it myself?
Densenet was added in keras version 2.1.3. What version of keras are you running?
Have you tried to update keras with pip install keras --upgrade since January?

Tenosrflow RandomForest 'TensorForestEstimator' Example predict() and score() throws no atrribute error

I am trying to run a random forest example using in tensorflow 'TensorForestEstimator'. Following is the code that I used from tensorflow git repo:
Github Code Link
Error I am getting:
File "/home/mckw9/tensorflow2/lib/python3.4/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1363, in score
eval_results, global_step = self._estimator._evaluate_model(
AttributeError: 'TensorForestEstimator' object has no attribute '_evaluate_model'
I am using tensorflow-gpu 1.0.1, NVIDIA Titan X Pascal 12GB GDDR5X, Ubuntu 14.04, Python 3.4
The problem disappears for Tensorflow 1.1+.

Resources