我已经将图像及其标签的目录转换为TFRecords文件,特征映射包括image_raw、label、height、width和depth。函数如下:
def convert_to_tfrecords(data_samples, filename):
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]))
writer = tf.python_io.TFRecordWriter(filename)
for fname, lb in data_samples:
im = cv2.imread(fname, cv2.IMREAD_UNCHANGED)
image_raw = im.tostring()
feats = tf.train.Features(
feature =
{
'image_raw': _bytes_feature(image_raw),
'label': _int64_feature(int(lb)),
'height': _int64_feature(im.shape[0]),
'width': _int64_feature(im.shape[1]),
'depth': _int64_feature(im.shape[2])
}
)
example = tf.train.Example(features=feats)
writer.write(example.SerializeToString())
writer.close()现在,我想读取这个TFRecords文件来提供输入管道。但是,由于image_raw已被展平,我们需要将其重塑为原始的[height, width, depth]大小。那么,如何从TFRecords文件中获取height、width和depth的值呢?下面的代码似乎无法工作,因为height是一个没有值的张量。
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
feats = {
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64)
}
features = tf.parse_single_example(serialized_example, features=feats)
image = tf.decode_raw(features['image_raw'], tf.uint8)
label = tf.cast(features['label'], tf.int32)
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
depth = tf.cast(features['depth'], tf.int32)
image = tf.reshape(image, [height, width, depth]) # <== not work
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
return image, label当我阅读Tensorflow的官方文档时,我发现它们通常会传递到一个已知的大小,即[224,224,3]。但是,我不喜欢它,因为这些信息已经存储到TFRecords文件中,手动传递到固定大小不能确保大小与文件中存储的数据一致。
有什么好主意吗?
发布于 2016-10-28 02:56:40
tf.parse_single_example返回的height是一个张量,获取其值的唯一方法是对其调用session.run()或类似方法。然而,我认为这有点过头了。
由于Tensorflow示例只是一个协议缓冲区(请参阅文档),因此您不必使用tf.parse_single_example来读取它。你可以自己解析它,然后直接读出你想要的形状。
你也可以考虑在Tensorflow的github问题跟踪器上提交一个特性请求-我同意这个API对于这个用例看起来有点笨拙。
发布于 2018-12-05 16:54:35
函数'tf.reshape‘只接受张量,不接受张量列表,因此您可以使用以下代码:
image = tf.reshape(image, tf.stack([height, width, depth]))发布于 2020-07-01 04:41:49
您还可以使用np.resize()将维数作为参数传递,从而从张量中获取numpy数组并进行整形
https://stackoverflow.com/questions/40258943
复制相似问题