【发布时间】:2018-06-04 23:47:27
【问题描述】:
我想使用 Tensorflow 的 Dataset API 来读取变体长度列表的 TFRecords 文件。这是我的代码。
def _int64_feature(value):
# value must be a numpy array.
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def main1():
# Write an array to TFrecord.
# a is an array which contains lists of variant length.
a = np.array([[0, 54, 91, 153, 177],
[0, 50, 89, 147, 196],
[0, 38, 79, 157],
[0, 49, 89, 147, 177],
[0, 32, 73, 145]])
writer = tf.python_io.TFRecordWriter('file')
for i in range(a.shape[0]): # i = 0 ~ 4
x_train = a[i]
feature = {'i': _int64_feature(np.array([i])), 'data': _int64_feature(x_train)}
# Create an example protocol buffer
example = tf.train.Example(features=tf.train.Features(feature=feature))
# Serialize to string and write on the file
writer.write(example.SerializeToString())
writer.close()
# Check TFRocord file.
record_iterator = tf.python_io.tf_record_iterator(path='file')
for string_record in record_iterator:
example = tf.train.Example()
example.ParseFromString(string_record)
i = (example.features.feature['i'].int64_list.value)
data = (example.features.feature['data'].int64_list.value)
#data = np.fromstring(data_string, dtype=np.int64)
print(i, data)
# Use Dataset API to read the TFRecord file.
def _parse_function(example_proto):
keys_to_features = {'i' :tf.FixedLenFeature([], tf.int64),
'data':tf.FixedLenFeature([], tf.int64)}
parsed_features = tf.parse_single_example(example_proto, keys_to_features)
return parsed_features['i'], parsed_features['data']
ds = tf.data.TFRecordDataset('file')
iterator = ds.map(_parse_function).make_one_shot_iterator()
i, data = iterator.get_next()
with tf.Session() as sess:
print(i.eval())
print(data.eval())
检查 TFRecord 文件
[0] [0, 54, 91, 153, 177]
[1] [0, 50, 89, 147, 196]
[2] [0, 38, 79, 157]
[3] [0, 49, 89, 147, 177]
[4] [0, 32, 73, 145]
但当我尝试使用 Dataset API 读取 TFRecord 文件时,它显示以下错误。
tensorflow.python.framework.errors_impl.InvalidArgumentError:名称: ,键:数据,索引:0。int64 值的数量!= 预期。 值大小:5 但输出形状:[]
谢谢。
更新:
我尝试使用以下代码通过 Dataset API 读取 TFRecord,但都失败了。
def _parse_function(example_proto):
keys_to_features = {'i' :tf.FixedLenFeature([], tf.int64),
'data':tf.VarLenFeature(tf.int64)}
parsed_features = tf.parse_single_example(example_proto, keys_to_features)
return parsed_features['i'], parsed_features['data']
ds = tf.data.TFRecordDataset('file')
iterator = ds.map(_parse_function).make_one_shot_iterator()
i, data = iterator.get_next()
with tf.Session() as sess:
print(sess.run([i, data]))
或
def _parse_function(example_proto):
keys_to_features = {'i' :tf.VarLenFeature(tf.int64),
'data':tf.VarLenFeature(tf.int64)}
parsed_features = tf.parse_single_example(example_proto, keys_to_features)
return parsed_features['i'], parsed_features['data']
ds = tf.data.TFRecordDataset('file')
iterator = ds.map(_parse_function).make_one_shot_iterator()
i, data = iterator.get_next()
with tf.Session() as sess:
print(sess.run([i, data]))
还有错误:
Traceback(最近一次调用最后):文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py”, 第 468 行,在 make_tensor_proto 中 str_values = [compat.as_bytes(x) for x in proto_values] 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", 第 468 行,在 str_values = [compat.as_bytes(x) for x in proto_values] 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/compat.py", 第 65 行,在 as_bytes 中 (bytes_or_text,)) TypeError: Expected binary or unicode string, got
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):文件“2tfrecord.py”,第 126 行,在 main1() 文件“2tfrecord.py”,第 72 行,在 main1 iterator = ds.map(_parse_function).make_one_shot_iterator() 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", 第 712 行,在地图中 返回 MapDataset(self, map_func) 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", 第 1385 行,在 init 中 self._map_func.add_to_graph(ops.get_default_graph()) 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/function.py", 第 486 行,在 add_to_graph self._create_definition_if_needed() 文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/function.py”, 第 321 行,在 _create_definition_if_needed self._create_definition_if_needed_impl() 文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/function.py”, 第 338 行,在 _create_definition_if_needed_impl 输出= self._func(*输入)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py”, 第 1376 行,在 tf_map_func 中 flattened_ret = [ops.convert_to_tensor(t) for t in nest.flatten(ret)] 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", 第 1376 行,在 flattened_ret = [ops.convert_to_tensor(t) for t in nest.flatten(ret)] 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", 第 836 行,在 convert_to_tensor 中 as_ref=False) 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", 第 926 行,internal_convert_to_tensor ret = conversion_func(值,dtype=dtype,name=name,as_ref=as_ref)文件 “/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py”, 第 229 行,在 _constant_tensor_conversion_function 返回常量(v,dtype=dtype,name=name)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py”, 第 208 行,保持不变 值,dtype=dtype,shape=shape,verify_shape=verify_shape)) 文件 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", 第 472 行,在 make_tensor_proto 中 “支持的类型。” % (type(values), values)) TypeError: 无法将类型对象转换为张量。 内容: SparseTensor(indices=Tensor("ParseSingleExample/Slice_Indices_i:0", 形状=(?, 1), dtype=int64), values=Tensor("ParseSingleExample/ParseExample/ParseExample:3", 形状=(?,), dtype=int64), dense_shape=Tensor("ParseSingleExample/Squeeze_Shape_i:0", shape=(1,), dtype=int64))。考虑将元素转换为支持的类型。
Python 版本:3.5.2
TensorFlow 版本:1.4.1
【问题讨论】:
标签: python tensorflow tfrecord