train.py 748 B

123456789101112131415161718192021
  1. from ultralytics import YOLO
  2. # Load a pretrained YOLO11n model
  3. model = YOLO("yolo11s.pt")
  4. # Train the model on the COCO8 dataset for 100 epochs
  5. train_results = model.train(
  6. data="coco8.yaml", # Path to dataset configuration file
  7. epochs=100, # Number of training epochs
  8. imgsz=640, # Image size for training
  9. device="cuda", # Device to run on (e.g., 'cpu', 0, [0,1,2,3])
  10. )
  11. # Evaluate the model's performance on the validation set
  12. metrics = model.val()
  13. # Perform object detection on an image
  14. results = model("../resources/first_frame.png") # Predict on an image
  15. results[0].show() # Display results
  16. # Export the model to ONNX format for deployment
  17. # path = model.export(format="onnx") # Returns the path to the exported model