模块“tensorflow.compat.v2.__internal__"没有属性“tf2" [英] module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'

查看:53
本文介绍了模块“tensorflow.compat.v2.__internal__"没有属性“tf2"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我尝试使用 TensorFlow 作为后端我可以使用它,但是今天当我尝试导入 Keras 时使用它显示一些错误消息,所以这是我的代码:

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

它显示此错误:

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'

因此我使用的是 TensorFlow 2.2 版和 Keras 2.3.1 版,昨天我可以运行,但今天似乎不能.我今天的 Keras 和 TensorFlow 版本导入是否错误?

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?

当我使用 from tensorFlow import keras 我想要的输出 using tensorflow backend 没有出现,然后当我加载 import segment_models as sm当我像上面一样使用 import Keras 时,它显示了同样的错误.

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.

推荐答案

这是您问题的解决方案,我已经在 colab 上进行了测试.

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.


更新

不需要安装任何特定版本的tensorflow/keras.2.x 以上的任何版本都可以运行,即 tf 2.4/2.5/2.6.但是在colab中,需要重启内核才能看到效果.但是如果你在kaggle内核上运行,你就不需要重新启动内核.见下文:


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:

在协作实验室中:

# 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

在 Kaggle 内核中:

import os 
!pip install -U -q segmentation-models --user
os.environ["SM_FRAMEWORK"] = "tf.keras"
import segmentation_models as sm

这篇关于模块“tensorflow.compat.v2.__internal__"没有属性“tf2"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆