【问题标题】:Create an int list feature to save as tfrecord in tensorflow?创建一个 int 列表功能以在 tensorflow 中另存为 tfrecord?
【发布时间】:2018-12-07 03:08:20
【问题描述】:

如何从列表中创建张量流记录?

documentation 看来,这似乎是可能的。还有这个example,他们使用来自numpy的.tostring()将一个numpy数组转换为一个字节数组。但是,当我尝试传入时:

labels = np.asarray([[1,2,3],[4,5,6]])
...
example = tf.train.Example(features=tf.train.Features(feature={
    'height': _int64_feature(rows),
    'width': _int64_feature(cols),
    'depth': _int64_feature(depth),
    'label': _int64_feature(labels[index]),
    'image_raw': _bytes_feature(image_raw)}))
writer.write(example.SerializeToString())

我得到错误:

TypeError: array([1, 2, 3]) has type type 'numpy.ndarray', but expected one of: (type 'int', type 'long')

这并不能帮助我弄清楚如何将整数列表存储到 tfrecord 中。我试过浏览文档。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    经过一段时间的处理并在文档中进一步查看后,我找到了自己的答案。 在上面的函数中使用示例代码作为基础:

    def _int64_feature(value):
      return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
    ...
    'label': _int64_feature(labels[index]),
    

    labels[index] 被作为 [value] 转换为列表,因此您有 [np.array([1,2,3])] 导致错误。

    上述转换在示例中是必要的,因为 tf.train.Int64List() 需要一个列表或 numpy 数组,并且该示例传入一个整数,因此他们将其类型转换为列表。
    在示例中是这样的

    label = [1,2,3,4]
    ...
    'label': _int64_feature(label[index]) 
    
    tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
    #Where value = [1] in this case
    

    如果您想传入一个列表,请执行此操作

    labels = np.asarray([[1,2,3],[4,5,6]])
    ...
    def _int64_feature(value):
      return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
    ...
    'label': _int64_feature(labels[index]),
    

    我可能会提出拉取请求,因为我发现 tf.train.Feature 的原始文档几乎不存在。

    TL;DR

    将列表或 numpy 数组传递给 tf.train.Int64List() 但不是列表列表或 numpy 数组列表。

    【讨论】:

    • 如何将 int 提取回 numpy?
    • 这是一个单独的问题,应该在其他地方提出。我不太了解 tfrecords 的内部表示,无法回答如何将其转换回来。给定 tfrecord 恢复数据的最简单方法是通过 tensorflow 图运行它,其中唯一的操作是 tf.identity(tfrecord)。然后就可以提取内容了。内部表示可能会随着时间而改变,所以我认为这是最适合未来的方法。
    • 到 TF int 张量:height = tf.cast(features['height'], tf.int64) 到 numpy 数组:stackoverflow.com/a/36026969/99379。你是对的,但它是如此密切相关。 :-) 谢谢!
    【解决方案2】:

    据我了解,您希望在 tfrecord 中存储整数列表。 可以根据文档存储其中一个打包的 BytesList、FloatList 或 Int64List https://github.com/tensorflow/tensorflow/blob/r0.9/tensorflow/core/example/example.proto

    如果您查看示例,他们正在使用函数 _int64_feature,在其中他们正在创建传递给函数的值列表

        def _int64_feature(value):
          return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
    

    在您的情况下,您试图将列表作为值传递给函数 _int64_feature,因此它会出错。

    因此请改用它,这将解决您存储 int 值列表的错误或根据您的需要修改上述函数。

    'label': tf.train.Feature(int64_list=tf.train.Int64List(value=labels[index]))
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      Int64ListBytesListFloatList 期望 iterator of the underlying elementsrepeated 字段)。对于您的函数_int64_feature,您使用列表作为迭代器。

      当您传递一个标量时,您的 _int64_feature 在其中创建一个包含一个 int64 元素的数组(完全符合预期)。但是,当您传递一个 ndarray 时,您会创建一个包含一个 ndarray 的列表并将其传递给一个需要 int64 列表的函数。

      所以只需从你的函数中删除数组的构造:int64_list=tf.train.Int64List(value=value)

      【讨论】:

        【解决方案4】:

        一种是把value=[value]改成value=value,但是如果你想传递list的list或者numpy.arrays的list,这是很常见的情况,如果你想保存x,y,一个分子的所有原子的 z 坐标,实际上您可以先将数组变平,然后使用 value = value。例如,

            array_1 = np.array([[1,2,3],[2,3,4]]).ravel()
        

        如果你想在阅读 tfrecord 文件或训练时把它放回去,你可以使用 reshape

            array_1 = array_1.reshape([2,3])
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-05-04
          • 1970-01-01
          • 2013-08-03
          • 2012-02-02
          • 2017-07-04
          • 1970-01-01
          相关资源
          最近更新 更多