voc2007数据集_Affectnet数据集转换成VOC数据集格式

在解决了cpu条件下编译fastrcnn的错误后,

菜园子:youtu one stage yolo make -j32 报错​zhuanlan.zhihu.com

就可以安排上训练等事宜了。

第一件事就是将自己要用的数据集转换成模型训练需要用的voc数据集的格式。VOC2007文件夹下文件组织目录如图

d8fcde2c57cc6a19c5d222e8c3317b3e.png

由于我要做检测任务,所以就不创建最后两个文件夹了。同时ImageSets文件夹下创建Main文件夹.

我在affect数据集目录下创建了notebook。同时将VOC2007文件夹也放在这里.

e2a97d2330be76727373262f138ce372.png

忽略这个VOCdevkit名字,其实是就是上面说的VOC2007,因为后面代码需要,所以改名了,但是代码里都是VOC2007.

然后开始上代码啦!

  1. 首先是读取csv文件,将train与test的csv 标签都放在一起
import pandas as pd
import os
train_labels = pd.read_csv('./Manually_Annotated_file_lists/training.csv')
train_len = len(train_labels['subDirectory_filePath'])
validation_labels = pd.read_csv('./Manually_Annotated_file_lists/validation.csv')
vali_len = len(validation_labels['subDirectory_filePath'])
print(train_len)
print(vali_len)
labels = pd.concat([train_labels,validation_labels])
print(len(labels))

2.然后就是将源文件的名字修改,同时复制到./VOC2007/JPEGImages/下,然后生成xml文件放到./VOC2007/Annotations/下

from lxml.etree import Element, SubElement, tostring
from xml.dom.minidom import parseString
import shutil
import matplotlib.image as mpimg

def getxml(label,image_name,shape):
    node_root = Element('annotation')

    node_folder = SubElement(node_root, 'folder')
    node_folder.text = 'JPEGImages'

    node_filename = SubElement(node_root, 'filename')
    node_filename.text = image_name

    node_size = SubElement(node_root, 'size')
    node_width = SubElement(node_size, 'width')
    node_width.text = '%s' % shape[0]

    node_height = SubElement(node_size, 'height')
    node_height.text = '%s' % shape[1]

    node_depth = SubElement(node_size, 'depth')
    node_depth.text = '%s' % shape[2]

    left, top, right, bottom = label[1], label[2], label[1]+label[3], label[2] + label[4]
    node_object = SubElement(node_root, 'object')
    node_name = SubElement(node_object, 'name')
    node_name.text = '%s' % label[6]
    node_difficult = SubElement(node_object, 'difficult')
    node_difficult.text = '0'
    node_bndbox = SubElement(node_object, 'bndbox')
    node_xmin = SubElement(node_bndbox, 'xmin')
    node_xmin.text = '%s' % left
    node_ymin = SubElement(node_bndbox, 'ymin')
    node_ymin.text = '%s' % top
    node_xmax = SubElement(node_bndbox, 'xmax')
    node_xmax.text = '%s' % right
    node_ymax = SubElement(node_bndbox, 'ymax')
    node_ymax.text = '%s' % bottom

    xml = tostring(node_root, pretty_print=True)  
    dom = parseString(xml)

    save_xml = os.path.join('./VOC2007/Annotations/', image_name.replace('jpg', 'xml'))
    with open(save_xml, 'wb') as f:
        f.write(xml)

    return
def getJPG_MV(labels):
    mv_dir = './VOC2007/JPEGImages/'
#     将每个图片重命名移动到jpeg路径下
    for i in range(235929,len(labels)):
        ori_name = labels.iloc[i][0]
        try:
            I = mpimg.imread(os.path.join('./Manually_Annotated_Images/',ori_name))
        except IOError:
            continue
        shape = I.shape
        ori_dir = os.path.join('./Manually_Annotated_Images/',ori_name)
        target_name = str(i).zfill(6)+'.jpg'
        target_dir = os.path.join(mv_dir,target_name)
        if os.path.exists(mv_dir):
            shutil.copy(ori_dir,target_dir)
        getxml(labels.iloc[i],target_name,shape)    
        

getJPG_MV(labels)

3.划分train test val trainval

import random

def _main():
    trainval_percent = 0.1
    train_percent = 0.9
    xmlfilepath = './VOC2007/Annotations'
    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('./VOC2007/ImageSets/Main/trainval.txt', 'w')
    ftest = open('./VOC2007/ImageSets/Main/test.txt', 'w')
    ftrain = open('./VOC2007/ImageSets/Main/train.txt', 'w')
    fval = open('./VOC2007/ImageSets/Main/val.txt', 'w')

    for i in list:
        name = total_xml[i][:-4] + 'n'
        if i in trainval:
            ftrainval.write(name)
            if i in train:
                ftest.write(name)
            else:
                fval.write(name)
        else:
            ftrain.write(name)

    ftrainval.close()
    ftrain.close()
    fval.close()
    ftest.close()


if __name__ == '__main__':
    _main()

结束!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VOC数据转换为TFRecord文件需要以下步骤: 1. 下载VOC数据集并解压缩。 2. 安装TensorFlow和Pillow库。 3. 编写脚本将VOC数据转换为TFRecord文件。 以下是一个简单的Python脚本示例,可以将VOC数据转换为TFRecord文件: ```python import tensorflow as tf import os import io import xml.etree.ElementTree as ET from PIL import Image def create_tf_example(example): # 读取图像文件 img_path = os.path.join('VOCdevkit/VOC2012/JPEGImages', example['filename']) with tf.io.gfile.GFile(img_path, 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = Image.open(encoded_jpg_io) width, height = image.size # 读取标注文件 xml_path = os.path.join('VOCdevkit/VOC2012/Annotations', example['filename'].replace('.jpg', '.xml')) with tf.io.gfile.GFile(xml_path, 'r') as fid: xml_str = fid.read() xml = ET.fromstring(xml_str) # 解析标注文件 xmins = [] xmaxs = [] ymins = [] ymaxs = [] classes_text = [] classes = [] for obj in xml.findall('object'): class_name = obj.find('name').text classes_text.append(class_name.encode('utf8')) classes.append(label_map[class_name]) bbox = obj.find('bndbox') xmins.append(float(bbox.find('xmin').text) / width) ymins.append(float(bbox.find('ymin').text) / height) xmaxs.append(float(bbox.find('xmax').text) / width) ymaxs.append(float(bbox.find('ymax').text) / height) # 构造TFRecord Example tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': tf.train.Feature(int64_list=tf.train.Int64List(value=[height])), 'image/width': tf.train.Feature(int64_list=tf.train.Int64List(value=[width])), 'image/filename': tf.train.Feature(bytes_list=tf.train.BytesList(value=[example['filename'].encode('utf8')])), 'image/source_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[example['filename'].encode('utf8')])), 'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[encoded_jpg])), 'image/format': tf.train.Feature(bytes_list=tf.train.BytesList(value=['jpeg'.encode('utf8')])), 'image/object/bbox/xmin': tf.train.Feature(float_list=tf.train.FloatList(value=xmins)), 'image/object/bbox/xmax': tf.train.Feature(float_list=tf.train.FloatList(value=xmaxs)), 'image/object/bbox/ymin': tf.train.Feature(float_list=tf.train.FloatList(value=ymins)), 'image/object/bbox/ymax': tf.train.Feature(float_list=tf.train.FloatList(value=ymaxs)), 'image/object/class/text': tf.train.Feature(bytes_list=tf.train.BytesList(value=classes_text)), 'image/object/class/label': tf.train.Feature(int64_list=tf.train.Int64List(value=classes)), })) return tf_example # 将VOC数据转换为TFRecord文件 def create_tf_record(output_file): examples = [...] # 从VOC数据集读取实例 writer = tf.io.TFRecordWriter(output_file) for example in examples: tf_example = create_tf_example(example) writer.write(tf_example.SerializeToString()) writer.close() label_map = {...} # 标签映射 output_file = 'voc_train.tfrecord' create_tf_record(output_file) ``` 其中`create_tf_example`函数将一个VOC样本转换为TFRecord Example,`create_tf_record`函数将整个VOC数据转换为TFRecord文件。在这个例子中,我们假设VOC数据集已经被解压缩到`VOCdevkit/VOC2012`目录下,标签映射已经定义为`label_map`变量。你需要根据自己的实际情况修改这些变量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值