MMDet中的anchor调整

 背景介绍

        一开始是用默认的anchor配置训练的,可视化分析后,对于特别扁的目标效果明显不好。于是研究了一下配置文件中的这3个参数。默认高宽比是1:2、1:1、2:1。而我的场景会出现1:20的目标,显然不合适要调整。

scales=[8],   # 共1*3=3种base_anchors,面积是一样的,
ratios=[0.5, 1.0, 2.0],   # 高宽比,1.0表示8*8的大小
strides=[4, 8, 16, 32, 64]),   # 5种尺度的anchor strides,对应于原图的比例

基于kmeans聚类统计代表性box

        可以用labelme或labelimg标注数据,基于标注数据聚类。注意设置CLUSTERS和SIZE两个参数。

import glob
import xml.etree.ElementTree as ET
import json
import numpy as np

from kmeans import kmeans, avg_iou

# ANNOTATIONS_PATH = r"mydata/train/xmls"
ANNOTATIONS_PATH = r"mydata/labels"
CLUSTERS = 4   # anchor数量
SIZE = 1333  # 训练尺寸


def load_dataset_xlm(path):
	dataset = []
	for xml_file in glob.glob("{}/*xml".format(path)):
		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(obj.findtext("bndbox/xmin")) / width
			ymin = int(obj.findtext("bndbox/ymin")) / height
			xmax = int(obj.findtext("bndbox/xmax")) / width
			ymax = int(obj.findtext("bndbox/ymax")) / height

			min_size = 5
			dataset.append([xmax - xmin, ymax - ymin])
			if (ymax - ymin)*height < min_size or (xmax - xmin)*width < min_size:
				print('err!!!!',xml_file)
	# print(dataset)
	return np.array(dataset)


def load_dataset_labelme(path):
	dataset = []
	files = glob.glob('{}/*json'.format(path))
	for file_path in files:
		with open(file_path, 'r') as f:
			info = json.load(f)
		height = info['imageHeight']
		width = info['imageWidth']
		for pol in info['shapes']:
			x = [i[0] for i in pol['points']]
			y = [i[1] for i in pol['points']]
			xmin = int(min(x)) / width
			ymin = int(min(y)) / height
			xmax = int(max(x)) / width
			ymax = int(max(y)) / height
			dataset.append([xmax - xmin, ymax - ymin])


	return np.array(dataset)

# 加载标注文件
# data = load_dataset_xlm(ANNOTATIONS_PATH)
data = load_dataset_labelme(ANNOTATIONS_PATH)

out = kmeans(data, k=CLUSTERS)   # 聚类中心

out_list = out.tolist()
res = sorted(out_list, key=(lambda x:x[0] * x[1]))

out = np.array(res)

print("Accuracy: {:.2f}%".format(avg_iou(data, out) * 100))   # 表征一个框上能覆盖多少比例,所有框的iou平均值
print("Boxes:\n {}".format(out * SIZE))   # size=640上的anchor 长宽

print("Boxes:\n {}-{}".format(out[:, 0] * SIZE, out[:, 1] * SIZE))   # 上一个print换一种表方法,不用care

# ratios = np.around(out[:, 0] / out[:, 1], decimals=2).tolist()
ratios = np.around(out[:, 1] / out[:, 0], decimals=2).tolist()   # mmdet中是高宽比
print("Ratios:\n {}".format(sorted(ratios)))   # 长宽比


kmeans.py

import numpy as np


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)
    :param 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_ = intersection / (box_area + cluster_area - intersection)

    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
    :param 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])])


def translate_boxes(boxes):
    """
    Translates all the boxes to the origin.
    :param boxes: numpy array of shape (r, 4)
    :return: numpy array of shape (r, 2)
    """
    new_boxes = boxes.copy()
    for row in range(new_boxes.shape[0]):
        new_boxes[row][2] = np.abs(new_boxes[row][2] - new_boxes[row][0])
        new_boxes[row][3] = np.abs(new_boxes[row][3] - new_boxes[row][1])
    return np.delete(new_boxes, [0, 1], axis=1)


def kmeans(boxes, k, dist=np.median):
    """
    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
    :param k: number of clusters
    :param dist: distance function
    :return: numpy array of shape (k, 2)
    """
    # print(boxes)
    print('k=',k)
    rows = boxes.shape[0]
    print('rows=',rows)

    distances = np.empty((rows, k))
    last_clusters = np.zeros((rows,))

    np.random.seed(111)

    # the Forgy method will fail if the whole array contains the same rows
    a=np.random.choice(rows, k, replace=False)
    print('a=',a)
    clusters = boxes[a]
    print('clusters=',clusters)

    while True:
        for row in range(rows):
            # print(boxes[row])
            distances[row] = 1 - iou(boxes[row], clusters)

        nearest_clusters = np.argmin(distances, axis=1)   # 每一个框和哪个anchor最接近
        # print(nearest_clusters,nearest_clusters.shape)

        if (last_clusters == nearest_clusters).all():   # anchor稳定了
            break

        for cluster in range(k): # 用中值做聚类中心
            clusters[cluster] = dist(boxes[nearest_clusters == cluster], axis=0)

        last_clusters = nearest_clusters

    return clusters

        统计不同CLUSTERS数量下的Accuracy,CLUSTERS越大Accuracy越接近100%,根据CLUSTERS增大时准确率的增益情况,主观选择一个CLUSTERS。获得此时的Boxes和Ratios,注意你的模型是高宽比还是宽高比,在mmdet中是高宽比。

        获得Boxes和Ratios后,需要设置scales大小。可用以下代码实验性获得合适的scales。

import math
anchor_stride = 16   # 模型固有的,可选范围[4, 8, 16, 32, 64]
anchor_ratios = 0.1  # 计算出的高宽比ratio=h/w  [0.05, 0.09, 0.22, 0.6]
anchor_scales = 14   # 需要自己设置的超参数,对每个ratio,选择合适的stride,尝试合适的scales,使最后生成的box接近聚类统计结果

w = anchor_stride
h = anchor_stride

h_ratios = math.sqrt(anchor_ratios)
w_ratios = 1 / h_ratios
# h_ratios/w_ratios = anchor_ratios
print(w_ratios,h_ratios)

ws = (w * w_ratios* anchor_scales)
hs = (h * h_ratios * anchor_scales)

print(ws,hs)

关于mmdet的anchor实现可参看另一篇笔记《基于MMdet的Cascade MASKRCNN 原理及源码解读》

你可能感兴趣的:(深度学习,人工智能)