取对数可缓解左峰、右峰等偏移现象。优化:对x轴取
统计MSCOCO数据集中所有images的尺度分布(每张图的尺度:图里所有目标尺度的中位数),并等分为4个区间。
步骤
1、json标注文件的加载
import json
import os
import numpy as np
data_root='coco/'
ann_file=data_root + "resize/annotations/instances_train2017_100x167.json"
jd=json.load(open(ann_file))
2、尺度的计算
def distribution_main(jd, num_part, mode="instance", func=np.mean, times=4):
def get_instance_scales(jd):
bbox_list=[]
for ann in jd['annotations']:
bbox_list.append(ann['bbox']) # [x1,y1,w,h]
bboxes=np.array(bbox_list)
scales=np.sqrt(bboxes[:,2]*bboxes[:,3])
scales.sort()
return scales
def get_image_scales(jd, num_part, func):
image_scales_list =[[] for i in jd['images']]
iid_ind, cnt={}, 0
for ann in jd['annotations']:
iid=ann['image_id']
if iid not in iid_ind:
iid_ind[iid]=cnt
cnt+=1
area=np.sqrt(ann['bbox'][2]*ann['bbox'][3])
image_scales_list[iid_ind[iid]].append(area)
scales=np.array([func(np.array(scas)) for scas in image_scales_list if scas])
scales.sort()
return scales
def separate_vals(scales: np.array, num_part: int) -> (np.array,np.array):
num_bbox=scales.shape[0]
intervals=round(num_bbox/num_part)
separate_inds=[i for i in range(0,num_bbox,intervals)]
if scales[separate_inds[-1]]!=scales[-1]:
separate_inds.append(-1)
return separate_inds, scales[separate_inds]
assert mode in ["instance", "image"]
scales=get_image_scales(jd, num_part, func) if mode=="image" else get_instance_scales(jd)
separate_inds, separate_scale=separate_vals(scales, num_part) #3
print(f"划分{num_part}的实例大小为{separate_scale*times}")
return scales, separate_scale
scales, sca = distribution_main(jd, num_part=4, mode="image", func=np.max)
- API样例:
统计所有annotations的分布,并等分为4个区间:
distribution_main(jd, num_part=4)
统计所有images的分布(图的尺度:图里目标尺度的中位数),并等分为3个区间:
distribution_main(jd, num_part=3, mode="image", func=np.max)
3、直方图可视化
import matplotlib.pyplot as plt
def show_distribution(bbox_scales, separate_scale, times=4):
plt.figure(dpi=200)
plt.hist(bbox_scales*times, bins=50,alpha = 0.6, label='scales')
parts=len(separate_scale)-1
for i in range(0, parts+1): ### edit here
plt.axvline(x=separate_scale[i]*times, color='orange',)
plt.savefig(f"coco_scale_max_{parts}.png",dpi=200, bbox_inches='tight')
show_distribution(scales, sca)