【问题标题】:how to make 1 mini batch out of 3 Keras generators如何用 3 个 Keras 生成器制作 1 个 mini batch
【发布时间】:2018-05-29 15:29:54
【问题描述】:

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

我做了以下生成器:

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 数组。 标签是标签数组;在这种情况下,数组中将有 3 个标签。

然后我尝试如下训练它:

model.fit_generator(generator_three_imgs(0),
                    steps_per_epoch=23, epochs=1, verbose=2)

但它不能训练。 Jupyter 笔记本通过提供以下消息而崩溃:

The kernel appears to have died. It will restart automatically.

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

提前谢谢你!

【问题讨论】:

    标签: tensorflow machine-learning neural-network keras convolutional-neural-network


    【解决方案1】:

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

    【讨论】:

    • 你可能是对的。我最初试图通过使用三元组损失方法来解决人脸验证/识别问题。当我训练连体网络时,我有 3 个输入流(用于锚、正、负图像)。但是当我以后想使用我的模型进行预测时,我没有 3 个输入,而只有 1 个。这里描述了这个问题:stackoverflow.com/questions/50558154/…你有什么 cmets 吗?
    • @edn 你不能使用变分自动编码器进行面部识别吗?
    • 你能解释一下什么是变分编码器吗?
    • @edn 它就像一个 CNN,但它接受一个输入并在表示它之前存储一个潜在的。这允许网络随后将输入与已训练的图像进行比较并给出分数。
    • 阅读这篇“机器学习很有趣!第 4 部分:现代人脸识别与深度学习”medium.com/@ageitgey/…
    猜你喜欢
    • 1970-01-01
    • 2018-03-01
    • 2022-01-15
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    相关资源
    最近更新 更多