首页 文章

切片Tensorflow FixedLengthRecordReader值

提问于
浏览
2

我正在实现一个回归网络映射图像到姿势,使用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) . 因为我的姿势数据是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不是它所采用的有效类型 .

在解码之前切片是否可行?或者我必须解码 - >案例回到字符串 - >切片 - >重铸?还有另一种方式吗?

2 回答

  • -1

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

    如果您的数据是例如:

    [float32][int16][int16]
    

    两次解码工作 . 但是,如果您的数据是:

    [int16][float32][int16]
    

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

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

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

    实际上是一个字符串(如果你愿意,还是一个字节串),让它自己分裂 .

  • 0

    找到了一个解决方案:解码两次并扔掉一半 . 效率不高(如果有人有更好的解决方案,我会很高兴听到它),但似乎有效 .

    key, value = reader.read(filename_queue)
    uint8_bytes = tf.decode_raw(value, tf.uint8)
    uint8_data = uint8_bytes[:n_uint8_vals]
    float32_bytes = tf.decode_raw(value, tf.float32)
    float32_start_index = n_uint8_vals // 4
    float32_data = float32_bytes[float32_start_index:]
    

    这要求 n_uint8_vals 是4的因子 .

相关问题