当图像以 0 到 9 的小数进行评级时,Earth Mover Loss 的输入类型应该是什么(Keras、Tensorflow)

Des*_*wal 6 python image-recognition deep-learning keras tensorflow

我正在尝试实施谷歌的 NIMA 研究论文,他们对图像质量进行评分。我正在使用 TID2013 数据集。我有 3000 张图像,每张图像的分数从 0.00 到 9.00

df.head()
>>
Image Name          Score
0   I01_01_1.bmp    5.51429
1   i01_01_2.bmp    5.56757
2   i01_01_3.bmp    4.94444
3   i01_01_4.bmp    4.37838
4   i01_01_5.bmp    3.86486

Run Code Online (Sandbox Code Playgroud)

找到了下面给出的损失函数代码

def earth_mover_loss(y_true, y_pred):
    cdf_true = K.cumsum(y_true, axis=-1)
    cdf_pred = K.cumsum(y_pred, axis=-1)
    emd = K.sqrt(K.mean(K.square(cdf_true - cdf_pred), axis=-1))
    return K.mean(emd)
Run Code Online (Sandbox Code Playgroud)

我将模型构建的代码编写为:

base_model = InceptionResNetV2(input_shape=(W,H, 3),include_top=False,pooling='avg',weights='imagenet')
for layer in base_model.layers: 
    layer.trainable = False

x = Dropout(0.45)(base_model.output)
out = Dense(10, activation='softmax')(x) # there are 10 classes

model = Model(base_model.input, out)
optimizer = Adam(lr=0.001)
model.compile(optimizer,loss=earth_mover_loss,)
Run Code Online (Sandbox Code Playgroud)

问题:当我ImageDataGenerator用作:

gen=ImageDataGenerator(validation_split=0.15,preprocessing_function=preprocess_input)

train = gen.flow_from_dataframe(df,TRAIN_PATH,x_col='Image Name',y_col='Score',subset='training',class_mode='sparse')

val = gen.flow_from_dataframe(df,TRAIN_PATH,x_col='Image Name',y_col='Score',subset='validation',class_mode='sparse')
Run Code Online (Sandbox Code Playgroud)

它要么在训练期间给出错误,要么给出损失值 nan

我尝试了几种方法:

  1. 创建分数rounded = math.round(score)并使用class_mode=sparse
  2. 创建分数作为str(rounded)然后使用class_mode=categorical

但我每次都有错误。

请帮助我加载图像使用ImageDataGenerator关于我应该如何将图像加载到这个模型中

模型结构不应改变。

Mar*_*ani 3

根据这里的介绍,我对NaN 梯度有一些想法......

我认为你的损失是 nan 因为 sqrt 是根据负数计算的,这是不允许的。所以有两种可能性:

  • 在应用 sqrt 之前剪裁值。通过这种方式,我们剪切所有 <= 0 的值,用一个小的 epsilon 替换它们

    def earth_mover_loss(y_true, y_pred):
        cdf_true = K.clip(K.cumsum(y_true, axis=-1), 0,1)
        cdf_pred = K.clip(K.cumsum(y_pred, axis=-1), 0,1)
        emd = K.sqrt(K.maximum(K.mean(K.square(cdf_true - cdf_pred), axis=-1), K.epsilon()))
        return K.mean(emd)
    
    Run Code Online (Sandbox Code Playgroud)
  • 排除 sqrt,这样,Earth Mover Loss 更类似于 CDF 之间的 MSE

    def earth_mover_loss(y_true, y_pred):
        cdf_true = K.clip(K.cumsum(y_true, axis=-1), 0,1)
        cdf_pred = K.clip(K.cumsum(y_pred, axis=-1), 0,1)
        emd = K.mean(K.square(cdf_true - cdf_pred), axis=-1)
        return K.mean(emd)
    
    Run Code Online (Sandbox Code Playgroud)