Search code examples
pythonobjecttensorflow2.0object-detection

Tensorflow object detection model_main_tf2.py trying to run tf1 code


I have a simple code snippet. I am running model_main_tf2.py. But I am getting an error that module 'tensorflow.compat.v1.io' has no attribute 'GFile'. It seems the program is trying to run tensorflow 1.x codebase. I am using tensorflow 2.8 and every package is updated. I am using Colab environment.

%env PYTHONPATH="$/env/python:/content/drive/MyDrive/tfod/Tensorflow/models:/content/drive/MyDrive/tfod/Tensorflow/models/research:/content/drive/MyDrive/tfod/Tensorflow/models/research/slim:/content/drive/MyDrive/tfod/Tensorflow/models/research/object_detection/protos"

TRAINING_SCRIPT = os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection', 'model_main_tf2.py')
command1 = "python {} --model_dir={} --pipeline_config_path={} --num_train_steps=10".format(TRAINING_SCRIPT, paths['CHECKPOINT_PATH'],files['PIPELINE_CONFIG'])
print(command1)
! {command1}

I have the traceback log as below. Any help is greatly appreciated.

Traceback (most recent call last):
File "/content/drive/MyDrive/tfod/Tensorflow/models/research/object_detection/model_main_tf2.py", line 115, in <module>
tf.compat.v1.app.run()
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/platform/app.py", line 36, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "/usr/local/lib/python3.7/dist-packages/absl/app.py", line 312, in run
_run_main(main, args)
File "/usr/local/lib/python3.7/dist-packages/absl/app.py", line 258, in _run_main
sys.exit(main(argv))
File "/content/drive/MyDrive/tfod/Tensorflow/models/research/object_detection/model_main_tf2.py", line 112, in main
record_summaries=FLAGS.record_summaries)
File "/content/drive/MyDrive/tfod/Tensorflow/models/research/object_detection/model_lib_v2.py", line 505, in train_loop
pipeline_config_path, config_override=config_override)
File "/content/drive/MyDrive/tfod/Tensorflow/models/research/object_detection/utils/config_util.py", line 137, in get_configs_from_pipeline_file
with tf.io.GFile(pipeline_config_path, "r") as f:
AttributeError: module 'tensorflow.compat.v1.io' has no attribute 'GFile'

Solution

  • Replace tf.gfile.GFile to tf.io.gfile.GFile