Build and Push / build (push) Successful in 6m17s
- /api/detection/object:YOLO11n COCO 80 类,返回 class/confidence/box,按类内 NMS - /api/classify/object:YOLO11n-cls ImageNet 1000 类,softmax 取 top-5 - 两个模型不入镜像(models/ 挂载卷),启动时存在即加载,缺失只影响对应接口 - 类型展示目录补两条能力 + 两个模型(2.62M / 2.81M 参数) - 试用页补字段映射
43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
"""在 anticaptcha 容器里导出「目标检测(COCO 80类)」与「整图分类(ImageNet 1000类)」的 ONNX。
|
||
|
||
产出到 /app/models/:
|
||
Detection_Object.onnx yolo11n COCO 检测,输入 1x3x640x640,输出 [1,84,8400]
|
||
object_labels.txt 80 行类别名
|
||
Classification_Object.onnx yolo11n-cls,输入 1x3x224x224,输出 [1,1000]
|
||
imagenet_labels.txt 1000 行类别名
|
||
"""
|
||
import os
|
||
import shutil
|
||
from pathlib import Path
|
||
|
||
from ultralytics import YOLO
|
||
|
||
MODELS = Path("/app/models")
|
||
WORK = Path("/tmp/export")
|
||
WORK.mkdir(parents=True, exist_ok=True)
|
||
os.chdir(WORK)
|
||
|
||
|
||
def export(weights: str, imgsz: int, target: str, labels_path: Path):
|
||
print(f"\n=== {weights} → {target} ===", flush=True)
|
||
m = YOLO(weights) # 权重不存在会自动下载
|
||
names = m.names # dict[int, str]
|
||
# 写标签表(按 index 排序)
|
||
lines = [names[i] for i in sorted(names)]
|
||
labels_path.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||
print(f"标签 {len(lines)} 类 → {labels_path}", flush=True)
|
||
|
||
out = m.export(format="onnx", imgsz=imgsz, opset=12, simplify=False, dynamic=False)
|
||
out = Path(str(out))
|
||
dst = MODELS / target
|
||
shutil.copyfile(out, dst)
|
||
print(f"ONNX → {dst} ({dst.stat().st_size/1e6:.2f} MB)", flush=True)
|
||
|
||
|
||
export("yolo11n.pt", 640, "Detection_Object.onnx", MODELS / "object_labels.txt")
|
||
export("yolo11n-cls.pt", 224, "Classification_Object.onnx", MODELS / "imagenet_labels.txt")
|
||
|
||
print("\n完成,/app/models 内容:")
|
||
for f in sorted(MODELS.iterdir()):
|
||
print(f" {f.name} {f.stat().st_size/1e6:.2f} MB")
|