【问题标题】:tensorflow record with float numpy array带有浮点数数组的张量流记录
【发布时间】:2017-05-05 21:54:02
【问题描述】:

我想创建 tensorflow 记录来支持我的模型; 到目前为止,我使用以下代码将 uint8 numpy 数组存储为 TFRecord 格式;

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


def _bytes_feature(value):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


def _floats_feature(value):
  return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))


def convert_to_record(name, image, label, map):
    filename = os.path.join(params.TRAINING_RECORDS_DATA_DIR, name + '.' + params.DATA_EXT)

    writer = tf.python_io.TFRecordWriter(filename)

    image_raw = image.tostring()
    map_raw   = map.tostring()
    label_raw = label.tostring()

    example = tf.train.Example(features=tf.train.Features(feature={
        'image_raw': _bytes_feature(image_raw),
        'map_raw': _bytes_feature(map_raw),
        'label_raw': _bytes_feature(label_raw)
    }))        
    writer.write(example.SerializeToString())
    writer.close()

我用这个示例代码读到的

features = tf.parse_single_example(example, features={
  'image_raw': tf.FixedLenFeature([], tf.string),
  'map_raw': tf.FixedLenFeature([], tf.string),
  'label_raw': tf.FixedLenFeature([], tf.string),
})

image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape(params.IMAGE_HEIGHT*params.IMAGE_WIDTH*3)
image = tf.reshape(image_, (params.IMAGE_HEIGHT,params.IMAGE_WIDTH,3))

map = tf.decode_raw(features['map_raw'], tf.uint8)
map.set_shape(params.MAP_HEIGHT*params.MAP_WIDTH*params.MAP_DEPTH)
map = tf.reshape(map, (params.MAP_HEIGHT,params.MAP_WIDTH,params.MAP_DEPTH))

label = tf.decode_raw(features['label_raw'], tf.uint8)
label.set_shape(params.NUM_CLASSES)

而且效果很好。现在我想对我的数组“map”做同样的事情,它是一个浮点数数组,而不是 uint8,我找不到如何做的例子; 我尝试了函数_floats_feature,如果我将标量传递给它,它会起作用,但不适用于数组; 使用 uint8 可以通过 tostring() 方法进行序列化;

如何序列化一个浮点 numpy 数组以及如何将其读回?

【问题讨论】:

    标签: numpy tensorflow


    【解决方案1】:

    FloatListBytesList 期待一个可迭代的。所以你需要向它传递一个浮动列表。删除_float_feature 中多余的括号,即

    def _floats_feature(value):
      return tf.train.Feature(float_list=tf.train.FloatList(value=value))
    
    numpy_arr = np.ones((3,)).astype(np.float)
    example = tf.train.Example(features=tf.train.Features(feature={"bytes": _floats_feature(numpy_arr)}))
    print(example)
    
    features {
      feature {
        key: "bytes"
        value {
          float_list {
            value: 1.0
            value: 1.0
            value: 1.0
          }
        }
      }
    }
    

    【讨论】:

    • 如何处理二维 NumPy 数组
    【解决方案2】:

    我将扩展雅罗斯拉夫的答案。

    Int64List、BytesList 和 FloatList 需要 iterator of the underlying elements(重复字段)。在您的情况下,您可以使用列表作为迭代器。

    您提到:如果我将标量传递给它,它会起作用,但对于数组则不起作用。这是意料之中的,因为当您传递一个标量时,您的 _floats_feature 在其中创建一个包含一个浮点元素的数组(完全符合预期)。但是,当您传递一个数组时,您会创建一个数组列表并将其传递给一个需要浮点数列表的函数。

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

    【讨论】:

      【解决方案3】:

      当输入 nd 数组时,Yaroslav 的示例失败:

      numpy_arr = np.ones((3,3)).astype(np.float)

      我发现当我使用 numpy_arr.ravel() 作为输入时它起作用了。但是有没有更好的方法呢?

      【讨论】:

      • Yaroslav 提到您需要浮动列表,num_arr 不是列表,因此您必须以某种方式将其展平,然后在将其传递给模型之前修复其形状。
      【解决方案4】:

      我在处理类似问题时偶然发现了这一点。由于原始问题的一部分是如何从tfrecords 读回float32 功能,我将把它留在这里以防它对任何人有帮助:

      如果使用map.ravel() 将维度[x, y, z]map 输入到_floats_feature

      features = {
          ...
          'map': tf.FixedLenFeature([x, y, z], dtype=tf.float32)
          ...
      }
      parsed_example = tf.parse_single_example(serialized=serialized, features=features)
      map = parsed_example['map']
      

      【讨论】:

        【解决方案5】:

        首先,非常感谢雅罗斯拉夫和萨尔瓦多的启发性答案。

        根据我的经验,他们的方法仅在输入为大小为(n, ) 的一维 NumPy 数组时才有效。当输入为维度大于2的Numpy数组时,会出现如下错误信息:

        def _float_feature(value):
            return tf.train.Feature(float_list=tf.train.FloatList(value=value))
        
        numpy_arr = np.arange(12).reshape(2, 2, 3).astype(np.float)
        example = tf.train.Example(features=tf.train.Features(feature={"bytes": 
        _float_feature(numpy_arr)}))
        print(example)
        
        
        TypeError: array([[0., 1., 2.],
           [3., 4., 5.]]) has type numpy.ndarray, but expected one of: int, long, float
        

        所以,我想扩展 Tsuan 的答案,即在输入到 TF 示例之前将其展平。修改后的代码如下:

        def _floats_feature(value):
            return tf.train.Feature(float_list=tf.train.FloatList(value=value))
        
        numpy_arr = np.arange(12).reshape(2, 2, 3).astype(np.float).flatten()
        example = tf.train.Example(features=tf.train.Features(feature={"bytes": 
        _float_feature(numpy_arr)}))
        print(example)
        

        另外,np.flatten()np.ravel() 更适用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-19
          相关资源
          最近更新 更多