• 【pytorch-ssd目标检测】制作类似pascal voc格式的目标检测数据集


    pascal voc目标检测数据集格式如下:

    其中:

    • Annotations为图像标注信息xml文件
    • ImageSets为训练集、测试集、验证、训练验证集图像名的txt文件 
    • JPEGImages为原始的图片

    pascal voc或yolo格式的数据可以使用labelimg进行标注:下载地址:

    链接:https://pan.baidu.com/s/1r8x7tu0sdO_UUuCXKVfELQ
    提取码:l325 

    操作挺简单的,就不介绍了。

    标注好的xml文件类似如下:

    <annotation>
        <folder>JPEGImages</folder>
        <filename>test_00000002.jpg</filename>
        <path>E:detectionpascal vocmaskornotJPEGImages	est_00000002.jpg</path>
        <source>
            <database>Unknown</database>
        </source>
        <size>
            <width>480</width>
            <height>600</height>
            <depth>3</depth>
        </size>
        <segmented>0</segmented>
        <object>
            <name>mask</name>
            <pose>Unspecified</pose>
            <truncated>0</truncated>
            <difficult>0</difficult>
            <bndbox>
                <xmin>112</xmin>
                <ymin>7</ymin>
                <xmax>352</xmax>
                <ymax>325</ymax>
            </bndbox>
        </object>
    </annotation>

    其对应的图像如下:

    然后划分训练集、测试集、验证集、训练验证集:在原始VOC2007数据集中,trainval大约占整个数据集的50%,test大约为整个数据集的50%;train大约是trainval的50%,val大约为trainval的50%

    import os
    import random
     
    trainval_percent = 0.5
    train_percent = 0.5
    xmlfilepath = '/content/drive/My Drive/pytorch_ssd/data/maskornot/Annotations'
    txtsavepath = '/content/drive/My Drive/pytorch_ssd/data/maskornot/ImageSets/Main'
    total_xml = os.listdir(xmlfilepath)
     
    num=len(total_xml)
    list=range(num)
    tv=int(num*trainval_percent)
    tr=int(tv*train_percent)
    trainval= random.sample(list,tv)
    train=random.sample(trainval,tr)
     
    ftrainval = open(txtsavepath+'/trainval.txt', 'w')
    ftest = open(txtsavepath+'/test.txt', 'w')
    ftrain = open(txtsavepath+'/train.txt', 'w')
    fval = open(txtsavepath+'/val.txt', 'w')
     
    for i  in list:
      name=total_xml[i][:-4]+'
    '
      if i in trainval:
          ftrainval.write(name)
          if i in train:
              ftrain.write(name)
          else:
              fval.write(name)
      else:
          ftest.write(name)
     
    ftrainval.close()
    ftrain.close()
    fval.close()
    ftest.close()

    运行之后:

    其中tranval.txt的部分结果为:

    test_00000002
    test_00000003
    test_00000006
    test_00000009
    test_00000008
    test_00000012
    test_00000013
    test_00000014
    test_00000020

    至此,目标检测数据集的创建就完成了。

    下一节,使用pytorch-ssd训练自己创建的数据集。

  • 相关阅读:
    (黑马十次方项目day06)@ConfigurationProperties报错的解决方式
    (黑马十次方项目day04)An attempt was made to call a method that does not exist. The attempt was made from the following location:
    (黑马十次方项目day02)使用map接收form表单的参数
    (黑马十次方项目day02)IDEA在方法之间添加分隔符及开启Run Dashboard管理
    (黑马十次方项目day01)spring-boot-starter-parent 包maven依赖报错
    (黑马十次方项目day01)从PDF文件中复制代码到pom文件中project报错
    ER图学习
    java 8 函数式库Vavr功能
    Guava Cache
    UML学习
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12546061.html
Copyright © 2020-2023  润新知