【问题标题】:How to use the PASCAL VOC dataset in the xml format to build the model in tensorflowtensorflow中如何使用xml格式的PASCAL VOC数据集搭建模型
【发布时间】:2019-05-07 14:49:22
【问题描述】:

我有一个 PASCAL VOC 数据集。我想用它在 tensorflow 中构建深度学习模型。我认为我需要将其转换为 TFRecord 文件格式来构建模型,但我不确定我的想法是否正确。如果是,将 PASCAL VOC 转换为 TFRecord 文件格式的代码是什么。如果不是,您是否有建议加载此 PASCAL VOC 数据集以在 tensorflow 中构建模型。这是我的 PASCAL VOC 数据集。

<annotation>
  <filename>000000000.jpg</filename>
  <source>
    <annotation>ArcGIS Pro 2.1</annotation>
  </source>
  <size>
    <width>256</width>
    <height>256</height>
    <depth>3</depth>
  </size>
  <object>
    <name>0</name>
    <bndbox>
        <xmin>209.62</xmin>
        <ymin>3.86</ymin>
        <xmax>256.00</xmax>
        <ymax>70.93</ymax>
    </bndbox>
 </object>
 <object>
    <name>0</name>
    <bndbox>
        <xmin>120.92</xmin>
        <ymin>126.09</ymin>
        <xmax>200.23</xmax>
        <ymax>209.97</ymax>
    </bndbox>
 </object>
 <object>
    <name>0</name>
    <bndbox>
        <xmin>237.72</xmin>
        <ymin>136.02</ymin>
        <xmax>256.00</xmax>
        <ymax>214.18</ymax>
    </bndbox>
 </object>

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    Tensorflow Object Detection API为其提供了tool,你可以运行如下命令:

    python object_detection/dataset_tools/create_pascal_tf_record.py \
        --label_map_path=<path/to/label/map.pbtxt> \
        --data_dir=<path/to/data/dir> --year=<year_directory_name> --set=<train|test|val> \
        --output_path=pascal_<train|test|val>.record
    

    这需要一个树形

    data_dir
    |- year_dir
       |- Annotations
          |- *.xml
       |- ImageSets
          |- Layout
            |- test.txt
            |- train.txt
            |- val.txt
            |- trainval.txt
          |- Main
            |- *.txt
       |- JPEGImages
          |- *.jpg
    

    例如,使用通常的 PASCAL 数据集,结果将是:

    python object_detection/dataset_tools/create_pascal_tf_record.py \
        --label_map_path=object_detection/data/pascal_label_map.pbtxt \
        --data_dir=VOCdevkit --year=VOC2012 --set=val \
        --output_path=pascal_val.record
    

    【讨论】:

    • 您能否阐明各个文件和文件夹的含义?我在网上找不到任何解释。编辑:与往常一样,在询问后一分钟我找到了一些东西;)programmer.help/blogs/…
    • 好的,我明白了,谢谢。但是从编码自动化来看,我想知道他们为什么不读取 xml 本身来获取文件信息?就像 xml 文件已经准备好了一样,他们引用的每个文件都是那个文件,而且文件格式也很重要。奇怪。
    【解决方案2】:

    VOC2007 在最新的tensorflow-datasets==1.0.2 版本中可用(pip 尚不可用)。

    要安装它,请在终端中运行:

    git clone https://github.com/tensorflow/datasets
    cd datasets
    python setup.py build
    python setup.py install
    

    用法示例(Jupyter 中的绘图):

    import tensorflow as tf
    import tensorflow_datasets as tfds
    import matplotlib.pyplot as plt
    import numpy as np
    from PIL import Image, ImageDraw
    %matplotlib inline
    
    OUTLINE = (0, 255, 0)
    
    builder = tfds.builder('voc2007')
    builder.download_and_prepare()
    datasets = builder.as_dataset()
    train_data, test_data = datasets['train'], datasets['test']
    iterator = train_data.repeat(1).batch(1).make_one_shot_iterator()
    next_batch = iterator.get_next()
    
    with tf.Session() as sess:
        for _ in range(1):
            batch = sess.run(next_batch)
            image = batch['image']
            bboxes = batch['objects']['bbox']
            bboxes, image = np.squeeze(bboxes), np.squeeze(image)
            pil_image = Image.fromarray(image.astype('uint8'), 'RGB')
            draw = ImageDraw.Draw(pil_image)
            height, width = image.shape[:2]
            try:
                if (isinstance(bboxes[0], np.float32)
                    or isinstance(bboxes[0], np.float64)):
                    bboxes = [bboxes]
    
                for bbox in bboxes:
                    ymin, xmin, ymax, xmax = bbox
                    xmin *= width
                    xmax *= width
                    ymin *= height
                    ymax *= height
                    c1 = (xmin, ymin)
                    c2 = (xmax, ymin)
                    c3 = (xmax, ymax)
                    c4 = (xmin, ymax)
                    draw.line([c1, c2, c3, c4, c1],
                              fill=OUTLINE,
                              width=3)
                asnumpy = np.array(pil_image)
                figure = plt.figure(figsize=tuple(x/50 for x in image.shape[:2]))
                plt.imshow(asnumpy)
            except TypeError:
                pass
    

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 2017-12-07
      • 2022-11-23
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2015-11-19
      相关资源
      最近更新 更多