Skip to content

Commit 55ef5f8

Browse files
committed
v2.0.0: Motion-informed inference, validation module, configurable backbones
BIG UPDATES: - New prepare weights (gbif-generic) with higher accuracy - Motion-informed inference: GMM background subtraction replaces YOLO for detection NEW FEATURES: - Validation module (bplusplus.validate) for model evaluation - Configurable ResNet backbone (resnet18, resnet50, resnet101) - Custom training transforms parameter - Validation split control (valid_fraction 0-1) - Detection confidence parameter for prepare - Class imbalance analysis and warnings during training - Detection config file support (YAML/JSON) IMPROVEMENTS: - Collect robustness: retry logic, progress tracking, graceful interruption handling - Enhanced GBIF quality filters REMOVED: - YOLO dependency for inference - Outdated test.py module
1 parent f0c14aa commit 55ef5f8

File tree

13 files changed

+2955
-1638
lines changed

13 files changed

+2955
-1638
lines changed

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,32 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.0.0] - 2025-01-06
8+
9+
### Added
10+
- **Motion-informed inference**: New detection pipeline using Gaussian Mixture Model (GMM) background subtraction instead of YOLO, with path topology analysis for confirming insect-like movement
11+
- **Validation module** (`validation.py`): New `bplusplus.validate()` function to evaluate model performance with precision, recall, and F1-score at all taxonomic levels
12+
- **Configurable ResNet backbone**: Choose between `resnet18`, `resnet50`, or `resnet101` for training and inference
13+
- **Custom training transforms**: New `train_transforms` parameter for custom data augmentation in `train()`
14+
- **Validation split control**: New `valid_fraction` parameter (0-1) in `prepare()` to control train/validation split ratio
15+
- **Detection confidence parameter**: New `conf` parameter in `prepare()` for YOLO confidence threshold
16+
- **Class imbalance warnings**: Training now analyzes and warns about class imbalance across taxonomic levels
17+
- **Detection configuration**: Support for YAML/JSON config files to customize motion detection parameters
18+
- **New prepare weights**: Higher accuracy `gbif-generic` weights for data preparation
19+
20+
### Improved
21+
- **Collect robustness**: Added retry logic with exponential backoff for GBIF API calls, progress tracking, and graceful handling of interruptions
22+
- **GBIF quality filters**: Enhanced filtering options including `occurrenceStatus`, `year` range, and more
23+
24+
### Changed
25+
- **Inference pipeline**: Replaced YOLO-based detection with motion-based detection using GMM
26+
- **Output structure**: Inference now outputs to a single directory with multiple files (`_annotated.mp4`, `_debug.mp4`, `_results.csv`, `_detections.csv`)
27+
- **Results CSV**: Now contains only aggregated results for confirmed tracks
28+
29+
### Removed
30+
- **YOLO dependency for inference**: No longer requires YOLO weights for video inference
31+
- **test.py**: Removed outdated YOLO test module
32+
733
## [1.2.2] - 2024-12-19
834

935
### Added

README.md

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ Using the `Bplusplus` library, this pipeline automates the entire machine learni
2020
- **Video Inference & Tracking**: Processes video files to detect, classify, and track individual insects over time, providing aggregated predictions.
2121
## Pipeline Overview
2222

23-
The process is broken down into six main steps, all detailed in the `full_pipeline.ipynb` notebook:
23+
The process is broken down into five main steps, all detailed in the `full_pipeline.ipynb` notebook:
2424

2525
1. **Collect Data**: Select your target species and fetch raw insect images from the web.
2626
2. **Prepare Data**: Filter, clean, and prepare images for training.
2727
3. **Train Model**: Train the hierarchical classification model.
28-
4. **Download Weights**: Fetch pre-trained weights for the detection model.
29-
5. **Test Model**: Evaluate the performance of the trained model.
30-
6. **Run Inference**: Run the full pipeline on a video file for real-world application.
28+
4. **Validate Model**: Evaluate the performance of the trained model.
29+
5. **Run Inference**: Run the full pipeline on a video file for real-world application.
3130

3231
## How to Use
3332

@@ -85,7 +84,9 @@ PREPARED_DATA_DIR = Path("./prepared_data")
8584
bplusplus.prepare(
8685
input_directory=GBIF_DATA_DIR,
8786
output_directory=PREPARED_DATA_DIR,
88-
img_size=640 # Target image size for training
87+
img_size=640, # Target image size for training
88+
conf=0.6, # Detection confidence threshold (0-1)
89+
valid_fraction=0.1, # Validation split ratio (0-1), set to 0 for no validation
8990
)
9091
```
9192
@@ -102,41 +103,70 @@ bplusplus.train(
102103
img_size=640,
103104
data_dir=PREPARED_DATA_DIR,
104105
output_dir=TRAINED_MODEL_DIR,
105-
species_list=names
106-
# num_workers=0 # Optional: force single-process loading (most stable)
106+
species_list=names,
107+
backbone="resnet50", # Choose: "resnet18", "resnet50", or "resnet101"
108+
# num_workers=0, # Optional: force single-process loading (most stable)
109+
# train_transforms=custom_transforms, # Optional: custom torchvision transforms
107110
)
108111
```
109112
110-
**Note:** The `num_workers` parameter controls DataLoader multiprocessing (defaults to 0 for stability). You can increase it for potentially faster data loading.
113+
**Note:** The `num_workers` parameter controls DataLoader multiprocessing (defaults to 0 for stability). The `backbone` parameter allows you to choose between different ResNet architectures—use `resnet18` for faster training or `resnet101` for potentially better accuracy.
111114
112-
#### Step 4: Download Detection Weights
113-
The inference pipeline uses a separate, pre-trained YOLO model for initial insect detection. You need to download its weights manually.
115+
#### Step 4: Validate Model
116+
Evaluate the trained model on a held-out validation set. This calculates precision, recall, and F1-score at all taxonomic levels.
114117
115-
You can download the weights file from [this link](https://github.com/Tvenver/Bplusplus/releases/download/v1.2.3/v11small-generic.pt).
118+
```python
119+
HIERARCHICAL_MODEL_PATH = TRAINED_MODEL_DIR / "best_multitask.pt"
116120
117-
Place it in the `trained_model` directory and ensure it is named `yolo_weights.pt`.
121+
results = bplusplus.validate(
122+
species_list=names,
123+
validation_dir=PREPARED_DATA_DIR / "valid",
124+
hierarchical_weights=HIERARCHICAL_MODEL_PATH,
125+
img_size=640, # Must match training
126+
batch_size=32,
127+
backbone="resnet50", # Must match training
128+
)
129+
```
118130
119131
#### Step 5: Run Inference on Video
120-
Process a video file to detect, classify, and track insects. The final output is an annotated video and a CSV file with aggregated results for each tracked insect.
132+
Process a video file to detect, classify, and track insects using motion-based detection. The pipeline uses background subtraction (GMM) to detect moving insects, tracks them across frames, and classifies confirmed tracks.
133+
134+
**Output files generated in `output_dir`:**
135+
- `{video}_annotated.mp4` - Video showing confirmed tracks with classifications
136+
- `{video}_debug.mp4` - Debug video with motion mask and all detections
137+
- `{video}_results.csv` - Aggregated results per confirmed track
138+
- `{video}_detections.csv` - Frame-by-frame detection data
121139
122140
```python
123141
VIDEO_INPUT_PATH = Path("my_video.mp4")
124-
VIDEO_OUTPUT_PATH = Path("my_video_annotated.mp4")
142+
OUTPUT_DIR = Path("./output")
125143
HIERARCHICAL_MODEL_PATH = TRAINED_MODEL_DIR / "best_multitask.pt"
126-
YOLO_WEIGHTS_PATH = TRAINED_MODEL_DIR / "yolo_weights.pt"
127144
128-
bplusplus.inference(
145+
results = bplusplus.inference(
129146
species_list=names,
130-
yolo_model_path=YOLO_WEIGHTS_PATH,
131147
hierarchical_model_path=HIERARCHICAL_MODEL_PATH,
132-
confidence_threshold=0.35,
133148
video_path=VIDEO_INPUT_PATH,
134-
output_path=VIDEO_OUTPUT_PATH,
135-
tracker_max_frames=60,
136-
fps=15 # Optional: set processing FPS
149+
output_dir=OUTPUT_DIR,
150+
fps=None, # None = process all frames
151+
backbone="resnet50", # Must match training
137152
)
153+
154+
print(f"Detected {results['tracks']} tracks ({results['confirmed_tracks']} confirmed)")
138155
```
139156
157+
**Custom Detection Configuration:**
158+
159+
For advanced control over detection parameters, provide a YAML config file:
160+
161+
```python
162+
results = bplusplus.inference(
163+
...,
164+
config="detection_config.yaml"
165+
)
166+
```
167+
168+
Download a template config from the [releases page](https://github.com/Tvenver/Bplusplus/releases). Parameters control cohesiveness filtering, shape filtering, tracking behavior, and path topology analysis for confirming insect-like movement.
169+
140170
### Customization
141171
142172
To train the model on your own set of insect species, you only need to change the `names` list in **Step 1**. The pipeline will automatically handle the rest.
@@ -168,8 +198,9 @@ names_with_unknown = [
168198
The pipeline will create the following directories to store artifacts:
169199

170200
- `GBIF_data/`: Stores the raw images downloaded from GBIF.
171-
- `prepared_data/`: Contains the cleaned, cropped, and resized images ready for training.
172-
- `trained_model/`: Saves the trained model weights (`best_multitask.pt`) and pre-trained detection weights.
201+
- `prepared_data/`: Contains the cleaned, cropped, and resized images ready for training (`train/` and optionally `valid/` subdirectories).
202+
- `trained_model/`: Saves the trained model weights (`best_multitask.pt`).
203+
- `output/`: Inference results including annotated videos and CSV files.
173204

174205
# Citation
175206

detection_config.yaml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# =============================================================================
2+
# B++ DETECTION CONFIGURATION
3+
# =============================================================================
4+
# This config controls motion-based insect detection parameters.
5+
# All values shown are defaults - modify as needed for your use case.
6+
#
7+
# Usage:
8+
# results = inference(..., config="detection_config.yaml")
9+
# =============================================================================
10+
11+
# -----------------------------------------------------------------------------
12+
# COHESIVENESS PARAMETERS
13+
# -----------------------------------------------------------------------------
14+
# These filter scattered motion (wind-blown plants) vs compact motion (insects)
15+
16+
# Min ratio of largest blob to total motion area in detection region
17+
# Higher = stricter, rejects fragmented detections
18+
min_largest_blob_ratio: 0.80
19+
20+
# Max number of separate blobs allowed in a detection region
21+
# Lower = stricter, rejects scattered motion
22+
max_num_blobs: 5
23+
24+
# -----------------------------------------------------------------------------
25+
# SHAPE PARAMETERS
26+
# -----------------------------------------------------------------------------
27+
# Filter detections by contour geometry
28+
29+
# Min contour area in pixels² (rejects noise/tiny detections)
30+
min_area: 200
31+
32+
# Max contour area in pixels² (rejects large moving objects)
33+
max_area: 40000
34+
35+
# Min area/perimeter ratio (rejects thin/elongated shapes)
36+
min_density: 3.0
37+
38+
# Min convex hull fill ratio (rejects irregular/fragmented shapes)
39+
# 1.0 = perfectly convex, lower = more irregular allowed
40+
min_solidity: 0.55
41+
42+
# -----------------------------------------------------------------------------
43+
# TRACKING PARAMETERS
44+
# -----------------------------------------------------------------------------
45+
# Control how detections are linked into tracks
46+
47+
# Min NET displacement in pixels for track confirmation
48+
# Filters stationary false positives
49+
min_displacement: 50
50+
51+
# Min path points required before topology analysis
52+
min_path_points: 10
53+
54+
# Max allowed jump between consecutive frames (pixels)
55+
# Larger = more tolerant of fast movement, but may link separate objects
56+
max_frame_jump: 100
57+
58+
# How long to remember a lost track (seconds)
59+
# Helps re-link tracks after brief occlusions
60+
lost_track_seconds: 1.5
61+
62+
# -----------------------------------------------------------------------------
63+
# PATH TOPOLOGY PARAMETERS
64+
# -----------------------------------------------------------------------------
65+
# Confirm insect-like movement patterns vs random/stationary motion
66+
67+
# Max ratio of revisited positions (insects explore new areas)
68+
# Lower = stricter, requires more exploration
69+
max_revisit_ratio: 0.30
70+
71+
# Min forward progression ratio (insects move purposefully)
72+
# Higher = stricter, requires more directional movement
73+
min_progression_ratio: 0.70
74+
75+
# Max directional variance (insects maintain relatively consistent heading)
76+
# Lower = stricter, requires more consistent direction
77+
max_directional_variance: 0.90

0 commit comments

Comments
 (0)