tensorflow objection detection model(输入模型加载)

上一篇是网络模型的加载,这一篇是输入模型的加载,之后还有训练模型的加载。
输入模型的加载的开始是train.py文件中的

create_input_dict_fn = functools.partial(input_reader_builder.build, input_config)

那就进入input_reader_builder.build看一看。

parallel_reader = tf.contrib.slim.parallel_reader
def build(input_reader_config):
#判断类型输入的类型是否为input_reader_pb2.InputReader)
  if not isinstance(input_reader_config, input_reader_pb2.InputReader):
    raise ValueError('input_reader_config not of type '
                     'input_reader_pb2.InputReader.')
#只接受输入类型为tf_record_input_reader的输入
  if input_reader_config.WhichOneof('input_reader') == 'tf_record_input_reader':
#获取数据集存放位置
    config = input_reader_config.tf_record_input_reader

    _, string_tensor = parallel_reader.parallel_read(
        config.input_path,
        reader_class=tf.TFRecordReader,
        num_epochs=(input_reader_config.num_epochs
                    if input_reader_config.num_epochs else None),
        num_readers=input_reader_config.num_readers,
        shuffle=input_reader_config.shuffle,
        dtypes=[tf.string, tf.string],
        capacity=input_reader_config.queue_capacity,
        min_after_dequeue=input_reader_config.min_after_dequeue)

    return tf_example_decoder.TfExampleDecoder().decode(string_tensor)

  raise ValueError('Unsupported input_reader_config.')

可以看出核心是使用了tf.contrib.slim.parallel_reader这个库中的函数。看看这个函数的说明。

ef parallel_read(data_sources,
                  reader_class,
                  num_epochs=None,
                  num_readers=4,
                  reader_kwargs=None,
                  shuffle=True,
                  dtypes=None,
                  capacity=256,
                  min_after_dequeue=128,
                  seed=None,
                  scope=None):
  """
#从原始的数据文件使用多个reader获取多个record。
#并行的使用ParallelReader从多个文件读取数据
#多个readers是根据 `reader_class` 和 `reader_kwargs'进行创建的。
#如果shuffle为true,则common_queue将会是一个RandomShuffleQueue ,否则就是一个FIFOQueue.
参数说明
    data_sources: 一系列的文件位置比如: /path/to/train@128, /path/to/train* or /tmp/.../train*
    reader_class: 一个继承了io_ops.ReaderBase 的子类比如 TFRecordReader
    num_epochs: 间隔多少次从数据源读取一次文件,如果没有给,就一直读取
    num_readers: 一个整数,表示创建多少个数据读取器。
    reader_kwargs: 一个可选的字典,表示of kwargs for the reader.
    shuffle: 是否进行数据的打乱操作。
    dtypes:  一个类型的列表,dtypes的长度一定等于每一个记录中元素的长度。如果为None,则为[tf.string, tf.string] for (key, value).
    capacity: 整数,表示common_queue中需要包含多少数据.
    min_after_dequeue: 一个整数,在出队后common_queue中最少的数据记录的量,和打乱有关。
    seed:RandomShuffleQueue所需的随机种子.
    scope: Optional name scope for the ops.
  Returns:
    key, value: a tuple of keys and values from the data_source.
  """

当然读取数据的最后一句话就是对获取到的信息进行解析。

return tf_example_decoder.TfExampleDecoder().decode(string_tensor)

tf_example_decoder是一个用于解析包含了序列化后的tensorflow.Exampleprotos的解析器。

 def decode(self, tf_example_string_tensor):
# 解析序列化后的tensroflow example并返回一个tensor的dict
# 传入参数:一个序列化后的tensorflow example proto对象
# 传出对象: 返回的tensor的dict包含如下内容:
# fields.InputDataFields.image - 一个三维类型为uint8的tensor,其大小为[None, None, 3]表示的是图片
#     fields.InputDataFields.source_id - 一个string类型的tensor包含的是图片的id
#      fields.InputDataFields.key - 一个string类型的tensor,是图片的hd5码
#      fields.InputDataFields.filename - 一个string类型的tensor,包含了数据库的名称
#      fields.InputDataFields.groundtruth_boxes - 二维的float32的 tensor格式为
#        [None, 4]包含box的四个顶点信息.
#      fields.InputDataFields.groundtruth_classes - 1维的 int64型 tensor格式为shape
#        [None]包含box所对应的object类型
#      fields.InputDataFields.groundtruth_area - 1维的 float32 类型的tensor格式为
#        [None] 包含了物品的像素掩膜信息。
#      fields.InputDataFields.groundtruth_is_crowd - 1D bool tensor of shape
#        [None] indicating if the boxes enclose a crowd.
#      fields.InputDataFields.groundtruth_difficult - 1D bool tensor of shape
#        [None] indicating if the boxes represent `difficult` instances.
#      fields.InputDataFields.groundtruth_instance_masks - 3D int64 tensor of
#        shape [None, None, None] containing instance masks.
#      fields.InputDataFields.groundtruth_instance_classes - 1D int64 tensor
#        of shape [None] containing classes for the instance masks.
    serialized_example = tf.reshape(tf_example_string_tensor, shape=[])
#构建解析器
    decoder = slim_example_decoder.TFExampleDecoder(self.keys_to_features,
                                                    self.items_to_handlers)
    keys = decoder.list_items()
#解析
    tensors = decoder.decode(serialized_example, items=keys)
    tensor_dict = dict(zip(keys, tensors))
    is_crowd = fields.InputDataFields.groundtruth_is_crowd
    tensor_dict[is_crowd] = tf.cast(tensor_dict[is_crowd], dtype=tf.bool)
    tensor_dict[fields.InputDataFields.image].set_shape([None, None, 3])
    return tensor_dict

数据已经获取,接下来就是solver了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容