PaddleDetection使用VOC数据集训练

当前PaddleDetection版本: release/2.4

以下采用PP-YOLOE为例

  1. 数据准备
    1. 将图片和标注文件保存至如下文件格式
      在这里插入图片描述

    2. 使用脚本切分训练集

      pip install matplotlib
      python3 make_voc.py --dataset datasets/测试数据集
      
      import argparse
      import random
      import re
      from pathlib import Path
      from matplotlib import pyplot as plt
      
      annotations_name = 'Annotations'
      jpegimages_name = 'JPEGImages'
      trainval_name = 'trainval.txt'
      text_file_name = 'test.txt'
      val_file_name = 'val.txt'
      
      def write_file(datas, file):
          results = []
          for data in datas:
              annotation = Path(annotations_name, f'{data}.xml')
              image =  Path(jpegimages_name, f'{data}.jpg')
              result = f'{image} {annotation}'
              results.append(result)
          with file.open('w')as f:
              f.write("\n".join(results))
          print(file, "success")
      
      def make_paddle_voc(dataset):
          dataset = Path(dataset)
          assert dataset.is_dir(), "Error dataset path"
          annotations_path = dataset / annotations_name
          images_path = dataset / jpegimages_name
          if not (annotations_path.is_dir() and images_path.is_dir()):
              print("Please use the following structure:")
              print(f"{dataset.name}")
              print(f'\t{annotations_name}\n\t\t|--00001.xml\n\t\t|--00002.xml')
              print(f'\t{jpegimages_name}\n\t\t|--00001.jpg\n\t\t|--00002.jpg')
              raise RuntimeError(f'Error {jpegimages_name} or {annotations_name} dir')
          paddle_path = dataset / 'paddle'
          paddle_path.mkdir(exist_ok=True, parents=True)
          trainval_path = paddle_path / trainval_name
          test_path = paddle_path / text_file_name
          val_path = paddle_path / val_file_name
          annotations = annotations_path.glob('*.xml')
          images = images_path.glob('*.jpg')
          images_list = [image.name for image in images]
          annotations_list = []
          for annotation in annotations:
              image = annotation.with_suffix('.jpg').name
              assert image in images_list, f"{annotation.name} has no image file"
              annotations_list.append(annotation.stem)
          random.shuffle(annotations_list)
          per = round(len(annotations_list)/10)
          trainval = annotations_list[:per*7] # 此处切分比例为8:1:1 可行修改比例
          test = annotations_list[per*7:per*9]
          val = annotations_list[per*9:]
          write_file(trainval, trainval_path)
          write_file(test, test_path)
          write_file(val, val_path)
      
      def check_labels(dataset):
          dataset = Path(dataset)
          annotations_path = dataset / annotations_name
        	class_list = {}
        	for annotation_path in annotations_path.glob('*.xml'):
        	    with annotation_path.open('r')as f:
        	        names = re.findall(r'<name>(.+?)</name>', f.read())
        	        for name in names:
        	            if name not in class_list.keys():
        	                class_list[name] = 0
        	            class_list[name] += 1
        	class_list = dict(sorted(class_list.items()))
        	label_list = dataset / 'label_list.txt'
        	with label_list.open('w')as f:
        	    f.write('\n'.join(class_list.keys()))
        	fig, ax = plt.subplots()
        	[ax.text(a, b + 1, b, ha='center', va='bottom') for a, b in class_list.items()]
        	plt.bar(*zip(*class_list.items()))
        	plt.xticks(rotation=270)
        	plt.title('Detection category distribution')
        	plt.xlabel('Class')
        	plt.ylabel('Number')
        	plt.tight_layout()
        	plt.savefig(f"{dataset / '类别分布.jpg'}", format="jpg")
        	print(f"Save as {dataset / '类别分布.jpg'}")
        	print(f"共{len(class_list)}类")
        	
      if __name__=='__main__':
          parser = argparse.ArgumentParser()
          parser.add_argument('--dataset', required=True, help='input dataset path, necessary parameter')
          
          opt = parser.parse_args()
          check_dataset(opt.dataset)
          make_paddle_voc(opt.dataset)
          check_labels(opt.dataset)
      

      在这里插入图片描述

    3. 创建数据集配置文件

      1. 将configs/datasets/voc.yml复制为custom.yml
        在这里插入图片描述
      2. 修改配置,红色标记处,根据自己的数据集修改
        在这里插入图片描述
  2. 修改参数
    1. 在configs中选择你要训练的配置,复制一份并将coco后缀改为voc
      在这里插入图片描述

    2. 修改配置

      第一处标记为自定义VOC数据集,第二、三处根据自己电脑配置,参照官方说明修改。
      在这里插入图片描述

  3. 开启训练
    python3 tools/train.py -c configs/ppyoloe/ppyoloe_crn_s_300e_voc.yml
    
  4. 模型评估
    python3 -u tools/eval.py -c configs/ppyoloe/ppyoloe_crn_s_300e_voc.yml
    
  5. 模型测试
    python3 -u tools/infer.py -c configs/ppyoloe/ppyoloe_crn_s_300e_voc.yml --infer_img=test.jpg --draw_threshold=0.5
    
  6. 模型导出
    python3 tools/export_model.py -c configs/ppyoloe/ppyoloe_crn_s_300e_voc.yml --output_dir=output/ppyoloe_crn_s_300e_voc/ -o weights=output/ppyoloe_crn_s_300e_voc/best_model
    
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Paddle图像数据集下载是指使用PaddlePaddle深度学习框架提供的数据集工具,从互联网上下载图像数据集用于训练深度学习模型。 在进行图像识别或其他计算机视觉任务时,我们通常需要大量的图像数据用来训练模型。然而,收集和标注大规模图像数据是一项非常耗时且费力的工作。为了方便用户,PaddlePaddle提供了图像数据集下载的功能,使用户能够通过简单的代码命令即可获取所需数据集使用Paddle图像数据集下载功能的步骤如下: 1. 导入必要的库:在Python程序中,首先需要导入PaddlePaddle和相关的库。 ```python import paddle.dataset as pd ``` 2. 选择需要的数据集PaddlePaddle提供了多种常用的图像数据集,如MNIST、CIFAR-10、ImageNet等,可以根据需要选择合适的数据集。例如,选择MNIST数据集可以使用以下命令: ```python dataset = pd.mnist.train() ``` 3. 下载数据集使用PaddlePaddle提供的数据集函数,可以直接从互联网上下载所需数据集。例如,下载MNIST训练集可以使用以下命令: ```python pd.mnist.train() ``` 4. 数据集使用:一旦数据集下载完成,便可以将其用于训练深度学习模型。通常需要将图像数据转换成模型可接受的格式,如将图像像素进行归一化、转换成Tensor等。 综上所述,Paddle图像数据集下载是一项方便快捷地获取所需图像数据集的功能,极大地简化了深度学习模型的训练流程。通过使用Paddle提供的数据集工具,开发者可以更加专注于模型的设计和优化,从而加速模型开发和性能提升。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值