对于voc 这样的图像数据集,占用空间比较大,之前一般以矩阵形式存在内存空间中进行模型训练,需要计算机大量内存空间
tensorflow 有 tf.io.TFRecordWriter的数据api 可以将数据进行压缩,
import time
import os
import hashlibfrom absl import app, flags, logging
from absl.flags import FLAGS
import tensorflow as tf
import lxml.etree
import tqdm# flags.DEFINE_string('data_dir', './data/voc2012_raw/VOCdevkit/VOC2012/',
#                     'path to raw PASCAL VOC dataset')
# flags.DEFINE_enum('split', 'train', [
#                   'train', 'val'], 'specify train or val spit')
# flags.DEFINE_string('output_file', './data/voc2012_train.tfrecord', 'outpot dataset')
# flags.DEFINE_string('classes', './data/voc2012.names', 'classes file')def build_example(annotation, class_map):img_path = os.path.join('./data/voc2012_raw/VOCdevkit/VOC2012', 'JPEGImages', annotation['filename'])img_raw = open(img_path, 'rb').read()key = hashlib.sha256(img_raw).hexdigest()width = int(annotation['size']['width'])height = int(annotation['size']['height'])xmin = []ymin = []xmax = []ymax = []classes = []classes_text = []truncated = []views = []difficult_obj = []if 'object' in annotation:for obj in annotation['object']:difficult = bool(int(obj['difficult']))difficult_obj.append(int(difficult))xmin.append(float(obj['bndbox']['xmin']) / width)ymin.append(float(obj['bndbox']['ymin']) / height)xmax.append(float(obj['bndbox']['xmax']) / width)ymax.append(float(obj['bndbox']['ymax']) / height)classes_text.append(obj['name'].encode('utf8'))classes.append(class_map[obj['name']])truncated.append(int(obj['truncated']))views.append(obj['pose'].encode('utf8'))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=[annotation['filename'].encode('utf8')])),'image/source_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[annotation['filename'].encode('utf8')])),'image/key/sha256': tf.train.Feature(bytes_list=tf.train.BytesList(value=[key.encode('utf8')])),'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),'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=xmin)),'image/object/bbox/xmax': tf.train.Feature(float_list=tf.train.FloatList(value=xmax)),'image/object/bbox/ymin': tf.train.Feature(float_list=tf.train.FloatList(value=ymin)),'image/object/bbox/ymax': tf.train.Feature(float_list=tf.train.FloatList(value=ymax)),'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)),'image/object/difficult': tf.train.Feature(int64_list=tf.train.Int64List(value=difficult_obj)),'image/object/truncated': tf.train.Feature(int64_list=tf.train.Int64List(value=truncated)),'image/object/view': tf.train.Feature(bytes_list=tf.train.BytesList(value=views)),}))return exampledef parse_xml(xml):if not len(xml):return {xml.tag: xml.text}result = {}for child in xml:child_result = parse_xml(child)if child.tag != 'object':result[child.tag] = child_result[child.tag]else:if child.tag not in result:result[child.tag] = []result[child.tag].append(child_result[child.tag])return {xml.tag: result}def main():#读取数据,并且对类别进行编码,这是一个人字典class_map = {name: idx for idx, name in enumerate(open('./data/voc2012.names').read().splitlines())}#在这里,用tf2 的新的数据格式,将图像信息重新编码,而不在是直接用矩阵格式#原因是编码后数据占用空间比较低,writer = tf.io.TFRecordWriter('./data/voc2012_train.tfrecord')image_list = open(os.path.join('./data/voc2012_raw/VOCdevkit/VOC2012', 'ImageSets', 'Main', '%s.txt' % 'train')).read().splitlines()logging.info("Image list loaded: %d", len(image_list))for name in tqdm.tqdm(image_list):annotation_xml = os.path.join('./data/voc2012_raw/VOCdevkit/VOC2012', 'Annotations', name + '.xml')annotation_xml = lxml.etree.fromstring(open(annotation_xml).read())annotation = parse_xml(annotation_xml)['annotation']tf_example = build_example(annotation, class_map)writer.write(tf_example.SerializeToString())writer.close()logging.info("Done")main()# if __name__ == '__main__':
#     app.run(main)

yolov3-tf2 数据格式压缩相关推荐

  1. 基于yolov3和pythorch框架的火焰识别检测算法

    这是本人第一次写博客,就当是自己实现算法的一个记录吧,有什么不好的地方也请多多指教.我会详细的从环境的配置到算法实现都说明一下,希望对大家能有帮助. 本火焰识别算法采用的是pytorch版本的yolo ...

  2. pytorch版本下的yolov3训练实现火焰检测

    时隔好多好多日子了,一直没写博客(小声bb,最近忙着接私活儿).马上就要开学了,害,回去就要加油干了!!! 本次教程写个pytorch版本的yolov3检测,用的火焰检测数据集,效果如下: 这就可以做 ...

  3. 压缩你的JS和CSS代码

    压缩你的JS和CSS代码 2008年04月01日 星期二 15:40 减小代码在传输中的大小,就能提高页面的传输速度,打开网页也就快了.在上一篇文章中提到了使用Apache2的mod_deflate模 ...

  4. 使用Yolo3进行目标检测实现流程记录1

    上个月需要使用目标检测进行项目开发,简单了解了下目标检测的技术之后,果断选择使用yolo来进行尝试. 在查看了无数篇博客之后,头疼脑热,各种花样的问题之后,果断记录了自行爬坑的过程,希望对后续使用yo ...

  5. 计算机视觉 | YOLO开源项目汇总

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 1.Pytorch Yolo V3 使用PyTorch实现的YOLO v3对象检测算法 https:/ ...

  6. 汇总|Yolo开源项目

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 1.Pytorch Yolo V3 使用PyTorch实现的YOLO v3对象检测算法 https:/ ...

  7. 连肝 7 个晚上,总结了 66 条计算机网络的知识点

    作者 | 哪吒 来源 | 程序员小灰(ID:chengxuyuanxiaohui) 计算机网络知识是面试常考的内容,在实际工作中经常涉及.最近,我总结了66条计算机网络相关的知识点. 1.比较http ...

  8. 毕业设计 基于python的网络爬虫-基于python的网络爬虫

    一.从小说网站抓取一本小说 import urllib2:python库,提供一系列针对url的操作方法 import re:re正则表达式,提供了一系列针对正则表达式的方法 BeautifulSou ...

  9. 2021-11-05深度学习

    关注微信公众号:人工智能技术与咨询.了解更多咨询 基于YOLOv3 和ASMS 的目标跟踪算法 人工智能技术与咨询 3天前 本文来自<光电工程>,作者吕晨等 1.   引言 目标跟踪一直是 ...

最新文章

  1. 2015各地高温补贴发放标准时间一览表
  2. JavaScript 图片的上传前预览(兼容所有浏览器)
  3. 用 ABAP 读取本地文本文件内容试读版
  4. 在JDeveloper 12.1.3中将Java API用于WebSockets
  5. springboot:web开发-Thymeleaf
  6. MATLAB点击运行并计时没反应,MATLAB计时器对象陷阱和不良用法
  7. 最小路径问题_Floyd
  8. 呉服屋 2011/03/24早会文章
  9. matlab 如何捕捉错误,【matlab|matlab运行错误捕捉方法】
  10. list 删除_算法面试题:一个List,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点!...
  11. vs关于_CRT_SECURE_NO_WARNINGS警告说明
  12. Atitit.信息论原理概论attilax总结
  13. python函数调用执行的四个步骤_如何调用python函数
  14. VCC,GND,VSS,VDD的理解
  15. 深入解析Javascript异步编程
  16. 理解计算机3D图形学中的坐标系变换
  17. “3D游戏之父”手游考古,网友:求支持智能手机!
  18. 首尾相连数组的最大子数组和
  19. nk.bin和nk.nb0
  20. 设计模式(10)[JS版]-JavaScript如何实现组合模式???

热门文章

  1. C++ 之常对象,常对象成员
  2. 一个express老系统csrf漏洞修复
  3. OpenStack安装过程备忘
  4. 获取height固定折叠元素真实高度方法
  5. Android Studio检测内存泄露和性能
  6. 【转】 Java中的变量赋值和参数传递
  7. 解决iptables和vsftpd设置的问题
  8. 基于Shibbloet实现的SSO单点登录
  9. Eclipse上安装GIT插件EGit及使用
  10. jquery 导航栏目