yolo_anchors.txt超参数值设置

article/2025/11/5 14:49:02

keras-yolov3在训练自定义图片集之前,设置合理的yolo_anchors.txt值,有利于模型训练的收敛,一般都带有默认的参数如下:

 在实际项目中,yolo_anchors的值可以根据kmeans.py计算获取,通过聚类得到最佳anchors数据;

kmeans.py执行后即可得到合适的yolo_anchors参数值,代码如下:

import numpy as npclass YOLO_Kmeans:def __init__(self, cluster_number, filename):self.cluster_number = cluster_numberself.filename = "2007_train.txt"def iou(self, boxes, clusters):  # 1 box -> k clustersn = boxes.shape[0]k = self.cluster_numberbox_area = boxes[:, 0] * boxes[:, 1]box_area = box_area.repeat(k)box_area = np.reshape(box_area, (n, k))cluster_area = clusters[:, 0] * clusters[:, 1]cluster_area = np.tile(cluster_area, [1, n])cluster_area = np.reshape(cluster_area, (n, k))box_w_matrix = np.reshape(boxes[:, 0].repeat(k), (n, k))cluster_w_matrix = np.reshape(np.tile(clusters[:, 0], (1, n)), (n, k))min_w_matrix = np.minimum(cluster_w_matrix, box_w_matrix)box_h_matrix = np.reshape(boxes[:, 1].repeat(k), (n, k))cluster_h_matrix = np.reshape(np.tile(clusters[:, 1], (1, n)), (n, k))min_h_matrix = np.minimum(cluster_h_matrix, box_h_matrix)inter_area = np.multiply(min_w_matrix, min_h_matrix)result = inter_area / (box_area + cluster_area - inter_area)return resultdef avg_iou(self, boxes, clusters):accuracy = np.mean([np.max(self.iou(boxes, clusters), axis=1)])return accuracydef kmeans(self, boxes, k, dist=np.median):box_number = boxes.shape[0]distances = np.empty((box_number, k))last_nearest = np.zeros((box_number,))np.random.seed()clusters = boxes[np.random.choice(box_number, k, replace=False)]  # init k clusterswhile True:distances = 1 - self.iou(boxes, clusters)current_nearest = np.argmin(distances, axis=1)if (last_nearest == current_nearest).all():break  # clusters won't changefor cluster in range(k):clusters[cluster] = dist(  # update clustersboxes[current_nearest == cluster], axis=0)last_nearest = current_nearestreturn clustersdef result2txt(self, data):f = open("yolo_anchors.txt", 'w')row = np.shape(data)[0]for i in range(row):if i == 0:x_y = "%d,%d" % (data[i][0], data[i][1])else:x_y = ", %d,%d" % (data[i][0], data[i][1])f.write(x_y)f.close()def txt2boxes(self):f = open(self.filename, 'r')dataSet = []for line in f:infos = line.split(" ")length = len(infos)for i in range(1, length):width = int(infos[i].split(",")[2]) - \int(infos[i].split(",")[0])height = int(infos[i].split(",")[3]) - \int(infos[i].split(",")[1])dataSet.append([width, height])result = np.array(dataSet)f.close()return resultdef txt2clusters(self):all_boxes = self.txt2boxes()result = self.kmeans(all_boxes, k=self.cluster_number)result = result[np.lexsort(result.T[0, None])]self.result2txt(result)print("K anchors:\n {}".format(result))print("Accuracy: {:.2f}%".format(self.avg_iou(all_boxes, result) * 100))if __name__ == "__main__":cluster_number = 16             # 类别数量filename = "2007_train.txt"kmeans = YOLO_Kmeans(cluster_number, filename)kmeans.txt2clusters()

替换原有默认的yolo_anchors.txt文件,再进行模型训练。

注意:执行kmeans.py的时机,需要在模型训练之前,模型训练之前需要先写入新的anchors的值,后续博客再做yolov3全流程开发补充。


http://chatgpt.dhexx.cn/article/bxrZ5XTe.shtml

相关文章

QML之anchors锚布局

QML的布局方式一般采用两种,一种就是直接设置,X与Y坐标的值。一种是采用相对位置布局,anchors锚布局。使用锚布局的能够使界面更紧凑,更有整体化。我个人相对比较喜欢使用锚布局,选取一个最合适的参考点,其…

YOLOV5源代码学习之check_anchors()函数

该函数主要在train.py中调用 为了方便直观的阅读代码,对代码中的变量值进行了输出 def check_anchors(dataset, model, thr4.0, imgsz640):# Check anchor fit to data, recompute if necessaryprefix colorstr(autoanchor: )print(f\n{prefix}Analyzing anchors.…

QML入门教程(3): anchors的用法

QML的布局方法是anchors,锚。 例如界面上四个按钮,可以用绝对坐标 (x, y) 设置他们的位置, 代码如下: import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5Window {visible: truewidth: 320height: 480title: qsTr("Hello World")co…

目标检测2——借Detectron2源码理解Anchors与RPN

文章目录 1.Regionn Proposal Network背景2.Regionn Proposal Network的结构3.Anchors4.Regionn Proposal Network的训练参考资料 欢迎访问个人网络日志🌹🌹知行空间🌹🌹 1.Regionn Proposal Network背景 RPN,Region …

QML基础:锚anchors

正文 除了传统的Grid、Row和Column外,Qt Quick还提供了一种使用锚点的方法来布局。可以将每个元素视为具有7条不可见的“锚定线”的集合:left、horizontalCenter、right、top、verticalCenter、baseline和bottom。 baseline(上图未展示)对应于文本所在的虚线。对于没有文…

细说目标检测中的Anchors

本文转载自AI公园。 作者:Raghul Asokan 编译:ronghuaiyang 导读 给大家再次解释一下Anchors在物体检测中的作用。 今天,我将讨论在物体检测器中引入的一个优雅的概念 —— Anchors,它是如何帮助检测图像中的物体,以及…

anchors布局

anchors布局 import QtQuick 2.2 import QtQuick.Window 2.2 Window {visible: truewidth: 640height: 480title: qsTr("Hello World")Rectangle {width: 300;height: 200;color: "blue";Rectangle {id: rectl;anchors.left: parent.left;anchors.leftMar…

PDU Session Anchors

在同一时刻,UE可以建立多条到同一个DN或不同DN的PDU会话;建立到同一个DN的多个PDU会话时,通过不同的UPF;每条PDU会话对应的SMF可以不同。 为了支持到DN的可选择路由功能或支持SSC mode 3模式,SMF可以控制PDU session的…

kmeans++聚类生成anchors

kmeans聚类生成anchors 说明 使用yolo系列通常需要通过kmeans聚类算法生成anchors, 但kmeans算法本身具有一定的局限性,聚类结果容易受初始值选取影响。 因此通过改进原kmeans_for_anchors.py实现 kmeans聚类生成anchors。具体实现如下: i…

anchors如何获得_Yolov3通过k-means聚类得到自己数据的anchors

本文代码参考: https://github.com/lars76/kmeans-anchor-boxes Yolov3中默认的9个anchors是作者通过对voc数据聚类得到的。 anchors 10,14, 23,27, 37,58, 81,82, 135,169, 344,319 不过,当我们训练自己的数据时,如果也采用默认的anchors,可…

Unity 锚点 Anchors

锚点介绍 锚点是Rect Transform组件中的属性,用于描述当前物体相对于父物体的对齐方式。 选中一个UI元素,就会显示其父物体的矩形框以及相对于父物体的锚点。 锚点只能位于父物体的矩形框之内。 锚点表现为四个相对出现的小三角形, 它们时而…

QML anchors 锚布局

锚布局 锚布局有7种锚线 anchors.leftanchors.rightanchors.topanchors.bottomanchors.horizontalCenteranchors.verticalCenteranchors.baseline 5种锚边距 anchors.leftMarginanchors.rightMarginanchors.topMarginanchors.bottomMarginanchors.margins 3种锚偏移 anchor…

anchors生成

关于修改anchor anchor与图片的输入分辨率有关系。 You should use this repository to get anchors: https://github.com/AlexeyAB/darknet By using this command for Yolo v3 (or v2): ./darknet detector calc_anchors data/hand.data -num_of_clusters 9 -width 720 -he…

使用k-means聚类anchors

在之前讲yolo理论基础知识时有提到过,从yolov2开始使用的anchors都是通过聚类得到的。如果想了解更多yolo相关的知识可以看看我在bilibili上录得视频:https://www.bilibili.com/video/BV1yi4y1g7ro 今天补下之前没有细讲的聚类anchors相关知识&#xff…

细说物体检测中的Anchors

点击上方“AI公园”,关注公众号,选择加“星标“或“置顶” 作者:Raghul Asokan 编译:ronghuaiyang 导读 给大家再次解释一下Anchors在物体检测中的作用。 今天,我将讨论在物体检测器中引入的一个优雅的概念 —— Ancho…

带图讲解,深度学习YOLO里面的anchors的进阶理解

如果有了解过yolo网络,那肯定也听说过anchors,当然anchors这个概念布置在YOLO里面才有,在其他的目标检测中也存在anchors这个概念。对于anchors计算的一些公式这篇文章就不进行讲解了,这篇文章主要是讲在训练网络模型过程中anchor…

Linux终端的网易云音乐——musicbox

网易云音乐是听歌的不错的选择,如果能够在命令行听歌就更cool了。特来推荐musicbox。 网易云音乐的musicbox是网易云音乐命令行版本,这款命令行的客户端使用 Python 构建,以 mpg123 作为播放后端。提供了很多使用的功能,如&#x…

MusicStore-2

1.按照MusicStore-1步骤创建mvc项目,并初始化数据库 2.修改HomeController using Chapter8.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;namespace Chapter8.Controllers {public class…

音乐i网站

开发工具(eclipse/idea/vscode等): 数据库(sqlite/mysql/sqlserver等): 功能模块(请用文字描述,至少200字) :

musicbox(暂停/启动,停止,下一曲上一首)

主界面 按下开始 按下暂停 按下停止 下一首 上一首 代码 activity_main.xml <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:orientation"vertical&…