Tensorflow importing error in Jupyter notebook - tensorflow

So I managed all other packages (such as cv2, pandas, matplotlib..) work well in the default kernel or Python 3 kernel, except Tensorflow.
Tensorflow does not import with Kernel: "Python 3" or "Python default".
import tensorflow as tf
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-41389fad42b5> in <module>()
----> 1 import tensorflow as tf
ImportError: No module named tensorflow
When I choose "env: tensorflow" kernel, my other packages such as pandas, cv2 all collapse. Thus, I try to use "default" kernel or "Python 3" kernel.
Thanks a lot.

You haven't loaded the tensorflow kernel, please follow instructions here.

Related

Tensorflow installation issues on RaspberryPi 3 B+

Tried installing Tensorflow on RaspberryPi 3b+ model which gives the following error:
Traceback (most recent call last):
File "/home/infant-cry-detector/infant-cry-detector/_cnn__real_time_audio.py", line 22, in <module>
from tensorflow.keras import losses, models, optimizers
ModuleNotFoundError: No module named 'tensorflow.keras'
Tried to install Tensorflow versions<1.6 on RaspberryPi 3 b+ model but while using the same it shows module not found and unable to import tensorflow.keras which also returns module not found error.
We are complete beginners in coding with RaspberryPi, hence requesting detailed help that would help us get through the error.

Error when "import tensorflow as tf" in colab

When I "import tensorflow as tf" in google colab I am getting the following error. Any idea how to fix it ?
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-37-07e0f991eb97> in <module>()
----> 1 import tensorflow as tf
2
3 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/client/pywrap_tf_session.py in <module>()
17 # pylint: disable=invalid-import-order,g-bad-import-order, wildcard-import, unused-import
18 from tensorflow.python import pywrap_tensorflow
---> 19 from tensorflow.python.client._pywrap_tf_session import *
20 from tensorflow.python.client._pywrap_tf_session import _TF_SetTarget
21 from tensorflow.python.client._pywrap_tf_session import _TF_SetConfig
ImportError: SystemError: <built-in method __contains__ of dict object at 0x7f79368db0a0> returned a result with an error set
---------------------------------------------------------------------------
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.
---------------------------------------------------------------------------
This issue has been fixed now in 2.8.0 stable version. If it still persists ,please install Numpy 1.21.5 manually and Try again.
I've post a similar error on tensorflow github issue.
Tensorflow 2.8.0 | Error Message: ImportError: SystemError.
My solution (since I'm using conda environment on Windows 10), is to downgrade the Tensorflow version to 2.7.0. Hope help helps.

Tensorflow problem in Pycharm Virtual environment: TypeError: 'Model' object is not iterable

I use Python 3.5, Tensorflow-gpu 1.12.0, Keras 2.2.4 in ubuntu. When I use system interpreter in Pycharm, the code is run without any problem. But, when I create a virtual environment in Pycharm and install the same versions of all necessary packages (OpenCV, Sklearn, pandas, Keras, Tensorflow), it gives the following error:
Traceback (most recent call last):
File "/media/ehsan/48BE4782BE476810/AA_MY_PYTHON_CODE/MultiLable_MultiTask_Light_Examples/CodeTwo/2_Main_Code_Training_Multitask_Network.py", line 338, in <module>
base_model, multi_model, feature_map = multi_model(loss_list, test_metrics, dd)
File "/media/ehsan/48BE4782BE476810/AA_MY_PYTHON_CODE/MultiLable_MultiTask_Light_Examples/CodeTwo/2_Main_Code_Training_Multitask_Network.py", line 40, in multi_model
_, base_model = VGG19(weights='imagenet', include_top=False, input_shape=(175, 100, 3))
TypeError: 'Model' object is not iterable
I tried to reinstall Tensorflow and Keras. Also, I recreated the virtual environment. But, I got the same error while using the virtual environment.
Try importing the model from TensorFlow instead of Keras.
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model

tf.VERSION is always 1.6.0 even though I installed the latest version

Please help me to solve the problem - I use Google's Colaboratory to run tensor_hub example Text classification with TF-Hub, it needs the version later than 1.7.0, but the version I installed is 1.8.0.
# Install the latest Tensorflow version.
!pip install --quiet "tensorflow>=1.7"
# Install TF-Hub.
!pip install tensorflow-hub
import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import re
import seaborn as sns
RuntimeErrorTraceback (most recent call last) <ipython-input-20-48e1bdaa8642> in <module>()
1 import tensorflow as tf
----> 2 import tensorflow_hub as hub
3 import matplotlib.pyplot as plt
4 import numpy as np
5 import os
/usr/local/lib/python2.7/dist-packages/tensorflow_hub/__init__.py in <module>()
63
64 # Comment/uncomment to skip checking the TensorFlow version.
---> 65 _check_tensorflow_version(tf.VERSION)
66
67 # Used by doc generation script.
/usr/local/lib/python2.7/dist-packages/tensorflow_hub/__init__.py in
_check_tensorflow_version(version)
60 "TensorFlow Hub depends on 'tf-nightly' build after %s or "
61 "'tensorflow~=%s'. Found tf.VERSION = %s" % (
---> 62 _NIGHTLY_VERSION, _MAIN_VERSION, version))
63
64 # Comment/uncomment to skip checking the TensorFlow version.
RuntimeError: TensorFlow Hub depends on 'tf-nightly' build after 20180308 or 'tensorflow~=1.7'. Found tf.VERSION = 1.6.0
Good news: TF 1.7 is now available by default. :)
The underlying problem you were hitting is just that python caches loaded modules in sys.modules -- re-importing a module doesn't pick up the new version until you restart the process, as Korakot noted.
You may need to restart the runtime.
menu > Runtime > Restart runtime...

Keras Import Error Tensorflow backend

---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in <module>()
4 try:
----> 5 from tensorflow.python.ops import ctc_ops as ctc
6 except ImportError:
ImportError: cannot import name 'ctc_ops'
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
<ipython-input-10-c74e2bd4ca71> in <module>()
----> 1 import keras
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/__init__.py in <module>()
1 from __future__ import absolute_import
----> 2 from . import backend
3 from . import datasets
4 from . import engine
5 from . import layers
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/__init__.py in <module>()
67 elif _BACKEND == 'tensorflow':
68 sys.stderr.write('Using TensorFlow backend.\n')
---> 69 from .tensorflow_backend import *
70 else:
71 raise Exception('Unknown backend: ' + str(_BACKEND))
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in <module>()
5 from tensorflow.python.ops import ctc_ops as ctc
6 except ImportError:
----> 7 import tensorflow.contrib.ctc as ctc
8
9 import numpy as np
ImportError: No module named 'tensorflow.contrib.ctc'
Seems like is an problem with Keras and not Tensorflow according to this post enter link description here
I was wondering if this has been fixed yet?
This is a problem between Keras and tensorflow. You should change this fix to use something like
if hasattr(tf.contrib, 'ctc'):
ctc = tf.contrib.ctc # old version
else:
ctc = tf.nn # New official version.
The current "official" way to access the ctc operations are in tf.nn, for instance, tf.nn.ctc_loss.
If keras is giving an import problem and you are facing "no module named keras" even if you have installed it.
Just upgrade your pip by:
python -m pip install –upgrade pip
Re-install upgraded keras and tensorflow by:
pip install keras
pip install tensorflow
Hope it will solve the problem. If not, try upgrading your conda (Anaconda) and then do steps 1 to 3 above again.