【发布时间】: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