深度学习目标检测通常需要较大的数据规模,但是数据集的标注通常是费时费力但又意义不大的一件事,我们可以先标注三五百张图片,然后训练一个较为初步的模型,然后使用这个模型对未标注的图像进行推理,将推理结果导出为voc格式,再将图片和标注文件使用本地图像标注软件labelimg打开,手动调整边框位置后,标注文件即可作为数据集用于后续的模型训练。
推理
!python /home/aistudio/PaddleDetection/tools/infer.py \
-c /home/aistudio/PaddleDetection/configs/cascade_rcnn/cascade_rcnn_r50_fpn_1x_coco.yml \
--draw_threshold=0.5 \
--infer_dir=img \
--output_dir=toolinfer \
--use_vdl=True \
--save_txt=True \
-o weights=/home/aistudio/output/cascade_rcnn_r50_fpn_1x_coco/model_final
将推理结果保存到txt文件中
推理结果修改为voc格式标注
将产生的推理结果文件移动到指定文件夹中,将对应的坐标信息进行修改对应,生成voc格式的xml标注文件。
转换代码
import os
import cv2
headstr = """\
<annotation>
<folder>VOC</folder>
<filename>%s</filename>
<source>
<database>My Database</database>
<annotation>VOC</annotation>
<image>flickr</image>
<flickrid>NULL</flickrid>
</source>
<owner>
<flickrid>NULL</flickrid>
<name>company</name>
</owner>
<size>
<width>%d</width>
<height>%d</height>
<depth>%d</depth>
</size>
<segmented>0</segmented>
"""
objstr = """\
<object>
<name>%s</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>%d</difficult>
<bndbox>
<xmin>%d</xmin>
<ymin>%d</ymin>
<xmax>%d</xmax>
<ymax>%d</ymax>
</bndbox>
</object>
"""
tailstr = '''\
</annotation>
'''
def save_annotations(boxes, img, filename):
H = img.shape[0]
W = img.shape[1]
C = img.shape[2]
# H,W,C = img.shape
img_name = filename.split('.')[0] + '.bmp'
head = headstr % (img_name, W, H, C) # 写入头文件
tail = tailstr # 写入尾文件
# 写入boxes
save_path = anno_path + filename.split('.')[0] + '.xml'
f = open(save_path, 'w')
f.write(head)
for box in boxes:
f.write(objstr % (str(box[0]), 0, float(box[2]), float(box[3]), float(box[2]) + float(box[4]), float(box[3]) + float(box[5])))
f.write(tail)
if __name__ == '__main__':
# 设置路径
root_path = './'
total_label_path = root_path + 'txt/' # txt存储的路径
total_img_path = root_path + 'img/' # 图像存储路径
anno_path = root_path + 'Annotations/' # 存储生成的xml标注文件
# 判断当前路径下是否存在Annotations这个文件夹,若不存在,自动创建一个
if not os.path.exists(anno_path):
os.mkdir(anno_path)
# 逐个读取txt标注文件
for filename in os.listdir(total_label_path):
cur_label_path = total_label_path + filename
cur_img_path = total_img_path + filename.split('.')[0] + '.bmp' # 换一下文件名后缀
cur_boxes = []
# 读取当前txt文件中的内容
with open(cur_label_path, 'r') as file:
while True:
line = file.readline().strip() # .strip()用来去掉'\r,\n'
if not line:
break
line_list = [ele for ele in line.split(' ')]
cur_boxes.append(line_list)
# 读取当前图像
cur_img = cv2.imread(cur_img_path)
# 进行xml文档存储
save_annotations(cur_boxes, cur_img, filename)
需要修改的地方:
- txt文件存放的路径
total_label_path
- 图像存放的路径
total_img_path
- 创建存放voc格式的文件夹
anno_path
- 图像文件的后缀名
bmp
注意事项:
- 一般需要三五百张初始标注完成的图像
- 尽量使用高精度的目标检测算法完成预训练
- 根据预测结果与真实目标物的情况,合理选择推理阈值。如果漏检较多,则降低置信度阈值;若误检较多,则提高置信度阈值。