Keras中的“地球移动器损耗”方法和输入参数数据类型的工作 [英] Working of the Earth Mover Loss method in Keras and input arguments data types

查看:68
本文介绍了Keras中的“地球移动器损耗”方法和输入参数数据类型的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Keras / Tensrflow中找到了一个有关地球破坏者损失的代码。我想计算图像得分的损失,但是直到了解以下给出的地球移动者损失的工作方法后,我才能做。有人可以描述一下代码中正在发生的事情吗?

I have found a code for the Earth Mover Loss in Keras/Tensrflow. I want to compute the loss for the scores given to images but I can not do it until I get to know the working of the Earth Mover Loss given below. Can someone please describe that what is happening in the code.

模型或输出层的最后一层是:

The last layer of the model or output layer is like:

out = Dense(10,activation='softmax')(x)

应该输入什么我的 y_labels 的形式为 1.2,4.9 等,我想使用它使用Keras / Tensorflow

What should be the input types for this method.I have my y_labels in the form of 1.2,4.9 etc etc. I want to use it with Keras/Tensorflow

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)


推荐答案

给定N个类别,您可以将EML看作是CDF概率函数的一种RMSE

you can see EML as a sort of RMSE for CDF probability functions

您需要的是为每个样本归一化的概率得分。在神经网络领域,这是通过softmax激活函数作为输出层来实现的。

given N classes, all you need is a normalized probability score for each sample. in neural network domains, this is achieved with softmax activation function as output layer

EML只是比较预测的CDF与现实的CDF

The EML simply compares the CDF of predictions vs realities

在具有10个类的分类问题中,对于单个样本,我们可以拥有这些数组

In a classification problem with 10 classes, for a single sample, we can have these arrays

y_true = [0,0,0,1,0 ,0,0,0,0,0]#样本属于第四类

y_true = [0,0,0,1,0,0,0,0,0,0] # the sample belong to the 4th class

y_pred = [0.1,0,0,0.9,0,0,0 ,0,0,0]#softmax层的概率输出

y_pred = [0.1,0,0,0.9,0,0,0,0,0,0] # probabilities output of softmax layer

在它们上,我们计算CDF并获得以下得分:

on them we compute CDFs and get the following scores:

CDF_y_true = [0,0,0,1,1,1,1,1,1,1]

CDF_y_true = [0,0,0,1,1,1,1,1,1,1]

CDF_y_pred = [0.1,0.1,0.1 ,1,1,1,1,1,1,1,1]

CDF_y_pred = [0.1,0.1,0.1,1,1,1,1,1,1,1]

如上定义,EML在此CDF上计算RMSE

as defined above, the EML compute the RMSE on this CDFs

y_true = np.asarray([0.,0.,0.,1.,0.,0.,0.,0.,0.,0.])
y_pred = np.asarray([0.1,0.,0.,0.9,0.,0.,0.,0.,0.,0.])

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))

在Google在TID2013上发表的NIMA Paper的特定案例中,N = 10,并且标签以浮点数的形式表示。为了使用EML训练网络,需要遵循以下步骤:

In the specific case of NIMA Paper by Google on TID2013, N=10 and the labels are express in the form of float scores. In order to train the network with EML these are the steps to follow:


  • 以10个间隔对浮点得分进行数字化

  • 一个热编码的标签以获取softmax概率并使EML最小化

在火车结束时,我们的神经网络能够在给定图像上为每个类别产生概率分数。
我们必须将此分数转换为具有本文定义的相关标准偏差的平均质量分数。
为此,我们将按照文件中定义的步骤

at the end of the train, our NN is able to produce, on a given image, a probability score for each class. we have to transform this score in a mean quality score with a related standard deviation as defined in the paper. to do this we follow the procedure defined in the paper

bins = [1,2,3,4,5,6,7,8,9 ,10]

bins = [1,2,3,4,5,6,7,8,9,10]

y_pred = [0.1,0,0,0.9,0,0,0,0,0,0]#softmax层的概率输出

y_pred = [0.1,0,0,0.9,0,0,0,0,0,0] # probabilities output of softmax layer

mu_score = sum(bins * y_pred)= 1 * 0.1 + 2 * 0 + 3 * 0 + 4 * 0.9 + ... + 10 * 0

mu_score = sum(bins*y_pred) = 1*0.1 + 2*0 + 3*0 + 4*0.9 + ... + 10*0

sigma_score = sum(((bins-mu_score)** 2)* y_pred)** 0.5

sigma_score = sum(((bins - mu_score)**2)*y_pred)**0.5

bins = np.arange(1,11)
y_pred = np.asarray([0.1,0.,0.,0.9,0.,0.,0.,0.,0.,0.])

mu_score = np.sum(bins*y_pred)
std_score = np.sum(((bins - mu_score)**2)*y_pred)**0.5

这篇关于Keras中的“地球移动器损耗”方法和输入参数数据类型的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆