I have created a custom generator that yields a tuple of corrupted_image
and original_image
:
def custom_image_generator(files, data_instances, batch_size = 64):
iter = 0
num_batches = data_instances / batch_size
while True:
iter = iter % num_batches
batch_input = []
proc_batch = []
start = random.randint(0, data_instances - batch_size)
for index in range(start, start batch_size):
original_image = Image.open(files[index])
corrupted_image = draw_square_on_image(files[index])
batch_input.append(original_image)
proc_batch.append(corrupted_image)
corrupted_images_batch = np.array(proc_batch)
original_images_batch = np.array(batch_input)
iter = iter 1
yield (corrupted_images_batch, original_images_batch)
If I call this generator like this:
(corrupted_images_batch, orig_images_batch) = next(test_generator)
It returns the expected output i.e 64 images in both the batches. For training my model I need to iterate over the entire batch.
But if I try doing something like:
for (corrupted_images_batch, orig_images_batch) in next(test_generator):
print(corrupted_images_batch)
I get an error: ValueError: too many values to unpack (expected 2)
.
CodePudding user response:
As demonstrated by (corrupted_images_batch, orig_images_batch) = next(test_generator)
, next(test_generator)
is a 2-tuple, so you can't loop over it unpacking each element into a 2-tuple.
What you're looking for is:
for (corrupted_images_batch, orig_images_batch) in test_generator:
print(corrupted_images_batch)
This way you're looping over the generator, not just the next element generated.