我已经建立了一个文件名队列,文件是* .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文件。
有没有解决方案?
答案 0 :(得分:1)
您无法在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))