google / automl

Google Brain AutoML
Apache License 2.0
6.25k stars 1.45k forks source link

efficientnetv2| AttributeError: module 'keras.api._v2.keras.mixed_precision' has no attribute 'set_policy' #1153

Closed exx8 closed 2 years ago

exx8 commented 2 years ago

I've tried to run efficientnetv2 on my gpu. python3 main.py --mode=train --model_name=efficientnetv2-s --dataset_cfg=imagenet --model_dir=~/effi/models/efficientnetv2 --use_tpu=False I get the following error:

W0503 10:34:10.524389 140544638949184 module_wrapper.py:149] From /home/eran/effi/models/efficientnetv2/utils.py:412: The name tf.keras.layers.enable_v2_dtype_behavior is deprecated. Please use tf.compat.v1.keras.layers.enable_v2_dtype_behavior instead.

<module 'keras.api._v2.keras.mixed_precision' from '/home/eran/.local/lib/python3.8/site-packages/keras/api/_v2/keras/mixed_precision/__init__.py'>
INFO:tensorflow:Mixed precision compatibility check (mixed_float16): OK
Your GPU will likely run quickly with dtype policy mixed_float16 as it has compute capability of at least 7.0. Your GPU: NVIDIA GeForce RTX 2080 Ti, compute capability 7.5
I0503 10:34:10.524871 140544638949184 device_compatibility_check.py:117] Mixed precision compatibility check (mixed_float16): OK
Your GPU will likely run quickly with dtype policy mixed_float16 as it has compute capability of at least 7.0. Your GPU: NVIDIA GeForce RTX 2080 Ti, compute capability 7.5
INFO:tensorflow:training_loop marked as finished
I0503 10:34:10.524967 140544638949184 error_handling.py:115] training_loop marked as finished
WARNING:tensorflow:Reraising captured error
W0503 10:34:10.525012 140544638949184 error_handling.py:149] Reraising captured error
Traceback (most recent call last):
  File "main.py", line 507, in <module>
    app.run(main)
  File "/home/eran/.local/lib/python3.8/site-packages/absl/app.py", line 312, in run
    _run_main(main, args)
  File "/home/eran/.local/lib/python3.8/site-packages/absl/app.py", line 258, in _run_main
    sys.exit(main(argv))
  File "main.py", line 499, in main
    est.train(
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/tpu/tpu_estimator.py", line 3102, in train
    rendezvous.raise_errors()
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/tpu/error_handling.py", line 150, in raise_errors
    six.reraise(typ, value, traceback)
  File "/home/eran/.local/lib/python3.8/site-packages/six.py", line 703, in reraise
    raise value
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/tpu/tpu_estimator.py", line 3092, in train
    return super(TPUEstimator, self).train(
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 360, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1186, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1214, in _train_model_default
    estimator_spec = self._call_model_fn(features, labels, ModeKeys.TRAIN,
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/tpu/tpu_estimator.py", line 2933, in _call_model_fn
    return super(TPUEstimator, self)._call_model_fn(features, labels, mode,
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1174, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/tpu/tpu_estimator.py", line 3191, in _model_fn
    estimator_spec = model_fn_wrapper.call_without_tpu(
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/tpu/tpu_estimator.py", line 1714, in call_without_tpu
    return self._call_model_fn(features, labels, is_export_mode=is_export_mode)
  File "/home/eran/.local/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/tpu/tpu_estimator.py", line 2057, in _call_model_fn
    estimator_spec = self._model_fn(features=features, **kwargs)
  File "main.py", line 94, in model_fn
    logits = utils.build_model_with_precision(precision, build_model, images,
  File "/home/eran/effi/models/efficientnetv2/utils.py", line 444, in build_model_with_precision
    set_precision_policy(pp)
  File "/home/eran/effi/models/efficientnetv2/utils.py", line 415, in set_precision_policy
    tf.keras.mixed_precision.set_policy(policy)
AttributeError: module 'keras.api._v2.keras.mixed_precision' has no attribute 'set_policy'

Tensorflow version: 2.8 python version 3.8.10 cuda version:11.6

mausam1112 commented 2 years ago

Referencing the AttributeError: After removing the "experimental" from keras all the classes related to it are depreciated and provided their functionality with another API. You can update the following line : tf.keras.mixed_precision.set_policy(policy) with tf.keras.mixed_precision.set_global_policy(policy)