簡體   English   中英

如何在隊列中解碼 Tensorflow 中的 pfm 文件?

[英]how to decode pfm files in Tensorflow in a queue?

我制作了一個文件名隊列,文件是 *.pfm 文件。 我編寫了一個轉換函數readPFM()將 *.pfm 文件轉換為 ndarray。

我想要做的是,當文件從隊列中出隊時,我將使用該函數將其轉換為 numpy ndarray。 然后它將被輸入到圖表中。 但是代碼不起作用。

def disparity(batch_size, path, LR, epochs=2):
    filenames = file_name(path, LR, 'pfm')
    filenames = sorted(filenames)

    filename_queue = tf.train.string_input_producer(filenames, shuffle=False, num_epochs=epochs)
    reader = tf.WholeFileReader()
    key, img_bytes = reader.read(filename_queue)
    disparity, _ = readPFM(img_bytes)

    return tf.train.batch([disparity], batch_size, dynamic_pad=True)

pfm 文件讀取功能在這里。

def readPFM(file):
    file = open(file, 'rb')

    color = None
    width = None
    height = None
    scale = None
    endian = None

    header = file.readline().rstrip()
    if header == 'PF':
        color = True
    elif header == 'Pf':
        color = False
    else:
        raise Exception('Not a PFM file.')

    dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline())
    if dim_match:
        width, height = map(int, dim_match.groups())
    else:
        raise Exception('Malformed PFM header.')

    scale = float(file.readline().rstrip())
    if scale < 0:  # little-endian
        endian = '<'
        scale = -scale
    else:
        endian = '>'  # big-endian

    data = np.fromfile(file, endian + 'f')
    shape = (height, width, 3) if color else (height, width)

    data = np.reshape(data, shape)
    data = np.flipud(data)
    return data, scale


def writePFM(file, image, scale=1):
    file = open(file, 'wb')

    color = None

    if image.dtype.name != 'float32':
        raise Exception('Image dtype must be float32.')

    image = np.flipud(image)

    if len(image.shape) == 3 and image.shape[2] == 3:  # color image
        color = True
    elif len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1:  # greyscale
        color = False
    else:
        raise Exception('Image must have H x W x 3, H x W x 1 or H x W dimensions.')

    file.write('PF\n' if color else 'Pf\n')
    file.write('%d %d\n' % (image.shape[1], image.shape[0]))

    endian = image.dtype.byteorder

    if endian == '<' or endian == '=' and sys.byteorder == 'little':
        scale = -scale

    file.write('%f\n' % scale)

    image.tofile(file)

錯誤消息顯示我的函數無法處理張量,因為它只能處理 *.pfm 文件。

有什么解決辦法嗎?

你不能像在 tensorflow 中那樣使用你的readPFM函數,你需要用tf.py_func把它包裹起來。

# helper function
def decode_pfm(path):
    data, _ = load_pfm(open(path, 'rb'))

    # http://netpbm.sourceforge.net/doc/pfm.html
    # pfm stores the data bottom-to-top, need to reverse
    data = np.flipud(data)
    data = np.expand_dims(data, 2)
    return data

def read_and_decode(path):
    image_decoded = tf.py_func(decode_pfm, [path], tf.float32)

    # py_func does not set the shape, you might need to explictly
    # set it
    image_decoded.set_shape((H, W, channels))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM