【问题标题】:How to use sample weights with data augmentation in Keras?如何在 Keras 中使用样本权重和数据增强?
【发布时间】:2017-10-14 12:25:15
【问题描述】:

我正在尝试在像素分割任务中使用带有ImageGenerator 的样本加权。

  • 图像和掩码是 numpy 数组 (634, 1, 64, 64)
  • 掩码有 5 个类(编码为 0 到 4)
  • sample_weight 是一个数组 (634, 64, 64)
  • 使用.flow 对具有相同seed=42batch_size=32 的2 个生成器进行数据增强
  • 正在使用sample_weight_mode='temporal' 编译模型
  • Unet 模型中的最后 3 层是:

    conv2d_19 (Conv2D)  (None, 5, 64, 64)     325        
    dropout_18[0][0]                 
    
    conv2d_20 (Conv2D)  (None, 1, 64, 64)     6          
    conv2d_19[0][0]                  
    
    activation_1 (Activation) (None, 1, 64, 64)     0
    conv2d_20[0][0]
    

这会引发错误:

ValueError: Found a sample_weight array with shape (634, 64, 64). In order to use timestep-wise sample weighting, you should pass a 2D sample_weight array.

如果我将 sample_weight 重塑为 (634, 4096),我会得到:

ValueError: Found a sample_weight array with shape (634, 4096) for an input with shape (32, 1, 64, 64). sample_weight cannot be broadcast.

这是我对如何使用ImageGenerator 的误解还是无法处理这种特殊情况?

如果我不使用样本权重,模型会运行并且不会引发错误。

Keras 2.0.4,Theano 0.9

【问题讨论】:

    标签: python machine-learning deep-learning keras theano


    【解决方案1】:

    Keras 样本权重在默认模式下以一维数组(每个特征一个权重)作为输入,在时间模式下以二维数组作为输入。时间模式可能适合您,但您需要进行以下更改。基本上,Keras 不支持 3D 权重,因此您必须在输入端将 64x64 图像展平,并在卷积层之前将张量重塑为 2D。

    1) 将 sample_weight 重塑为 634,4096

    2) 添加一个输入层,用于接受 4096 像素长的展平图像:Keras 现在将接受每像素权重,将像素视为时间序列(一维信号)

    3) 在第一个 2D 卷积之前添加一个 Reshape 层

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2018-03-29
      相关资源
      最近更新 更多