PaddlePaddle/PaddleOCR · error · Exception
[DBLoss]: Unrecognized main loss type!
Error message
[DBLoss]: Unrecognized main loss type!
What it means
DBLoss.__init__ branches on main_loss_type and only wires up 'BCELoss' (bce_loss path with BalanceLoss) and 'DiceLoss' (BalanceLoss + DiceLoss). Even though BalanceLoss itself supports five types, DBLoss as a whole only accepts these two, and anything else raises Exception('[DBLoss]: Unrecognized main loss type!').
Source
Thrown at ppocr/losses/det_db_loss.py:74
self.l1_loss = MaskL1Loss(eps=eps)
if main_loss_type == "DiceFocalLoss":
self.bce_loss = DiceFocalLoss(
dice_weight=dice_weight,
focal_weight=focal_weight,
focal_alpha=focal_alpha,
focal_gamma=focal_gamma,
eps=eps,
)
self.dice_loss = self.bce_loss
elif main_loss_type == "DiceLoss":
self.bce_loss = BalanceLoss(
balance_loss=balance_loss,
main_loss_type=main_loss_type,
negative_ratio=ohem_ratio,
)
self.dice_loss = DiceLoss(eps=eps)
else:
raise Exception("[DBLoss]: Unrecognized main loss type!")
self.aux_weight_p4 = aux_weight_p4
self.aux_weight_p3 = aux_weight_p3
self.aux_weight_p2 = aux_weight_p2
def forward(self, predicts, labels):
predict_maps = predicts["maps"]
(
label_threshold_map,
label_threshold_mask,
label_shrink_map,
label_shrink_mask,
) = labels[1:]
shrink_maps = predict_maps[:, 0, :, :]
threshold_maps = predict_maps[:, 1, :, :]
binary_maps = predict_maps[:, 2, :, :]
loss_shrink_maps = self.bce_loss(
shrink_maps, label_shrink_map, label_shrink_maskView on GitHub (pinned to 2661c7c0ef)
Solutions
- Set main_loss_type to 'BCELoss' or 'DiceLoss' in the DBLoss config
- Verify you are editing the Loss section of the actual det model config being loaded (not a base config that is overridden)
Example fix
# before (config yaml) Loss: name: DBLoss main_loss_type: MaskL1Loss # after Loss: name: DBLoss main_loss_type: DiceLoss
Defensive patterns
Strategy: validation
Validate before calling
DB_LOSS_TYPES = {'BCELoss', 'DiceLoss'}
assert main_loss_type in DB_LOSS_TYPES, f'DBLoss supports only {DB_LOSS_TYPES}, got {main_loss_type!r}' Type guard
def is_db_loss_type(t: str) -> bool:
return t in {'BCELoss', 'DiceLoss'} Prevention
- Note the narrower set vs BalanceLoss: DBLoss accepts only BCELoss/DiceLoss
- Assert the loss config the moment it is parsed so the failure points at the YAML, not at model build
- Run a 1-iteration smoke train on config changes
When it happens
Trigger: Constructing DBLoss(main_loss_type='MaskL1Loss') or 'CrossEntropy' — values legal for BalanceLoss but not for DBLoss; typically via a det_db config YAML.
Common situations: Copying a BalanceLoss-supported type into a DB config, or upgrading configs from another detection model where those types were valid, then hitting a confusing error because the inner class accepts them.
Related errors
- main_loss_type in BalanceLoss() can only be one of {}
- The mode.lower() if KLJSLoss should be one of ['kl', 'js']
- {} is not supported in MultiLoss yet
- error!!!!!!
- mode[{model_name}_model] is not implemented!
AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14).
Data as JSON: /api/errors/ec178980cf19e6f4.
Report an issue: GitHub.