目标检测数据集目标尺度分析

目标检测数据集目标尺度分析

目录

  • 目标检测数据集目标尺度分析
      • 尺度计算
      • 代码

尺度计算

计算公式分绝对尺度和相对尺度:
A S ( G i j ) = w i j ∗ h i j R S ( G i j ) = w i j ∗ h i j W i ∗ H i \begin{aligned} A S\left(G_{i j}\right) & =\sqrt{w_{i j} * h_{i j}} \\ R S\left(G_{i j}\right) & =\sqrt{\frac{w_{i j} * h_{i j}}{W_{i} * H_{i}}} \end{aligned} AS(Gij)RS(Gij)=wijhij =WiHiwijhij

代码

import os
import os.path
import numpy as np
import cv2
import matplotlib.pyplot as plt

train_list = os.listdir("/data/facias/DOTA/train_split/labelTxt")
base_path = "/data/facias/DOTA/train_split/labelTxt/"

def poly2obb_np_le90(poly):
    bboxps = np.array(poly).reshape((4, 2))
    rbbox = cv2.minAreaRect(bboxps)
    x, y, w, h, a = rbbox[0][0], rbbox[0][1], rbbox[1][0], rbbox[1][1], rbbox[
        2]
    if w < 2 or h < 2:
        return
    a = a / 180 * np.pi
    if w < h:
        w, h = h, w
        a += np.pi / 2
    while not np.pi / 2 > a >= -np.pi / 2:
        if a >= np.pi / 2:
            a -= np.pi
        else:
            a += np.pi
    assert np.pi / 2 > a >= -np.pi / 2
    return x, y, w, h, a

total_dict = {}
relative_max = 0
relative_min = 0
isum = 0
number = 0

for file in train_list:
    with open(base_path + file) as f:
        s = f.readlines()
        for si in s:
            bbox_info = si.split()
            poly = np.array(bbox_info[:8], dtype=np.float32)
            label = bbox_info[8]
            try:
                x, y, w, h, a = poly2obb_np_le90(poly)
            except:  # noqa: E722
                continue
            absolute_size = pow((w * h), 0.5)
            relative_size = pow((w * h) / (1024.0 * 1024.0), 0.5)

            if relative_size > relative_max:
                relative_max = relative_size
            elif relative_size < relative_min:
                relative_min = relative_size
            isum = isum + relative_size
            number = number + 1

            if label not in total_dict.keys():
                total_dict[label] = {}
                total_dict[label]['absolute'] = []
                total_dict[label]['relative'] = []
                total_dict[label]['absolute'].append(absolute_size)
                total_dict[label]['relative'].append(relative_size)
            else:
                total_dict[label]['absolute'].append(absolute_size)
                total_dict[label]['relative'].append(relative_size)

#画直方图
for label in total_dict:
    medium = isum / number
    label_dict = total_dict[label]
    relative_list = total_dict[label]['relative']
    relative_np = np.array(relative_list)
    # hist, bins = np.histogram(relative_np, bins=50, range=(relative_min, relative_max))
    plt.hist(relative_np, bins=200, histtype='stepfilled', alpha=0.3)
    plt.savefig('./' + label + '.jpg')
    plt.clf()

你可能感兴趣的:(深度学习)