Spaces:
Running
Running
File size: 1,979 Bytes
2012550 4eed331 |
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 |
from ultralytics import YOLO
from roboflow import Roboflow
import logging
from pathlib import Path
import os
from dotenv import load_dotenv
class ModelTrainer:
def __init__(self, roboflow_api_key):
self.rf = Roboflow(api_key=roboflow_api_key)
self.setup_logging()
def setup_logging(self):
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
self.logger = logging.getLogger(__name__)
def download_dataset(self, project_name, project_version, workspace):
self.logger.info(f"Downloading dataset: {project_name} v{project_version}")
project = self.rf.workspace(workspace).project(project_name)
dataset = project.version(project_version).download("yolov8", "data\\raw")
print(dataset.location)
return dataset.location
def train(
self,
data_yaml,
model_type = "yolov8n.pt",
epochs = 30,
imgsz = 640,
batch_size = 16,
device = "0"
):
self.logger.info("Starting training process")
model = YOLO(model_type)
results = model.train(
data = data_yaml,
epochs = epochs,
imgsz = imgsz,
batch = batch_size,
device = device,
project = "runs/train",
name = "face_detection"
)
self.logger.info("Training Complete")
return results
def main():
load_dotenv()
trainer = ModelTrainer(os.getenv("ROBOFLOW_API_KEY"))
data_path = trainer.download_dataset(
workspace="large-benchmark-datasets",
project_name="wider-face-ndtcz",
project_version=1
)
trainer.train(
data_yaml=f"{data_path}\\data.yaml",
epochs=20,
batch_size=16,
device="0"
)
if __name__ == "__main__":
main() |