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.
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:
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:
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:
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:
If that file is imported again, the same flag is defined again.
Better pattern:
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:
- Runtime
- Restart runtime
- rerun the setup cells once
- 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.pydirectly 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
abslflags 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
- '
DuplicateFlagErroris usually caused byabsl.flagsbeing 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.
Related reading
- dyld Library not loaded rpath/libcudart.8.0.dylib, while building tensorflow on Mac OSX
- Dynamic size for tf.zeros for use with placeholders with None dimensions
- Eager Execution - InternalError Could not find valid device for node name Sqrt
- Eager execution in Tensorflow 2
- Edit tensorflow inceptionV3 retraining-example.py for multiple classificiations
- Efficiently implementing erode/dilate
- Duplicating training examples to handle class imbalance in a pandas data frame
- Early stopping with Keras and sklearn GridSearchCV cross-validation
System design interview problems on this topic
- Design an E-commerce Recommendation System: solution and walkthrough
- Design an LLM Inference Service: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough
- Design ChatGPT: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.