1. Python的调用
from ultralytics import YOLO
import os
def detect_predict():
model = YOLO('../weights/yolo11n.pt')
print(model)
results = model('../ultralytics/assets/bus.jpg')
if not os.path.exists(results[0].save_dir):
os.makedirs(results[0].save_dir)
for result in results:
filename = result.path.split("\\")[-1]
filedir = result.save_dir + "\\" + filename
result.save(filedir)
运行结果:

模型训练,基于coco8数据:
from ultralytics import YOLO
def detect_train():
model = YOLO("yolo11l.pt") # Load a model
train_results = model.train( # Train the model
data="coco8.yaml", # path to dataset YAML
epochs=100, # number of training epochs
imgsz=640, # training image size
device="0", # device to run on, i.e. device=0 or device=0,1,2,3 or device=cpu
)
metrics = model.val() # Evaluate model performance on the validation set
results = model("../ultralytics/assets/bus.jpg") # Perform object detection on an image
results[0].show()
# Export the model to ONNX format
path = model.export(format="onnx") # return path to exported model
2. 网络结构图

图2-1 yolo11-detection网络结构图
其中depth参数控制C3k2,即C3k2_X中,X*depth.
3. 损失函数
3.1 损失函数的定位
ultralytics中损失函数定位:ultralytics.engine.train()-->ultralytics.engine.trainer.train()--> ultralytics.engine.trainer._do_train(),其中以下为调用损失位置:

即模型的前向推理过程触发损失函数计算。同debug到该处,获取模型名称如下:

DetectionModel类中的函数如下:

其中DetectionModel函数继承自BaseModel,损失调用如下

经debug调用self.loss函数,其中ultralytics.utls.loss.v8DetectionLoss函数是具体损失计算位置

self.criterion(preds, batch)形式调用,即v8DetectionLoss类中的__call__函数,具体如下

3.2 损失函数具体分析
(1) 前向推理与anchor构造
前向处理分成三个尺度:
,其中B表示batch_size,

17万+

被折叠的 条评论
为什么被折叠?



