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上。
附录
- https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/using_your_own_dataset.md
- https://zhuanlan.zhihu.com/p/27469690
1.为官网原文;2.为工程师的操作细节。
阅读全文