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


在中显示图片 , 我们用这种方法
import osimport numpy as np import matplotlib.pyplot as pltimport xml.etree.ElementTree as ETdef draw_single_image(imgpath,annpath,savepath):"""_summary_Args:imgpath (_type_): 图片路径annpath (_type_): 标签文件savepath (_type_): 保存位置"""img = cv2.imdecode(np.fromfile(imgpath,dtype=np.uint8),1) #bgrif img is None or not img.any():raise Exceeption("read img failded!")tree = ET.parse(annpath)root = tree.getroot()result = root.findall("object")for obj in result:name = obj.find("name").textx1 = int(obj.find("bndbox").find("xmin").text)y1 = int(obj.find("bndbox").find("ymin").text)x2 = int(obj.find("bndbox").find("xmax").text)y2 = int(obj.find("bndbox").find("ymax").text)cv2.rectangle(img,(x1,y1),(x2,y2),(0,0,255),2)if savepath is None:imgrgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)plt.figure(figsize=(20,10))plt.imshow(imgrgb)else:cv2.imencode('.jpg',img)[1].tofile(save_path)
draw_single_image(imgpath="dataset/pqdetection_sliced2voc/pq_160_75/images/1_280_120_440_280.jpg", annpath="dataset/pqdetection_sliced2voc/pq_160_75/labels/1_280_120_440_280.xml", savepath=None)
draw_single_image(imgpath="dataset/pqdetection_sliced2voc/pq_224_75/images/2023_0302_184834_009_3270_392_392_616_616.jpg", annpath="dataset/pqdetection_sliced2voc/pq_224_75/labels/2023_0302_184834_009_3270_392_392_616_616.xml", savepath=None)
看到一张没有问题 , 大概率我们的程序处理流程就没有问题 , 可以放心往下走
4、生成训练数据
训练数据主要包括三个文件train.txt,val.txt,.txt
而我们有两个尺度 , 所以会有两份的train.txt和val.txt , 因为是同一个标签 , 所以.txt就是ball
with open("dataset/pqdetection_sliced2voc/label_list.txt","w") as f:f.writelines(["ball"])
# 416的尺度size_416=[320,352,384,416,448,480,512]# 256的尺度size_256=[160,192,224,256,288,320,352]
import osimport randomimg_xmls=[]for i in size_416:imgpath = f"dataset/pqdetection_sliced2voc/pq_{i}_75/images"labpath = f"dataset/pqdetection_sliced2voc/pq_{i}_75/labels"imgs = os.listdir(imgpath)for img in imgs:imgbasename = os.path.splitext(img)[0]imgname = os.path.join(f"pq_{i}_75/images",img)xmlname = os.path.join(f"pq_{i}_75/labels",imgbasename+'.xml')img_xml = f"{imgname} {xmlname}\n"img_xmls.append(img_xml)for i in range(5):random.shuffle(img_xmls)with open("dataset/pqdetection_sliced2voc/train_416.txt","w") as f:f.writelines(img_xmls)img_xmls=[]for i in size_256:imgpath = f"dataset/pqdetection_sliced2voc/pq_{i}_75/images"labpath = f"dataset/pqdetection_sliced2voc/pq_{i}_75/labels"imgs = os.listdir(imgpath)for img in imgs:imgbasename = os.path.splitext(img)[0]imgname = os.path.join(f"pq_{i}_75/images",img)xmlname = os.path.join(f"pq_{i}_75/labels",imgbasename+'.xml')img_xml = f"{imgname} {xmlname}\n"img_xmls.append(img_xml)for i in range(5):random.shuffle(img_xmls)with open("dataset/pqdetection_sliced2voc/train_256.txt","w") as f:f.writelines(img_xmls)
#看几行训练文件!head dataset/pqdetection_sliced2voc/train_256.txt#总的训练样本数!cat dataset/pqdetection_sliced2voc/train_256.txt | wc -l
pq_192_75/images/2023_0302_175706_008_240_384_432_576_624.jpg pq_192_75/labels/2023_0302_175706_008_240_384_432_576_624.xmlpq_288_75/images/2023_0302_164151_003_780_144_216_432_504.jpg pq_288_75/labels/2023_0302_164151_003_780_144_216_432_504.xmlpq_224_75/images/pingpang_720_00384_560_224_784_448.jpg pq_224_75/labels/pingpang_720_00384_560_224_784_448.xmlpq_160_75/images/2000_0107_022408_002_4830_600_520_760_680.jpg pq_160_75/labels/2000_0107_022408_002_4830_600_520_760_680.xmlpq_256_75/images/2023_0302_164336_005_150_448_464_704_720.jpg pq_256_75/labels/2023_0302_164336_005_150_448_464_704_720.xmlpq_320_75/images/pingpang_720_08579_560_80_880_400.jpg pq_320_75/labels/pingpang_720_08579_560_80_880_400.xmlpq_256_75/images/2023_0302_164336_005_1830_192_256_448_512.jpg pq_256_75/labels/2023_0302_164336_005_1830_192_256_448_512.xmlpq_320_75/images/2023_0302_175706_008_1860_480_160_800_480.jpg pq_320_75/labels/2023_0302_175706_008_1860_480_160_800_480.xmlpq_224_75/images/2023_0302_164336_005_870_392_336_616_560.jpg pq_224_75/labels/2023_0302_164336_005_870_392_336_616_560.xmlpq_224_75/images/10_112_256_336_480.jpg pq_224_75/labels/10_112_256_336_480.xml255337