Paddle
Paddle copied to clipboard
How to use multiple item data reader creator?
When I yield multiple item, and wrapper for batch, I can not get multiple item by using next(reader). I got this issue:
Traceback (most recent call last):
File "data_reader.py", line 251, in <module>
data_thr,data_tlr=next(THR_reader)
ValueError: too many values to unpack
This is my code:
import numpy as np
from PIL import Image
import utils
import os
# print train_hr_img_list
def reader_creater_hr(list_file, shuffle=True, return_name=False):
# print(list_file.shape)
def reader():
while True:
if shuffle:
np.random.shuffle(list_file)
for file in list_file:
# file = file.strip("\n\r\t ")
image = Image.open(file)
image = np.array(image)
image = crop_sub_imgs_fn(image)
image_lr = downsample_fn(image)
# print(image.shape)
# image = image.resize((256, 256))
# image = np.array(image) / 127.5 - 1
# if len(image.shape) != 3:
# continue
image = image[:, :, 0:3].astype("float32")
image_lr = image_lr[:, :, 0:3].astype("float32")
image = image.transpose([2, 0, 1])
image_lr = image_lr.transpose([2, 0, 1])
# print(image[np.newaxis, :].shape)
if return_name:
yield image[np.newaxis, :], image_lr[np.newaxis, :], os.path.basename(file)
else:
yield image[np.newaxis, :], image_lr[np.newaxis, :]
return reader
def train_hr_reader():
return reader_creater_hr(train_hr_img_list)
import paddle.v2 as paddle
import paddle.fluid as fluid
THR_reader = paddle.batch(train_hr_reader(), 16)()
data_thr,data_tlr=next(THR_reader)
I find the doc of paddlepaddle which yield multiple item data.
def reader_creator_random_image_and_label(width, height, label):
def reader():
while True:
yield numpy.random.uniform(-1, 1, size=width*height), label
return reader
So How is the correct way using yield for multiple item?
This is because the return of next(THR_reader) is a list with length 16, and each element is a tuple yield by the reader. Try these code:
values = next(THR_reader)
data_thr = map(lambda x : x[0], values)
data_tlr= map(lambda x : x[1], values)
This is because the return of
next(THR_reader)is a list with length 16, and each element is a tuple yield by the reader. Try these code:values = next(THR_reader) data_thr = map(lambda x : x[0], values) data_tlr= map(lambda x : x[1], values)
Thanks a lot:+1: It work for me.