Skip to main content
 首页 » 编程设计

python之切片 Tensorflow FixedLengthRecordReader 值

2025年05月04日22zdz8207

我正在使用 Tensorflow/python API 实现一个将图像映射到姿势的回归网络,并尝试处理 FixedLengthRecordReader 的输出。

我正在尝试调整 cifar10 example最低限度地满足我的目的。

cifar10 示例读取原始字节、解码,然后拆分。

result.key, value = reader.read(filename_queue) 
 
# Convert from a string to a vector of uint8 that is record_bytes long. 
record_bytes = tf.decode_raw(value, tf.uint8) 
 
# The first bytes represent the label, which we convert from uint8->int32. 
result.label = tf.cast( 
    tf.slice(record_bytes, [0], [label_bytes]), tf.int32) 
 
# The remaining bytes after the label represent the image, which we reshape 
# from [depth * height * width] to [depth, height, width]. 
depth_major = tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]), 
                         [result.depth, result.height, result.width]) 
# Convert from [depth, height, width] to [height, width, depth]. 
result.uint8image = tf.transpose(depth_major, [1, 2, 0]) 

我正在读取二进制文件列表,其中数据保存为 (pose_data, image_data)。因为我的pose数据是float32,图像数据是uint8,所以想先切片,再转换。不幸的是,reader.read 的值结果是一个零维字符串张量,因此切片不起作用。

key, value = reader.read(filename_queue) 
print value.dtype 
print value.get_shape() 
 
<dtype: 'string'> 
() 

tf.decode_raw(value, dtype) 的结果是一维数组,但需要指定 dtype,而 tf.string 不是它采用的有效类型。

解码前是否可以分片?或者我是否必须解码 -> 大小写返回字符串 -> 切片 -> 重铸?有别的办法吗?

请您参考如下方法:

OP 提到的 cifar10 示例以及解码两次的解决方案在您的数据具有多种类型(OP 的问题)并且没有“排队”(更一般的情况)时不起作用。

例如,如果您的数据是:

[float32][int16][int16] 

两次解码有效。但是,如果您的数据是:

[int16][float32][int16] 

它不起作用,因为 tf.decode_raw 不接受半个 float32 的偏移量。

在这种情况下有效的是 tf.substr() , 返回值来自

result.key, value = reader.read(filename_queue) 

实际上是一个字符串(如果你愿意的话,也可以是一个字节串)并让自己 split 。