本文主要介绍Python中,在google colab运行 tensorflow模型(model)报错:AttributeError: module 'tensorflow._api.v1.compat.v2' has no attribute '__internal__'解决方法。

报错信息:

 Using TensorFlow backend.
Traceback (most recent call last):
File "train.py", line 6, in <module>
from yolo import create_yolov3_model, dummy_loss
File "/content/drive/MyDrive/yolo/yolo_plz_work/yolo.py", line 1, in <module>
from keras.layers import Conv2D, Input, BatchNormalization, LeakyReLU, ZeroPadding2D, UpSampling2D, Lambda
File "/usr/local/lib/python3.7/dist-packages/keras/__init__.py", line 3, in <module>
from . import utils
File "/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py", line 26, in <module>
from .vis_utils import model_to_dot
File "/usr/local/lib/python3.7/dist-packages/keras/utils/vis_utils.py", line 7, in <module>
from ..models import Model
File "/usr/local/lib/python3.7/dist-packages/keras/models.py", line 10, in <module>
from .engine.input_layer import Input
File "/usr/local/lib/python3.7/dist-packages/keras/engine/__init__.py", line 3, in <module>
from .input_layer import Input
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_layer.py", line 7, in <module>
from .base_layer import Layer
File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 12, in <module>
from .. import initializers
File "/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py", line 124, in <module>
populate_deserializable_objects()
File "/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py", line 49, in populate_deserializable_objects
LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/module_wrapper.py", line 193, in __getattr__
attr = getattr(self._tfmw_wrapped_module, name)
AttributeError: module 'tensorflow._api.v1.compat.v2' has no attribute '__internal__'

解决方法:

!pip3 uninstall keras-nightly
!pip3 uninstall -y tensorflow
!pip3 install keras==2.1.6
!pip3 install tensorflow==1.15.0
!pip3 install h5py==2.10.0

推荐文档