Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to make a model less context-dependent? #1016

Open
1 task done
matteo-ferraro opened this issue Feb 10, 2025 · 2 comments
Open
1 task done

how to make a model less context-dependent? #1016

matteo-ferraro opened this issue Feb 10, 2025 · 2 comments
Labels
HUB Ultralytics HUB issues question Further information is requested

Comments

@matteo-ferraro
Copy link

Search before asking

Question

I have a really easy task: categorize beans into bad and good.

I have a training set of photos with either all good or all bad beans, where bad beans are darker than good ones ( link to two example pics ). I tried to train a model on this photos and, unsurprisingly, when I try to detect a bad bean among good beans, all good beans close to the bad bean become "bad" (here it is an example of a bad bean making every bean be detected as "bad"). How can I deal with it? Is there any custom setting in ultralytics hub I can set to make the model look at small (i.e. just the bounding box of a bean) area?

Additional

No response

@matteo-ferraro matteo-ferraro added the question Further information is requested label Feb 10, 2025
@UltralyticsAssistant UltralyticsAssistant added the HUB Ultralytics HUB issues label Feb 10, 2025
@UltralyticsAssistant
Copy link
Member

👋 Hello @matteo-ferraro, thank you for raising your issue about Ultralytics HUB 🚀! Please visit our HUB Docs for detailed guidance on using Ultralytics HUB:

It seems you’re tackling an interesting problem with your dataset 🌟! If this is a ❓ question, please ensure you’ve added all relevant details regarding your dataset, training parameters, and model settings to help us provide specific assistance. Additionally, consider these ideas:

  • Ensure your bounding boxes tightly enclose individual beans to improve the model's context-awareness.
  • You may want to explore training your model with augmented datasets that mix both good and bad beans in the same image.
  • Fine-tuning anchor sizes and hyperparameters available in Ultralytics HUB could also help the model focus on smaller regions of interest (like individual beans).

If you believe this is a potential 🐛 bug or issue, providing a minimum reproducible example (MRE) with the specific settings and steps you followed would help us investigate further. Screenshots or detailed reproductions of your issue (as you’ve partially shared above) are also very welcome! 📸

An Ultralytics engineer will review and assist shortly. Thank you for your patience and contributions to the Ultralytics community! 😊

@pderrenger
Copy link
Member

@matteo-ferraro thank you for sharing your bean classification challenge! 🌱 This appears to be a case of context overfitting, where the model is learning global image patterns rather than individual bean characteristics. Here's how to address this using Ultralytics HUB and best practices:

1. Dataset Enhancement

  • Mix Good/Bad Beans: Create training images containing both good and bad beans (like your inference scenario) to force the model to examine individual beans. This breaks the "all good/all bad" pattern in your current training set.
  • Augmentation Strategies: Use HUB's built-in augmentations like:
    augment: 
      - hsv_h: 0.2  # hue variation
      - hsv_s: 0.7  # saturation variation
      - translate: 0.2  # positional variation
    This helps the model focus on bean color/texture rather than positional context.

2. Model Adjustments

  • Confidence Threshold: Increase conf during inference to reduce false positives:
    model.predict(..., conf=0.7)  # Default is 0.25
  • IoU Threshold: Adjust iou to handle bean overlaps:
    model.predict(..., iou=0.45)  # Default is 0.7

3. Color-Based Post-Processing

Since bad beans are darker, add a simple HSV filter after detection:

for result in results:
    for box in result.boxes:
        x1, y1, x2, y2 = box.xyxy[0]
        bean_patch = image[int(y1):int(y2), int(x1):int(x2)]
        hsv = cv2.cvtColor(bean_patch, cv2.COLOR_RGB2HSV)
        if np.mean(hsv[:,:,2]) > 100:  # Adjust brightness threshold
            box.conf *= 0.5  # Demote "bad" classification confidence

4. Architecture Considerations

For better localization of small objects:

  • Use YOLOv8n (nano) or YOLOv8s (small) which have finer feature grids
  • Ensure your input image size matches the bean resolution (test with 640px vs 1280px)

5. Evaluation

Use HUB's Confusion Matrix to analyze false positives. If class imbalance exists (more good than bad), try:

  • Class Weights: model.train(..., cls=1.5) to upweight bad beans
  • Stratified Sampling: Ensure balanced representation in each batch

Would you like me to elaborate on any of these strategies? For immediate testing, I recommend starting with dataset mixing and confidence threshold adjustments, as these can be implemented directly in HUB without code changes. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HUB Ultralytics HUB issues question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants