ModuleNotFoundError: No module named 'tensorflow.python.compiler.tensorrt' - python

My code:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.python.compiler.tensorrt import trt_convert as trt
# import tensorflow.contrib.slim as slim
When I run it, get this error:
ModuleNotFoundError: No module named 'tensorflow.python.compiler.tensorrt'
Also, instead of from tensorflow.python.compiler.tensorrt import trt_convert as trt I used from tensorflow.python.compiler.tensorrt import trt but it did not change anything.
I use TensorFlow version 1.14.

Also worth mentioning https://forums.developer.nvidia.com/t/error-converting-tensorflow-model-to-tensorrt-on-windows-10/83892 its not supported on windows 10

In version that lower then 1.14.1 the right import is:
import tensorflow.contrib.tensorrt as trt

try to change
from tensorflow.python.compiler.mlcompute import mlcompute
for setting the GPU device in Metal plugin. We honor the Tensorflow's device placement logic. So depending on the supported operations in Metal plugin the layers will be mapped to GPU by TF's device placer. You can use tf.debugging.set_log_device_placement(
True) to dump out the layers mapped to GPU. I just removed the mlcompute import below and was able to train the network.

Related

Error while importing tensorflow as tf on Colab

I had been using a simple "import tensorflow as tf" command to import tensorflow on my Colab notebook for months. However, just recently the same code and notebooks started giving me the error "from future imports must occur at the beginning of the file".
The tensorflow package version I found on using "!pip list" was "2.8.2+zzzcolab20220719082949".
The main thing confusing me is how a notebook/code that was working perfectly 2 weeks back suddenly starts giving errors.
The code for the cell I ran which gave this error was:
import sys
from tflite_model_maker import image_classifier
from tflite_model_maker.image_classifier import DataLoader
from tflite_model_maker.image_classifier import EfficientNetLite0Spec
from tflite_model_maker.image_classifier import EfficientNetLite4Spec
from __future__ import absolute_import, division, print_function, unicode_literals
import matplotlib.pylab as plt
import tensorflow as tf
The error I got was -
File "", line 9
import tensorflow as tf
^
SyntaxError: from future imports must occur at the beginning of the file
You only have to move your __future__ import at the top of your code to make it work:
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
from tflite_model_maker import image_classifier
from tflite_model_maker.image_classifier import DataLoader
from tflite_model_maker.image_classifier import EfficientNetLite0Spec
from tflite_model_maker.image_classifier import EfficientNetLite4Spec
import matplotlib.pylab as plt
import tensorflow as tf
Please have a look at the future-statements docs:
A future statement must appear near the top of the module. The only
lines that can appear before a future statement are:
the module docstring (if any),
comments,
blank lines, and
other future statements.
In your case you have other imports before your __future__, which is not allowed.

need to import tensorflow_hub

I am getting below error while trying to import tensorflow_hub. I have applied the already available solutions on Github or on stackoverflow and even downgraded my tesnorflow and estimator too. Still I am getting below. Can you please help ?
ImportError: cannot import name 'dnn_logit_fn_builder' from partially initialized module 'tensorflow_estimator.python.estimator.canned.dnn' (most likely due to a circular import) (C:\Users\kumar\anaconda3\lib\site-packages\tensorflow_estimator\python\estimator\canned\dnn.py)
code -
import tensorflow as tf
from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
import tensorflow_hub as hub
Given code worked fine with Tf v2.8 and v2.9.1
import tensorflow as tf
from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
import tensorflow_hub as hub

Cannot set up tensorflow gpu object detection

I tried to follow a tutorial on how to setup tensorflow gpu (https://www.youtube.com/watch?v=tPq6NIboLSc) but on the jupyter step i am getting this error :https://pastebin.com/gm3g8cC5
(ive set in in a pastebin because its very long)
I dont know what to do, ive also tried others tutorial but none of the others works, they also got errors ( but they are differents) at the jupyter step.
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import ops as utils_ops
if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):
raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')
Here is the part where the errors pops.
I dont know what is my tensorflow version since when i am trying to import it i am getting OSError: [WinError 193] %1 is not a valid Win32 application

In-code solution to remove warnings in my Python code

I have a deep learning python code (Anaconda3, Ubuntu 16.04) which uses TensorFlow for some machine learning algorithms on video inputs. It imports the following packages:
import time, os, glob, subprocess
import skvideo.io
import numpy as np
import tensorflow as tf
import time, cv2, librosa
import skvideo.io
import numpy as np
import tensorflow as tf
from sklearn.externals import joblib
When running, it get the following warning. How can I remove this warning (or hide it from printing)?
/home/user/anaconda3/lib/python3.5/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator SVC from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
UserWarning)

Import statments when using Tensorflow contrib keras

I have a bunch of code written using Keras that was installed as a separate pip install and the import statements are written like from keras.models import Sequential, etc..
On a new machine, I have Tensorflow installed which now includes Keras inside the contrib directory. In order to keep the versions consistent I thought it would be best to use what's in contrib instead of installing Keras separately, however this causes some import issues.
I can import Keras using import tensorflow.contrib.keras as keras but doing something like from tensorflow.contrib.keras.models import Sequential gives ImportError: No module named models, and from keras.models import Sequential gives a similar ImportError: No module named keras.models.
Is there a simple method to get the from x.y import z statements to work? If not it means changing all the instances to use the verbose naming (ie.. m1 = keras.models.Sequential()) which isn't my preferred syntax but is do-able.
Try this with recent versions of tensorflow:
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import LSTM, TimeDistributed, Dense, ...
Try with tensorflow.contrib.keras.python.keras:
from tensorflow.contrib.keras.python.keras.models import Sequential
Unfortunately from tensorflow.contrib.keras.python.keras.models import Sequential no longer works. It looks like they are changing the interface as of version 1.4 (currently at RC0). There is a note saying the tensorflow.contrib.keras interface is deprecated and you should use tensorflow.keras but this doesn't work either without python in the line.
The following did work for me under V1.4rc0
from tensorflow.python.keras.models import Sequential
import tensorflow.python.keras
import tensorflow.contrib.keras as keras
The following did not...
import tensorflow.python.keras as keras
Hopefully this gets cleaned up a bit more before final release.

Categories

Resources