tensorflow: AttributeError: module 'keras.layers' has no attribute 'experimental': M1 Mac Tensorflow Metal

I am trying to run a TensorFlow model on M1 Mac with the following settings:

  1. MacBook Pro M1
  2. macOS 12.4
  3. tensorflow-deps & tensorflow-estimator --> 2.9.0
  4. tensorflow-macos --> 2.9.2
  5. tensorflow-metal --> 0.5.0
  6. keras --> 2.9.0
  7. keras-preprocessing --> 1.1.2
  8. Python 3.8.13

When resizing and rescaling from keras.layers, I got the following error:

resize_and_rescale = keras.Sequential([
   layers.experimental.preprocessing.Resizing(IMAGE_SIZE, IMAGE_SIZE),
   layers.experimental.preprocessing.Rescaling(1./255),
])

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [15], in <cell line: 1>()
      1 resize_and_rescale = keras.Sequential([
----> 2   layers.experimental.preprocessing.Resizing(IMAGE_SIZE, IMAGE_SIZE),
      3   layers.experimental.preprocessing.Rescaling(1./255),
      4 ])
AttributeError: module 'keras.layers' has no attribute 'experimental'

Any suggestions? Thanks

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

AttributeError Traceback (most recent call last) Cell In[111], line 12 10 from object_detection.utils import config_util 11 from object_detection.utils import visualization_utils as viz_utils —> 12 from object_detection.builders import model_builder File ~\Desktop\Tensor\RealTimeObjectDetection\Tensorflow\models\research\object_detection\builders\model_builder.py:26 24 from object_detection.builders import box_coder_builder 25 from object_detection.builders import box_predictor_builder —> 26 from object_detection.builders import hyperparams_builder 27 from object_detection.builders import image_resizer_builder 28 from object_detection.builders import losses_builder File ~\Desktop\Tensor\RealTimeObjectDetection\Tensorflow\models\research\object_detection\builders\hyperparams_builder.py:27 25 # pylint: disable=g-import-not-at-top 26 if tf_version.is_tf2(): —> 27 from object_detection.core import freezable_sync_batch_norm 28 # pylint: enable=g-import-not-at-top 31 class KerasLayerHyperparams(object): File ~\Desktop\Tensor\RealTimeObjectDetection\Tensorflow\models\research\object_detection\core\freezable_sync_batch_norm.py:20 16 “”“A freezable batch norm layer that uses Keras sync batch normalization.”“” 17 import tensorflow as tf —> 20 class FreezableSyncBatchNorm(tf.keras.layers.experimental.SyncBatchNormalization 21 ): 22 “”“Sync Batch normalization layer (Ioffe and Szegedy, 2014). 23 24 This is a freezable batch norm layer that supports setting the training (…) 48 Internal Covariate Shift](https://arxiv.org/abs/1502.03167) 49 “”” 51 def init(self, training=None, **kwargs): AttributeError: module ‘keras._tf_keras.keras.layers’ has no attribute ‘experimental’

I am getting the same error, have you fixed it?

I solved it by limiting the versions of tf-models-official >=2.5.1, <2.16.0 and comment out keras in the research/object_detection/packages/tf2/setup.py I run the code in colab and colab’s keras is version 2.15.0. I suspect the newest keras messed up the object detection API

@Laelmukeni

And how does one limit the versions of tf-models-official? Thank you

Modify: research/object_detection/packages/tf2/setup.py

"""Setup script for object_detection with TF2.0."""
import os
from setuptools import find_packages
from setuptools import setup

REQUIRED_PACKAGES = [
    # Required for apache-beam with PY3
    'avro-python3',
    'apache-beam',
    'pillow',
    'lxml',
    'matplotlib',
    'Cython',
    'contextlib2',
    'tf-slim',
    'six',
    'pycocotools',
    'lvis',
    'scipy',
    'pandas',
    'tf-models-official >=2.5.1, <2.16.0',
    'tensorflow_io',
    #'keras',
    'pyparsing==2.4.7',  # TODO(b/204103388)
    'sacrebleu<=2.2.0'  # https://github.com/mjpost/sacrebleu/issues/209
]

setup(
    name='object_detection',
    version='0.1',
    install_requires=REQUIRED_PACKAGES,
    include_package_data=True,
    packages=(
        [p for p in find_packages() if p.startswith('object_detection')] +
        find_packages(where=os.path.join('.', 'slim'))),
    package_dir={
        'datasets': os.path.join('slim', 'datasets'),
        'nets': os.path.join('slim', 'nets'),
        'preprocessing': os.path.join('slim', 'preprocessing'),
        'deployment': os.path.join('slim', 'deployment'),
        'scripts': os.path.join('slim', 'scripts'),
    },
    description='Tensorflow Object Detection Library',
    python_requires='>3.6',
)

For me, this fixed the issue

import matplotlib import matplotlib.pyplot as plt
import io import os import numpy as np from six import BytesIO from PIL import Image, ImageDraw, ImageFont
import tensorflow as tf
from object_detection.utils import label_map_util from object_detection.utils import config_util from object_detection.utils import visualization_utils as viz_utils from object_detection.builders import model_builder