首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从3个Keras生成器中制作1个小批量

如何从3个Keras生成器中制作1个小批量
EN

Stack Overflow用户
提问于 2018-05-28 12:58:46
回答 1查看 226关注 0票数 0

我正在尝试训练一个CNN,我有3个数据源。换句话说,我有3个包含图像的文件夹,并且我需要在每个训练步骤中从每个文件夹中获取1张图像。

我制作了以下生成器:

代码语言:javascript
复制
def generator_three_imgs(index, batch_size=1):
    anchor_paths = [r'C:\Users\sinthes\Desktop\AI_anaconda\face_recognition\dataset\train\E\Anchor',
                    r'C:\Users\sinthes\Desktop\AI_anaconda\face_recognition\dataset\train\T\Anchor']
    positive_paths = [r'C:\Users\sinthes\Desktop\AI_anaconda\face_recognition\dataset\train\E\Positive',
                      r'C:\Users\sinthes\Desktop\AI_anaconda\face_recognition\dataset\train\T\Positive']
    negative_paths = [r'C:\Users\sinthes\Desktop\AI_anaconda\face_recognition\dataset\train\E\Negative',
                      r'C:\Users\sinthes\Desktop\AI_anaconda\face_recognition\dataset\train\T\Negative']

    generator1 = ImageDataGenerator()
    generator2 = ImageDataGenerator()
    generator3 = ImageDataGenerator()
    anchor_train_batches = generator1.flow_from_directory(anchor_paths[index], target_size=(224, 224), batch_size=batch_size)
    positive_train_batches = generator2.flow_from_directory(positive_paths[index], target_size=(224, 224), batch_size=batch_size)
    negative_train_batches = generator3.flow_from_directory(negative_paths[index], target_size=(224, 224), batch_size=batch_size)
    while True:
        anchor_imgs, anchor_labels = anchor_train_batches.next()
        positive_imgs, positive_labels = positive_train_batches.next()
        negative_imgs, negative_labels = negative_train_batches.next()
        input_imgs = np.append(anchor_imgs, positive_imgs, axis=0)
        input_imgs = np.append(input_imgs, negative_imgs, axis=0)
        labels = np.append(anchor_labels, positive_labels, axis=0)
        labels = np.append(labels, negative_labels, axis=0)
        yield input_imgs, labels

因此,input_imgs是一个(3,224,224,3)维Numpy数组。labels是标签的数组;在本例中,数组中将有3个标签。

然后我试着按如下方式训练它:

代码语言:javascript
复制
model.fit_generator(generator_three_imgs(0),
                    steps_per_epoch=23, epochs=1, verbose=2)

但它不能训练。Jupyter notebook崩溃,原因是出现以下消息:

代码语言:javascript
复制
The kernel appears to have died. It will restart automatically.

我应该在这里做什么?尝试构建一个迷你批次,用3个不同的Keras生成器从不同的目录中获取图像,这是错误的吗?

提前谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2018-05-29 06:43:25

你为什么不像我们其他人一样把文件复制到组合文件夹中呢?你似乎在为一个不需要存在的问题设计一个解决方案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50559373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档