4、picodet 小目标训练全流程( 四 )


slice(image_dir="dataset/pqdetection_voc/images/",dataset_json_path="dataset/pqdetection_coco/pddetection.json",output_dir="dataset/pqdetection_sliced/pq_256_075",slice_size=256,overlap_ratio=0.75,ignore_negative_samples=True,min_area_ratio=0.1 )
生成文件结构是
dataset/pqdetection_sliced/pq_256_075||----pddetections_images_256_075/*.jpg||----pddetection_256_075.json
文件的状态是:
from sahi.utils.coco import Cocococo=Coco.from_coco_dict_or_path("dataset/pqdetection_sliced/pq_256_075/pddetection_256_075.json")coco.stats
indexing coco dataset annotations...Loading coco annotations: 100%|██████████| 44695/44695 [00:05<00:00, 8403.17it/s]{'num_images': 44695,'num_annotations': 67009,'num_categories': 1,'num_negative_images': 0,'num_images_per_category': {'ball': 44695},'num_annotations_per_category': {'ball': 67009},'min_num_annotations_in_image': 1,'max_num_annotations_in_image': 12,'avg_num_annotations_in_image': 1.4992504754446807,'min_annotation_area': 6,'max_annotation_area': 65536,'avg_annotation_area': 3860.9125938306793,'min_annotation_area_per_category': {'ball': 6},'max_annotation_area_per_category': {'ball': 65536}}
同理完成其它尺度的切割 , 过程比较慢 , 可以用个多进程来完成 以下是我最终使用的代码
from multiprocessing import Processimport ossizes=[160,192,224,256,288,320,352,384,416,448,480,512]ratios=[0.75]*len(sizes)out_size_ratio=[[f"dataset/pqdetection_sliced/pq_{size}_{int(ratio*100)}",size,ratio] for size,ratio in zip(sizes,ratios)]def slice_my(image_dir="dataset/pqdetection_voc/images/",dataset_json_path="dataset/pqdetection_coco/pddetection.json",output_dir="dataset/pqdetection_sliced/pq_256_075",slice_size=256,overlap_ratio=0.75,ignore_negative_samples=True,min_area_ratio=0.1 ):print(f"output_dir:{output_dir} pid:{os.getpid()}") #只是想加一个进程id的获取 , 方便把进程手动停止slice(image_dir=image_dir,dataset_json_path=dataset_json_path,output_dir=output_dir,slice_size=slice_size,overlap_ratio=overlap_ratio,ignore_negative_samples=ignore_negative_samples,min_area_ratio= min_area_ratio)out_size_ratio
【4、picodet 小目标训练全流程】[['dataset/pqdetection_sliced/pq_160_75', 160, 0.75],['dataset/pqdetection_sliced/pq_192_75', 192, 0.75],['dataset/pqdetection_sliced/pq_224_75', 224, 0.75],['dataset/pqdetection_sliced/pq_256_75', 256, 0.75],['dataset/pqdetection_sliced/pq_288_75', 288, 0.75],['dataset/pqdetection_sliced/pq_320_75', 320, 0.75],['dataset/pqdetection_sliced/pq_352_75', 352, 0.75],['dataset/pqdetection_sliced/pq_384_75', 384, 0.75],['dataset/pqdetection_sliced/pq_416_75', 416, 0.75],['dataset/pqdetection_sliced/pq_448_75', 448, 0.75],['dataset/pqdetection_sliced/pq_480_75', 480, 0.75],['dataset/pqdetection_sliced/pq_512_75', 512, 0.75]]
kwargs=dict(image_dir="dataset/pqdetection_voc/images/",dataset_json_path="dataset/pqdetection_coco/pddetection.json",output_dir=out_size_ratio[0][0],slice_size=out_size_ratio[0][1],overlap_ratio=out_size_ratio[0][2],ignore_negative_samples=True,min_area_ratio=0.1 )kwargs
{'image_dir': 'dataset/pqdetection_voc/images/','dataset_json_path': 'dataset/pqdetection_coco/pddetection.json','output_dir': 'dataset/pqdetection_sliced/pq_160_75','slice_size': 160,'overlap_ratio': 0.75,'ignore_negative_samples': True,'min_area_ratio': 0.1}