Skip to content

Conversation

@lixiaolei1982
Copy link

  • Replace direct unpacking with flexible handling
  • Support both single model and tuple return patterns
  • Fix ValueError when building full model
    #Description:
    The current build_model() function has inconsistent return behavior depending on configuration flags:
    When args.encoder_only=True or args.backbone_only=True: returns a tuple (component, None, None)
    When building full model: returns a single model object

This causes issues in the main function where the code expects three return values but sometimes receives only one. The error occurs when trying to unpack: model, criterion, postprocessors = build_model(args).

#Changes Made:

1.Modified the model building section in main() to handle both return patterns
2.Replaced the direct unpacking with flexible handling that works for both single values and tuples
3.Added logic to extract the model from tuple when needed, or use the single value directly

#Code Changes:

python

Before (fails when build_model returns single value):

model, criterion, postprocessors = build_model(args)

After (handles both patterns):

result = build_model(args)
model = result[0] if isinstance(result, tuple) else result

#Benefits:

✅ Fixes ValueError: not enough values to unpack when building full model

✅ Maintains backward compatibility with existing configurations

✅ Supports both encoder_only/backbone_only modes and full model building

✅ No changes needed to the build_model() function itself

✅ Clear and concise error handling

#Impact:

The code now works correctly for all configuration modes without runtime errors
Future changes to build_model() return patterns will be handled gracefully
Model export functionality (ONNX/TensorRT) continues to work as expected

- Replace direct unpacking with flexible handling
- Support both single model and tuple return patterns
- Fix ValueError when building full model
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a ValueError that occurs when build_model() returns a single model object in full model mode, by replacing direct tuple unpacking with flexible handling that works for both single values and tuples.

Changes:

  • Modified model initialization to handle both single return values (full model) and tuple return values (encoder_only/backbone_only modes)
  • Replaced direct unpacking model, criterion, postprocessors = build_model(args) with conditional extraction based on return type
Comments suppressed due to low confidence (1)

rfdetr/deploy/export.py:240

  • While this fix addresses the full model case, there's still an issue with encoder_only and backbone_only modes. When these modes are used, lines 233-240 attempt to access attributes (model.backbone, model.transformer) that don't exist on encoder or backbone objects, which will cause AttributeError. Consider adding conditional checks (e.g., hasattr or checking args.encoder_only/args.backbone_only) before accessing these attributes to prevent runtime errors in these modes.
    n_backbone_parameters = sum(p.numel() for p in model.backbone.parameters())
    print(f"number of backbone parameters: {n_backbone_parameters}")
    n_projector_parameters = sum(p.numel() for p in model.backbone[0].projector.parameters())
    print(f"number of projector parameters: {n_projector_parameters}")
    n_backbone_encoder_parameters = sum(p.numel() for p in model.backbone[0].encoder.parameters())
    print(f"number of backbone encoder parameters: {n_backbone_encoder_parameters}")
    n_transformer_parameters = sum(p.numel() for p in model.transformer.parameters())
    print(f"number of transformer parameters: {n_transformer_parameters}")

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Borda Borda added the bug Something isn't working label Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants