tensorflow/models · error · Exception
Model has to be built before number of boxes can be…
Error message
Model has to be built before number of boxes can be determined.
What it means
Property guard in YoloHead.num_boxes: the box count is computed over feature levels from min_level to max_level, but those attributes are still None because the head's build()/call() has never run (no input shapes seen). Until the model is built the level range — and therefore the anchor/box total — is unknown, so reading num_boxes early raises. Note it raises the overly-broad Exception rather than ValueError.
Solutions
- Call the model on a dummy input (or model.build(input_shape)) before reading num_boxes
- Construct the head with explicit level configuration so min_level/max_level are set at init
- Read num_boxes only after building/compiling/running the full model
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at official/projects/yolo/modeling/heads/yolo_head.py:131 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24).
Data as JSON: /api/errors/359e1bc0359ed7ec.
Report an issue: GitHub.
Appendix: source
Thrown at official/projects/yolo/modeling/heads/yolo_head.py:131
scale = 2**int(key)
self._head[key] = nn_blocks.ConvBN(
bias_initializer=self.bias_init(scale, input_shape[key][-1]),
**self._conv_config)
def call(self, inputs):
outputs = dict()
for key in self._key_list:
outputs[key] = self._head[key](inputs[key])
return outputs
@property
def output_depth(self):
return (self._classes + self._output_extras + 5) * self._boxes_per_level
@property
def num_boxes(self):
if self._min_level is None or self._max_level is None:
raise Exception(
'Model has to be built before number of boxes can be determined.')
return (self._max_level - self._min_level + 1) * self._boxes_per_level
@property
def num_heads(self):
return self._max_level - self._min_level + 1
def get_config(self):
config = dict(
min_level=self._min_level,
max_level=self._max_level,
classes=self._classes,
boxes_per_level=self._boxes_per_level,
output_extras=self._output_extras,
**self._base_config)
return config
@classmethodView on GitHub (pinned to e006f5f0d5)