Tensorflow error - cannot find attribute '__internal__' [duplicate] - python

I'm trying to use TensorFlow as backend yesterday I can use it, but today when I use it to show some error message when I'm trying to import Keras, so here's my code:
# Install required libs
# NOTE: Run this one code, then restart this runtime and run again for next all... (PENTING!!!)
### please update Albumentations to version>=0.3.0 for `Lambda` transform support
!pip install -U segmentation-models
!pip install q tensorflow==2.1
!pip install q keras==2.3.1
!pip install tensorflow-estimator==2.1.
## Imports libs
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import cv2
import Keras
import NumPy as np
import matplotlib.pyplot as plt
it shows this error:
AttributeError Traceback (most recent call last)
<ipython-input-3-9c78a7be919d> in <module>()
5
6 import cv2
----> 7 import keras
8 import numpy as np
9 import matplotlib.pyplot as plt
8 frames
/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py in populate_deserializable_objects()
47
48 LOCAL.ALL_OBJECTS = {}
---> 49 LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
50
51 # Compatibility aliases (need to exist in both V1 and V2).
AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'
while therefore I was using TensorFlow version 2.2 and Keras version 2.3.1, yesterday I can run, but today it seems can't. did I was the wrong version import for my Keras and TensorFlow for today?
Edit:
when I use from tensorFlow import keras the output I want using tensorflow backend doesn't show up, And then when I load import segmentation_models as sm it shows the same error when I use import Keras like on above.

Here is the solution to your problem, I've tested it on colab.
!pip install -U -q segmentation-models
!pip install -q tensorflow==2.1
!pip install -q keras==2.3.1
!pip install -q tensorflow-estimator==2.1.
## Imports libs
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ["SM_FRAMEWORK"] = "tf.keras"
from tensorflow import keras
import segmentation_models as sm
|████████████████████████████████| 51kB 3.3MB/s
|████████████████████████████████| 421.8MB 42kB/s
|████████████████████████████████| 450kB 35.7MB/s
|████████████████████████████████| 3.9MB 33.6MB/s
Building wheel for gast (setup.py) ... done
ERROR: tensorflow-probability 0.12.1 has requirement gast>=0.3.2,
but you'll have gast 0.2.2 which is incompatible.
|████████████████████████████████| 378kB 2.1MB/s
Segmentation Models: using `tf.keras` framework.
Update
You don't need to install any specific version of tensorflow / keras. Any version above 2.x would be ok to run, i.e tf 2.4/ 2.5/ 2.6. However, in colab, you need to restart the kernel to see the effect. but if you run on the kaggle kernel, you don't need to restart the kernel. See below:
In colab:
# Cell: 1
import os
!pip install -U -q segmentation-models --user
os.kill(os.getpid(), 9)
It will auto-restart the kernel. After restarting, run the following code in the new cell.
#Cell: 2
import os
os.environ["SM_FRAMEWORK"] = "tf.keras"
import segmentation_models as sm
In Kaggle Kernel:
import os
!pip install -U -q segmentation-models --user
os.environ["SM_FRAMEWORK"] = "tf.keras"
import segmentation_models as sm

specifying below, before importing segmentation models, alone worked for me in colab
os.environ["SM_FRAMEWORK"] = "tf.keras"

import tensorflow as tf
import keras
print(tf.__version__, keras.__version__)
output: 2.7.0 2.7.0
I tried a lot of answers but none of them worked for me.
The reason of the error: AttributeError: module 'tensorflow.compat.v2.internal.distribute' has no attribute 'strategy_supports_no_merge_call'
in my case was that I had tensorflow 2.7.0 and keras 2.6.0 installed on my device.
output while getting this error: 2.7.0 2.6.0
just match the versions, it worked for me.

I was getting this error message after upgrading Tensorflow to 2.7.0 . Downgrading to 2.5.0 is a temporary working fix.
pip install tensorflow==2.5.0

Related

How to fix "ModuleNotFoundError: No module named 'keras.layers.advanced_activations' "

I'm trying to import imageai
from imageai.Detection import VideoObjectDetection
but get error message below
ModuleNotFoundError: No module named 'keras.layers.advanced_activations'
My tensorflow version 2.9.1,
keras version 2.9.0,
keras-preprocessing version 1.1.2,
image ai version 2.1.5,
I installed the imageai via pip install imageai-2.0.2-py3-none-any.whl and download from here.
The following code is an example for calling several types of advanced activation, Try to call it by
from keras.layers import ELU, PReLU, LeakyReLU
The import works with keras version 2.9.0 as below.
from keras.layers import LeakyReLU
I encountered the same error today. Downgrading my keras to 2.1.0 and tensorflow to 2.2.0 got rid of the error.
I faced the same error. ModuleNotFoundError: No module named 'keras.layers.advanced_activations'
To solve this problem:
Try to install TensorFlow 2.8.2 which is compatible with Keras 2.8.0
pip install tensorflow==2.8.2
Please install imageai using this link or use below code as you must install few libraries before installing imageai.
pip install cython pillow>=7.0.0 numpy>=1.18.1 opencv-python>=4.1.2 torch>=1.9.0 --extra-index-url https://download.pytorch.org/whl/cpu torchvision>=0.10.0 --extra-index-url https://download.pytorch.org/whl/cpu pytest==7.1.3 tqdm==4.64.1 scipy>=1.7.3 matplotlib>=3.4.3 mock==4.0.3
pip install imageai --upgrade
then import VideoObjectDetection as below:
from imageai.Detection import VideoObjectDetection

ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental'

I'm trying to create object detection using tensorflow as an assignment.
ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (c:\Project\.venv\lib\site-packages\tensorflow\_api\v2\compat\v2\experimental\__init__.py)
I would appreciate any help!
I got the same error. I downgraded to Keras v2.6.0, which fixed the issue.
Hence, as of 16.05.2022, this seems to be caues by a bug in v2.9.*
To downgrade to v2.6.0, run:
!pip install keras==2.6.*
Please try again by upgrading the TensorFlow version to the latest version 2.9 and restart the kernel(runtime) to fix this issue.
!pip install tensorflow==2.9
#Or
!pip install --quiet --upgrade --pre tensorflow
For example:
import tensorflow as tf
from tensorflow.experimental import dtensor
print('TensorFlow version:', tf.__version__)
mesh_1d = dtensor.create_mesh([('x', 6)])#, devices=DEVICES)
print(mesh_1d)
Output:
TensorFlow version: 2.9.1
<tensorflow.dtensor.python.layout.Mesh object at 0x7f890d7ce690>
Please check this link for more details.

Installing waymo opendataset on windows

I am trying to install waymo open dataset in the windows environment. However when I try I get the following error:
(python3.6) C:\Users\19729>pip install waymo-open-dataset-tf-2-3-0 ERROR: Could not find a version that satisfies the requirement waymo-open-dataset-tf-2-3-0 (from versions: none) ERROR: No matching distribution found for waymo-open-dataset-tf-2-3-0
How to resolve this?
This piece of code does not show any error when I tried this in colab with TensorFlow 2.7.
However you can try again using below code :
pip3 install --upgrade pip
pip3 install waymo-open-dataset-tf-2-6-0 --user # for tf 2.6.0. OR
pip3 install waymo-open-dataset-tf-2-5-0 --user # for tf 2.5.0. OR
pip3 install waymo-open-dataset-tf-2-4-0 --user # for tf 2.4.0.
You can also load this built-in tensorflow dataset from tensorflow_datasets using:
!pip install tensorflow_datasets
import tensorflow_datasets as tfds
from tensorflow_datasets.object_detection import WaymoOpenDataset
ds = tfds.load('waymo_open_dataset', split='train', shuffle_files=True)
Note: This may take some time as it is 336.62 GiB dataset.
There are some waymo_open_datasets also available in Kaggle.

module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'

I'm trying to use TensorFlow as backend yesterday I can use it, but today when I use it to show some error message when I'm trying to import Keras, so here's my code:
# Install required libs
# NOTE: Run this one code, then restart this runtime and run again for next all... (PENTING!!!)
### please update Albumentations to version>=0.3.0 for `Lambda` transform support
!pip install -U segmentation-models
!pip install q tensorflow==2.1
!pip install q keras==2.3.1
!pip install tensorflow-estimator==2.1.
## Imports libs
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import cv2
import Keras
import NumPy as np
import matplotlib.pyplot as plt
it shows this error:
AttributeError Traceback (most recent call last)
<ipython-input-3-9c78a7be919d> in <module>()
5
6 import cv2
----> 7 import keras
8 import numpy as np
9 import matplotlib.pyplot as plt
8 frames
/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py in populate_deserializable_objects()
47
48 LOCAL.ALL_OBJECTS = {}
---> 49 LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
50
51 # Compatibility aliases (need to exist in both V1 and V2).
AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'
while therefore I was using TensorFlow version 2.2 and Keras version 2.3.1, yesterday I can run, but today it seems can't. did I was the wrong version import for my Keras and TensorFlow for today?
Edit:
when I use from tensorFlow import keras the output I want using tensorflow backend doesn't show up, And then when I load import segmentation_models as sm it shows the same error when I use import Keras like on above.
Here is the solution to your problem, I've tested it on colab.
!pip install -U -q segmentation-models
!pip install -q tensorflow==2.1
!pip install -q keras==2.3.1
!pip install -q tensorflow-estimator==2.1.
## Imports libs
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ["SM_FRAMEWORK"] = "tf.keras"
from tensorflow import keras
import segmentation_models as sm
|████████████████████████████████| 51kB 3.3MB/s
|████████████████████████████████| 421.8MB 42kB/s
|████████████████████████████████| 450kB 35.7MB/s
|████████████████████████████████| 3.9MB 33.6MB/s
Building wheel for gast (setup.py) ... done
ERROR: tensorflow-probability 0.12.1 has requirement gast>=0.3.2,
but you'll have gast 0.2.2 which is incompatible.
|████████████████████████████████| 378kB 2.1MB/s
Segmentation Models: using `tf.keras` framework.
Update
You don't need to install any specific version of tensorflow / keras. Any version above 2.x would be ok to run, i.e tf 2.4/ 2.5/ 2.6. However, in colab, you need to restart the kernel to see the effect. but if you run on the kaggle kernel, you don't need to restart the kernel. See below:
In colab:
# Cell: 1
import os
!pip install -U -q segmentation-models --user
os.kill(os.getpid(), 9)
It will auto-restart the kernel. After restarting, run the following code in the new cell.
#Cell: 2
import os
os.environ["SM_FRAMEWORK"] = "tf.keras"
import segmentation_models as sm
In Kaggle Kernel:
import os
!pip install -U -q segmentation-models --user
os.environ["SM_FRAMEWORK"] = "tf.keras"
import segmentation_models as sm
specifying below, before importing segmentation models, alone worked for me in colab
os.environ["SM_FRAMEWORK"] = "tf.keras"
import tensorflow as tf
import keras
print(tf.__version__, keras.__version__)
output: 2.7.0 2.7.0
I tried a lot of answers but none of them worked for me.
The reason of the error: AttributeError: module 'tensorflow.compat.v2.internal.distribute' has no attribute 'strategy_supports_no_merge_call'
in my case was that I had tensorflow 2.7.0 and keras 2.6.0 installed on my device.
output while getting this error: 2.7.0 2.6.0
just match the versions, it worked for me.
I was getting this error message after upgrading Tensorflow to 2.7.0 . Downgrading to 2.5.0 is a temporary working fix.
pip install tensorflow==2.5.0

How to fix ' ModuleNotFoundError: No module named 'tensorflow.python.keras' importing ImagePrediction?

I want to run tensorflow for image recognition. I have followed all the steps for it and both keras and tensorflow are installed on my computer.
Steps in this post: https://github.com/OlafenwaMoses/ImageAI/
But when I try:
from imageai.Prediction import ImagePrediction
I keep getting the error:
from tensorflow.python.keras.preprocessing import image
ModuleNotFoundError: No module named 'tensorflow.python.keras'
I think the error comes from my installation of 'tensorflow'. When I tried the method:
pip3 install --upgrade tensorflow
I got the error:
Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow
So I used instead:
python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py3-none-any.whl
I got it from this post: TensorFlow not found using pip
My guess is that the first error drives me to the second, but I don't know how to fix any of them.
Any suggestions?
My code until the problem is:
import tensorflow
from imageai.Prediction import ImagePrediction
Try writing
from keras.preprocessing import image
Instead of
from tensorflow.python.keras.preprocessing import image
And do the same with all Keras calls.
Make sure you have the latest version of tensorflow (2.0)
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras.preprocessessing.text import Tokenizer

Categories