相关疑难解决方法(0)

如何将numpy数组存储为tfrecord?

我正在尝试从numpy数组创建tfrecord格式的数据集.我想存储2d和3d坐标.

2d坐标是float64类型的numpy数组(2,10)3d坐标是float64类型的numpy数组(3,10)

这是我的代码:

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


train_filename = 'train.tfrecords'  # address to save the TFRecords file
writer = tf.python_io.TFRecordWriter(train_filename)


for c in range(0,1000):

    #get 2d and 3d coordinates and save in c2d and c3d

    feature = {'train/coord2d': _floats_feature(c2d),
                   'train/coord3d': _floats_feature(c3d)}
    sample = tf.train.Example(features=tf.train.Features(feature=feature))
    writer.write(sample.SerializeToString())

writer.close()
Run Code Online (Sandbox Code Playgroud)

当我运行这个我得到错误:

  feature = {'train/coord2d': _floats_feature(c2d),
  File "genData.py", line 19, in _floats_feature
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\google\protobuf\internal\python_message.py", line 510, in init
copy.extend(field_value)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\google\protobuf\internal\containers.py", line 275, in extend
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
  File …
Run Code Online (Sandbox Code Playgroud)

python numpy tensorflow tfrecord

12
推荐指数
3
解决办法
5615
查看次数

标签 统计

numpy ×1

python ×1

tensorflow ×1

tfrecord ×1