Python uses wrong Package version - python

Hi i try to launch the object_detection_tutorial on my PC. When i run the following code to load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
ValueError: No op named NonMaxSuppressionV2 in defined operations.
I googled the error and upgrading the tensorflow version to 1.4 should fix the bug. In my code i used tensorflow 1.13 and it worked in google cloud. But even after uninstalling and installing eg. tensorflow 1.4, python uses 1.2.1
Picture of my code: https://ibb.co/VYkq2rF

Looks like the module is not installed correctly. Try creating a new environment using conda and set up Object Detection in it. It should resolve your issue.
Also, as best practices, it is always better to work on a conda environment than working on the base environment.
Please use below command to create an empty new environment and then install packages on it.
conda create --no-default-packages --name <env_name> python=<version>

Related

module 'tensorflow.python.keras.backend' has no attribute 'slice'?

I am trying to implement https://github.com/ec-jrc/GHS-S2Net this project (in google colab). A week ago it was working by installing the requirements:
!pip install tensorflow-gpu==2.0.0
!pip install Keras==2.3.1
Unfortunately, from this week it is showing me module 'tensorflow_core.compat.v2' has no attribute '__internal__' for import keras.
Ok, I wanted to try with the latest versions of tensor and keras (both 2.5.0), but now I am getting following error:
AttributeError: module 'tensorflow.python.keras.backend' has no attribute 'slice'
How can I handle this?
AttributeError: module 'tensorflow.python.keras.backend' has no
attribute 'slice'
It looks like an installation issue. Please try to uninstall and reinstall Keras
pip uninstall keras
pip install keras --upgrade
module 'tensorflow_core.compat.v2' has no attribute 'internal'
Due to incompatibility between Tensorflow and Keras you get this issue.
As mentioned above, if you upgrade to latest keras version and import keras or import keras from tensorflow as from tensorflow import keras will resolve this issue. For more information you can refer here.
Keras.backend doesnt have a slice operation. Instead, you can go to the location where the crf.py file is stored locally on your machine (this you can find mentioned in the error dialogue, i.e.(myenv\Lib\site-packages\keras_contrib\layers\crf.py) and do the following:
add the line --> import tensorflow as tf.
Goto line number where it is mentioned K.slice,
It is in the function: def step(self, input_energy_t, states, return_logZ=True.
You can do a search for "slice".
Then replace K.slice by tf.slice.
Restart the jupyter notebook session. This should work.
Change the line 463 in crf.py as import tensorflow as tf; tf.slice. Then save the crf.py.

How to disable TensorFlow GPU?

I first created my TensorFlow code in python on my GPU using :
import tensorflow-gpu as tf
I used it for training purpose and everything went very well. But now, I want to deploy my python scripts on a device without GPU. So, I uninstalled tensorflow-gpu with pip and import normal TensorFlow :
import tensorflow as tf
But when I run the script, it is still using the gpu :
I tried this code :
try:
# Disable all GPUS
tf.config.set_visible_devices([], 'GPU')
visible_devices = tf.config.get_visible_devices()
print(visible_devices)
for device in visible_devices:
assert device.device_type != 'GPU'
except Exception as e:
# Invalid device or cannot modify virtual devices once initialized.
print(e)
But still not working (even if the gpu seems disable as you can see in white on the screenshot).
I just want to return to the default TensorFlow installation without GPU features.
I tried to uninstall and install tensorflow, remove the virtual environment and create a new one, nothing worked.
Thanks for your help !
Tensorflow > 2.x has default GPU support. To know more please visit Tensorflow site.
As per the above screenshot, it is showing only CPU.
And also observe Adding visible GPU devices: 0
If you still want to use only CPU enable Tensorflow use Tensorflow==1.15

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

Is it possible to run tensorflow-gpu on a computer without a GPU or CUDA?

I have two Windows computers, one with and one without a GPU.
I would like to deploy the same python script on both (TensorFlow 1.8 Object Detection), without changing the packaged version of TensorFlow. In other words, I want to run tensorflow-gpu on a CPU.
In the event where my script cannot detect nvcuda.dll, I've tried using a Session config to disable the GPU, like so:
config = tf.ConfigProto(
device_count = {'GPU': 0}
)
and:
with tf.Session(graph=detection_graph, config=config) as sess:
However, this is not sufficient, as TensorFlow still returns the error:
ImportError: Could not find 'nvcuda.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable.
Typically it is installed in 'C:\Windows\System32'. If it is not present, ensure that you have a CUDA-capable GPU with the correct driver installed.
Is there any way to disable checking for a GPU/CUDA entirely and default to CPU?
EDIT: I have read the year-old answer regarding tensorflow-gpu==1.0 on Linux posted here, which suggests this is impossible. I'm interested to know if this is still how tensorflow-gpu is compiled, 9 versions later.

Load Tensorflow Model in Python 2.7

I have a fully trained and saved Tensorflow model that I would like to load and use as a plug-in to a third party application (UCSF Chimera). This third party application runs on Python 2.7 which does not support Tensorflow. If even possible, is there a way for me to use this model at all in python 2.7?
I was originally looking at this previous post but it was for Java/C++.
First, save your Tensorflow model using pickle
with open("xxx.pkl", "wb") as outfile:
pickle.dump(checkpointfile, outfile)
Second, install anaconda and create a python2.7 environment
Third, install tensorflow again in the python2.7 environment
conda install tensorflow
Fourth, read the model using pickle
pkl_file = open("xxx.pkl", "rb")
data = pickle.load(pkl_file, encoding="latin1")

Categories

Resources