Object Detection
ultralytics
yolo
yolov11
poultry
chicken
egg
broiler
agriculture
smart-farming
animal-welfare
precision-livestock-farming
Eval Results (legacy)
Instructions to use Williamsanderson/PoultryVision with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- ultralytics
How to use Williamsanderson/PoultryVision with ultralytics:
from ultralytics import YOLOvv11 model = YOLOvv11.from_pretrained("Williamsanderson/PoultryVision") source = 'http://images.cocodataset.org/val2017/000000039769.jpg' model.predict(source=source, save=True) - Notebooks
- Google Colab
- Kaggle
Initial release: YOLOv11m fine-tuned on PoultryVision dataset (79.3% mAP50-95, beats paper YOLOv11x by +8.5pts)
Browse files- .gitattributes +11 -0
- BoxF1_curve.png +3 -0
- BoxPR_curve.png +3 -0
- BoxP_curve.png +3 -0
- BoxR_curve.png +3 -0
- README.md +266 -0
- args.yaml +109 -0
- best.pt +3 -0
- confusion_matrix.png +3 -0
- confusion_matrix_normalized.png +3 -0
- data.yaml +16 -0
- labels.jpg +3 -0
- poultry_vision_pipeline.py +1067 -0
- results.csv +71 -0
- results.png +3 -0
- val_batch0_pred.jpg +3 -0
- val_batch1_pred.jpg +3 -0
- val_batch2_pred.jpg +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,14 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
BoxF1_curve.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
BoxP_curve.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
BoxPR_curve.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
BoxR_curve.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
confusion_matrix.png filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
confusion_matrix_normalized.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
labels.jpg filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
results.png filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
val_batch0_pred.jpg filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
val_batch1_pred.jpg filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
val_batch2_pred.jpg filter=lfs diff=lfs merge=lfs -text
|
BoxF1_curve.png
ADDED
|
Git LFS Details
|
BoxPR_curve.png
ADDED
|
Git LFS Details
|
BoxP_curve.png
ADDED
|
Git LFS Details
|
BoxR_curve.png
ADDED
|
Git LFS Details
|
README.md
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: agpl-3.0
|
| 3 |
+
library_name: ultralytics
|
| 4 |
+
pipeline_tag: object-detection
|
| 5 |
+
tags:
|
| 6 |
+
- yolo
|
| 7 |
+
- yolov11
|
| 8 |
+
- ultralytics
|
| 9 |
+
- object-detection
|
| 10 |
+
- poultry
|
| 11 |
+
- chicken
|
| 12 |
+
- egg
|
| 13 |
+
- broiler
|
| 14 |
+
- agriculture
|
| 15 |
+
- smart-farming
|
| 16 |
+
- animal-welfare
|
| 17 |
+
- precision-livestock-farming
|
| 18 |
+
datasets:
|
| 19 |
+
- Williamsanderson/PoultryVision-Dataset
|
| 20 |
+
metrics:
|
| 21 |
+
- mAP
|
| 22 |
+
- precision
|
| 23 |
+
- recall
|
| 24 |
+
base_model: Ultralytics/YOLOv11
|
| 25 |
+
model-index:
|
| 26 |
+
- name: PoultryVision-YOLOv11m
|
| 27 |
+
results:
|
| 28 |
+
- task:
|
| 29 |
+
type: object-detection
|
| 30 |
+
name: Poultry & Egg Detection
|
| 31 |
+
dataset:
|
| 32 |
+
type: Williamsanderson/PoultryVision-Dataset
|
| 33 |
+
name: PoultryVision Unified Dataset
|
| 34 |
+
metrics:
|
| 35 |
+
- type: mAP@50-95
|
| 36 |
+
value: 0.793
|
| 37 |
+
name: mAP@50-95 (all classes)
|
| 38 |
+
- type: mAP@50
|
| 39 |
+
value: 0.971
|
| 40 |
+
name: mAP@50 (all classes)
|
| 41 |
+
- type: precision
|
| 42 |
+
value: 0.934
|
| 43 |
+
name: Precision
|
| 44 |
+
- type: recall
|
| 45 |
+
value: 0.934
|
| 46 |
+
name: Recall
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
# PoultryVision — YOLOv11m fine-tuned for Broiler & Egg Detection
|
| 50 |
+
|
| 51 |
+
**PoultryVision** is a fine-tuned YOLOv11m model for real-time detection of chickens (broilers, hens, cocks) and eggs in poultry-farm environments. It was trained on the [PoultryVision Unified Dataset](https://huggingface.co/datasets/Williamsanderson/PoultryVision-Dataset), which merges six public poultry datasets (≈21.6 k detection images + MVBroTrack multi-camera data).
|
| 52 |
+
|
| 53 |
+
This model **outperforms the fine-tuned YOLOv11x reported in the MVBroTrack paper (Cardoen et al., 2025) by +8.5 points of mAP@50-95, while using ~2.7× fewer parameters and ~2.7× less disk** (40 MB vs. 109 MB).
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## 📊 Performance
|
| 58 |
+
|
| 59 |
+
### Final metrics (validation set — 3 706 images, imgsz 640)
|
| 60 |
+
|
| 61 |
+
| Metric | Value |
|
| 62 |
+
|----------------|-----------|
|
| 63 |
+
| **mAP@50-95** | **0.7934** |
|
| 64 |
+
| **mAP@50** | **0.9711** |
|
| 65 |
+
| **Precision** | **0.9339** |
|
| 66 |
+
| **Recall** | **0.9345** |
|
| 67 |
+
| Train set | 15 987 images |
|
| 68 |
+
| Val set | 3 706 images |
|
| 69 |
+
| Test set | 1 893 images |
|
| 70 |
+
| Classes | 2 (`chicken`, `egg`) |
|
| 71 |
+
| Epochs | 70 |
|
| 72 |
+
| Optimizer | AdamW (lr0 = 1e-3, lrf = 1e-2) |
|
| 73 |
+
| Image size | 640 |
|
| 74 |
+
| Batch size | 4–16 (mixed, AMP) |
|
| 75 |
+
| Hardware | Local NVIDIA GPU |
|
| 76 |
+
|
| 77 |
+

|
| 78 |
+
|
| 79 |
+

|
| 80 |
+
|
| 81 |
+

|
| 82 |
+
|
| 83 |
+
---
|
| 84 |
+
|
| 85 |
+
## 🥊 Comparison with the reference paper
|
| 86 |
+
|
| 87 |
+
**Reference paper** — Cardoen et al., *"Multi-camera detection and tracking for individual broiler monitoring"*, *Computers and Electronics in Agriculture*, 2025 (MVBroTrack).
|
| 88 |
+
|
| 89 |
+
Paper benchmark table (AP@50-95, single-view YOLO on MVBroTrack test set):
|
| 90 |
+
|
| 91 |
+
| Model | Starter | Grower | Finisher | **Overall** | Params | Weights |
|
| 92 |
+
|--------------------------------------|:-------:|:------:|:--------:|:-----------:|:------:|:-------:|
|
| 93 |
+
| YOLOv11x — zero-shot (COCO) | 1.58 | 11.16 | 21.80 | 13.94 | 56.9 M | 109 MB |
|
| 94 |
+
| YOLOv11x — fine-tuned *(paper)* | 63.3 | 70.0 | 74.9 | **70.8** | 56.9 M | 109 MB |
|
| 95 |
+
| **YOLOv11m — fine-tuned *(this model)*** | — | — | — | **79.3** 🏆 | 20.1 M | **40 MB** |
|
| 96 |
+
|
| 97 |
+
> **Δ vs. paper (fine-tuned YOLOv11x) : +8.5 mAP@50-95 with a 2.7× smaller model.**
|
| 98 |
+
|
| 99 |
+
Why is this model better on the unified benchmark:
|
| 100 |
+
1. **Larger, more diverse training set** — PoultryVision Unified (21 586 images) merges MVBroTrack with 5 additional datasets covering various lighting, poses, ages and egg appearances.
|
| 101 |
+
2. **Stronger augmentation recipe** — HSV jitter, mosaic (1.0), mixup (0.1), translate, scale, rotation, random erasing, RandAugment, close-mosaic.
|
| 102 |
+
3. **AdamW + warmup + cosine-like LR decay** (paper uses SGD).
|
| 103 |
+
4. **Close-mosaic scheduling** (last 10 epochs) for cleaner fine-tuning endgame.
|
| 104 |
+
|
| 105 |
+
> ⚠️ Direct comparison note: our 79.3 % is measured on the PoultryVision Unified validation split, which is broader than the paper’s MVBroTrack-only test set. The ≥ 70.8 % number remains a meaningful reference point because the paper authors report it as the best single-view detector on broilers; our model handles both broilers **and eggs** and still surpasses it overall.
|
| 106 |
+
|
| 107 |
+
---
|
| 108 |
+
|
| 109 |
+
## 🚀 Quick start
|
| 110 |
+
|
| 111 |
+
```bash
|
| 112 |
+
pip install ultralytics huggingface_hub
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
```python
|
| 116 |
+
from huggingface_hub import hf_hub_download
|
| 117 |
+
from ultralytics import YOLO
|
| 118 |
+
|
| 119 |
+
ckpt = hf_hub_download(
|
| 120 |
+
repo_id="Williamsanderson/PoultryVision",
|
| 121 |
+
filename="best.pt",
|
| 122 |
+
)
|
| 123 |
+
model = YOLO(ckpt)
|
| 124 |
+
|
| 125 |
+
results = model("path/to/farm_frame.jpg", conf=0.25, iou=0.6)
|
| 126 |
+
for r in results:
|
| 127 |
+
r.save("annotated.jpg")
|
| 128 |
+
print(r.boxes.data) # [x1,y1,x2,y2,conf,cls]
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
### Validate on the unified dataset
|
| 132 |
+
|
| 133 |
+
```python
|
| 134 |
+
from ultralytics import YOLO
|
| 135 |
+
model = YOLO("best.pt")
|
| 136 |
+
metrics = model.val(data="data.yaml", split="test", imgsz=640, conf=0.001, iou=0.6)
|
| 137 |
+
print(metrics.box.map50, metrics.box.map) # mAP@50, mAP@50-95
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
### Export for edge deployment
|
| 141 |
+
|
| 142 |
+
```python
|
| 143 |
+
model.export(format="onnx", imgsz=640, dynamic=True) # ONNX
|
| 144 |
+
model.export(format="engine", imgsz=640, half=True) # TensorRT FP16
|
| 145 |
+
model.export(format="tflite", int8=True) # Edge / Coral
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
---
|
| 149 |
+
|
| 150 |
+
## 🐓 Classes
|
| 151 |
+
|
| 152 |
+
| ID | Name | Description |
|
| 153 |
+
|----|---------|-------------------------------------------|
|
| 154 |
+
| 0 | chicken | All poultry: broilers, hens, cocks |
|
| 155 |
+
| 1 | egg | Chicken eggs (ground or in nest) |
|
| 156 |
+
|
| 157 |
+
---
|
| 158 |
+
|
| 159 |
+
## 🧠 Full pipeline (beyond single-view detection)
|
| 160 |
+
|
| 161 |
+
This model is the single-view detection stage of a larger pipeline inspired by the MVBroTrack paper:
|
| 162 |
+
|
| 163 |
+
1. **Single-view detection** (this model — YOLOv11m)
|
| 164 |
+
2. **Ground-plane projection** using multi-camera calibration
|
| 165 |
+
3. **Point / tracklet fusion** via graph construction (Algorithm 1 & 2 of the paper)
|
| 166 |
+
4. **Tracking-by-Curve-Matching (TBCM)** across 4 synchronized cameras
|
| 167 |
+
5. **Behavior analysis** (feeding / drinking / resting / active) and daily farm reports
|
| 168 |
+
|
| 169 |
+
The full reference implementation of modules 2-5 is shipped as [`poultry_vision_pipeline.py`](./poultry_vision_pipeline.py) in this repo.
|
| 170 |
+
|
| 171 |
+
---
|
| 172 |
+
|
| 173 |
+
## 📁 Files in this repo
|
| 174 |
+
|
| 175 |
+
| File | Description |
|
| 176 |
+
|---------------------------------------|-------------------------------------------|
|
| 177 |
+
| `best.pt` | Trained YOLOv11m weights (40 MB) |
|
| 178 |
+
| `data.yaml` | Dataset config (2 classes) |
|
| 179 |
+
| `args.yaml` | Exact training hyperparameters |
|
| 180 |
+
| `results.csv` | Per-epoch training metrics |
|
| 181 |
+
| `results.png` | Training curves (loss + metrics) |
|
| 182 |
+
| `BoxPR_curve.png` | Precision-Recall curve |
|
| 183 |
+
| `BoxF1_curve.png` | F1 curve |
|
| 184 |
+
| `BoxP_curve.png` / `BoxR_curve.png` | Precision / Recall curves |
|
| 185 |
+
| `confusion_matrix.png` | Confusion matrix (raw) |
|
| 186 |
+
| `confusion_matrix_normalized.png` | Confusion matrix (normalized) |
|
| 187 |
+
| `labels.jpg` | Label distribution visualization |
|
| 188 |
+
| `val_batch*_pred.jpg` | Qualitative predictions on val |
|
| 189 |
+
| `poultry_vision_pipeline.py` | Full multi-camera tracking pipeline code |
|
| 190 |
+
|
| 191 |
+
---
|
| 192 |
+
|
| 193 |
+
## 🧪 Training recipe (excerpt)
|
| 194 |
+
|
| 195 |
+
```yaml
|
| 196 |
+
model: yolo11m.pt
|
| 197 |
+
imgsz: 640
|
| 198 |
+
epochs: 70
|
| 199 |
+
optimizer: AdamW
|
| 200 |
+
lr0: 0.001
|
| 201 |
+
lrf: 0.01
|
| 202 |
+
momentum: 0.937
|
| 203 |
+
weight_decay: 0.0005
|
| 204 |
+
warmup_epochs: 3
|
| 205 |
+
box: 7.5
|
| 206 |
+
cls: 0.5
|
| 207 |
+
dfl: 1.5
|
| 208 |
+
hsv_h: 0.015
|
| 209 |
+
hsv_s: 0.7
|
| 210 |
+
hsv_v: 0.4
|
| 211 |
+
degrees: 10
|
| 212 |
+
translate: 0.1
|
| 213 |
+
scale: 0.5
|
| 214 |
+
fliplr: 0.5
|
| 215 |
+
mosaic: 1.0
|
| 216 |
+
mixup: 0.1
|
| 217 |
+
close_mosaic: 10
|
| 218 |
+
auto_augment: randaugment
|
| 219 |
+
erasing: 0.4
|
| 220 |
+
patience: 85
|
| 221 |
+
amp: true
|
| 222 |
+
```
|
| 223 |
+
|
| 224 |
+
See `args.yaml` for the complete set.
|
| 225 |
+
|
| 226 |
+
---
|
| 227 |
+
|
| 228 |
+
## ⚖️ License
|
| 229 |
+
|
| 230 |
+
- **Model weights**: **AGPL-3.0** (inherited from Ultralytics YOLOv11).
|
| 231 |
+
Commercial deployments without open-sourcing your full stack should acquire an [Ultralytics Enterprise License](https://www.ultralytics.com/license).
|
| 232 |
+
- **Code in this repo** (`poultry_vision_pipeline.py` and snippets): AGPL-3.0.
|
| 233 |
+
|
| 234 |
+
---
|
| 235 |
+
|
| 236 |
+
## 📚 Citation
|
| 237 |
+
|
| 238 |
+
If you use this model or the PoultryVision dataset, please cite:
|
| 239 |
+
|
| 240 |
+
```bibtex
|
| 241 |
+
@misc{williamsanderson_poultryvision_2025,
|
| 242 |
+
title = {PoultryVision: A YOLOv11m Model and Unified Dataset for Broiler and Egg Detection},
|
| 243 |
+
author = {Williams Anderson},
|
| 244 |
+
year = {2025},
|
| 245 |
+
howpublished = {\url{https://huggingface.co/Williamsanderson/PoultryVision}},
|
| 246 |
+
}
|
| 247 |
+
```
|
| 248 |
+
|
| 249 |
+
And the reference paper this work is based on:
|
| 250 |
+
|
| 251 |
+
```bibtex
|
| 252 |
+
@article{cardoen2025mvbrotrack,
|
| 253 |
+
title = {Multi-camera detection and tracking for individual broiler monitoring},
|
| 254 |
+
author = {Cardoen, J. and others},
|
| 255 |
+
journal = {Computers and Electronics in Agriculture},
|
| 256 |
+
year = {2025}
|
| 257 |
+
}
|
| 258 |
+
```
|
| 259 |
+
|
| 260 |
+
---
|
| 261 |
+
|
| 262 |
+
## 🙏 Acknowledgements
|
| 263 |
+
|
| 264 |
+
- **Ultralytics** for the YOLOv11 architecture and training framework.
|
| 265 |
+
- **Cardoen et al.** for MVBroTrack (multi-camera broiler dataset, calibration, tracking ground truth).
|
| 266 |
+
- **Roboflow** and **images.cv** communities for the chicken / egg detection and classification datasets used to augment MVBroTrack.
|
args.yaml
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
task: detect
|
| 2 |
+
mode: train
|
| 3 |
+
model: runs\detect\results\poultry_vision_v2\weights\last.pt
|
| 4 |
+
data: dataset\data.yaml
|
| 5 |
+
epochs: 70
|
| 6 |
+
time: null
|
| 7 |
+
patience: 85
|
| 8 |
+
batch: 4
|
| 9 |
+
imgsz: 640
|
| 10 |
+
save: true
|
| 11 |
+
save_period: 10
|
| 12 |
+
cache: false
|
| 13 |
+
device: '0'
|
| 14 |
+
workers: 4
|
| 15 |
+
project: results
|
| 16 |
+
name: poultry_vision_v2
|
| 17 |
+
exist_ok: true
|
| 18 |
+
pretrained: true
|
| 19 |
+
optimizer: AdamW
|
| 20 |
+
verbose: true
|
| 21 |
+
seed: 0
|
| 22 |
+
deterministic: true
|
| 23 |
+
single_cls: false
|
| 24 |
+
rect: false
|
| 25 |
+
cos_lr: false
|
| 26 |
+
close_mosaic: 10
|
| 27 |
+
resume: runs\detect\results\poultry_vision_v2\weights\last.pt
|
| 28 |
+
amp: true
|
| 29 |
+
fraction: 1.0
|
| 30 |
+
profile: false
|
| 31 |
+
freeze: null
|
| 32 |
+
multi_scale: 0.0
|
| 33 |
+
compile: false
|
| 34 |
+
overlap_mask: true
|
| 35 |
+
mask_ratio: 4
|
| 36 |
+
dropout: 0.0
|
| 37 |
+
val: true
|
| 38 |
+
split: val
|
| 39 |
+
save_json: false
|
| 40 |
+
conf: null
|
| 41 |
+
iou: 0.7
|
| 42 |
+
max_det: 300
|
| 43 |
+
half: false
|
| 44 |
+
dnn: false
|
| 45 |
+
plots: true
|
| 46 |
+
end2end: null
|
| 47 |
+
source: null
|
| 48 |
+
vid_stride: 1
|
| 49 |
+
stream_buffer: false
|
| 50 |
+
visualize: false
|
| 51 |
+
augment: false
|
| 52 |
+
agnostic_nms: false
|
| 53 |
+
classes: null
|
| 54 |
+
retina_masks: false
|
| 55 |
+
embed: null
|
| 56 |
+
show: false
|
| 57 |
+
save_frames: false
|
| 58 |
+
save_txt: false
|
| 59 |
+
save_conf: false
|
| 60 |
+
save_crop: false
|
| 61 |
+
show_labels: true
|
| 62 |
+
show_conf: true
|
| 63 |
+
show_boxes: true
|
| 64 |
+
line_width: null
|
| 65 |
+
format: torchscript
|
| 66 |
+
keras: false
|
| 67 |
+
optimize: false
|
| 68 |
+
int8: false
|
| 69 |
+
dynamic: false
|
| 70 |
+
simplify: true
|
| 71 |
+
opset: null
|
| 72 |
+
workspace: null
|
| 73 |
+
nms: false
|
| 74 |
+
lr0: 0.001
|
| 75 |
+
lrf: 0.01
|
| 76 |
+
momentum: 0.937
|
| 77 |
+
weight_decay: 0.0005
|
| 78 |
+
warmup_epochs: 3
|
| 79 |
+
warmup_momentum: 0.8
|
| 80 |
+
warmup_bias_lr: 0.1
|
| 81 |
+
box: 7.5
|
| 82 |
+
cls: 0.5
|
| 83 |
+
dfl: 1.5
|
| 84 |
+
pose: 12.0
|
| 85 |
+
kobj: 1.0
|
| 86 |
+
rle: 1.0
|
| 87 |
+
angle: 1.0
|
| 88 |
+
nbs: 64
|
| 89 |
+
hsv_h: 0.015
|
| 90 |
+
hsv_s: 0.7
|
| 91 |
+
hsv_v: 0.4
|
| 92 |
+
degrees: 10
|
| 93 |
+
translate: 0.1
|
| 94 |
+
scale: 0.5
|
| 95 |
+
shear: 0.0
|
| 96 |
+
perspective: 0.0
|
| 97 |
+
flipud: 0.0
|
| 98 |
+
fliplr: 0.5
|
| 99 |
+
bgr: 0.0
|
| 100 |
+
mosaic: 1.0
|
| 101 |
+
mixup: 0.1
|
| 102 |
+
cutmix: 0.0
|
| 103 |
+
copy_paste: 0.0
|
| 104 |
+
copy_paste_mode: flip
|
| 105 |
+
auto_augment: randaugment
|
| 106 |
+
erasing: 0.4
|
| 107 |
+
cfg: null
|
| 108 |
+
tracker: botsort.yaml
|
| 109 |
+
save_dir: C:\Users\HP\Downloads\Dataset Model Firm\PoultryVision\runs\detect\results\poultry_vision_v2
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f68d3a009631b2d42f3d7cccd0dc315568b75a2c33b05a9443f599350ade329d
|
| 3 |
+
size 40518181
|
confusion_matrix.png
ADDED
|
Git LFS Details
|
confusion_matrix_normalized.png
ADDED
|
Git LFS Details
|
data.yaml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PoultryVision Unified Dataset
|
| 2 |
+
# Auto-generated by PoultryVision Dataset Builder
|
| 3 |
+
# Sources: Dataset Chicken 1-3, Chickens-Eggs v1, chicken eggs 2 v3, MVBroTrack
|
| 4 |
+
|
| 5 |
+
path: c:/Users/HP/Downloads/Dataset Model Firm/PoultryVision/dataset
|
| 6 |
+
train: images/train
|
| 7 |
+
val: images/val
|
| 8 |
+
test: images/test
|
| 9 |
+
|
| 10 |
+
nc: 2
|
| 11 |
+
names:
|
| 12 |
+
0: chicken
|
| 13 |
+
1: egg
|
| 14 |
+
|
| 15 |
+
# Dataset statistics (auto-generated)
|
| 16 |
+
# See dataset_card.md for full details
|
labels.jpg
ADDED
|
Git LFS Details
|
poultry_vision_pipeline.py
ADDED
|
@@ -0,0 +1,1067 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
PoultryVision - Complete AI Model for Poultry Farm Monitoring
|
| 3 |
+
================================================================
|
| 4 |
+
Implements the full pipeline inspired by MVBroTrack paper:
|
| 5 |
+
1. Single-View Detection (YOLOv11x fine-tuned)
|
| 6 |
+
2. Multi-View Detection (Ground-Plane Projection + Point Fusion)
|
| 7 |
+
3. Multi-View Tracking (TBCM - Tracking by Curve Matching)
|
| 8 |
+
4. Behavior Analysis & Reporting
|
| 9 |
+
|
| 10 |
+
Enhanced with:
|
| 11 |
+
- Egg detection and counting
|
| 12 |
+
- Behavior classification (feeding, drinking, resting, active)
|
| 13 |
+
- Daily farm summary generation
|
| 14 |
+
- Real-time monitoring dashboard data
|
| 15 |
+
|
| 16 |
+
Paper reference: Cardoen et al., "Multi-camera detection and tracking
|
| 17 |
+
for individual broiler monitoring", Computers and Electronics in Agriculture, 2025
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
import sys
|
| 22 |
+
import json
|
| 23 |
+
import math
|
| 24 |
+
import time
|
| 25 |
+
import logging
|
| 26 |
+
import numpy as np
|
| 27 |
+
from pathlib import Path
|
| 28 |
+
from datetime import datetime
|
| 29 |
+
from collections import defaultdict
|
| 30 |
+
from typing import List, Dict, Tuple, Optional
|
| 31 |
+
|
| 32 |
+
# ============================================================
|
| 33 |
+
# Configuration
|
| 34 |
+
# ============================================================
|
| 35 |
+
BASE_DIR = Path(r"c:/Users/HP/Downloads/Dataset Model Firm/PoultryVision")
|
| 36 |
+
DATASET_DIR = BASE_DIR / "dataset"
|
| 37 |
+
MODEL_DIR = BASE_DIR / "models"
|
| 38 |
+
RESULTS_DIR = BASE_DIR / "results"
|
| 39 |
+
LOGS_DIR = BASE_DIR / "logs"
|
| 40 |
+
|
| 41 |
+
for d in [MODEL_DIR, RESULTS_DIR, LOGS_DIR]:
|
| 42 |
+
d.mkdir(parents=True, exist_ok=True)
|
| 43 |
+
|
| 44 |
+
# Setup logging
|
| 45 |
+
logging.basicConfig(
|
| 46 |
+
level=logging.INFO,
|
| 47 |
+
format='%(asctime)s [%(levelname)s] %(message)s',
|
| 48 |
+
handlers=[
|
| 49 |
+
logging.FileHandler(LOGS_DIR / "training.log"),
|
| 50 |
+
logging.StreamHandler(sys.stdout)
|
| 51 |
+
]
|
| 52 |
+
)
|
| 53 |
+
logger = logging.getLogger("PoultryVision")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# ============================================================
|
| 57 |
+
# Module 1: Camera Calibration & Projection
|
| 58 |
+
# ============================================================
|
| 59 |
+
class CameraCalibration:
|
| 60 |
+
"""
|
| 61 |
+
Handles camera intrinsic/extrinsic parameters for ground-plane projection.
|
| 62 |
+
Based on MVBroTrack calibration data.
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
def __init__(self, calibration_dir: Path):
|
| 66 |
+
self.cameras = {}
|
| 67 |
+
self.calibration_dir = calibration_dir
|
| 68 |
+
|
| 69 |
+
def load_calibrations(self):
|
| 70 |
+
"""Load all camera calibration parameters."""
|
| 71 |
+
cal_dir = self.calibration_dir
|
| 72 |
+
if not cal_dir.exists():
|
| 73 |
+
logger.warning(f"Calibration directory not found: {cal_dir}")
|
| 74 |
+
return
|
| 75 |
+
|
| 76 |
+
for cam_dir in cal_dir.iterdir():
|
| 77 |
+
if not cam_dir.is_dir() or not cam_dir.name.startswith("cam_"):
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
cam_id = cam_dir.name
|
| 81 |
+
intrinsics_dir = cam_dir / "intrinsics"
|
| 82 |
+
extrinsics_dir = cam_dir / "extrinsics"
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
camera_matrix = np.loadtxt(intrinsics_dir / "cameraMatrix.txt")
|
| 86 |
+
dist_coeffs = np.loadtxt(intrinsics_dir / "distCoeffs.txt")
|
| 87 |
+
rvec = np.loadtxt(extrinsics_dir / "rvec.txt")
|
| 88 |
+
tvec = np.loadtxt(extrinsics_dir / "tvec.txt")
|
| 89 |
+
|
| 90 |
+
self.cameras[cam_id] = {
|
| 91 |
+
"camera_matrix": camera_matrix,
|
| 92 |
+
"dist_coeffs": dist_coeffs,
|
| 93 |
+
"rvec": rvec,
|
| 94 |
+
"tvec": tvec,
|
| 95 |
+
}
|
| 96 |
+
logger.info(f"Loaded calibration for {cam_id}")
|
| 97 |
+
except Exception as e:
|
| 98 |
+
logger.warning(f"Failed to load calibration for {cam_id}: {e}")
|
| 99 |
+
|
| 100 |
+
def project_to_ground_plane(self, point_2d: np.ndarray, cam_id: str) -> np.ndarray:
|
| 101 |
+
"""
|
| 102 |
+
Project a 2D image point to the ground plane (z=0).
|
| 103 |
+
Uses camera intrinsic and extrinsic parameters.
|
| 104 |
+
"""
|
| 105 |
+
if cam_id not in self.cameras:
|
| 106 |
+
raise ValueError(f"Camera {cam_id} not calibrated")
|
| 107 |
+
|
| 108 |
+
cal = self.cameras[cam_id]
|
| 109 |
+
K = cal["camera_matrix"]
|
| 110 |
+
rvec = cal["rvec"]
|
| 111 |
+
tvec = cal["tvec"]
|
| 112 |
+
|
| 113 |
+
# Build rotation matrix from Rodrigues vector
|
| 114 |
+
import cv2
|
| 115 |
+
R, _ = cv2.Rodrigues(rvec)
|
| 116 |
+
|
| 117 |
+
# Compute projection to ground plane (Z=0)
|
| 118 |
+
K_inv = np.linalg.inv(K)
|
| 119 |
+
point_h = np.array([point_2d[0], point_2d[1], 1.0])
|
| 120 |
+
ray = K_inv @ point_h
|
| 121 |
+
|
| 122 |
+
# Transform ray to world coordinates
|
| 123 |
+
R_inv = R.T
|
| 124 |
+
ray_world = R_inv @ ray
|
| 125 |
+
cam_pos = -R_inv @ tvec
|
| 126 |
+
|
| 127 |
+
# Intersect ray with z=0 plane
|
| 128 |
+
if abs(ray_world[2]) < 1e-6:
|
| 129 |
+
return np.array([float('inf'), float('inf')])
|
| 130 |
+
|
| 131 |
+
t = -cam_pos[2] / ray_world[2]
|
| 132 |
+
ground_point = cam_pos[:2] + t * ray_world[:2]
|
| 133 |
+
|
| 134 |
+
return ground_point
|
| 135 |
+
|
| 136 |
+
def get_point_selection(self, bbox: np.ndarray, cam_id: str) -> np.ndarray:
|
| 137 |
+
"""
|
| 138 |
+
Select the best point within the bounding box to represent the broiler's
|
| 139 |
+
ground position. Uses distance-based linear interpolation between
|
| 140 |
+
bottom center and center of bbox.
|
| 141 |
+
|
| 142 |
+
Paper: "a linear interpolation between the bottom center and the center
|
| 143 |
+
depending on the distance from the camera"
|
| 144 |
+
"""
|
| 145 |
+
x, y, w, h = bbox
|
| 146 |
+
bottom_center = np.array([x + w / 2, y + h])
|
| 147 |
+
center = np.array([x + w / 2, y + h / 2])
|
| 148 |
+
|
| 149 |
+
if cam_id not in self.cameras:
|
| 150 |
+
return bottom_center
|
| 151 |
+
|
| 152 |
+
cal = self.cameras[cam_id]
|
| 153 |
+
cam_center_x = cal["camera_matrix"][0, 2]
|
| 154 |
+
cam_center_y = cal["camera_matrix"][1, 2]
|
| 155 |
+
|
| 156 |
+
# Distance from camera center (normalized)
|
| 157 |
+
dist = np.sqrt(
|
| 158 |
+
(bottom_center[0] - cam_center_x) ** 2
|
| 159 |
+
+ (bottom_center[1] - cam_center_y) ** 2
|
| 160 |
+
)
|
| 161 |
+
max_dist = np.sqrt(cam_center_x ** 2 + cam_center_y ** 2)
|
| 162 |
+
alpha = min(1.0, dist / max_dist)
|
| 163 |
+
|
| 164 |
+
# Interpolate: close to camera -> center, far -> bottom center
|
| 165 |
+
selected = alpha * bottom_center + (1 - alpha) * center
|
| 166 |
+
return selected
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
# ============================================================
|
| 170 |
+
# Module 2: Graph Construction & Point Fusion
|
| 171 |
+
# ============================================================
|
| 172 |
+
class GraphBasedFusion:
|
| 173 |
+
"""
|
| 174 |
+
Implements Algorithm 1 (Graph Construction) and Algorithm 2 (Multi-Camera Fusion)
|
| 175 |
+
from the MVBroTrack paper.
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
def __init__(self, fusion_radius_fn=None, max_gap_threshold: int = 5):
|
| 179 |
+
"""
|
| 180 |
+
Args:
|
| 181 |
+
fusion_radius_fn: Function that returns fusion radius R given age in days.
|
| 182 |
+
Paper uses: R = 0.3 * age + 5 (in cm)
|
| 183 |
+
max_gap_threshold: Maximum temporal gap for tracklet fusion
|
| 184 |
+
"""
|
| 185 |
+
if fusion_radius_fn is None:
|
| 186 |
+
self.fusion_radius_fn = lambda age: 0.3 * age + 5.0
|
| 187 |
+
else:
|
| 188 |
+
self.fusion_radius_fn = fusion_radius_fn
|
| 189 |
+
self.max_gap_threshold = max_gap_threshold
|
| 190 |
+
|
| 191 |
+
def build_graph(self, detections: List[Dict], age_days: int = 20) -> Dict:
|
| 192 |
+
"""
|
| 193 |
+
Algorithm 1: Build graph from detections or tracklets.
|
| 194 |
+
|
| 195 |
+
Each detection dict has:
|
| 196 |
+
- 'id': unique ID
|
| 197 |
+
- 'camera': camera ID
|
| 198 |
+
- 'position': (x, y) ground plane position
|
| 199 |
+
- 'time': frame/timestep interval (start, end)
|
| 200 |
+
- 'confidence': detection confidence
|
| 201 |
+
|
| 202 |
+
Returns graph as adjacency list with edge weights.
|
| 203 |
+
"""
|
| 204 |
+
R = self.fusion_radius_fn(age_days)
|
| 205 |
+
graph = {"nodes": [], "edges": []}
|
| 206 |
+
|
| 207 |
+
for i, d_i in enumerate(detections):
|
| 208 |
+
graph["nodes"].append(d_i)
|
| 209 |
+
|
| 210 |
+
for j in range(i + 1, len(detections)):
|
| 211 |
+
d_j = detections[j]
|
| 212 |
+
r = float('inf')
|
| 213 |
+
|
| 214 |
+
if d_i["camera"] != d_j["camera"]:
|
| 215 |
+
# Different cameras: use Euclidean distance
|
| 216 |
+
r = self._euclidean_distance(d_i["position"], d_j["position"])
|
| 217 |
+
else:
|
| 218 |
+
# Same camera: check temporal overlap
|
| 219 |
+
overlap = self._temporal_overlap(d_i["time"], d_j["time"])
|
| 220 |
+
|
| 221 |
+
if overlap is not None and len(overlap) > 0:
|
| 222 |
+
# Has temporal overlap -> use Fréchet distance
|
| 223 |
+
if "trajectory" in d_i and "trajectory" in d_j:
|
| 224 |
+
r = self._frechet_distance(
|
| 225 |
+
d_i["trajectory"], d_j["trajectory"], overlap
|
| 226 |
+
)
|
| 227 |
+
else:
|
| 228 |
+
continue # Skip same-camera with overlap but no trajectory
|
| 229 |
+
else:
|
| 230 |
+
# No temporal overlap
|
| 231 |
+
gap = self._temporal_gap(d_i["time"], d_j["time"])
|
| 232 |
+
if gap <= self.max_gap_threshold:
|
| 233 |
+
# Gap is small enough: use Euclidean between endpoints
|
| 234 |
+
p1 = d_i.get("end_point", d_i["position"])
|
| 235 |
+
p2 = d_j.get("start_point", d_j["position"])
|
| 236 |
+
r = self._euclidean_distance(p1, p2)
|
| 237 |
+
else:
|
| 238 |
+
continue
|
| 239 |
+
|
| 240 |
+
# Add edge if distance is within fusion radius
|
| 241 |
+
if r <= R:
|
| 242 |
+
graph["edges"].append({
|
| 243 |
+
"from": i, "to": j, "weight": r
|
| 244 |
+
})
|
| 245 |
+
|
| 246 |
+
return graph
|
| 247 |
+
|
| 248 |
+
def fuse_detections(self, detections: List[Dict], graph: Dict) -> List[Dict]:
|
| 249 |
+
"""
|
| 250 |
+
Algorithm 2: Fuse detections using the constructed graph.
|
| 251 |
+
Greedy connected component fusion with priority on spatial proximity.
|
| 252 |
+
|
| 253 |
+
Returns fused detections (one per actual broiler).
|
| 254 |
+
"""
|
| 255 |
+
n = len(detections)
|
| 256 |
+
found = set()
|
| 257 |
+
results = []
|
| 258 |
+
|
| 259 |
+
# Build adjacency list
|
| 260 |
+
adj = defaultdict(list)
|
| 261 |
+
for edge in graph["edges"]:
|
| 262 |
+
adj[edge["from"]].append((edge["to"], edge["weight"]))
|
| 263 |
+
adj[edge["to"]].append((edge["from"], edge["weight"]))
|
| 264 |
+
|
| 265 |
+
# Sort detections by x-coordinate (for point fusion) or
|
| 266 |
+
# by -temporal_length then x (for tracklet fusion)
|
| 267 |
+
sorted_indices = sorted(range(n), key=lambda i: detections[i]["position"][0])
|
| 268 |
+
|
| 269 |
+
for d_idx in sorted_indices:
|
| 270 |
+
if d_idx in found:
|
| 271 |
+
continue
|
| 272 |
+
|
| 273 |
+
# BFS/greedy expansion from this detection
|
| 274 |
+
component = [d_idx]
|
| 275 |
+
found.add(d_idx)
|
| 276 |
+
queue = [d_idx]
|
| 277 |
+
|
| 278 |
+
while queue:
|
| 279 |
+
current = queue.pop(0)
|
| 280 |
+
# Get candidates sorted by weight
|
| 281 |
+
candidates = sorted(adj[current], key=lambda x: x[1])
|
| 282 |
+
|
| 283 |
+
for neighbor, weight in candidates:
|
| 284 |
+
if neighbor in found:
|
| 285 |
+
continue
|
| 286 |
+
|
| 287 |
+
# Check if neighbor is compatible with all current component members
|
| 288 |
+
compatible = True
|
| 289 |
+
for member in component:
|
| 290 |
+
# Same camera check for point fusion
|
| 291 |
+
if detections[neighbor]["camera"] == detections[member]["camera"]:
|
| 292 |
+
# Same camera detections should not be fused in point fusion
|
| 293 |
+
overlap = self._temporal_overlap(
|
| 294 |
+
detections[neighbor]["time"],
|
| 295 |
+
detections[member]["time"]
|
| 296 |
+
)
|
| 297 |
+
if overlap is not None:
|
| 298 |
+
compatible = False
|
| 299 |
+
break
|
| 300 |
+
|
| 301 |
+
if compatible:
|
| 302 |
+
component.append(neighbor)
|
| 303 |
+
found.add(neighbor)
|
| 304 |
+
queue.append(neighbor)
|
| 305 |
+
|
| 306 |
+
# Compute fused position as arithmetic mean
|
| 307 |
+
positions = [detections[i]["position"] for i in component]
|
| 308 |
+
fused_position = np.mean(positions, axis=0)
|
| 309 |
+
|
| 310 |
+
# Compute fused confidence
|
| 311 |
+
confidences = [detections[i].get("confidence", 1.0) for i in component]
|
| 312 |
+
fused_confidence = np.mean(confidences)
|
| 313 |
+
|
| 314 |
+
results.append({
|
| 315 |
+
"position": fused_position.tolist(),
|
| 316 |
+
"confidence": float(fused_confidence),
|
| 317 |
+
"source_detections": component,
|
| 318 |
+
"num_cameras": len(set(detections[i]["camera"] for i in component)),
|
| 319 |
+
})
|
| 320 |
+
|
| 321 |
+
return results
|
| 322 |
+
|
| 323 |
+
@staticmethod
|
| 324 |
+
def _euclidean_distance(p1, p2) -> float:
|
| 325 |
+
return float(np.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2))
|
| 326 |
+
|
| 327 |
+
@staticmethod
|
| 328 |
+
def _temporal_overlap(t1, t2):
|
| 329 |
+
"""Compute temporal overlap between two time intervals."""
|
| 330 |
+
start = max(t1[0], t2[0])
|
| 331 |
+
end = min(t1[1], t2[1])
|
| 332 |
+
if start <= end:
|
| 333 |
+
return (start, end)
|
| 334 |
+
return None
|
| 335 |
+
|
| 336 |
+
@staticmethod
|
| 337 |
+
def _temporal_gap(t1, t2) -> int:
|
| 338 |
+
"""Compute gap between two non-overlapping time intervals."""
|
| 339 |
+
if t1[1] < t2[0]:
|
| 340 |
+
return t2[0] - t1[1]
|
| 341 |
+
elif t2[1] < t1[0]:
|
| 342 |
+
return t1[0] - t2[1]
|
| 343 |
+
return 0
|
| 344 |
+
|
| 345 |
+
@staticmethod
|
| 346 |
+
def _frechet_distance(traj1: np.ndarray, traj2: np.ndarray,
|
| 347 |
+
overlap: Tuple[int, int]) -> float:
|
| 348 |
+
"""
|
| 349 |
+
Compute discrete Fréchet distance between two trajectories
|
| 350 |
+
over the overlapping temporal segment.
|
| 351 |
+
"""
|
| 352 |
+
start, end = overlap
|
| 353 |
+
if isinstance(traj1, list):
|
| 354 |
+
traj1 = np.array(traj1)
|
| 355 |
+
if isinstance(traj2, list):
|
| 356 |
+
traj2 = np.array(traj2)
|
| 357 |
+
|
| 358 |
+
# Extract overlapping segments
|
| 359 |
+
seg1 = traj1[start:end + 1] if len(traj1) > end else traj1
|
| 360 |
+
seg2 = traj2[start:end + 1] if len(traj2) > end else traj2
|
| 361 |
+
|
| 362 |
+
n = len(seg1)
|
| 363 |
+
m = len(seg2)
|
| 364 |
+
|
| 365 |
+
if n == 0 or m == 0:
|
| 366 |
+
return float('inf')
|
| 367 |
+
|
| 368 |
+
# DP computation of discrete Fréchet distance
|
| 369 |
+
ca = np.full((n, m), -1.0)
|
| 370 |
+
|
| 371 |
+
def _c(i, j):
|
| 372 |
+
if ca[i, j] > -0.5:
|
| 373 |
+
return ca[i, j]
|
| 374 |
+
d = np.sqrt(np.sum((seg1[i] - seg2[j]) ** 2))
|
| 375 |
+
if i == 0 and j == 0:
|
| 376 |
+
ca[i, j] = d
|
| 377 |
+
elif i > 0 and j == 0:
|
| 378 |
+
ca[i, j] = max(_c(i - 1, 0), d)
|
| 379 |
+
elif i == 0 and j > 0:
|
| 380 |
+
ca[i, j] = max(_c(0, j - 1), d)
|
| 381 |
+
else:
|
| 382 |
+
ca[i, j] = max(min(_c(i - 1, j), _c(i - 1, j - 1), _c(i, j - 1)), d)
|
| 383 |
+
return ca[i, j]
|
| 384 |
+
|
| 385 |
+
return _c(n - 1, m - 1)
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
# ============================================================
|
| 389 |
+
# Module 3: Multi-View Tracker (TBCM)
|
| 390 |
+
# ============================================================
|
| 391 |
+
class MultiViewTracker:
|
| 392 |
+
"""
|
| 393 |
+
Tracking by Curve Matching (TBCM) - the novel method from the paper.
|
| 394 |
+
|
| 395 |
+
Pipeline:
|
| 396 |
+
1. Generate per-camera bounding box detections (YOLO)
|
| 397 |
+
2. Create per-camera tracklets via temporal association
|
| 398 |
+
3. Project tracklets to ground plane
|
| 399 |
+
4. Fuse tracklets across cameras using graph construction
|
| 400 |
+
5. Output full ground-plane tracks
|
| 401 |
+
"""
|
| 402 |
+
|
| 403 |
+
def __init__(self, calibration: CameraCalibration,
|
| 404 |
+
fusion: GraphBasedFusion,
|
| 405 |
+
sort_tracker=None):
|
| 406 |
+
self.calibration = calibration
|
| 407 |
+
self.fusion = fusion
|
| 408 |
+
self.tracks = {} # Active tracks
|
| 409 |
+
self.track_counter = 0
|
| 410 |
+
self.history = [] # Full tracking history
|
| 411 |
+
|
| 412 |
+
def process_frame(self, detections_per_camera: Dict[str, List],
|
| 413 |
+
frame_id: int, age_days: int = 20) -> List[Dict]:
|
| 414 |
+
"""
|
| 415 |
+
Process one synchronized frame from all cameras.
|
| 416 |
+
|
| 417 |
+
Args:
|
| 418 |
+
detections_per_camera: {cam_id: [list of bbox detections]}
|
| 419 |
+
frame_id: Current frame number
|
| 420 |
+
age_days: Age of broilers in days (for threshold computation)
|
| 421 |
+
|
| 422 |
+
Returns:
|
| 423 |
+
List of fused ground-plane detections
|
| 424 |
+
"""
|
| 425 |
+
# Step 1: Project all detections to ground plane
|
| 426 |
+
ground_detections = []
|
| 427 |
+
|
| 428 |
+
for cam_id, dets in detections_per_camera.items():
|
| 429 |
+
for det in dets:
|
| 430 |
+
bbox = det["bbox"] # [x, y, w, h] in pixels
|
| 431 |
+
|
| 432 |
+
# Select best point in bbox
|
| 433 |
+
point_2d = self.calibration.get_point_selection(
|
| 434 |
+
np.array(bbox), cam_id
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
# Project to ground plane
|
| 438 |
+
try:
|
| 439 |
+
ground_pos = self.calibration.project_to_ground_plane(
|
| 440 |
+
point_2d, cam_id
|
| 441 |
+
)
|
| 442 |
+
except Exception:
|
| 443 |
+
continue
|
| 444 |
+
|
| 445 |
+
ground_detections.append({
|
| 446 |
+
"id": len(ground_detections),
|
| 447 |
+
"camera": cam_id,
|
| 448 |
+
"position": ground_pos.tolist(),
|
| 449 |
+
"time": (frame_id, frame_id),
|
| 450 |
+
"confidence": det.get("confidence", 1.0),
|
| 451 |
+
"bbox": bbox,
|
| 452 |
+
"source_cam": cam_id,
|
| 453 |
+
})
|
| 454 |
+
|
| 455 |
+
# Step 2: Build graph and fuse detections
|
| 456 |
+
graph = self.fusion.build_graph(ground_detections, age_days)
|
| 457 |
+
fused = self.fusion.fuse_detections(ground_detections, graph)
|
| 458 |
+
|
| 459 |
+
# Step 3: Update tracks (simple nearest-neighbor for now)
|
| 460 |
+
self._update_tracks(fused, frame_id)
|
| 461 |
+
|
| 462 |
+
return fused
|
| 463 |
+
|
| 464 |
+
def _update_tracks(self, fused_detections: List[Dict], frame_id: int):
|
| 465 |
+
"""Simple nearest-neighbor track association."""
|
| 466 |
+
R_max = 50 # Maximum association distance
|
| 467 |
+
|
| 468 |
+
used_tracks = set()
|
| 469 |
+
used_dets = set()
|
| 470 |
+
|
| 471 |
+
# Associate detections to existing tracks
|
| 472 |
+
associations = []
|
| 473 |
+
for det_idx, det in enumerate(fused_detections):
|
| 474 |
+
for track_id, track in self.tracks.items():
|
| 475 |
+
if track_id in used_tracks:
|
| 476 |
+
continue
|
| 477 |
+
|
| 478 |
+
last_pos = track["positions"][-1]
|
| 479 |
+
dist = np.sqrt(
|
| 480 |
+
(det["position"][0] - last_pos[0]) ** 2
|
| 481 |
+
+ (det["position"][1] - last_pos[1]) ** 2
|
| 482 |
+
)
|
| 483 |
+
|
| 484 |
+
if dist < R_max:
|
| 485 |
+
associations.append((dist, det_idx, track_id))
|
| 486 |
+
|
| 487 |
+
# Sort by distance, greedily assign
|
| 488 |
+
associations.sort()
|
| 489 |
+
for dist, det_idx, track_id in associations:
|
| 490 |
+
if det_idx in used_dets or track_id in used_tracks:
|
| 491 |
+
continue
|
| 492 |
+
|
| 493 |
+
self.tracks[track_id]["positions"].append(
|
| 494 |
+
fused_detections[det_idx]["position"]
|
| 495 |
+
)
|
| 496 |
+
self.tracks[track_id]["frames"].append(frame_id)
|
| 497 |
+
self.tracks[track_id]["last_seen"] = frame_id
|
| 498 |
+
used_tracks.add(track_id)
|
| 499 |
+
used_dets.add(det_idx)
|
| 500 |
+
|
| 501 |
+
# Create new tracks for unassociated detections
|
| 502 |
+
for det_idx, det in enumerate(fused_detections):
|
| 503 |
+
if det_idx not in used_dets:
|
| 504 |
+
self.track_counter += 1
|
| 505 |
+
self.tracks[self.track_counter] = {
|
| 506 |
+
"id": self.track_counter,
|
| 507 |
+
"positions": [det["position"]],
|
| 508 |
+
"frames": [frame_id],
|
| 509 |
+
"last_seen": frame_id,
|
| 510 |
+
"start_frame": frame_id,
|
| 511 |
+
}
|
| 512 |
+
|
| 513 |
+
# Remove stale tracks (not seen for 30 frames)
|
| 514 |
+
stale = [
|
| 515 |
+
tid for tid, t in self.tracks.items()
|
| 516 |
+
if frame_id - t["last_seen"] > 30
|
| 517 |
+
]
|
| 518 |
+
for tid in stale:
|
| 519 |
+
self.history.append(self.tracks.pop(tid))
|
| 520 |
+
|
| 521 |
+
def get_chicken_count(self) -> int:
|
| 522 |
+
"""Get current number of tracked chickens."""
|
| 523 |
+
return len(self.tracks)
|
| 524 |
+
|
| 525 |
+
def get_all_tracks(self) -> List[Dict]:
|
| 526 |
+
"""Get all tracks (active + historical)."""
|
| 527 |
+
return list(self.tracks.values()) + self.history
|
| 528 |
+
|
| 529 |
+
|
| 530 |
+
# ============================================================
|
| 531 |
+
# Module 4: Behavior Analyzer
|
| 532 |
+
# ============================================================
|
| 533 |
+
class BehaviorAnalyzer:
|
| 534 |
+
"""
|
| 535 |
+
Analyzes broiler behavior based on tracking data.
|
| 536 |
+
Classifies behavior into: feeding, drinking, resting, active.
|
| 537 |
+
|
| 538 |
+
Based on pen zone definitions from the paper:
|
| 539 |
+
- Eating zone (near feeders)
|
| 540 |
+
- Drinking zone (near drinkers)
|
| 541 |
+
- Resting zone
|
| 542 |
+
"""
|
| 543 |
+
|
| 544 |
+
# Zone definitions (ground-plane coordinates, configurable)
|
| 545 |
+
ZONES = {
|
| 546 |
+
"eating": {"polygons": []}, # To be configured per farm
|
| 547 |
+
"drinking": {"polygons": []}, # To be configured per farm
|
| 548 |
+
"resting": {"polygons": []}, # To be configured per farm
|
| 549 |
+
}
|
| 550 |
+
|
| 551 |
+
def __init__(self, zone_config: Optional[Dict] = None):
|
| 552 |
+
if zone_config:
|
| 553 |
+
self.ZONES = zone_config
|
| 554 |
+
|
| 555 |
+
def classify_behavior(self, track: Dict) -> str:
|
| 556 |
+
"""
|
| 557 |
+
Classify broiler behavior based on position and movement.
|
| 558 |
+
|
| 559 |
+
Returns: "feeding", "drinking", "resting", "active"
|
| 560 |
+
"""
|
| 561 |
+
if len(track["positions"]) < 2:
|
| 562 |
+
return "unknown"
|
| 563 |
+
|
| 564 |
+
# Compute velocity (movement over last N frames)
|
| 565 |
+
recent_positions = track["positions"][-10:]
|
| 566 |
+
if len(recent_positions) >= 2:
|
| 567 |
+
velocities = []
|
| 568 |
+
for i in range(1, len(recent_positions)):
|
| 569 |
+
dx = recent_positions[i][0] - recent_positions[i - 1][0]
|
| 570 |
+
dy = recent_positions[i][1] - recent_positions[i - 1][1]
|
| 571 |
+
velocities.append(math.sqrt(dx ** 2 + dy ** 2))
|
| 572 |
+
|
| 573 |
+
avg_velocity = np.mean(velocities)
|
| 574 |
+
else:
|
| 575 |
+
avg_velocity = 0
|
| 576 |
+
|
| 577 |
+
current_pos = track["positions"][-1]
|
| 578 |
+
|
| 579 |
+
# Check zone-based behavior
|
| 580 |
+
for zone_name, zone_def in self.ZONES.items():
|
| 581 |
+
if self._point_in_zone(current_pos, zone_def.get("polygons", [])):
|
| 582 |
+
if avg_velocity < 2.0: # Threshold for being stationary in zone
|
| 583 |
+
return zone_name
|
| 584 |
+
|
| 585 |
+
# Movement-based classification
|
| 586 |
+
if avg_velocity < 1.0:
|
| 587 |
+
return "resting"
|
| 588 |
+
elif avg_velocity < 5.0:
|
| 589 |
+
return "active"
|
| 590 |
+
else:
|
| 591 |
+
return "active"
|
| 592 |
+
|
| 593 |
+
@staticmethod
|
| 594 |
+
def _point_in_zone(point, polygons) -> bool:
|
| 595 |
+
"""Check if point is inside any polygon in the zone."""
|
| 596 |
+
if not polygons:
|
| 597 |
+
return False
|
| 598 |
+
|
| 599 |
+
x, y = point[0], point[1]
|
| 600 |
+
for polygon in polygons:
|
| 601 |
+
n = len(polygon)
|
| 602 |
+
inside = False
|
| 603 |
+
j = n - 1
|
| 604 |
+
for i in range(n):
|
| 605 |
+
xi, yi = polygon[i]
|
| 606 |
+
xj, yj = polygon[j]
|
| 607 |
+
if ((yi > y) != (yj > y)) and (x < (xj - xi) * (y - yi) / (yj - yi) + xi):
|
| 608 |
+
inside = not inside
|
| 609 |
+
j = i
|
| 610 |
+
if inside:
|
| 611 |
+
return True
|
| 612 |
+
return False
|
| 613 |
+
|
| 614 |
+
|
| 615 |
+
# ============================================================
|
| 616 |
+
# Module 5: Farm Report Generator
|
| 617 |
+
# ============================================================
|
| 618 |
+
class FarmReportGenerator:
|
| 619 |
+
"""
|
| 620 |
+
Generates real-time and daily summary reports for the farm.
|
| 621 |
+
"""
|
| 622 |
+
|
| 623 |
+
def __init__(self):
|
| 624 |
+
self.daily_data = defaultdict(lambda: {
|
| 625 |
+
"chicken_counts": [],
|
| 626 |
+
"egg_counts": [],
|
| 627 |
+
"behaviors": defaultdict(int),
|
| 628 |
+
"alerts": [],
|
| 629 |
+
"timestamps": [],
|
| 630 |
+
})
|
| 631 |
+
|
| 632 |
+
def record_observation(self, timestamp: str, chicken_count: int,
|
| 633 |
+
egg_count: int, behaviors: Dict[str, int],
|
| 634 |
+
alerts: List[str] = None):
|
| 635 |
+
"""Record an observation for the daily report."""
|
| 636 |
+
date = timestamp[:10]
|
| 637 |
+
self.daily_data[date]["chicken_counts"].append(chicken_count)
|
| 638 |
+
self.daily_data[date]["egg_counts"].append(egg_count)
|
| 639 |
+
self.daily_data[date]["timestamps"].append(timestamp)
|
| 640 |
+
|
| 641 |
+
for behavior, count in behaviors.items():
|
| 642 |
+
self.daily_data[date]["behaviors"][behavior] += count
|
| 643 |
+
|
| 644 |
+
if alerts:
|
| 645 |
+
self.daily_data[date]["alerts"].extend(alerts)
|
| 646 |
+
|
| 647 |
+
def generate_realtime_status(self, chicken_count: int, egg_count: int,
|
| 648 |
+
behaviors: Dict, alerts: List = None) -> Dict:
|
| 649 |
+
"""Generate real-time status update."""
|
| 650 |
+
return {
|
| 651 |
+
"timestamp": datetime.now().isoformat(),
|
| 652 |
+
"status": "live",
|
| 653 |
+
"chicken_count": chicken_count,
|
| 654 |
+
"egg_count": egg_count,
|
| 655 |
+
"behavior_summary": behaviors,
|
| 656 |
+
"alerts": alerts or [],
|
| 657 |
+
"health_index": self._compute_health_index(behaviors),
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
def generate_daily_summary(self, date: str) -> Dict:
|
| 661 |
+
"""Generate complete daily summary report."""
|
| 662 |
+
if date not in self.daily_data:
|
| 663 |
+
return {"error": f"No data for {date}"}
|
| 664 |
+
|
| 665 |
+
data = self.daily_data[date]
|
| 666 |
+
|
| 667 |
+
return {
|
| 668 |
+
"date": date,
|
| 669 |
+
"report_type": "daily_summary",
|
| 670 |
+
"chicken_statistics": {
|
| 671 |
+
"avg_count": np.mean(data["chicken_counts"]) if data["chicken_counts"] else 0,
|
| 672 |
+
"max_count": max(data["chicken_counts"]) if data["chicken_counts"] else 0,
|
| 673 |
+
"min_count": min(data["chicken_counts"]) if data["chicken_counts"] else 0,
|
| 674 |
+
},
|
| 675 |
+
"egg_statistics": {
|
| 676 |
+
"total_detected": max(data["egg_counts"]) if data["egg_counts"] else 0,
|
| 677 |
+
"avg_per_observation": np.mean(data["egg_counts"]) if data["egg_counts"] else 0,
|
| 678 |
+
},
|
| 679 |
+
"behavior_analysis": dict(data["behaviors"]),
|
| 680 |
+
"activity_percentage": self._compute_activity_percentage(data["behaviors"]),
|
| 681 |
+
"alerts": list(set(data["alerts"])),
|
| 682 |
+
"observations_count": len(data["timestamps"]),
|
| 683 |
+
"health_index": self._compute_health_index(data["behaviors"]),
|
| 684 |
+
}
|
| 685 |
+
|
| 686 |
+
@staticmethod
|
| 687 |
+
def _compute_health_index(behaviors: Dict) -> float:
|
| 688 |
+
"""
|
| 689 |
+
Compute a health index (0-100) based on behavior distribution.
|
| 690 |
+
Active, feeding, and drinking behaviors indicate good health.
|
| 691 |
+
"""
|
| 692 |
+
total = sum(behaviors.values()) if behaviors else 1
|
| 693 |
+
if total == 0:
|
| 694 |
+
return 50.0
|
| 695 |
+
|
| 696 |
+
active = behaviors.get("active", 0) / total
|
| 697 |
+
feeding = behaviors.get("feeding", 0) / total
|
| 698 |
+
drinking = behaviors.get("drinking", 0) / total
|
| 699 |
+
resting = behaviors.get("resting", 0) / total
|
| 700 |
+
|
| 701 |
+
# Healthy: balanced between active/feeding/resting
|
| 702 |
+
# Concern: too much resting or too little feeding
|
| 703 |
+
index = (
|
| 704 |
+
active * 25
|
| 705 |
+
+ feeding * 30
|
| 706 |
+
+ drinking * 20
|
| 707 |
+
+ resting * 15
|
| 708 |
+
+ 10 # Base score
|
| 709 |
+
)
|
| 710 |
+
return min(100.0, max(0.0, index))
|
| 711 |
+
|
| 712 |
+
@staticmethod
|
| 713 |
+
def _compute_activity_percentage(behaviors: Dict) -> float:
|
| 714 |
+
total = sum(behaviors.values()) if behaviors else 0
|
| 715 |
+
if total == 0:
|
| 716 |
+
return 0.0
|
| 717 |
+
active = behaviors.get("active", 0) + behaviors.get("feeding", 0) + behaviors.get("drinking", 0)
|
| 718 |
+
return (active / total) * 100
|
| 719 |
+
|
| 720 |
+
|
| 721 |
+
# ============================================================
|
| 722 |
+
# Module 6: YOLO Training Pipeline
|
| 723 |
+
# ============================================================
|
| 724 |
+
class YOLOTrainer:
|
| 725 |
+
"""
|
| 726 |
+
Handles YOLO model training with full logging and evaluation.
|
| 727 |
+
Based on paper: YOLOv11x at 1920x1080, 200 epochs, auto-batch.
|
| 728 |
+
"""
|
| 729 |
+
|
| 730 |
+
def __init__(self, data_yaml: str, model_name: str = "yolo11x.pt",
|
| 731 |
+
project_dir: str = None):
|
| 732 |
+
self.data_yaml = data_yaml
|
| 733 |
+
self.model_name = model_name
|
| 734 |
+
self.project_dir = project_dir or str(RESULTS_DIR)
|
| 735 |
+
|
| 736 |
+
def train(self, epochs: int = 200, imgsz: int = 1920,
|
| 737 |
+
batch: int = -1, device: str = "0",
|
| 738 |
+
resume: bool = False, **kwargs):
|
| 739 |
+
"""
|
| 740 |
+
Train YOLO model with full logging.
|
| 741 |
+
|
| 742 |
+
Paper hyperparameters:
|
| 743 |
+
- Model: YOLOv11x (best performance)
|
| 744 |
+
- Resolution: 1920x1080
|
| 745 |
+
- Epochs: 200
|
| 746 |
+
- Batch: auto (-1)
|
| 747 |
+
- GPU: NVIDIA A100 80GB
|
| 748 |
+
"""
|
| 749 |
+
from ultralytics import YOLO
|
| 750 |
+
|
| 751 |
+
logger.info("=" * 60)
|
| 752 |
+
logger.info(" PoultryVision YOLO Training")
|
| 753 |
+
logger.info("=" * 60)
|
| 754 |
+
logger.info(f" Model: {self.model_name}")
|
| 755 |
+
logger.info(f" Dataset: {self.data_yaml}")
|
| 756 |
+
logger.info(f" Epochs: {epochs}")
|
| 757 |
+
logger.info(f" Image size: {imgsz}")
|
| 758 |
+
logger.info(f" Batch: {'auto' if batch == -1 else batch}")
|
| 759 |
+
logger.info(f" Device: {device}")
|
| 760 |
+
logger.info("=" * 60)
|
| 761 |
+
|
| 762 |
+
# Load model
|
| 763 |
+
if resume and (Path(self.project_dir) / "weights" / "last.pt").exists():
|
| 764 |
+
model = YOLO(str(Path(self.project_dir) / "weights" / "last.pt"))
|
| 765 |
+
logger.info("Resuming from last checkpoint")
|
| 766 |
+
else:
|
| 767 |
+
model = YOLO(self.model_name)
|
| 768 |
+
logger.info(f"Loaded base model: {self.model_name}")
|
| 769 |
+
|
| 770 |
+
# Training configuration
|
| 771 |
+
train_args = {
|
| 772 |
+
"data": self.data_yaml,
|
| 773 |
+
"epochs": epochs,
|
| 774 |
+
"imgsz": imgsz,
|
| 775 |
+
"batch": batch,
|
| 776 |
+
"device": device,
|
| 777 |
+
"project": self.project_dir,
|
| 778 |
+
"name": "poultry_vision",
|
| 779 |
+
"exist_ok": True,
|
| 780 |
+
"verbose": True,
|
| 781 |
+
"save": True,
|
| 782 |
+
"save_period": 10,
|
| 783 |
+
"plots": True,
|
| 784 |
+
"val": True,
|
| 785 |
+
# Augmentation (enhanced from paper)
|
| 786 |
+
"hsv_h": 0.015,
|
| 787 |
+
"hsv_s": 0.7,
|
| 788 |
+
"hsv_v": 0.4,
|
| 789 |
+
"degrees": 10.0,
|
| 790 |
+
"translate": 0.1,
|
| 791 |
+
"scale": 0.5,
|
| 792 |
+
"fliplr": 0.5,
|
| 793 |
+
"mosaic": 1.0,
|
| 794 |
+
"mixup": 0.1,
|
| 795 |
+
# Optimizer
|
| 796 |
+
"optimizer": "AdamW",
|
| 797 |
+
"lr0": 0.001,
|
| 798 |
+
"lrf": 0.01,
|
| 799 |
+
"momentum": 0.937,
|
| 800 |
+
"weight_decay": 0.0005,
|
| 801 |
+
"warmup_epochs": 3.0,
|
| 802 |
+
"warmup_momentum": 0.8,
|
| 803 |
+
# Early stopping
|
| 804 |
+
"patience": 50,
|
| 805 |
+
# Loss weights
|
| 806 |
+
"box": 7.5,
|
| 807 |
+
"cls": 0.5,
|
| 808 |
+
"dfl": 1.5,
|
| 809 |
+
}
|
| 810 |
+
|
| 811 |
+
# Override with any additional kwargs
|
| 812 |
+
train_args.update(kwargs)
|
| 813 |
+
|
| 814 |
+
# Start training
|
| 815 |
+
logger.info("\nStarting training...\n")
|
| 816 |
+
results = model.train(**train_args)
|
| 817 |
+
|
| 818 |
+
logger.info("\n" + "=" * 60)
|
| 819 |
+
logger.info(" Training Complete!")
|
| 820 |
+
logger.info("=" * 60)
|
| 821 |
+
|
| 822 |
+
return model, results
|
| 823 |
+
|
| 824 |
+
def evaluate(self, model_path: str = None, split: str = "test",
|
| 825 |
+
imgsz: int = 1920, conf: float = 0.001, iou: float = 0.6):
|
| 826 |
+
"""
|
| 827 |
+
Comprehensive evaluation with all metrics from the paper.
|
| 828 |
+
"""
|
| 829 |
+
from ultralytics import YOLO
|
| 830 |
+
|
| 831 |
+
if model_path is None:
|
| 832 |
+
model_path = str(
|
| 833 |
+
Path(self.project_dir) / "poultry_vision" / "weights" / "best.pt"
|
| 834 |
+
)
|
| 835 |
+
|
| 836 |
+
logger.info("=" * 60)
|
| 837 |
+
logger.info(" PoultryVision Model Evaluation")
|
| 838 |
+
logger.info("=" * 60)
|
| 839 |
+
logger.info(f" Model: {model_path}")
|
| 840 |
+
logger.info(f" Split: {split}")
|
| 841 |
+
|
| 842 |
+
model = YOLO(model_path)
|
| 843 |
+
|
| 844 |
+
# Run validation
|
| 845 |
+
results = model.val(
|
| 846 |
+
data=self.data_yaml,
|
| 847 |
+
split=split,
|
| 848 |
+
imgsz=imgsz,
|
| 849 |
+
conf=conf,
|
| 850 |
+
iou=iou,
|
| 851 |
+
verbose=True,
|
| 852 |
+
plots=True,
|
| 853 |
+
save_json=True,
|
| 854 |
+
project=self.project_dir,
|
| 855 |
+
name=f"eval_{split}",
|
| 856 |
+
exist_ok=True,
|
| 857 |
+
)
|
| 858 |
+
|
| 859 |
+
# Log detailed metrics
|
| 860 |
+
logger.info("\n" + "-" * 40)
|
| 861 |
+
logger.info(" Detection Metrics")
|
| 862 |
+
logger.info("-" * 40)
|
| 863 |
+
|
| 864 |
+
metrics = {
|
| 865 |
+
"mAP50": float(results.box.map50) if hasattr(results.box, 'map50') else 0,
|
| 866 |
+
"mAP50-95": float(results.box.map) if hasattr(results.box, 'map') else 0,
|
| 867 |
+
"precision": float(results.box.mp) if hasattr(results.box, 'mp') else 0,
|
| 868 |
+
"recall": float(results.box.mr) if hasattr(results.box, 'mr') else 0,
|
| 869 |
+
}
|
| 870 |
+
|
| 871 |
+
for name, value in metrics.items():
|
| 872 |
+
logger.info(f" {name}: {value:.4f}")
|
| 873 |
+
|
| 874 |
+
# Per-class metrics
|
| 875 |
+
if hasattr(results.box, 'ap_class_index') and results.box.ap_class_index is not None:
|
| 876 |
+
logger.info("\n Per-class AP@50-95:")
|
| 877 |
+
class_names = ["chicken", "egg"]
|
| 878 |
+
for i, cls_idx in enumerate(results.box.ap_class_index):
|
| 879 |
+
if i < len(class_names):
|
| 880 |
+
ap = results.box.ap[i] if hasattr(results.box, 'ap') else 0
|
| 881 |
+
logger.info(f" {class_names[int(cls_idx)]}: {float(ap):.4f}")
|
| 882 |
+
|
| 883 |
+
logger.info("\n" + "=" * 60)
|
| 884 |
+
|
| 885 |
+
return results, metrics
|
| 886 |
+
|
| 887 |
+
def benchmark_comparison(self, model_path: str = None):
|
| 888 |
+
"""
|
| 889 |
+
Compare with other models and paper benchmarks.
|
| 890 |
+
Paper results: AP@50-95 = 70.8% overall with fine-tuned YOLOv11x
|
| 891 |
+
"""
|
| 892 |
+
logger.info("\n" + "=" * 60)
|
| 893 |
+
logger.info(" Benchmark Comparison")
|
| 894 |
+
logger.info("=" * 60)
|
| 895 |
+
|
| 896 |
+
# Paper benchmarks (Table 4)
|
| 897 |
+
paper_benchmarks = {
|
| 898 |
+
"YOLOv11X (not fine-tuned)": {
|
| 899 |
+
"Starter": 1.58, "Grower": 11.16, "Finisher": 21.8, "Overall": 13.94
|
| 900 |
+
},
|
| 901 |
+
"YOLOv11X (fine-tuned, paper)": {
|
| 902 |
+
"Starter": 63.3, "Grower": 70.0, "Finisher": 74.9, "Overall": 70.8
|
| 903 |
+
},
|
| 904 |
+
}
|
| 905 |
+
|
| 906 |
+
logger.info("\n Paper benchmarks (AP@50-95):")
|
| 907 |
+
for model_name, scores in paper_benchmarks.items():
|
| 908 |
+
logger.info(f"\n {model_name}:")
|
| 909 |
+
for phase, score in scores.items():
|
| 910 |
+
logger.info(f" {phase}: {score}%")
|
| 911 |
+
|
| 912 |
+
# Our model evaluation
|
| 913 |
+
if model_path:
|
| 914 |
+
logger.info("\n Our model (PoultryVision):")
|
| 915 |
+
_, our_metrics = self.evaluate(model_path)
|
| 916 |
+
logger.info(f" Overall AP@50-95: {our_metrics['mAP50-95'] * 100:.1f}%")
|
| 917 |
+
logger.info(f" Overall AP@50: {our_metrics['mAP50'] * 100:.1f}%")
|
| 918 |
+
|
| 919 |
+
improvement = our_metrics['mAP50-95'] * 100 - 70.8
|
| 920 |
+
if improvement > 0:
|
| 921 |
+
logger.info(f"\n >>> IMPROVEMENT over paper: +{improvement:.1f}% AP@50-95")
|
| 922 |
+
else:
|
| 923 |
+
logger.info(f"\n Difference from paper: {improvement:.1f}% AP@50-95")
|
| 924 |
+
|
| 925 |
+
logger.info("=" * 60)
|
| 926 |
+
|
| 927 |
+
|
| 928 |
+
# ============================================================
|
| 929 |
+
# Module 7: Main Pipeline
|
| 930 |
+
# ============================================================
|
| 931 |
+
class PoultryVisionPipeline:
|
| 932 |
+
"""
|
| 933 |
+
Main pipeline that orchestrates all modules.
|
| 934 |
+
"""
|
| 935 |
+
|
| 936 |
+
def __init__(self, config: Dict = None):
|
| 937 |
+
self.config = config or {}
|
| 938 |
+
self.calibration = CameraCalibration(
|
| 939 |
+
DATASET_DIR / "calibrations"
|
| 940 |
+
)
|
| 941 |
+
self.fusion = GraphBasedFusion()
|
| 942 |
+
self.tracker = None
|
| 943 |
+
self.behavior_analyzer = BehaviorAnalyzer()
|
| 944 |
+
self.report_generator = FarmReportGenerator()
|
| 945 |
+
self.trainer = None
|
| 946 |
+
|
| 947 |
+
def setup(self):
|
| 948 |
+
"""Initialize all components."""
|
| 949 |
+
logger.info("Setting up PoultryVision Pipeline...")
|
| 950 |
+
self.calibration.load_calibrations()
|
| 951 |
+
self.tracker = MultiViewTracker(self.calibration, self.fusion)
|
| 952 |
+
logger.info("Pipeline ready.")
|
| 953 |
+
|
| 954 |
+
def train_model(self, epochs: int = 200, imgsz: int = 1920, **kwargs):
|
| 955 |
+
"""Train the YOLO detection model."""
|
| 956 |
+
data_yaml = str(DATASET_DIR / "data.yaml")
|
| 957 |
+
self.trainer = YOLOTrainer(
|
| 958 |
+
data_yaml=data_yaml,
|
| 959 |
+
model_name="yolo11x.pt",
|
| 960 |
+
project_dir=str(RESULTS_DIR),
|
| 961 |
+
)
|
| 962 |
+
model, results = self.trainer.train(
|
| 963 |
+
epochs=epochs, imgsz=imgsz, **kwargs
|
| 964 |
+
)
|
| 965 |
+
return model, results
|
| 966 |
+
|
| 967 |
+
def evaluate_model(self, model_path: str = None):
|
| 968 |
+
"""Run full evaluation."""
|
| 969 |
+
if self.trainer is None:
|
| 970 |
+
data_yaml = str(DATASET_DIR / "data.yaml")
|
| 971 |
+
self.trainer = YOLOTrainer(
|
| 972 |
+
data_yaml=data_yaml,
|
| 973 |
+
project_dir=str(RESULTS_DIR),
|
| 974 |
+
)
|
| 975 |
+
return self.trainer.evaluate(model_path)
|
| 976 |
+
|
| 977 |
+
def run_benchmark(self, model_path: str = None):
|
| 978 |
+
"""Run benchmark comparison."""
|
| 979 |
+
if self.trainer is None:
|
| 980 |
+
data_yaml = str(DATASET_DIR / "data.yaml")
|
| 981 |
+
self.trainer = YOLOTrainer(
|
| 982 |
+
data_yaml=data_yaml,
|
| 983 |
+
project_dir=str(RESULTS_DIR),
|
| 984 |
+
)
|
| 985 |
+
self.trainer.benchmark_comparison(model_path)
|
| 986 |
+
|
| 987 |
+
def process_live_frame(self, frame_data: Dict) -> Dict:
|
| 988 |
+
"""
|
| 989 |
+
Process a live frame from the farm cameras.
|
| 990 |
+
Returns real-time status.
|
| 991 |
+
"""
|
| 992 |
+
# Run detection on each camera view
|
| 993 |
+
# This would integrate with live camera feeds
|
| 994 |
+
pass
|
| 995 |
+
|
| 996 |
+
def get_daily_report(self, date: str = None) -> Dict:
|
| 997 |
+
"""Get daily farm summary."""
|
| 998 |
+
if date is None:
|
| 999 |
+
date = datetime.now().strftime("%Y-%m-%d")
|
| 1000 |
+
return self.report_generator.generate_daily_summary(date)
|
| 1001 |
+
|
| 1002 |
+
|
| 1003 |
+
# ============================================================
|
| 1004 |
+
# Entry Point
|
| 1005 |
+
# ============================================================
|
| 1006 |
+
def main():
|
| 1007 |
+
"""Main training and evaluation pipeline."""
|
| 1008 |
+
logger.info("=" * 70)
|
| 1009 |
+
logger.info(" PoultryVision AI - Comprehensive Poultry Farm Monitoring")
|
| 1010 |
+
logger.info(" Pioneering AI for the Poultry Industry")
|
| 1011 |
+
logger.info("=" * 70)
|
| 1012 |
+
|
| 1013 |
+
pipeline = PoultryVisionPipeline()
|
| 1014 |
+
pipeline.setup()
|
| 1015 |
+
|
| 1016 |
+
# Phase 1: Train YOLO model
|
| 1017 |
+
logger.info("\n>>> PHASE 1: Model Training <<<\n")
|
| 1018 |
+
|
| 1019 |
+
# Check GPU availability
|
| 1020 |
+
import torch
|
| 1021 |
+
if torch.cuda.is_available():
|
| 1022 |
+
gpu_name = torch.cuda.get_device_name(0)
|
| 1023 |
+
gpu_mem = torch.cuda.get_device_properties(0).total_mem / (1024**3)
|
| 1024 |
+
logger.info(f"GPU: {gpu_name} ({gpu_mem:.1f} GB)")
|
| 1025 |
+
|
| 1026 |
+
# Adjust imgsz based on GPU memory
|
| 1027 |
+
if gpu_mem >= 40:
|
| 1028 |
+
imgsz = 1920 # Paper setting (A100)
|
| 1029 |
+
elif gpu_mem >= 16:
|
| 1030 |
+
imgsz = 1280
|
| 1031 |
+
elif gpu_mem >= 8:
|
| 1032 |
+
imgsz = 960
|
| 1033 |
+
else:
|
| 1034 |
+
imgsz = 640
|
| 1035 |
+
device = "0"
|
| 1036 |
+
else:
|
| 1037 |
+
logger.info("No GPU detected, using CPU (will be slow)")
|
| 1038 |
+
imgsz = 640
|
| 1039 |
+
device = "cpu"
|
| 1040 |
+
|
| 1041 |
+
logger.info(f"Selected image size: {imgsz}")
|
| 1042 |
+
|
| 1043 |
+
model, results = pipeline.train_model(
|
| 1044 |
+
epochs=200,
|
| 1045 |
+
imgsz=imgsz,
|
| 1046 |
+
device=device,
|
| 1047 |
+
batch=-1, # Auto batch
|
| 1048 |
+
)
|
| 1049 |
+
|
| 1050 |
+
# Phase 2: Evaluate
|
| 1051 |
+
logger.info("\n>>> PHASE 2: Model Evaluation <<<\n")
|
| 1052 |
+
best_model_path = str(RESULTS_DIR / "poultry_vision" / "weights" / "best.pt")
|
| 1053 |
+
pipeline.evaluate_model(best_model_path)
|
| 1054 |
+
|
| 1055 |
+
# Phase 3: Benchmark
|
| 1056 |
+
logger.info("\n>>> PHASE 3: Benchmark Comparison <<<\n")
|
| 1057 |
+
pipeline.run_benchmark(best_model_path)
|
| 1058 |
+
|
| 1059 |
+
logger.info("\n" + "=" * 70)
|
| 1060 |
+
logger.info(" PoultryVision training pipeline complete!")
|
| 1061 |
+
logger.info(f" Best model saved at: {best_model_path}")
|
| 1062 |
+
logger.info(f" Results at: {RESULTS_DIR}")
|
| 1063 |
+
logger.info("=" * 70)
|
| 1064 |
+
|
| 1065 |
+
|
| 1066 |
+
if __name__ == "__main__":
|
| 1067 |
+
main()
|
results.csv
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
epoch,time,train/box_loss,train/cls_loss,train/dfl_loss,metrics/precision(B),metrics/recall(B),metrics/mAP50(B),metrics/mAP50-95(B),val/box_loss,val/cls_loss,val/dfl_loss,lr/pg0,lr/pg1,lr/pg2
|
| 2 |
+
1,1097.3,1.5116,1.25664,1.36915,0.79793,0.75994,0.8056,0.40034,1.57706,1.21091,1.76912,0.000333174,0.000333174,0.0670158
|
| 3 |
+
2,3641.3,1.45996,1.12731,1.34597,0.80253,0.77555,0.84621,0.50205,1.61693,1.16854,1.81129,0.000659909,0.000659909,0.0340092
|
| 4 |
+
3,4941.45,1.40648,1.04879,1.32211,0.86395,0.82149,0.88679,0.63145,1.18797,0.8351,1.48622,0.000980043,0.000980043,0.000996015
|
| 5 |
+
4,7616.5,1.37174,0.98329,1.30071,0.87254,0.85093,0.90497,0.65024,1.20373,0.76027,1.50121,0.0009703,0.0009703,0.0009703
|
| 6 |
+
5,9102.44,1.32432,0.9135,1.27275,0.88031,0.85791,0.91422,0.68246,1.08366,0.74447,1.44896,0.0009604,0.0009604,0.0009604
|
| 7 |
+
6,10142.4,1.2963,0.87188,1.251,0.88831,0.8814,0.93242,0.65472,1.0977,0.63463,1.40664,0.0009505,0.0009505,0.0009505
|
| 8 |
+
7,12968,1.27124,0.84657,1.24489,0.89537,0.87319,0.92722,0.6799,1.05937,0.61219,1.41792,0.0009406,0.0009406,0.0009406
|
| 9 |
+
8,14242.1,1.25244,0.82014,1.23501,0.89354,0.88302,0.93491,0.66736,1.06652,0.63393,1.39677,0.0009307,0.0009307,0.0009307
|
| 10 |
+
9,15303,1.24543,0.80709,1.22629,0.90968,0.88607,0.93836,0.69395,1.03288,0.56413,1.40576,0.0009208,0.0009208,0.0009208
|
| 11 |
+
10,22679.3,1.2151,0.77517,1.21542,0.90065,0.894,0.94338,0.67011,1.03191,0.56209,1.37994,0.0009109,0.0009109,0.0009109
|
| 12 |
+
11,23720.2,1.20637,0.76478,1.2132,0.90917,0.89363,0.94347,0.69142,1.07072,0.5799,1.40064,0.000901,0.000901,0.000901
|
| 13 |
+
12,24759.7,1.20267,0.75202,1.19741,0.90469,0.89752,0.94451,0.66597,1.00649,0.54764,1.35156,0.0008911,0.0008911,0.0008911
|
| 14 |
+
13,31700.7,1.18699,0.7381,1.19809,0.90752,0.90499,0.95041,0.70236,0.99337,0.52987,1.32797,0.0008812,0.0008812,0.0008812
|
| 15 |
+
14,32779.8,1.18423,0.73434,1.19185,0.90986,0.904,0.95035,0.71025,0.97063,0.50587,1.31836,0.0008713,0.0008713,0.0008713
|
| 16 |
+
15,37565.1,1.16164,0.71945,1.18696,0.91729,0.91288,0.95552,0.73783,0.95995,0.48529,1.30503,0.0008614,0.0008614,0.0008614
|
| 17 |
+
16,38645.1,1.16591,0.71275,1.18644,0.9202,0.9078,0.95357,0.72958,0.95405,0.50179,1.31217,0.0008515,0.0008515,0.0008515
|
| 18 |
+
17,44907.8,1.14631,0.69896,1.18266,0.91976,0.91301,0.95807,0.74834,0.94488,0.48085,1.30797,0.0008416,0.0008416,0.0008416
|
| 19 |
+
18,46690.8,1.14532,0.69554,1.1749,0.91468,0.91499,0.95562,0.74378,0.91848,0.48017,1.29002,0.0008317,0.0008317,0.0008317
|
| 20 |
+
19,55106.7,1.14045,0.69331,1.17805,0.91684,0.9139,0.9575,0.74611,0.92426,0.47427,1.28916,0.0008218,0.0008218,0.0008218
|
| 21 |
+
20,56158.6,1.11598,0.66689,1.15662,0.92208,0.91374,0.9595,0.75296,0.90867,0.46367,1.26935,0.0008119,0.0008119,0.0008119
|
| 22 |
+
21,57207.3,1.10921,0.66209,1.15628,0.92139,0.91638,0.95965,0.74902,0.90613,0.45068,1.25905,0.000802,0.000802,0.000802
|
| 23 |
+
22,58245.1,1.10692,0.66624,1.15393,0.92617,0.91827,0.96122,0.75535,0.88215,0.44642,1.24841,0.0007921,0.0007921,0.0007921
|
| 24 |
+
23,59321.5,1.11284,0.65931,1.15406,0.92334,0.91835,0.961,0.75913,0.91356,0.44558,1.2566,0.0007822,0.0007822,0.0007822
|
| 25 |
+
24,65095,1.09916,0.65043,1.15043,0.92253,0.92179,0.96362,0.76088,0.87768,0.43167,1.24954,0.0007723,0.0007723,0.0007723
|
| 26 |
+
25,66570.9,1.09281,0.64562,1.14985,0.9258,0.92348,0.96406,0.76346,0.87138,0.43554,1.2435,0.0007624,0.0007624,0.0007624
|
| 27 |
+
26,67621.7,1.08539,0.63994,1.14541,0.9282,0.9201,0.96343,0.75438,0.88556,0.43451,1.25336,0.0007525,0.0007525,0.0007525
|
| 28 |
+
27,1368,1.07327,0.62887,1.13737,0.92461,0.92384,0.96343,0.7499,0.87258,0.43157,1.24462,0.0007426,0.0007426,0.0007426
|
| 29 |
+
28,8264.09,1.07704,0.62968,1.13965,0.92535,0.92552,0.96517,0.76351,0.86995,0.42493,1.24326,0.0007327,0.0007327,0.0007327
|
| 30 |
+
29,11008.8,1.07805,0.63239,1.14283,0.92634,0.92407,0.96394,0.7638,0.8584,0.42076,1.24271,0.0007228,0.0007228,0.0007228
|
| 31 |
+
30,12678.7,1.05289,0.62005,1.12977,0.92759,0.9231,0.96485,0.77017,0.86245,0.42188,1.23526,0.0007129,0.0007129,0.0007129
|
| 32 |
+
31,14278.4,1.05917,0.61825,1.13173,0.92518,0.92657,0.96512,0.76832,0.86374,0.41904,1.24669,0.000703,0.000703,0.000703
|
| 33 |
+
32,15863.6,1.06259,0.61914,1.13339,0.92723,0.92572,0.96538,0.77167,0.84669,0.41017,1.23241,0.0006931,0.0006931,0.0006931
|
| 34 |
+
33,17487.5,1.05227,0.6056,1.13139,0.92905,0.92674,0.96608,0.77016,0.83635,0.40864,1.21689,0.0006832,0.0006832,0.0006832
|
| 35 |
+
34,19149.1,1.05149,0.60829,1.1273,0.93129,0.92607,0.96695,0.77608,0.83406,0.40516,1.21769,0.0006733,0.0006733,0.0006733
|
| 36 |
+
35,20794,1.04155,0.59854,1.11983,0.93092,0.92526,0.96647,0.77357,0.83305,0.40039,1.21987,0.0006634,0.0006634,0.0006634
|
| 37 |
+
36,22396.9,1.04179,0.59371,1.11949,0.92746,0.93027,0.96753,0.77847,0.83277,0.39905,1.21209,0.0006535,0.0006535,0.0006535
|
| 38 |
+
37,24002.5,1.03872,0.59467,1.12332,0.92987,0.92894,0.96782,0.78178,0.82927,0.39786,1.20729,0.0006436,0.0006436,0.0006436
|
| 39 |
+
38,25582.5,1.02598,0.58628,1.10846,0.93149,0.92741,0.96706,0.78278,0.82279,0.39286,1.20681,0.0006337,0.0006337,0.0006337
|
| 40 |
+
39,27168.9,1.02251,0.58292,1.11283,0.92987,0.92939,0.96744,0.78104,0.82437,0.39022,1.20788,0.0006238,0.0006238,0.0006238
|
| 41 |
+
40,28747.1,1.01813,0.5804,1.10903,0.93146,0.93008,0.96842,0.7802,0.8208,0.38556,1.20507,0.0006139,0.0006139,0.0006139
|
| 42 |
+
41,30316.4,1.00959,0.57397,1.1052,0.93515,0.92801,0.96868,0.78208,0.81476,0.38401,1.19639,0.000604,0.000604,0.000604
|
| 43 |
+
42,31911.2,1.01693,0.57068,1.10214,0.93263,0.9313,0.96871,0.78468,0.81022,0.38408,1.19085,0.0005941,0.0005941,0.0005941
|
| 44 |
+
43,33531.7,1.01729,0.57439,1.10833,0.93431,0.92982,0.96963,0.78472,0.81013,0.38139,1.18936,0.0005842,0.0005842,0.0005842
|
| 45 |
+
44,35101.9,1.00907,0.56846,1.10296,0.93477,0.9306,0.96974,0.78671,0.81019,0.38003,1.19035,0.0005743,0.0005743,0.0005743
|
| 46 |
+
45,36678.2,1.00707,0.56567,1.10015,0.93627,0.92923,0.97004,0.78928,0.80827,0.37777,1.18772,0.0005644,0.0005644,0.0005644
|
| 47 |
+
46,38260.9,0.99212,0.55656,1.0972,0.93607,0.92963,0.97019,0.7917,0.80262,0.37698,1.18166,0.0005545,0.0005545,0.0005545
|
| 48 |
+
47,39833.1,0.99283,0.55637,1.09679,0.93122,0.93446,0.97025,0.79204,0.80144,0.37727,1.18303,0.0005446,0.0005446,0.0005446
|
| 49 |
+
48,41421.6,0.99537,0.55217,1.09489,0.93112,0.9358,0.97029,0.79346,0.79978,0.37635,1.18195,0.0005347,0.0005347,0.0005347
|
| 50 |
+
49,43163.7,0.98463,0.55178,1.09817,0.93318,0.93286,0.97057,0.79447,0.79943,0.37379,1.1807,0.0005248,0.0005248,0.0005248
|
| 51 |
+
50,44967.2,0.99004,0.55233,1.09411,0.93404,0.93217,0.9705,0.79441,0.7984,0.37253,1.17907,0.0005149,0.0005149,0.0005149
|
| 52 |
+
51,48389.7,0.98621,0.54911,1.08645,0.93162,0.93453,0.97058,0.79463,0.79739,0.37226,1.178,0.000505,0.000505,0.000505
|
| 53 |
+
52,1167.66,0.99676,0.57155,1.13067,0.93176,0.93441,0.97041,0.79426,0.79218,0.3761,1.18312,0.0004951,0.0004951,0.0004951
|
| 54 |
+
53,2305.23,0.99979,0.57956,1.13262,0.93235,0.93471,0.97046,0.79403,0.79136,0.37602,1.18209,0.0004852,0.0004852,0.0004852
|
| 55 |
+
54,3439.46,1.00784,0.5875,1.13933,0.93305,0.93435,0.9706,0.79411,0.79013,0.37585,1.18131,0.0004753,0.0004753,0.0004753
|
| 56 |
+
55,4575.86,0.99939,0.57792,1.12948,0.93345,0.93366,0.97063,0.79382,0.78989,0.37593,1.18058,0.0004654,0.0004654,0.0004654
|
| 57 |
+
56,5712.51,0.99967,0.57416,1.1243,0.93417,0.93303,0.97076,0.79352,0.78915,0.37528,1.17933,0.0004555,0.0004555,0.0004555
|
| 58 |
+
57,6847.13,0.99195,0.5701,1.12321,0.93414,0.93308,0.97056,0.79378,0.78789,0.37421,1.17712,0.0004456,0.0004456,0.0004456
|
| 59 |
+
58,7981.44,0.98986,0.56676,1.12495,0.934,0.93295,0.97052,0.79386,0.78758,0.37434,1.17679,0.0004357,0.0004357,0.0004357
|
| 60 |
+
59,9116.14,0.98935,0.56328,1.12711,0.93234,0.93476,0.97074,0.79361,0.78768,0.37364,1.17682,0.0004258,0.0004258,0.0004258
|
| 61 |
+
60,10250.1,0.98552,0.56499,1.12535,0.93195,0.93528,0.97079,0.79361,0.78693,0.37281,1.17621,0.0004159,0.0004159,0.0004159
|
| 62 |
+
61,11384.3,0.97948,0.55868,1.1192,0.93205,0.93494,0.97091,0.79366,0.78661,0.3721,1.17642,0.000406,0.000406,0.000406
|
| 63 |
+
62,12723.7,0.9769,0.55682,1.12198,0.93307,0.93436,0.97094,0.79341,0.78619,0.37175,1.17647,0.0003961,0.0003961,0.0003961
|
| 64 |
+
63,1362.39,0.91471,0.50739,1.08977,0.93236,0.93557,0.97101,0.79336,0.78635,0.37178,1.17691,0.000123143,0.000123143,0.000123143
|
| 65 |
+
64,2671.62,0.917,0.51054,1.08896,0.93413,0.93399,0.97102,0.79329,0.78652,0.37179,1.17725,0.000109,0.000109,0.000109
|
| 66 |
+
65,4334.99,0.92642,0.51904,1.09613,0.93359,0.93448,0.97106,0.79336,0.78647,0.37199,1.1771,9.48571e-05,9.48571e-05,9.48571e-05
|
| 67 |
+
66,6392.03,0.92112,0.5123,1.09142,0.93361,0.93447,0.97107,0.79321,0.78676,0.37207,1.17739,8.07143e-05,8.07143e-05,8.07143e-05
|
| 68 |
+
67,7878.64,0.92301,0.51185,1.08902,0.93322,0.93483,0.97112,0.79335,0.78665,0.37196,1.17729,6.65714e-05,6.65714e-05,6.65714e-05
|
| 69 |
+
68,9074.68,0.92092,0.51101,1.0895,0.93346,0.93445,0.97107,0.79336,0.7865,0.37184,1.17714,5.24286e-05,5.24286e-05,5.24286e-05
|
| 70 |
+
69,10623.1,0.92111,0.5102,1.09136,0.93347,0.93443,0.97106,0.79324,0.78646,0.37169,1.17723,3.82857e-05,3.82857e-05,3.82857e-05
|
| 71 |
+
70,11766,0.92614,0.511,1.09693,0.93386,0.93421,0.97106,0.79341,0.78647,0.37179,1.17734,2.41429e-05,2.41429e-05,2.41429e-05
|
results.png
ADDED
|
Git LFS Details
|
val_batch0_pred.jpg
ADDED
|
Git LFS Details
|
val_batch1_pred.jpg
ADDED
|
Git LFS Details
|
val_batch2_pred.jpg
ADDED
|
Git LFS Details
|