from ultralytics import YOLO
import multiprocessing
if __name__ =='__main__':# 添加 Windows 多进程支持
multiprocessing.freeze_support()# Load a model
model = YOLO("yolo11n.pt")# load a pretrained model (recommended for training)# Train the model with MPS
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)# 量化
results = model.val(half=True)# evaluate model performance on the validation set# Run inference with the model# results = model("datasets/coco8/images/val/000000000061.jpg") # predict on an image# result.save(filename="result.jpg")# 另存为模型
model.save("best.pt")
验证识别
from ultralytics import YOLO
# Load a model
model = YOLO("best.pt")# pretrained YOLO11n model# Run batched inference on a list of images
results = model(["datasets/coco8/images/train/000000000034.jpg"])# return a list of Results objects# Process results listfor result in results:
boxes = result.boxes # Boxes object for bounding box outputs
masks = result.masks # Masks object for segmentation masks outputs
keypoints = result.keypoints # Keypoints object for pose outputs
probs = result.probs # Probs object for classification outputs
obb = result.obb # Oriented boxes object for OBB outputs
result.show()# display to screen
result.save(filename="result.jpg")# save to disk