昨天我尝试使用 TensorFlow 作为后端我可以使用它,但是今天当我尝试导入 Keras 时使用它来显示一些错误消息,所以这是我的代码:
# 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 版本有误吗?
编辑:当我使用 from tensorFlow import keras
我想要的输出 using tensorflow backend
没有出现,然后当我加载 import segmentation_models as sm
import Keras
显示错误 ---
如上。
原文由 uzrsa 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是您问题的解决方案,我已经在 colab 上对其进行了测试。
更新
您 不需要安装 任何特定版本的
tensorflow / keras
。 任何高于2.x
的版本都可以运行,即tf 2.4/ 2.5/ 2.6
。但是在colab中需要重启内核才能看到效果。但是如果你在kaggle内核上运行,你不需要重启内核。见下文:在协作中:
它会 自动重启 内核。重新启动后,在新单元格中运行以下代码。
在 Kaggle 内核中: