Tensorflow 使用自己的数据集(翻译官网)

来源:互联网 发布:詹姆斯去年总决赛数据 编辑:程序博客网 时间:2024/06/05 15:57

在Tensorflow object_detection上使用自己的数据集

准备输入

使用你自己的数据集在tf的目标检测API,你必须转换数据集到TFRecord flie 形式。这个文档主要讲解如何写一个脚本去生成TFRecord flie。

图片标记

每一个数据集被要求有一个图片标记和它联系在一起。这个图片标记定义了一个从行层次名称到整数层次id。(string class names, integer class Ids)这个图片标记应该是StringIntLabelMap text protobuf。样本标记可以被检测在object_detection/data。这个图片标记应该一直从Id1开始。

数据要求

对于每一个数据集中的图片,你应该有下述的信息
1. Jpeg或者png格式的RGB图片。
2. 一个包含边界信息的列表。每一个边界应该包括;A.一个边界坐标定义了4个float 格式的数字[ymin,xmin,ymax,xmax]。注意我们储存这个归一化的坐标(x/width,y/height)在tfrecord数据集上。B.物品的标签在个边界上。
举个栗子:

item {  id: 1  name: 'Cat'}item {  id: 2  name: 'Dog'}

我们能够使用下面的代码生成一个tf.Example proto 给这个图片:

def create_cat_tf_example(encoded_cat_image_data):   """Creates a tf.Example proto from sample cat image.  Args:    encoded_cat_image_data: The jpg encoded data of the cat image.  Returns:    example: The created tf.Example.  """  height = 1032.0  width = 1200.0  filename = 'example_cat.jpg'  image_format = b'jpg'  xmins = [322.0 / 1200.0]  xmaxs = [1062.0 / 1200.0]  ymins = [174.0 / 1032.0]  ymaxs = [761.0 / 1032.0]  classes_text = ['Cat']  classes = [1]  tf_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),      'image/source_id': dataset_util.bytes_feature(filename),      'image/encoded': dataset_util.bytes_feature(encoded_image_data),      'image/format': dataset_util.bytes_feature(image_format),      'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),      'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),      'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),      'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),      'image/object/class/label': dataset_util.int64_list_feature(classes),  }))  return tf_example

转换脚本提纲
一个使用自己的数据集的脚本大概会像这种格式

import tensorflow as tffrom object_detection.utils import dataset_utilflags = tf.app.flagsflags.DEFINE_string('output_path', '', 'Path to output TFRecord')FLAGS = flags.FLAGSdef create_tf_example(example):  # TODO(user): Populate the following variables from your example.  height = None # Image height  width = None # Image width  filename = None # Filename of the image. Empty if image is not from file  encoded_image_data = None # Encoded image bytes  image_format = None # b'jpeg' or b'png'  xmins = [] # List of normalized left x coordinates in bounding box (1 per box)  xmaxs = [] # List of normalized right x coordinates in bounding box             # (1 per box)  ymins = [] # List of normalized top y coordinates in bounding box (1 per box)  ymaxs = [] # List of normalized bottom y coordinates in bounding box             # (1 per box)  classes_text = [] # List of string class name of bounding box (1 per box)  classes = [] # List of integer class id of bounding box (1 per box)  tf_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),      'image/source_id': dataset_util.bytes_feature(filename),      'image/encoded': dataset_util.bytes_feature(encoded_image_data),      'image/format': dataset_util.bytes_feature(image_format),      'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),      'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),      'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),      'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),      'image/object/class/label': dataset_util.int64_list_feature(classes),  }))  return tf_exampledef main(_):  writer = tf.python_io.TFRecordWriter(FLAGS.output_path)  # TODO(user): Write code to read in your dataset to examples variable  for example in examples:    tf_example = create_tf_example(example)    writer.write(tf_example.SerializeToString())  writer.close()if __name__ == '__main__':  tf.app.run()

注意:你可能会注意到附加的领域在其他的数据集。他们现在不被使用或者一部分不被使用在这个api上。

附录

  1. https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/using_your_own_dataset.md
  2. https://zhuanlan.zhihu.com/p/27469690
    1.为官网原文;2.为工程师的操作细节。
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 初一月考模拟考试 九月初八 初一月考作文 8月初8 初一月考 豆蔻梢头二月初 9月初 初三月考反思 九月初十 九月初 十月结晶和子初哪个好 子初和十月结晶哪个好 初一月考数学必考题 十月结晶和子初 初一月考试卷 十月初十 初三月考作文 农门巧厨娘姜初月 朝桄桐全集 朝桄桐作品在线播放 朝桄桐光优在线播放 桄榔 朝桄桐超清播放 朝桄桐办公室在线播放 朝桄桐在线播放 桄榔粉 朝桄桐高清激烈 桄怎么读 朝桄桐高清 朝桄桐试炼在线播放 朝桄桐办公室 朝桄桐高清黑人 朝桄桐作品在线手机 朝桄桐所有作品 钢铁虫潮 桭桄 于立桄 朝桄桐所有作品在线播放 部长太太朝桄桐在线播放 朝桄桐经典作品在线播放 朝桄桐作品在线播放2019