-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick_precision_boost.py
More file actions
104 lines (84 loc) Β· 3.55 KB
/
Copy pathquick_precision_boost.py
File metadata and controls
104 lines (84 loc) Β· 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Quick Precision Boost Training Script
Uses current best model to quickly improve precision above 95%
"""
import os
import sys
from ultralytics import YOLO
import torch
def quick_precision_training():
"""Quick training to boost precision"""
print("π QUICK PRECISION BOOST TRAINING")
print("=" * 50)
# Check for best model
best_model_path = "runs/detect/train/weights/best.pt"
if not os.path.exists(best_model_path):
print("β Best model not found. Please run regular training first.")
return
print(f"β
Using best model: {best_model_path}")
# Load model
model = YOLO(best_model_path)
print("π― Starting precision-focused fine-tuning...")
# Quick precision training with conservative settings
results = model.train(
data="yolo_params.yaml",
epochs=20, # Quick training
imgsz=1024, # Larger image size for precision
batch=4, # Smaller batch for stability
# Very conservative learning rate for precision
lr0=0.0001,
lrf=0.001,
momentum=0.95,
weight_decay=0.002,
# Minimal augmentation for precision
mosaic=0.0, # Disable mosaic
mixup=0.0, # Disable mixup
copy_paste=0.0, # Disable copy-paste
hsv_h=0.005, # Minimal hue
hsv_s=0.3, # Minimal saturation
hsv_v=0.2, # Minimal value
degrees=1.0, # Minimal rotation
translate=0.02, # Minimal translation
scale=0.95, # Minimal scale
shear=0.0, # No shear
perspective=0.0, # No perspective
flipud=0.0, # No vertical flip
fliplr=0.5, # Keep horizontal flip
# Precision-focused loss weights
box=10.0, # High box loss for precise localization
cls=0.3, # Lower class loss
dfl=1.5, # Standard DFL loss
# Training settings
patience=15,
save_period=5,
optimizer='AdamW',
cos_lr=True,
# Project settings
project='runs/train',
name='precision_boost',
resume=False, # Don't resume, start fresh
# Additional precision settings
conf=0.25, # Lower confidence threshold during training
iou=0.5, # Standard IoU threshold
max_det=300 # Maximum detections
)
print(f"β
Training completed!")
print(f"π Results saved at: {results.save_dir}")
print(f"π Best model: {results.save_dir}/weights/best.pt")
# Quick evaluation
print("\nπ Quick evaluation...")
eval_results = model.val(data="yolo_params.yaml", split='test')
print(f"π Final Metrics:")
print(f" Precision: {eval_results.box.mp:.4f} ({eval_results.box.mp*100:.2f}%)")
print(f" Recall: {eval_results.box.mr:.4f} ({eval_results.box.mr*100:.2f}%)")
print(f" mAP@0.5: {eval_results.box.map50:.4f} ({eval_results.box.map50*100:.2f}%)")
print(f" mAP@0.5-0.95: {eval_results.box.map:.4f} ({eval_results.box.map*100:.2f}%)")
# Precision assessment
if eval_results.box.mp > 0.95:
print("π EXCELLENT! Precision > 95% achieved!")
elif eval_results.box.mp > 0.90:
print("β
GOOD! Precision > 90% achieved!")
else:
print("β οΈ Precision needs improvement. Consider longer training.")
if __name__ == "__main__":
quick_precision_training()