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.
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.
This is how I resolved it!
import tensorflow.compat.v1 as tf tf.disable_v2_behavior()
from tensorflow.python.keras.layers import Input, Dense from tensorflow.python.keras.models import Sequential
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.