最新推送

This commit is contained in:
琉璃月光
2026-03-10 13:58:21 +08:00
parent 032479f558
commit eb16eeada3
97 changed files with 16865 additions and 670 deletions

View File

@ -0,0 +1,30 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLO11-cls image classification model. For Usage examples see https://docs.ultralytics.com/tasks/classify
# Parameters
nc: 3 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n-cls.yaml' will call yolo11-cls.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.50, 0.25, 1024] # summary: 151 layers, 1633584 parameters, 1633584 gradients, 3.3 GFLOPs
s: [0.50, 0.50, 1024] # summary: 151 layers, 5545488 parameters, 5545488 gradients, 12.2 GFLOPs
m: [0.50, 1.00, 512] # summary: 187 layers, 10455696 parameters, 10455696 gradients, 39.7 GFLOPs
l: [1.00, 1.00, 512] # summary: 309 layers, 12937104 parameters, 12937104 gradients, 49.9 GFLOPs
x: [1.00, 1.50, 512] # summary: 309 layers, 28458544 parameters, 28458544 gradients, 111.1 GFLOPs
# YOLO11n backbone
backbone:
# [from, repeats, module, args]
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
- [-1, 2, C3k2, [256, False, 0.25]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 2, C3k2, [512, False, 0.25]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 2, C3k2, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 2, C3k2, [1024, True]]
- [-1, 2, C2PSA, [1024]] # 9
# YOLO11n head
head:
- [-1, 1, Classify, [nc]] # Classify

View File

@ -0,0 +1,18 @@
from ultralytics import YOLO
if __name__ == '__main__':
model = YOLO(r'3cls.yaml')
results = model.train(
data='/home/dy/dataset/muju',
epochs=300,
imgsz=640,
batch=4,
workers=5,
device='0',
project='runs/train/muju_cls',
name='exp_cls',
exist_ok=False,
optimizer='AdamW',
lr0=0.0005,
patience=0,
)

View File

@ -0,0 +1,73 @@
import cv2
from ultralytics import YOLO
# ---------------------------
# 配置路径(请按需修改)
# ---------------------------
MODEL_PATH = "muju.pt"
IMAGE_PATH = "./test_img/class0.png" # 替换为你要测试的单张图片路径
# 类别映射:必须与训练时 data.yaml 的 names 顺序一致
CLASS_NAMES = {
0: "模具车非f块",
1: "模具车f块",
2: "有遮挡"
}
# ---------------------------
# 单张图片推理函数
# ---------------------------
def classify_single_image(model_path, image_path, class_names):
# 加载模型
model = YOLO(model_path)
print(f"模型加载成功: {model_path}")
# 读取图像
img = cv2.imread(image_path)
if img is None:
print(f"无法读取图像: {image_path}")
return
print(f"📷 正在推理: {image_path}")
# 推理
results = model(img)
probs = results[0].probs.data.cpu().numpy()
pred_class_id = int(probs.argmax())
pred_label = class_names[pred_class_id]
confidence = float(probs[pred_class_id])
# 输出结果
print("\n" + "=" * 40)
print(f"🔍 预测结果:")
print(f" 类别: {pred_label}")
print(f" 置信度: {confidence:.4f}")
print(f" 类别ID: {pred_class_id}")
print("=" * 40)
# (可选)在图像上显示结果并保存/显示
# 这里我们只打印,不保存。如需可视化,取消下面注释:
"""
label_text = f"{pred_label} ({confidence:.2f})"
cv2.putText(img, label_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2)
cv2.imshow("Result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 或保存带标签的图
# output_img_path = image_path.replace(".jpg", "_result.jpg")
# cv2.imwrite(output_img_path, img)
# print(f"带标签图像已保存: {output_img_path}")
"""
# ---------------------------
# 运行入口
# ---------------------------
if __name__ == "__main__":
classify_single_image(
model_path=MODEL_PATH,
image_path=IMAGE_PATH,
class_names=CLASS_NAMES
)

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 703 KiB

View File

@ -0,0 +1,18 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license
# DOTA8 dataset 8 images from split DOTAv1 dataset by Ultralytics
# Documentation: https://docs.ultralytics.com/datasets/obb/dota8/
# Example usage: yolo train model=yolov8n-obb.pt data=dota8.yaml
# parent
# ├── ultralytics
# └── datasets
# └── dota8 ← downloads here (1MB)
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ./dataset
train: train # 数据集路径下的train.txt
val: val # 数据集路径下的val.txt
test: test # 数据集路径下的test.txt
nc: 1
names: ['clamp']

View File

@ -0,0 +1,42 @@
from ultralytics import YOLO
if __name__ == '__main__':
# ✅ 推荐:加载官方预训练模型 或 自己的 best.pt
model = YOLO(r'yolo11-obb.yaml') #
results = model.train(
data='obb_data.yaml',
epochs=500, # 减少 epochs配合早停
patience=0, # 50 轮无提升则停止
imgsz=640,
batch=4,
workers=10,
device='0',
project='runs/train',
name='exp_obb5', # 建议递增实验编号
exist_ok=False,
# 优化器
optimizer='AdamW',
lr0=0.0005, # 更稳定的学习率
weight_decay=0.01,
momentum=0.937,
# 数据增强OBB 关键)
degrees=5.0, # 随机旋转 ±10°
translate=0.1, # 平移
scale=0.5, # 缩放比例
shear=1.0, # 剪切
flipud=0.0, # 不推荐上下翻转(角度易错)
fliplr=0.5, # ✅ 水平翻转OBB 支持良好
hsv_h=0.015, # 色调扰动
hsv_s=0.7, # 饱和度
hsv_v=0.4, # 明度
# 其他
close_mosaic=50, # 最后10轮关闭 Mosaic 增强,提升稳定性
val=True, # 每轮验证
#
amp=False,
)

View File

@ -0,0 +1,47 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLO11 Oriented Bounding Boxes (OBB) model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/obb
# Parameters
nc: 1 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n-obb.yaml' will call yolo11-obb.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.50, 0.25, 1024] # summary: 344 layers, 2695747 parameters, 2695731 gradients, 6.9 GFLOPs
s: [0.50, 0.50, 1024] # summary: 344 layers, 9744931 parameters, 9744915 gradients, 22.7 GFLOPs
m: [0.50, 1.00, 512] # summary: 434 layers, 20963523 parameters, 20963507 gradients, 72.2 GFLOPs
l: [1.00, 1.00, 512] # summary: 656 layers, 26220995 parameters, 26220979 gradients, 91.3 GFLOPs
x: [1.00, 1.50, 512] # summary: 656 layers, 58875331 parameters, 58875315 gradients, 204.3 GFLOPs
# YOLO11n backbone
backbone:
# [from, repeats, module, args]
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
- [-1, 2, C3k2, [256, False, 0.25]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 2, C3k2, [512, False, 0.25]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 2, C3k2, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 2, C3k2, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
- [-1, 2, C2PSA, [1024]] # 10
# YOLO11n head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 2, C3k2, [512, False]] # 13
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 2, C3k2, [256, False]] # 16 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 13], 1, Concat, [1]] # cat head P4
- [-1, 2, C3k2, [512, False]] # 19 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 10], 1, Concat, [1]] # cat head P5
- [-1, 2, C3k2, [1024, True]] # 22 (P5/32-large)
- [[16, 19, 22], 1, OBB, [nc, 1]] # Detect(P3, P4, P5)