Keras:使用flow_from _directory()函数为两个输入模型创建定制生成器 [英] Keras: Create a custom generator for two input model using flow_from _directory() function

查看:297
本文介绍了Keras:使用flow_from _directory()函数为两个输入模型创建定制生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用fit_generator()训练我的暹罗网络,我从以下答案中学到了:

I was trying to train my siamese network with fit_generator(),I learned from this answer: Keras: How to use fit_generator with multiple inputs that the best way to do this was to create your own generator that yield the multiple data points, my problem was that I retrieve my data with flow_from_directory() function and I didn't know if that was possible.

This is my attempt to readapt a generator for my problem:

from keras.models import load_model
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
import numpy as np

model = load_model("siamese_model.h5")

train_datagen = ImageDataGenerator(rescale = 1./255)

def generator():
    t1 = train_datagen.flow_from_directory(base_dir,target_size = (150, 150), batch_size = 20, class_mode = 'categorical',shuffle = True)
    t2 = train_datagen.flow_from_directory(base_dir,target_size = (150, 150), batch_size = 20, class_mode = 'categorical', shuffle = True)
    while True:
        d1,y = t1.next()
        d2 = t2.next()
        yield ([d1[0], d2[0]],y)

model.compile(loss = 'categorical_crossentropy',optimizer= optimizers.RMSprop(lr=2e-5),metrics=['acc'])

history = model.fit_generator(generator(),
                              steps_per_epoch = 10,
                              epochs = 5)

我的代码给了我与尝试在没有自定义生成器的情况下拟合模型时完全相同的错误:

My code give me the exact same error as when I tried to fit my model without the custom generator:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[[0.14509805, 0.15686275, 0.16862746],
         [0.14509805, 0.15686275, 0.16862746],
         [0.14509805, 0.15686275, 0.16862746],
         ...,
         [0.14117648, 0.15294118, 0.16862746...

我在做什么错了?

推荐答案

尝试以下方法:

while True:
    d1 = t1.next()
    d2 = t2.next()
    yield ([d1[0], d2[0]], d1[1])

此外,您输入的内容将以不同的方式随机播放,因此,如果您按一定的顺序将它们放置在文件夹中,它们将失去链接.

Also , your input will be shuffle in a different way so they will lose their link if you put them in a certain order in your folder.

我建议:

t1 = train_datagen.flow_from_directory(base_dir,target_size = (150, 150), batch_size = 20, class_mode = 'categorical', shuffle = False, seed='13')

t2 = train_datagen.flow_from_directory(base_dir,target_size = (150, 150), batch_size = 20, class_mode = 'categorical', shuffle = False, seed='13')

或具有相同的洗牌种子

t1 = train_datagen.flow_from_directory(base_dir,target_size = (150, 150), batch_size = 20, class_mode = 'categorical', shuffle = True, seed='13')

t2 = train_datagen.flow_from_directory(base_dir,target_size = (150, 150), batch_size = 20, class_mode = 'categorical', shuffle = True, seed='13')

这篇关于Keras:使用flow_from _directory()函数为两个输入模型创建定制生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆