【问题标题】:How to reuse images already loaded to memory in a CNN Keras?如何在 CNN Keras 中重用已加载到内存中的图像?
【发布时间】:2019-04-15 05:32:41
【问题描述】:

背景

我正在构建一个以图像和其他数字数据作为输入的神经网络。我正在使用带有 Tensorflow 后端的 Keras。 该网络有 2 个输入部分:
1) 图像,然后是几个卷积层
2) 数值数据,然后是密集层

唯一图像的数量很少,但训练数据量很大。
大多数图像在输入数据中出现多次。
(唯一图像数:约 2,000
训练数据:500,000 行)

例子:

|---------------------|------------------|  
|      Image_id       | Other data cols  |  
|---------------------|------------------|  
|          1          |         1        |  
|---------------------|------------------|  
|          1          |         2        |  
|---------------------|------------------|  
|          1          |         3        |  
|---------------------|------------------|  
|          2          |         4        |  
|---------------------|------------------|  
|          2          |         5        |  
|---------------------|------------------|  

我的问题:

有没有办法在训练期间重用内存中已经存在的图像数据,而不是用所有相应的图像数据创建一个巨大的 numpy 数组?

通常的方法是这样的:

scaled pixel values for image 1 - other data 1  
scaled pixel values for image 1 - other data 2  
scaled pixel values for image 1 - other data 3  
...  
scaled pixel values for image 1 - other data 1000  

如您所见,这种方法会导致一个带有重复图像数据的 numpy 数组占用过多内存。

在网上很难找到类似的案例。谢谢你的建议。

【问题讨论】:

  • 您是否在单个输入中重复相同的图像?如果是,为什么?
  • 我在不同的输入行中使用相同的图像。想象一个模型试图预测什么样的用户会喜欢 Instagram 图片。 (如果不同,我的实际用例。)
  • 25 岁,女性,input_image_1 -> LIKE | 26 岁,女性,input_image_1 -> LIKE | 42 岁,男性,input_image_1 -> 不喜欢我有很多行输入数据,但实际图像要少得多。我想在训练时按 id 查找图像。
  • 你可以编写一个自定义的DataLoader,你只需要加载一次不重复的图像,然后在每批根据其ID分配相应的图像
  • 我明白了!我是个白痴,我完全错过了它。像这样:model.fit_generator(generator=data_generator_class_made_by_me.my_function(params), ...)

标签: image-processing keras conv-neural-network


【解决方案1】:

您可以使用 Keras 中的自定义 Data Geberator 类来实现。查看here 以了解如何操作。你需要在这里修改什么

  1. 加载一次图像并保存在ID(string)-data(numpy array) 字典中。
  2. 在__init__ 中传递此字典 (data_dict)
  3. 在__data_generation 中使用X[i,] = data_dict[ID]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2020-04-15
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多