Working of the Earth Mover Loss method in Keras and input arguments data types

2024/11/17 6:32:06

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)

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

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

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

The EML simply compares the CDF of predictions vs realities

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] # the sample belong to the 4th class

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

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_pred = [0.1,0.1,0.1,1,1,1,1,1,1,1]

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

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:

  • digitalize the float scores in 10 intervals
  • one-hot encode the labels to get softmax probabilities and minimize 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]

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

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
https://en.xdnf.cn/q/71578.html

Related Q&A

Django Rest Framework writable nested serializer with multiple nested objects

Im trying to create a writable nested serializer. My parent model is Game and the nested models are Measurements. I am trying to post this data to my DRF application using AJAX. However, when try to po…

Django How to Serialize from ManyToManyField and List All

Im developing a mobile application backend with Django 1.9.1 I implemented the follower model and now I want to list all of the followers of a user but Im currently stuck to do that. I also use Django…

PyDrive and Google Drive - automate verification process?

Im trying to use PyDrive to upload files to Google Drive using a local Python script which I want to automate so it can run every day via a cron job. Ive stored the client OAuth ID and secret for the G…

Using rm * (wildcard) in envoy: No such file or directory

Im using Python and Envoy. I need to delete all files in a directory. Apart from some files, the directory is empty. In a terminal this would be:rm /tmp/my_silly_directory/*Common sense dictates that i…

cant import django model into celery task

i have the following task:from __future__ import absolute_importfrom myproject.celery import appfrom myapp.models import Entity@app.task def add(entity_id):entity = Entity.objects.get(pk=entity_id)retu…

Running unit tests with Nose inside a Python environment such as Autodesk Maya?

Id like to start creating unit tests for my Maya scripts. These scripts must be run inside the Maya environment and rely on the maya.cmds module namespace.How can I run Nose tests from inside a runnin…

Python Newline \n not working in jupyter notebooks

Im trying to display the tuples of a postgreSQL table neatly in my Jupyter Notebook, but the newline \n escape character doesnt seem to work here (it works for my python scripts w/ same code outside of…

Dynamically calling functions - Python

I have a list of functions... e.g.def filter_bunnies(pets): ...def filter_turtles(pets): ...def filter_narwhals(pets): ...Is there a way to call these functions by using a string representing their nam…

py2exe windows service problem

I have successfully converted my python project to a service. When using the usual options of install and start/stop, everything works correctly. However, I wish to compile the project using py2exe, …

Draw a cumulative chart from a pandas dataframe?

I have a dataframe as follows:df = pd.DataFrame({cost_saving: [10, 10, 20, 40, 60, 60],id: [a, b, c, d, e, f]})How can I draw a cumulative chart of the savings?Im thinking of a line chart with number …