Tensorflow Object Detection API生成自己的tfrecord训练数据集
Object Detection API谷歌
该文章部分参考别的大佬的,由于忘了内容出处,所以没有加转载链接,请谅解,有原创作者看到可以联系我添加。
========转载请注明出处==========
此python文件放在dataset_tools下面
生成自己训练的数据集主要看个人annotation文件是什么格式的。我这里的每张图都有自己的annotation文件,例如:
图片xxx.jpg,其annotation文件为xxx.box
box文件内容为:
Xmin Ymin Xmax Ymax label 如下图:如果有多个label ,可以继续追加在下一行:
Xmin Ymin Xmax Ymax label \n
Xmin Ymin Xmax Ymax label
from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport hashlib
import io
import os
import PIL.Image
import tensorflow as tf
import pandas as pd
import cv2
from functools import reduce
import operator
from object_detection.utils import dataset_utilflags = tf.app.flags
flags.DEFINE_string('train_imgs_dir', '/home/ai/Downloads/competition_change_box_img/img', 'Root directory to bc train dataset.')
flags.DEFINE_string('train_labels', '/home/ai/Downloads/competition_change_box_img/box','(Relative) path to annotations directory.')
flags.DEFINE_string('train_output', '../All_tf_record/competition_img_test.record', 'Path to output TFRecord')
FLAGS = flags.FLAGSdef create_coordinate_info_of_content_list(image_dir,label_dir):content_list_all = []for item,file_name in enumerate(os.listdir(label_dir)):img = cv2.imread(os.path.join(image_dir,file_name.replace('.box','.jpg')))height = img.shape[0]width = img.shape[1]deepth = img.shape[2]content_list = [[file_name.replace('.box', '.jpg'), height, width, deepth]]with open(os.path.join(label_dir,file_name), 'r') as f: lines = f.readlines()for line in lines:new_line = line.split(' ')[:]content_one = [new_line[0],new_line[1],new_line[2],new_line[3],new_line[4]]content_list.append(content_one)a = reduce(operator.add,content_list)content_list_all.append(a)return content_list_alldef create_tf_example(content_list, imgs_dir):height = int(content_list[1])width = int(content_list[2])filename = content_list[0]img_path = os.path.join(imgs_dir, filename)with tf.gfile.GFile(img_path, 'rb') as fid:encoded_jpg = fid.read()encoded_jpg_io = io.BytesIO(encoded_jpg)image = PIL.Image.open(encoded_jpg_io)if image.format != 'JPEG':raise ValueError('Image format not JPEG')key = hashlib.sha256(encoded_jpg).hexdigest()xmin = []ymin = []xmax = []ymax = []classes = []classes_text = []box_num = int((len(content_list) - 4) / 5) #一张图上可能有多个labelfor i in range(box_num):xmin.append(float(content_list[5 * i + 4 + 0]) / width)ymin.append(float(content_list[5 * i + 4 + 1]) / height)xmax.append(float(content_list[5 * i + 4 + 2]) / width)ymax.append(float(content_list[5 * i + 4 + 3]) / height)classes_text.append(content_list[5 * i + 4 + 4].encode('utf8'))classes.append(classMap[content_list[5 * i + 4 + 4]])print('the class id is {} '.format(classMap[content_list[5 * i + 4 + 4]]))example = tf.train.Example(features=tf.train.Features(feature={'image/height': dataset_util.int64_feature(height),'image/width': dataset_util.int64_feature(width),'image/filename': dataset_util.bytes_feature(filename.encode('utf8')),'image/source_id': dataset_util.bytes_feature(filename.encode('utf8')),'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),'image/encoded': dataset_util.bytes_feature(encoded_jpg),'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),'image/object/bbox/xmin': dataset_util.float_list_feature(xmin),'image/object/bbox/xmax': dataset_util.float_list_feature(xmax),'image/object/bbox/ymin': dataset_util.float_list_feature(ymin),'image/object/bbox/ymax': dataset_util.float_list_feature(ymax),'image/object/class/text': dataset_util.bytes_list_feature(classes_text),'image/object/class/label': dataset_util.int64_list_feature(classes),}))return exampledef main(_):# train tfrecord generateprint("Reading from {}".format(FLAGS.train_imgs_dir))writer = tf.python_io.TFRecordWriter(FLAGS.train_output)content_list_all = create_coordinate_info_of_content_list(FLAGS.train_imgs_dir, FLAGS.train_labels)for line in content_list_all:content_list = linetf_example = create_tf_example(content_list, FLAGS.train_imgs_dir)writer.write(tf_example.SerializeToString())writer.close()if __name__ == '__main__':tf.app.run()
Tensorflow Object Detection API生成自己的tfrecord训练数据集相关推荐
- Tensorflow object detection API 搭建自己的目标检测模型并迁移到Android上
参考链接:https://blog.csdn.net/dy_guox/article/details/79111949 之前参考上述一系列博客在Windows10下面成功运行了TensorFlow A ...
- ssd目标检测训练自己的数据_目标检测Tensorflow object detection API之训练自己的数据集...
构建自己的模型之前,推荐先跑一下Tensorflow object detection API的demo JustDoIT:目标检测Tensorflow object detection APIzh ...
- 使用tensorflow object detection API 训练自己的目标检测模型 (三)
在上一篇博客"使用tensorflow object detection API 训练自己的目标检测模型 (二)"中介绍了如何使用LabelImg标记数据集,生成.xml文件,经过 ...
- 建立自己的数据集 并用Tensorflow object detection API进行训练
ps: 欢迎大家光临我的博客 建立数据集 标注工具: ubuntu 图像标注工具labelImg sudo apt-get install pyqt5-dev-tools sudo pip3 inst ...
- TensorFlow Object Detection API 超详细教程和踩坑过程
安装Anacond:https://blog.csdn.net/ITLearnHall/article/details/81708148 安装Pycharm:https://blog.csdn.net ...
- TensorFlow Object Detection API入门例子 (小浣熊检测上)
玩了一周的TensorFlow Object Detection API了,是时候记录一下,踩过的坑都快忘记了~ 首先,总结一下检测的流程,实验分以下几步完成: 收集并标注数据 数据格式转换 确定训练 ...
- TensorFlow Object Detection API 技术手册(5)——制作自己的目标检测数据集
TensorFlow Object Detection API 技术手册(5)--制作自己的目标检测数据集 (一)收集图片 (二)安装图像打标工具labelImg (三)将XML文件转化为CSV文件 ...
- Tensorflow object detection API 搭建属于自己的物体识别模型(转载修改)
Tensorflow object detection API 搭建属于自己的物体识别模型 电脑配置信息 开始搭建环境 测试自带案例 准备训练图片 配置文件与模型 开始训练模型 生成最终的训练文件 测 ...
- 基于TensorFlow Object Detection API训练自己的目标识别模型
基于TensorFlow Object Detection API训练自己的目标识别模型 环境 Windows10 CUDA_9 Cudnn_9.0 Anaconda3-5.2.0 Tensorflo ...
最新文章
- 【Linux】在虚拟机上安装CentOS7
- [NET] 如何从 Winform 移植到 Webform [自己搞定HTTP协议]
- python数据处理实例-入门Python数据分析最好的实战项目(一)
- 尝试为文件附加自动命名的数据库,但失败。已存在同名的数据库,或指定的文件无法打开或位于 UNC 共享目录中...
- unity安装,sdk,jdk问题
- 力扣刷题常用数据结构和方法(java版本)
- 可以部署在广域网执行QQ高仿版 GG2014 (源代码)
- html 只能输入正数,vue 限制input只能输入正数
- SPOJ COT Count on a tree(主席树+倍增lca)
- Kaggle 数据清洗挑战 Day 3 - 快速解析日期(date)数据
- 嵌入html_音视频格式转换神器与html视频元素加字幕——零基础自学网页制作
- If you already have a 64-bit JDK installed。。。。
- ARM与x86的战争史诗(连载1):Wintel帝国(ZZ)
- 优化模型验证关键代码06:多行程旅行商问题(mTSP)
- 系统性能统计(CPU占用率,内存占用率,系统平均负载)
- 台式计算机启动时 每次按f1,开机按f1的解决方法_电脑开机每次都要按F1,怎么解决...
- 将腾讯视频QLV格式转换为MP4格式
- python进阶——AI视觉实现口罩检测实时语音报警系统
- 中国软件外包企业的出路
- 搭建个人家用NAS网络存储服务器