Search code examples
pythontensorflow2.0object-detectionobject-detection-api

How to generate .ckpt file for training and inference in tensorflow object detection api


I am trying to do object detection using TensorFlow Object detection API using EfficientDet D3 model from TensorFlow zoo hear. I found folder for pre-trained checkpoint hear under section 2 pre-trained checkpoint.

I need to specify the path of checkpoint in .config file for training. But I am unable to find .ckpt file not from model, not from pre-trained checkpoint folder downloaded above.

I found similar issue hear for magenta. but that not worked for me. If anyone know how to generate model.ckpt file from model.ckpt-data-00000-of-000001, model.ckpt.index, model.ckpt.meta in tensorflow 2 then tell me it might solve my problem

I am using TensorFlow 2 from google colab

Edit 1: I downloaded model from TensorFlow model zoo. It has bellow structure.

At path /content/models/research/object_detection/EfficientDet_D3/ # this is model dir

efficientdet_d3_coco17_tpu-32/
efficientdet_d3_coco17_tpu-32/checkpoint/
efficientdet_d3_coco17_tpu-32/checkpoint/ckpt-0.data-00000-of-00001
efficientdet_d3_coco17_tpu-32/checkpoint/checkpoint
efficientdet_d3_coco17_tpu-32/checkpoint/ckpt-0.index
efficientdet_d3_coco17_tpu-32/pipeline.config
efficientdet_d3_coco17_tpu-32/saved_model/
efficientdet_d3_coco17_tpu-32/saved_model/saved_model.pb
efficientdet_d3_coco17_tpu-32/saved_model/assets/
efficientdet_d3_coco17_tpu-32/saved_model/variables/
efficientdet_d3_coco17_tpu-32/saved_model/variables/variables.data-00000-of-00001
efficientdet_d3_coco17_tpu-32/saved_model/variables/variables.index

I also downloaded checkpoint from using this link from EfficientDet readme in github. It's structure looks like bellow.

At path/content/models/research/object_detection/efficientdet-d3/ # this is checkpoint dir

efficientdet-d3/
efficientdet-d3/model.meta
efficientdet-d3/d3_coco_val_softnms.txt
efficientdet-d3/d3_coco_test-dev2017_softnms.txt
efficientdet-d3/model.index
efficientdet-d3/detections_test-dev2017_d3_results.zip
efficientdet-d3/checkpoint
efficientdet-d3/model.data-00000-of-00001

I specified path to .ckpt in pipeline.config as bellow.

fine_tune_checkpoint: "/content/models/research/object_detection/efficientdet-d3/model.ckpt"

But It seems incorrect as I got error bellow.

Traceback (most recent call last):
  File "model_main_tf2.py", line 113, in <module>
    tf.compat.v1.app.run()
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/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 300, in run
    _run_main(main, args)
  File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 251, in _run_main
    sys.exit(main(argv))
  File "model_main_tf2.py", line 110, in main
    record_summaries=FLAGS.record_summaries)
  File "/root/.local/lib/python3.6/site-packages/object_detection-0.1-py3.6.egg/object_detection/model_lib_v2.py", line 569, in train_loop
    unpad_groundtruth_tensors)
  File "/root/.local/lib/python3.6/site-packages/object_detection-0.1-py3.6.egg/object_detection/model_lib_v2.py", line 345, in load_fine_tune_checkpoint
    if not is_object_based_checkpoint(checkpoint_path):
  File "/root/.local/lib/python3.6/site-packages/object_detection-0.1-py3.6.egg/object_detection/model_lib_v2.py", line 308, in is_object_based_checkpoint
    var_names = [var[0] for var in tf.train.list_variables(checkpoint_path)]
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/checkpoint_utils.py", line 98, in list_variables
    reader = load_checkpoint(ckpt_dir_or_file)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/checkpoint_utils.py", line 67, in load_checkpoint
    return py_checkpoint_reader.NewCheckpointReader(filename)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/py_checkpoint_reader.py", line 99, in NewCheckpointReader
    error_translator(e)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/py_checkpoint_reader.py", line 35, in error_translator
    raise errors_impl.NotFoundError(None, None, error_message)
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /content/models/research/object_detection/efficientdet-d3/model.ckpt

Solution

  • Usually when you download a pretrained model you have 7 files.

    • The saved_model, the model in the Tensorflow saved format (https://www.tensorflow.org/guide/saved_model)
    • The frozen_inference_graph, the model that can be used only for inference where all its weights has been frozen, it cannot be trained anymore.
    • The three checkpoints files. The .meta for all the metadata, the index to point at the right checkpoint and one or more datafiles.
    • The pipeline.config containing the config used for the previous training.
    • A last checkpoint file with those lines :
        model_checkpoint_path: "model.ckpt"
        all_model_checkpoint_paths: "model.ckpt"
    

    In conclusion the .ckpt you're looking for doesn't really exists, it is only the assembly of the 4 checkpoints files. To use it just put in your config file :

      fine_tune_checkpoint: ".../efficientnet/models/model.ckpt"
    

    Tensorflow doc on checkpoints : https://www.tensorflow.org/guide/checkpoint