tensorflow / tensorflow/models

module 'tensorflow._api.v1.compat.v2.compat' has no attribute 'v2'

Open
#10,207 0 comments 0 reactions 3 assignees View on GitHub

@pkulzc is already working on this.

Since Aug 13, 2021.

models:research:odapi type:bug
Dominant language
Python
Stars
77.7k
Forks
44.8k
PR merge metrics
No merged PRs in 30d

Description

Prerequisites

1. The entire URL of the file you are using

https://github.com/tensorflow/models/tree/master/research/object_detection/model_main_tf2.py

2. Describe the bug

Traceback (most recent call last):
  File "model_main_tf2.py", line 122, in <module>
    tf.compat.v1.app.run()
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/platform/app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 299, in run
    _run_main(main, args)
  File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 250, in _run_main
    sys.exit(main(argv))
  File "model_main_tf2.py", line 106, in main
    strategy = tf.compat.v2.distribute.MirroredStrategy()
  File "/usr/local/lib/python3.6/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.compat' has no attribute 'v2'

I am using model_main_tf2.py with the SSD MobileNet v2 320x320 model from https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md

python model_main_tf2.py -model_dir /root/models/research/object_detection --num_train_steps 400 --pipeline_config_path pipeline.config --sample_1_of_n_eval_examples 1

3. Steps to reproduce

I have taken the pipeline.config (for 13 classes and 400 steps) and the checkpoints provided for SSD MobileNet v2 320x320 pretrained model (see https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md).
The tfrecords are very small (2mb).

Also, I am using sagemaker.estimator (Framework: https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html) to launch the training process on AWS Sagemaker, but this shouldn't make any difference, since I get the same error even on my local machine.

4. Additional context

I think line
strategy = tf.compat.v2.distribute.MirroredStrategy()
from model_main_tf2.py is not correctly interpreting tf (tf as import tensorflow.compat.v2 as tf). Substituting tf.compat.v2 by tf seems to fix the problem. However, it later crashes with the call to model_lib_v2 one line later (for the same reason).

I have seen other related issues (https://github.com/tensorflow/models/issues/9651, https://github.com/tensorflow/models/issues/8088), but none of them seems to have a clear answer, just to downgrade the whole models repository.

5. System information

I am using the Docker image: tensorflow/tensorflow:2.5.0-gpu

  • OS Platform and Distribution: Ubuntu 18.04.
  • TensorFlow installed from (source or binary): Docker image.
  • TensorFlow version (use command below): 2.5.0
  • Python version: 3.6.9
  • CUDA/cuDNN version: cuda-11.2
  • GPU model and memory: The GPU from AWS Sagemaker ml.p3.2xlarge

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.