【问题标题】:Using ImageDataGenerator with images in .npy format将 ImageDataGenerator 与 .npy 格式的图像一起使用
【发布时间】:2019-01-08 23:08:24
【问题描述】:
我对 Keras 很陌生。我正在尝试使用 ImageDataGenerator 训练模型。我有大量以 .npy 格式保存的训练图像。我想使用 flow_from_directory() 所以我按照文档中的建议存储了图像(每个类一个文件夹)。问题是这仅适用于 png、jpeg、tiff 等,但不适用于我的 .npy 文件。
我有什么方法可以使用这个函数或类似的东西,让我获得 ImageDataGenerator 提供的所有增强可能性?
非常感谢,感谢您的帮助
【问题讨论】:
标签:
python
image
keras
deep-learning
【解决方案1】:
是的,如果您愿意修改 ImageDataGenerator 的源代码(这实际上非常易于阅读和理解),这是可能的。查看keras-preprocessing github,我认为将DirectoryIterator 类中的load_img 方法替换为您自己的从磁盘读取.npy 文件而不是图像的load_array 方法就足够了:
...
# build batch of image data
for i, j in enumerate(index_array):
fname = self.filenames[j]
## Replace the code below with your own function
img = load_img(os.path.join(self.directory, fname),
color_mode=self.color_mode,
target_size=self.target_size,
interpolation=self.interpolation)
x = img_to_array(img, data_format=self.data_format)
...
因此,您至少可以对该行进行以下更改:
...
# build batch of image data
for i, j in enumerate(index_array):
fname = self.filenames[j]
img = np.load(os.path.join(self.directory, fname))
...
但您可能希望实现 Keras 的 load_img 实用函数也具有的一些附加逻辑,例如颜色模式、目标大小等,并将所有内容包装在您自己的 load_array 函数中。