File size: 1,393 Bytes
7b98a3a 7873ee1 7b98a3a 7873ee1 7b98a3a 7873ee1 7b98a3a 7873ee1 7b98a3a 7873ee1 7b98a3a 7873ee1 7b98a3a 7873ee1 7b98a3a 7873ee1 7b98a3a 7873ee1 7b98a3a | 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 |
from ultralytics import YOLO
from datetime import datetime
import os
import shutil
def train_yolo_model(data_path, model='yolov9s.pt', epochs=10, batch_size=8, imgsz=320):
"""
Train YOLOv9s model on Raspberry Pi 5 with light settings and CSV export.
"""
model = YOLO(model)
results = model.train(
data=data_path,
epochs=epochs,
batch=batch_size,
imgsz=imgsz,
device='cpu',
workers=2,
cache=True, # PS: como True consome RAM, mas acelera. Desligar se travar
amp=False,
patience=5,
save_period=5,
plots=False,
verbose=True
)
save_results_csv()
return results
def save_results_csv():
now = datetime.now().strftime("%Y%m%d_%H%M%S")
source = "runs/detect/train/results.csv"
target_dir = "training_logs"
target = f"{target_dir}/yolov9s_{now}.csv"
os.makedirs(target_dir, exist_ok=True)
if os.path.exists(source):
shutil.copy(source, target)
print(f"✅ Training metrics saved to: {target}")
else:
print("⚠️ results.csv not found. Was training successful?")
if __name__ == "__main__":
data_path = "fasdd.yaml"
train_yolo_model(
data_path=data_path,
model='yolov9s.pt',
epochs=10,
batch_size=8, # Adjust if it crashes
imgsz=320 # Test 416 later
)
|