我正在尝试训练一个CNN,我有3个数据源。换句话说,我有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数组。labels是标签的数组;在本例中,数组中将有3个标签。
然后我试着按如下方式训练它:
model.fit_generator(generator_three_imgs(0),
steps_per_epoch=23, epochs=1, verbose=2)
但它不能训练。Jupyter notebook崩溃,原因是出现以下消息:
The kernel appears to have died. It will restart automatically.
我应该在这里做什么?尝试构建一个迷你批次,用3个不同的Keras生成器从不同的目录中获取图像,这是错误的吗?
提前谢谢你!
发布于 2018-05-29 06:43:25
你为什么不像我们其他人一样把文件复制到组合文件夹中呢?你似乎在为一个不需要存在的问题设计一个解决方案。
https://stackoverflow.com/questions/50559373
复制相似问题