Skip to content Skip to sidebar Skip to footer

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 req

Solution 1:

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 libsimport 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 --useros.kill(os.getpid(), 9)

It will auto-restart the kernel. After restarting, run the following code in the new cell.

#Cell: 2import 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

Solution 2:

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

os.environ["SM_FRAMEWORK"] = "tf.keras"

Solution 3:

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

Post a Comment for "Module 'tensorflow.compat.v2.__internal__' Has No Attribute 'tf2'"