Reputation: 61
When I try to import Keras.layers as shown below in Jupyter notebook, I am met with this error.
# Basic Imports
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
from PIL import Image
# Imports for Building CNN
from tensorflow import keras
from keras.layers import Input, Lambda, Dense, Flatten
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, load_model
from tensorflow.keras.preprocessing.image import load_img
# Ignore Warnings
import warnings
warnings.filterwarnings("ignore")
AttributeError: module 'tensorflow.compat.v2.internal' has no attribute 'dispatch'
I got this error from this error feed:
> ---------------------------------------------------------------------------
> AttributeError Traceback (most recent call last)
> Input In [1], in <cell line: 9>()
> 7 # Imports for Building CNN
> 8 from tensorflow import keras
> ----> 9 from keras.layers import Input, Lambda, Dense, Flatten
> 10 from keras.models import Model
> 11 from keras.applications.vgg16 import VGG16
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\__init__.py:20,
> in <module>
> 1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
> 2 #
> 3 # Licensed under the Apache License, Version 2.0 (the "License");
> (...)
> 13 # limitations under the License.
> 14 # ==============================================================================
> 15 """Implementation of the Keras API, the high-level API of TensorFlow.
> 16
> 17 Detailed documentation and user guides are available at
> 18 [keras.io](https://keras.io).
> 19 """
> ---> 20 from keras import distribute
> 21 from keras import models
> 22 from keras.engine.input_layer import Input
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\distribute\__init__.py:18,
> in <module>
> 1 # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
> 2 #
> 3 # Licensed under the Apache License, Version 2.0 (the "License");
> (...)
> 13 # limitations under the License.
> 14 # ==============================================================================
> 15 """Keras' Distribution Strategy library."""
> ---> 18 from keras.distribute import sidecar_evaluator
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\distribute\sidecar_evaluator.py:22,
> in <module>
> 20 from tensorflow.python.platform import tf_logging as logging
> 21 from tensorflow.python.util import deprecation
> ---> 22 from keras.optimizers.optimizer_experimental import (
> 23 optimizer as optimizer_experimental,
> 24 )
> 25 from tensorflow.python.util.tf_export import keras_export
> 27 _PRINT_EVAL_STEP_EVERY_SEC = 60.0
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\optimizers\__init__.py:25,
> in <module>
> 22 import tensorflow.compat.v2 as tf
> 24 # Imports needed for deserialization.
> ---> 25 from keras import backend
> 26 from keras.optimizers.legacy import adadelta as adadelta_legacy
> 27 from keras.optimizers.legacy import adagrad as adagrad_legacy
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\backend.py:32,
> in <module>
> 29 import numpy as np
> 30 import tensorflow.compat.v2 as tf
> ---> 32 from keras import backend_config
> 33 from keras.distribute import distribute_coordinator_utils as dc
> 34 from keras.engine import keras_tensor
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\backend_config.py:33,
> in <module>
> 28 # Default image data format, one of "channels_last", "channels_first".
> 29 _IMAGE_DATA_FORMAT = "channels_last"
> 32 @keras_export("keras.backend.epsilon")
> ---> 33 @tf.__internal__.dispatch.add_dispatch_support
> 34 def epsilon():
> 35 """Returns the value of the fuzz factor used in numeric expressions.
> 36
> 37 Returns:
> (...)
> 42 1e-07
> 43 """
> 44 return _EPSILON
>
> AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch'
Confused on why I am getting this error. I just want to import it so I can run my code.
Upvotes: 3
Views: 5788
Reputation: 91
this solved it for me (source: https://www.cnblogs.com/wwj321/p/16832760.html) [Got to translate the page]
In my case I'm using Tensorflow version 2.4.0, so I just added this before running the model builder (I'm using Google Colab):
!pip install -U -q segmentation-models
!pip install -q keras==2.4.3
from tensorflow import keras
import segmentation_models as sm
Upvotes: 2