如何组合两个 keras 生成器功能

How do I combine two keras generator functions(如何组合两个 keras 生成器功能)
本文介绍了如何组合两个 keras 生成器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

I am trying to implement a Siamese network in Keras and I want to apply image transformations to the 2 input images using Keras Image Data Generators. As per the example in the docs- https://keras.io/preprocessing/image/, I've tried to implement it like this-

datagen_args = dict(rotation_range=10,
                    width_shift_range=0.1,
                    height_shift_range=0.1,
                    horizontal_flip=True)

in_gen1 = ImageDataGenerator(**datagen_args)
in_gen2 = ImageDataGenerator(**datagen_args)

train_generator = zip(in_gen1, in_gen2)

model.fit(train_generator.flow([pair_df[:, 0,::],pair_df[:, 1,::]],
                          y_train,batch_size=16), epochs, verbose = 1)

But this code throws this error:

TypeError: zip argument #1 must support iteration

I've tried using itertools.izip as suggested in Keras - Generator for large dataset of Images and Masks but this throws the same error.

How do I resolve this?

EDIT: In case anyone is interested, this worked finally-

datagen_args = dict(
    featurewise_center=False,
    rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True)

in_gen1 = ImageDataGenerator(**datagen_args)
in_gen2 = ImageDataGenerator(**datagen_args)

in_gen1 = in_gen1.flow(pair_df[:, 0,::], y_train, batch_size = 16, shuffle = False)
in_gen2 = in_gen2.flow(pair_df[:, 1,::], y_train, batch_size = 16, shuffle = False)

for e in range(epochs):
    batches = 0
    for x1, x2 in itertools.izip(in_gen1,in_gen2):
    # x1, x2 are tuples returned by the generator, check whether targets match
        assert sum(x1[1] != x2[1]) == 0  
        model.fit([x1[0], x2[0]], x1[1], verbose = 1)
        batches +=1
        if(batches >= len(pair_df)/16):
            break

解决方案

Using zip() to combine generators leads to generation of an infinite iterator. Use this instead:

def combine_generator(gen1, gen2):
    while True:
        yield(next(gen1), next(gen2))

Modified code would look something like this:

datagen_args = dict(rotation_range=10,
                    width_shift_range=0.1,
                    height_shift_range=0.1,
                    horizontal_flip=True)

in_gen1 = ImageDataGenerator(**datagen_args)
in_gen2 = ImageDataGenerator(**datagen_args)

def combine_generator(gen1, gen2):
    while True:
        yield(next(gen1), next(gen2))

train_generator = combine_generator(in_gen1, in_gen2)

model.fit(train_generator.flow([pair_df[:, 0,::],pair_df[:, 1,::]],
                          y_train,batch_size=16), epochs, verbose = 1)

See this thread for further reference.

这篇关于如何组合两个 keras 生成器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

cv2.approxPolyDP() , cv2.arcLength() How these works(cv2.approxPolyDP() , cv2.arcLength() 这些是如何工作的)
How To Detect Red Color In OpenCV Python?(如何在 OpenCV Python 中检测红色?)
Distance between 2 pixels(2个像素之间的距离)
How to get length of each side of a shape in an image?(如何获取图像中形状每一边的长度?)
How to upload dataset in google colaboratory?(如何在谷歌合作实验室上传数据集?)
Fast and Robust Image Stitching Algorithm for many images in Python?(Python中许多图像的快速而强大的图像拼接算法?)