Object Scale Analysis of Object Detection Dataset

Object Scale Analysis of Object Detection Dataset

scale calculation

The calculation formula is divided into absolute scale and relative scale:
AS ( G ij ) = wij ∗ hij RS ( G ij ) = wij ∗ hij W i ∗ H i \begin{aligned} AS\left(G_{ij}\right) & = \sqrt{w_{ij} * h_{ij}} \\ RS\left(G_{ij}\right) & =\sqrt{\frac{w_{ij} * h_{ij}}{W_{i} * H_{i}}} \end{aligned}AS(Gij)RS(Gij)=wijhij =WiHiwijhij

the code

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()

Guess you like

Origin blog.csdn.net/weixin_45453121/article/details/131926390