[目标检测] 如何获取数据集对应的anchor size

背景:由于yolov5需要根据size生成候选anchor,因此用yolov5训练某个特定的数据集时,如果没有设置对应的anchor size。那么可能出现很多乱框的现象。

可以通过.pt文件就能读取到anchor size的参数。输入是.pt文件,输出是

python 复制代码
const int anchor0[6] = {3, 5, 6, 5, 5, 11};
const int anchor1[6] = {10, 9, 10, 17, 21, 11};
const int anchor2[6] = {20, 25, 39, 21, 47, 47};
python 复制代码
import torch
from models.experimental import attempt_load

model = attempt_load('/root/yolov5-master/runs/train/exp8/weights/best.pt')
m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1]

# Convert to CPU for easier manipulation
for i, grid in enumerate(m.anchor_grid):
    grid = grid.cpu()  # Move to CPU if on GPU
    anchor_sets = []

    # Traverse the second dimension (3 anchor sets)
    for j in range(3):  # Since the second dimension is of size 3
        # Extract the first two 'anchors' from each set (assuming we only want the first two for each set)
        anchor_pair = grid[0, j, 0, 0].tolist()  # [0, 0] for spatial dimensions, assuming we want the first anchor
        anchor_sets.append([round(x) for x in anchor_pair])

    # Flatten and format for C-style declaration
    flat_anchors = [item for pair in anchor_sets for item in pair]
    print(f"const int anchor{i}[6] = {{{', '.join(map(str, flat_anchors))}}};")
相关推荐
HyperAI超神经几秒前
在线教程丨端侧TTS新SOTA!NeuTTS-Air基于0.5B模型实现3秒音频克隆
人工智能·深度学习·机器学习·音视频·tts·音频克隆·neutts-air
wwwzhouhui4 分钟前
2025年11月1日-AI 驱动教学革命:3 分钟生成专业级动画课件,还能导出视频 GIF!
人工智能·音视频·ai动画教学
国科安芯9 分钟前
抗辐照MCU芯片在无人叉车领域的性能评估与选型建议
网络·人工智能·单片机·嵌入式硬件·安全
用户51914958484519 分钟前
原型污染攻击工具揭秘:Prototype Pollution Gadgets Finder
人工智能·aigc
躺平的赶海人24 分钟前
Halcon实战:精准定位与提取:基于形态学处理的猴子眼睛区域检测完整方案
图像处理·计算机视觉·halcon·形态学操作
VXHAruanjian88827 分钟前
以智促效,释放创新力量,RPA助力企业全面自动化变革
大数据·人工智能
Godspeed Zhao27 分钟前
自动驾驶中的传感器技术76——Navigation(13)
人工智能·机器学习·自动驾驶
CoovallyAIHub28 分钟前
首届AI交易大赛对决!中国模型包揽冠亚军,GPT-5亏损62%垫底
人工智能·google·数据分析
王中阳Go29 分钟前
5 - 工具调用 - AI 超级智能体项目教程
人工智能
智塑未来33 分钟前
广州全运会即将开幕,获得文远知行自动驾驶技术支持
人工智能·机器学习·自动驾驶