Limiting bounding box sizes during training

Hey, doing an exp in which I have many ring like particles, I am vibrating them then studying their clusters. using Yolo to detect particles but since my particle size are almost same I want to limit bounding box size to a maximum while training. how to?

Great question! It sounds like an interesting experiment you’re working on! To limit the bounding box size during training, you can restrict the aspect ratio and size of the bounding boxes by modifying your dataset annotations or applying constraints in your training configuration. Here’s how you can go about it:

1. Filter Annotations by Size

If you’re using a custom dataset, you can preprocess your annotations to exclude bounding boxes that exceed your desired maximum dimensions. For example, you can write a script to filter out bounding boxes larger than your defined threshold.

Here’s a Python snippet to filter bounding boxes in a COCO-style dataset:

import json

max_width, max_height = 50, 50  # Define your maximum bounding box dimensions

with open('dataset/annotations.json') as f:
    data = json.load(f)

filtered_annotations = []
for ann in data['annotations']:
    width, height = ann['bbox'][2], ann['bbox'][3]
    if width <= max_width and height <= max_height:
        filtered_annotations.append(ann)

data['annotations'] = filtered_annotations

with open('dataset/filtered_annotations.json', 'w') as f:
    json.dump(data, f)

Replace the original annotation file with the new filtered one for training.


2. Set Constraints During Training

YOLO models don’t have an explicit bounding box size restriction during training, but you can influence this by adjusting the anchor sizes in the model configuration. Anchors determine the initial bounding box proposals, so setting smaller anchors will prioritize detecting smaller objects.

To modify anchors:

  • Use the autoanchor option during training to automatically generate anchors based on your dataset using:
    yolo train data=dataset.yaml model=yolov8n.pt autoanchor=True
    
  • Alternatively, manually define smaller anchors in your model configuration file.

3. Post-Processing Filters

If you’re concerned about large bounding boxes during inference, you can filter detections using the model.predict arguments:

  • Use the classes argument to focus on specific object classes.
  • Apply post-processing to discard detections with bounding boxes larger than your defined size.

Example:

predictions = model.predict(source='images/', conf=0.25)
max_width, max_height = 50, 50

filtered_results = []
for result in predictions:
    for box in result.boxes:
        if box.width <= max_width and box.height <= max_height:
            filtered_results.append(box)

Resources

Let me know if you have further questions or need help with implementation. Good luck with your particle cluster analysis! :rocket: