TensorFlow
Object Detection
Google Colaboratory
Error Handling
Machine Learning

DuplicateFlagError when trying to train tensorflow object detection api on google collaboratory

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

DuplicateFlagError in Colab usually has very little to do with your model. It happens because the TensorFlow Object Detection API uses absl.flags, and notebook environments keep Python state alive across cell reruns, so a flag that was defined once gets defined again.

Why the error appears in Colab

The Object Detection API training entry points are written as standalone scripts. They expect to be started once from the command line, define their flags once, and then exit. Colab does the opposite: you import modules, rerun cells, and keep the process alive.

That difference is enough to trigger errors like this:

python
from object_detection import model_main_tf2

If model_main_tf2 or a module it imports calls flags.DEFINE_string during import, rerunning the cell tries to register the same flag name again in the global absl registry. The second registration raises DuplicateFlagError.

The most reliable fix: run training as a separate process

In Colab, the safest pattern is to avoid importing the training script into the notebook process. Call it as a separate Python process instead:

bash
1!python model_main_tf2.py \
2  --model_dir=/content/training \
3  --pipeline_config_path=/content/data/pipeline.config \
4  --num_train_steps=2000 \
5  --alsologtostderr

This works because each !python invocation starts a fresh interpreter with an empty flag registry. That matches how the script was designed to run.

The same approach is useful for evaluation:

bash
1!python model_main_tf2.py \
2  --model_dir=/content/training \
3  --pipeline_config_path=/content/data/pipeline.config \
4  --checkpoint_dir=/content/training \
5  --alsologtostderr

If you have custom code, isolate flag definitions

Sometimes the problem comes from your own wrapper script. The fix is to keep absl.flags definitions in the actual command-line entry point instead of a module that gets imported repeatedly.

Bad pattern:

python
1from absl import flags
2
3FLAGS = flags.FLAGS
4flags.DEFINE_string("config_path", None, "Pipeline config")

If that file is imported again, the same flag is defined again.

Better pattern:

python
1from absl import app, flags
2
3FLAGS = flags.FLAGS
4
5def define_flags():
6    flags.DEFINE_string("config_path", None, "Pipeline config")
7
8def main(argv):
9    del argv
10    print("Training with:", FLAGS.config_path)
11
12if __name__ == "__main__":
13    define_flags()
14    app.run(main)

The key idea is that the module can be imported safely without immediately mutating the global flag registry.

When a restart is the right answer

Once the notebook kernel already contains duplicate flag state, there is usually no clean in-process reset. In practice, the fastest fix is:

  1. Runtime
  2. Restart runtime
  3. rerun the setup cells once
  4. launch training through !python

Trying to surgically remove flags from absl.flags.FLAGS is fragile and not worth it for normal training workflows.

Colab-specific workflow that avoids repeated failures

Use notebooks for orchestration and scripts for execution:

  • install dependencies in the notebook
  • write or edit config files in the notebook
  • launch training and evaluation with shell commands
  • inspect checkpoints and metrics from the notebook after the process exits

That separation keeps the notebook interactive without forcing script-style code into a long-lived interpreter.

Common Pitfalls

  • Importing model_main_tf2.py directly into a notebook cell instead of launching it as a separate process.
  • Rerunning setup cells that define flags without restarting the Colab runtime first.
  • Defining absl flags at module import time inside helper files.
  • Mixing notebook logic and command-line entry-point logic in the same file.
  • Spending time debugging TensorFlow itself when the real issue is the notebook's persistent Python state.

Summary

  • 'DuplicateFlagError is usually caused by absl.flags being defined more than once in the same Colab kernel.'
  • The safest fix is to run TensorFlow Object Detection API scripts with !python, not with direct imports.
  • Move custom flag definitions behind a real entry point so imports stay side-effect free.
  • Restart the runtime when the flag registry is already polluted.
  • Treat Colab as the controller and training scripts as separate processes.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design