tf.train.Features TypeError:不允许位置参数

时间:2017-04-23 04:25:36

标签: python tensorflow

我可能在这里做些蠢事,但我不确定为什么我会收到这个错误。

此代码有效:

example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': _int64_feature(FLAGS.img_height),
      'image/width': _int64_feature(FLAGS.img_width),
      'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
      'image/channels': _int64_feature(channels),
      'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
      'image/label': _bytes_feature(label_img_buffer),
      'image/label_path': _bytes_feature(tf.compat.as_bytes(os.path.basename(lbl_path))),
      'image/fn_0': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[0]))),
      'image/encoded_0': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[0])),
      'image/fn_1': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[1]))),
      'image/encoded_1': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[1])),
      'image/fn_2': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[2]))),
      'image/encoded_2': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[2]))}))
return example

但是这段代码不起作用(在帖子标题中抛出TypeError):

feature_dict={
      'image/height': _int64_feature(FLAGS.img_height),
      'image/width': _int64_feature(FLAGS.img_width),
      'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
      'image/channels': _int64_feature(channels),
      'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
      'image/label': _bytes_feature(label_img_buffer),
      'image/label_path': _bytes_feature(tf.compat.as_bytes(os.path.basename(lbl_path))),
      }

  for idx, image in sorted(ex_image_buffers.iteritems()):
    img_key = 'image/encoded_' + str(idx)
    fn_key = 'image/fn_' + str(idx)
    feature_dict[img_key] = _bytes_feature(tf.compat.as_bytes(image))
    feature_dict[fn_key] = _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[idx])))

  example = tf.train.Example(features=tf.train.Features(feature_dict))
  return example

ex_image_buffers是一个列表。

据我所知,tf.train.Features将字典作为参数,我在第一个例子和第二个例子中组装了相同的字典(我认为)。第二个允许我根据其他一些代码调整字典,所以我宁愿避免硬编码不同的字段。

想法?谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

是的,我认为你有一个愚蠢的错误。尝试

example = tf.train.Example(features=tf.train.Features(feature=feature_dict))

作为错误状态,tf.train.Features要求您传递关键字/参数对。您需要添加关键字feature,就像您提供的第一个示例中所做的那样。