|
|
|
@@ -1,44 +1,170 @@ |
|
|
|
import glob |
|
|
|
import xml.etree.ElementTree as ET |
|
|
|
|
|
|
|
import numpy as np |
|
|
|
|
|
|
|
from kmeans import kmeans, avg_iou |
|
|
|
|
|
|
|
ANNOTATIONS_PATH = r"C:\Users\Aministration\Desktop\annotations_test" |
|
|
|
CLUSTERS = 9 |
|
|
|
|
|
|
|
def load_dataset(path): |
|
|
|
dataset = [] |
|
|
|
for xml_file in glob.glob("{}/*xml".format(path)): |
|
|
|
# print(xml_file) |
|
|
|
tree = ET.parse(xml_file) |
|
|
|
|
|
|
|
height = int(tree.findtext("./size/height")) |
|
|
|
width = int(tree.findtext("./size/width")) |
|
|
|
|
|
|
|
for obj in tree.iter("object"): |
|
|
|
xmin = int(float(obj.findtext("bndbox/xmin"))) / width |
|
|
|
ymin = int(float(obj.findtext("bndbox/ymin"))) / height |
|
|
|
xmax = int(float(obj.findtext("bndbox/xmax"))) / width |
|
|
|
ymax = int(float(obj.findtext("bndbox/ymax"))) / height |
|
|
|
|
|
|
|
dataset.append([xmax - xmin, ymax - ymin]) |
|
|
|
|
|
|
|
return np.array(dataset) |
|
|
|
|
|
|
|
|
|
|
|
data = load_dataset(ANNOTATIONS_PATH) |
|
|
|
print('data shape is {}'.format(data.shape)) |
|
|
|
out = kmeans(data, k=CLUSTERS) |
|
|
|
|
|
|
|
yolov3clusters = [[10,13],[16,30],[33,23],[30,61],[62,45],[59,119],[116,90],[156,198],[373,326]] |
|
|
|
yolov3out= np.array(yolov3clusters)/416.0 |
|
|
|
|
|
|
|
print("self data Accuracy: {:.2f}%".format(avg_iou(data, out) * 100)) |
|
|
|
print("yolov3 Accuracy: {:.2f}%".format(avg_iou(data, yolov3out) * 100)) |
|
|
|
print("Boxes:\n {}-{}".format(out[:, 0]*416, out[:, 1]*416)) |
|
|
|
# print("Boxes:\n {}".format(out)) |
|
|
|
|
|
|
|
ratios = np.around(out[:, 0] / out[:, 1], decimals=2).tolist() |
|
|
|
print("Ratios:\n {}".format(sorted(ratios))) |
|
|
|
import glob
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import random
|
|
|
|
import math
|
|
|
|
# from kmeans import kmeans, avg_iou
|
|
|
|
# from sklearn.cluster import KMeans
|
|
|
|
|
|
|
|
ANNOTATIONS_PATH = "/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train_xml"
|
|
|
|
CLUSTERS = 7
|
|
|
|
|
|
|
|
def load_dataset(path):
|
|
|
|
dataset = []
|
|
|
|
for xml_file in glob.glob("{}/*xml".format(path)):
|
|
|
|
# print(xml_file)
|
|
|
|
tree = ET.parse(xml_file)
|
|
|
|
|
|
|
|
height = int(tree.findtext("./size/height"))
|
|
|
|
width = int(tree.findtext("./size/width"))
|
|
|
|
|
|
|
|
for obj in tree.iter("object"):
|
|
|
|
xmin = int(float(obj.findtext("bndbox/xmin"))) / width
|
|
|
|
ymin = int(float(obj.findtext("bndbox/ymin"))) / height
|
|
|
|
xmax = int(float(obj.findtext("bndbox/xmax"))) / width
|
|
|
|
ymax = int(float(obj.findtext("bndbox/ymax"))) / height
|
|
|
|
|
|
|
|
dataset.append([xmax - xmin, ymax - ymin])
|
|
|
|
|
|
|
|
return np.array(dataset)
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
def wh_iou(wh1, wh2):
|
|
|
|
# Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
|
|
|
|
wh1 = wh1[:, None] # [N,1,2]
|
|
|
|
wh2 = wh2[None] # [1,M,2]
|
|
|
|
inter = np.minimum(wh1, wh2).prod(2) # [N,M]
|
|
|
|
return inter / (wh1.prod(2) + wh2.prod(2) - inter) # iou = inter / (area1 + area2 - inter)
|
|
|
|
|
|
|
|
|
|
|
|
def k_means(boxes, k, dist=np.median):
|
|
|
|
"""
|
|
|
|
yolo k-means methods
|
|
|
|
refer: https://github.com/qqwweee/keras-yolo3/blob/master/kmeans.py
|
|
|
|
Args:
|
|
|
|
boxes: 需要聚类的bboxes
|
|
|
|
k: 簇数(聚成几类)
|
|
|
|
dist: 更新簇坐标的方法(默认使用中位数,比均值效果略好)
|
|
|
|
"""
|
|
|
|
box_number = boxes.shape[0]
|
|
|
|
last_nearest = np.zeros((box_number,))
|
|
|
|
|
|
|
|
# 在所有的bboxes中随机挑选k个作为簇的中心。
|
|
|
|
clusters = boxes[np.random.choice(box_number, k, replace=False)]
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# 计算每个bboxes离每个簇的距离 1-IOU(bboxes, anchors)
|
|
|
|
distances = 1 - wh_iou(boxes, clusters)
|
|
|
|
# 计算每个bboxes距离最近的簇中心
|
|
|
|
current_nearest = np.argmin(distances, axis=1)
|
|
|
|
# 每个簇中元素不在发生变化说明以及聚类完毕
|
|
|
|
if (last_nearest == current_nearest).all():
|
|
|
|
break # clusters won't change
|
|
|
|
for cluster in range(k):
|
|
|
|
# 根据每个簇中的bboxes重新计算簇中心
|
|
|
|
clusters[cluster] = dist(boxes[current_nearest == cluster], axis=0)
|
|
|
|
|
|
|
|
last_nearest = current_nearest
|
|
|
|
|
|
|
|
return clusters
|
|
|
|
def iou(box, clusters):
|
|
|
|
"""
|
|
|
|
Calculates the Intersection over Union (IoU) between a box and k clusters.
|
|
|
|
param:
|
|
|
|
box: tuple or array, shifted to the origin (i. e. width and height)
|
|
|
|
clusters: numpy array of shape (k, 2) where k is the number of clusters
|
|
|
|
return:
|
|
|
|
numpy array of shape (k, 0) where k is the number of clusters
|
|
|
|
"""
|
|
|
|
x = np.minimum(clusters[:, 0], box[0])
|
|
|
|
y = np.minimum(clusters[:, 1], box[1])
|
|
|
|
if np.count_nonzero(x == 0) > 0 or np.count_nonzero(y == 0) > 0:
|
|
|
|
raise ValueError("Box has no area")
|
|
|
|
|
|
|
|
intersection = x * y # 相交面积
|
|
|
|
box_area = box[0] * box[1]
|
|
|
|
cluster_area = clusters[:, 0] * clusters[:, 1]
|
|
|
|
|
|
|
|
iou_ = np.true_divide(intersection, box_area + cluster_area - intersection + 1e-10) # 交并比 = 相交面积 / 两个框面积相加并减去相交面积
|
|
|
|
# iou_ = intersection / (box_area + cluster_area - intersection + 1e-10)
|
|
|
|
|
|
|
|
return iou_
|
|
|
|
|
|
|
|
|
|
|
|
def avg_iou(boxes, clusters):
|
|
|
|
"""
|
|
|
|
Calculates the average Intersection over Union (IoU) between a numpy array of boxes and k clusters.
|
|
|
|
param:
|
|
|
|
boxes: numpy array of shape (r, 2), where r is the number of rows
|
|
|
|
clusters: numpy array of shape (k, 2) where k is the number of clusters
|
|
|
|
return:
|
|
|
|
average IoU as a single float
|
|
|
|
"""
|
|
|
|
return np.mean([np.max(iou(boxes[i], clusters)) for i in range(boxes.shape[0])]) # 查看训练集框和anchor box的交并比
|
|
|
|
|
|
|
|
|
|
|
|
def kmeans(boxes, k, dist=np.mean):
|
|
|
|
"""
|
|
|
|
Calculates k-means clustering with the Intersection over Union (IoU) metric.
|
|
|
|
param:
|
|
|
|
boxes: numpy array of shape (r, 2), where r is the number of rows
|
|
|
|
k: number of clusters
|
|
|
|
dist: distance function
|
|
|
|
return:
|
|
|
|
numpy array of shape (k, 2)
|
|
|
|
"""
|
|
|
|
rows = boxes.shape[0] # boxes就是result,看看有多少个框
|
|
|
|
|
|
|
|
distances = np.empty((rows, k)) # 存放每个点与中心点的距离
|
|
|
|
last_clusters = np.zeros((rows,)) # 存放上一次的距离,用于结束循环
|
|
|
|
|
|
|
|
np.random.seed()
|
|
|
|
|
|
|
|
# the Forgy method will fail if the whole array contains the same rows
|
|
|
|
clusters = boxes[np.random.choice(rows, k, replace=False)] # 随机选取k个中心点,默认是9个
|
|
|
|
|
|
|
|
while True:
|
|
|
|
for row in range(rows):
|
|
|
|
distances[row] = 1 - iou(boxes[row], clusters) # 上述的距离公式,用1减去每个框和中心点的交并比,得到每个框到中心点的距离
|
|
|
|
|
|
|
|
nearest_clusters = np.argmin(distances, axis=1) # 得到每个框距离哪个中心点距离最小
|
|
|
|
|
|
|
|
if (last_clusters == nearest_clusters).all(): # 如果上一次距离和这次距离一样,跳出循环,结束(距离一样,聚类结果肯定一样)
|
|
|
|
break
|
|
|
|
|
|
|
|
for cluster in range(k):
|
|
|
|
clusters[cluster] = dist(boxes[nearest_clusters == cluster], axis=0) # 更新中心点
|
|
|
|
'''
|
|
|
|
利用与第几个点距离最小的框求均值得到聚类结果,如现在求第一个anchor box,
|
|
|
|
那么就取出nearest_clusters == 0的box,因为这些box是与第一个中心点距离最近的(nearest_clusters)=0
|
|
|
|
然后利用均值,求出新的中心点
|
|
|
|
'''
|
|
|
|
|
|
|
|
last_clusters = nearest_clusters
|
|
|
|
|
|
|
|
return clusters
|
|
|
|
|
|
|
|
|
|
|
|
data = load_dataset(ANNOTATIONS_PATH)
|
|
|
|
print('data shape is {}'.format(data.shape))
|
|
|
|
out = k_means(data, k=CLUSTERS)
|
|
|
|
|
|
|
|
yolov5clusters = [[10,13],[16,30],[33,23],[30,61],[62,45],[59,119],[116,90],[156,198],[373,326]]
|
|
|
|
yolov5out= np.array(yolov5clusters)/640.0
|
|
|
|
|
|
|
|
# print("self data Accuracy: {:.2f}%".format(avg_iou(data, out) * 100))
|
|
|
|
# print("yolov5 Accuracy: {:.2f}%".format(avg_iou(data, yolov5out) * 100))
|
|
|
|
print("Boxes:\n {}-{}".format(out[:, 0]*640, out[:, 1]*640))
|
|
|
|
# print("Boxes:\n {}".format(out))
|
|
|
|
|
|
|
|
ratios = np.around(out[:, 0] / out[:, 1], decimals=2).tolist()
|
|
|
|
print("Ratios:\n {}".format(sorted(ratios)))
|
|
|
|
|
|
|
|
# [41,54],[10,17],[16,22],[9,10],[6,8],[148,209],[6,13],[23,35],[73,95]
|
|
|
|
# [6,8],[6,13],[9,10],[10,17],[16,22],[23,35],[41,54],[73,95],[148,209]
|
|
|
|
[6,10],[10,14],[14,23],[23,33],[40,53],[72,93],[147,207] |