【问题标题】:module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'模块“tensorflow.compat.v2.__internal__”没有属性“tf2”
【发布时间】:2021-08-14 04:50:55
【问题描述】:

我昨天尝试使用 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 时,它显示相同的错误。

【问题讨论】:

    标签: python tensorflow machine-learning keras deep-learning


    【解决方案1】:

    这是您问题的解决方案,我已经在 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 / keras2.x 以上的任何版本都可以运行,即tf 2.4/ 2.5/ 2.6。但是在colab中,需要重启内核才能看到效果。但是如果你在 kaggle 内核上运行,则不需要重新启动内核。见下文:

    在 colab 中:

    # Cell: 1
    import os 
    !pip install -U -q segmentation-models --user
    os.kill(os.getpid(), 9)
    

    它将自动重启内核。重启后,在新单元格中运行以下代码。

    #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
    

    【讨论】:

    • 请问,为什么当我从导入 Keras 加载时,我无法使用 TensorFlow 后端?为什么没有使用 TensorFlow 后端的输出?或者我必须使用 tf.Keras 而不是 Keras?
    • tf 2.x 之后,独立的keras 不再维护,它成为tf 的一部分。因此,以前我们使用import keras,现在(在tf .2.x 之后,我们应该使用from ten.. import keras。因为您使用的是tf 2.,所以您应该以这种方式导入。
    • 没有显示TensorFlow backend的原因是因为在旧的keras中它有三个支持,其中一个是tf(AFAIK)但在tf 2.x之后,只有一个支持,即tf,因此该消息现在已被贬值
    【解决方案2】:

    在下面指定,在导入分割模型之前,单独在 colab 中为我工作

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

    【讨论】:

      【解决方案3】:

      我在将 Tensorflow 升级到 2.7.0 后收到此错误消息。降级到 2.5.0 是一个临时的工作修复。

      pip install tensorflow==2.5.0

      【讨论】:

      • 你用过os.environ["SM_FRAMEWORK"] = "tf.keras"吗?我认为现在可以使用任何版本的tf 2.x
      • 我试过了,得到了同样的错误信息,没有任何改变
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多