[目标检测] 如何获取数据集对应的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))}}};")
相关推荐
Niuguangshuo11 分钟前
深度学习基本模块:Conv2D 二维卷积层
人工智能·深度学习
b***251114 分钟前
深圳比斯特|多维度分选:圆柱电池品质管控的自动化解决方案
大数据·人工智能
金井PRATHAMA28 分钟前
AI赋能训诂学:解码古籍智能新纪元
人工智能·自然语言处理·知识图谱
练习两年半的工程师29 分钟前
AWS TechFest 2025: 智能体企业级开发流程、Strands Agents
人工智能·云计算·aws
Hello123网站41 分钟前
Whismer-你的定制化AI问答助手
人工智能·chatgpt·ai工具
yinmaisoft1 小时前
当低代码遇上AI,有趣,实在有趣
android·人工智能·低代码·开发工具·rxjava
正经教主1 小时前
【慢教程】Ollama4:ollama命令汇总
人工智能·ollama
大翻哥哥1 小时前
Python 2025:AI工程化与智能代理开发实战
开发语言·人工智能·python
中杯可乐多加冰1 小时前
深度解析文心大模型X1.1:智能涌现与技术革新
人工智能
用户5191495848452 小时前
揭秘LedgerCTF的AES白盒挑战:逆向工程与密码学分析
人工智能·aigc