Spaces:
Sleeping
Sleeping
Fadhili Sumaye commited on
Commit ·
e70cef4
1
Parent(s): 837b205
Optimize training parameters and upgrade model to YOLOv8m in train_model.py
Browse files- train_model.py +16 -7
train_model.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
from ultralytics import YOLO
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# 2. IMPORTANT: Path to the 'data.yaml' file inside the unzipped dataset.
|
| 8 |
dataset_yaml_path = r'c:\Users\fadhi\StudioProjects\pestDetection\datasets\cereal_pests\data.yaml'
|
|
@@ -15,15 +17,22 @@ def start_training():
|
|
| 15 |
|
| 16 |
print("--- Starting AI Training for Cereal Pests ---")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Train the model
|
| 19 |
# epochs=50: The AI will study the images 50 times.
|
| 20 |
# imgsz=640: Standard resolution for YOLOv8.
|
| 21 |
-
# device='cpu': Uses your computer processor (change to '0' if you have an NVIDIA GPU).
|
| 22 |
results = model.train(
|
| 23 |
data=dataset_yaml_path,
|
| 24 |
-
epochs=
|
| 25 |
imgsz=640,
|
| 26 |
-
device=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
print("\nSUCCESS!")
|
|
@@ -38,7 +47,7 @@ def start_training():
|
|
| 38 |
best_model_path = save_dir / "weights" / "best.pt"
|
| 39 |
|
| 40 |
backend_dir = Path(__file__).resolve().parent / "backend"
|
| 41 |
-
backend_model_path = backend_dir / "
|
| 42 |
|
| 43 |
if best_model_path.exists():
|
| 44 |
backend_dir.mkdir(parents=True, exist_ok=True)
|
|
@@ -48,7 +57,7 @@ def start_training():
|
|
| 48 |
print(f"\nWarning: Could not locate best.pt at {best_model_path}")
|
| 49 |
except Exception as e:
|
| 50 |
print(f"\nWarning: Failed to auto-copy trained model to backend: {e}")
|
| 51 |
-
print("Please copy the best.pt file manually
|
| 52 |
|
| 53 |
print("\nNext step: Start your backend server and run the mobile app.")
|
| 54 |
|
|
|
|
| 1 |
from ultralytics import YOLO
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# 1. Load the base YOLOv8 Medium model (better capacity for feature learning)
|
| 7 |
+
model = YOLO('yolov8m.pt')
|
| 8 |
|
| 9 |
# 2. IMPORTANT: Path to the 'data.yaml' file inside the unzipped dataset.
|
| 10 |
dataset_yaml_path = r'c:\Users\fadhi\StudioProjects\pestDetection\datasets\cereal_pests\data.yaml'
|
|
|
|
| 17 |
|
| 18 |
print("--- Starting AI Training for Cereal Pests ---")
|
| 19 |
|
| 20 |
+
# Automatically detect if NVIDIA GPU (CUDA) is available for 10x-50x faster training
|
| 21 |
+
device = 0 if torch.cuda.is_available() else 'cpu'
|
| 22 |
+
print(f"Using device: {device} ({'GPU' if device == 0 else 'CPU'})")
|
| 23 |
+
|
| 24 |
# Train the model
|
| 25 |
# epochs=50: The AI will study the images 50 times.
|
| 26 |
# imgsz=640: Standard resolution for YOLOv8.
|
|
|
|
| 27 |
results = model.train(
|
| 28 |
data=dataset_yaml_path,
|
| 29 |
+
epochs=50,
|
| 30 |
imgsz=640,
|
| 31 |
+
device=device,
|
| 32 |
+
batch=16, # Stable batch size (reduce to 8 or 4 if GPU runs out of memory)
|
| 33 |
+
freeze=10, # Freeze backbone layers to prevent overfitting on small datasets
|
| 34 |
+
weight_decay=0.005, # Weight decay (L2 regularization) to improve generalization
|
| 35 |
+
close_mosaic=10 # Turn off mosaic augmentation for the last 10 epochs for stable bounding boxes
|
| 36 |
)
|
| 37 |
|
| 38 |
print("\nSUCCESS!")
|
|
|
|
| 47 |
best_model_path = save_dir / "weights" / "best.pt"
|
| 48 |
|
| 49 |
backend_dir = Path(__file__).resolve().parent / "backend"
|
| 50 |
+
backend_model_path = backend_dir / "best_cereal.pt"
|
| 51 |
|
| 52 |
if best_model_path.exists():
|
| 53 |
backend_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 57 |
print(f"\nWarning: Could not locate best.pt at {best_model_path}")
|
| 58 |
except Exception as e:
|
| 59 |
print(f"\nWarning: Failed to auto-copy trained model to backend: {e}")
|
| 60 |
+
print("Please copy the best.pt file manually as best_cereal.pt in the backend/ folder.")
|
| 61 |
|
| 62 |
print("\nNext step: Start your backend server and run the mobile app.")
|
| 63 |
|