Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 682
uzrsa Avatar asked May 25 '21 22:05

uzrsa


People also ask

Which module does not have the attribute ‘TF2’?

module ‘tensorflow.compat.v2.__internal__‘ has no attribute ‘tf2‘ kunkun_1230于 2021-11-16 15:04:55 发布1978收藏3

Why is my TensorFlow not working with Keras?

This can be caused by a version mismatch between Keras installation and Tensorflow. Make sure their versions match. Show activity on this post. Thanks for contributing an answer to Stack Overflow!

What version of TensorFlow do I need to install?

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.


Video Answer


3 Answers

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
like image 157
M.Innat Avatar answered Nov 14 '22 00:11

M.Innat


specifying below, before importing segmentation models, alone worked for me in colab

os.environ["SM_FRAMEWORK"] = "tf.keras"
like image 30
Trapti Kalra Avatar answered Nov 13 '22 22:11

Trapti Kalra


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.

like image 40
parmar Komil Avatar answered Nov 13 '22 22:11

parmar Komil