Skip to content

Instantly share code, notes, and snippets.

@prerakmody
Last active June 24, 2022 14:34
Show Gist options
  • Save prerakmody/e014b180d15c08e0c62f10ae52a2bf14 to your computer and use it in GitHub Desktop.
Save prerakmody/e014b180d15c08e0c62f10ae52a2bf14 to your computer and use it in GitHub Desktop.
Segmentation Loss
import numpy as np
import matplotlib.pyplot as plt
f,axarr = plt.subplots(1,2)
# Step 1
x = [0.01, 0.02, 0.03, 0.04] + np.arange(0.05,1.05,0.05).tolist()
y_ce = [-np.log(each) for each in x]
y_focal1 = [-1*(1 - each)**1*np.log(each) for each in x]
y_focal2 = [-1*(1 - each)**2*np.log(each) for each in x]
y_focal3 = [-1*(1 - each)**3*np.log(each) for each in x]
y_mse = [(1-each)**2 for each in x]
dice = lambda x: 1.0 - 2*(x*1.0) / (x + 1.0)
y_dice = [dice(each) for each in x]
axarr[0].plot(x, y_mse, label='Mean Squared Error (p)^2')
axarr[0].plot(x, y_ce, label='Cross Entropy (log(p))')
axarr[0].plot(x, y_focal1, label='Focal ((1-p)^1.log(p))')
axarr[0].plot(x, y_focal2, label='Focal ((1-p)^2.log(p))')
axarr[0].plot(x, y_focal3, label='Focal ((1-p)^3.log(p))')
axarr[0].plot(x, y_dice, label='DICE (1 - 2p/(p+1))')
axarr[0].legend()
axarr[0].set_xlim([0,1])
axarr[0].set_ylim(bottom=0)
x = np.arange(0.75,1.05,0.05).tolist()
y_ce = [-np.log(each) for each in x]
y_focal1 = [-1*(1 - each)**1*np.log(each) for each in x]
y_focal2 = [-1*(1 - each)**2*np.log(each) for each in x]
y_focal3 = [-1*(1 - each)**3*np.log(each) for each in x]
y_mse = [(1-each)**2 for each in x]
dice = lambda x: 1.0 - 2*(x*1.0) / (x + 1.0)
y_dice = [dice(each) for each in x]
axarr[1].plot(x, y_mse, label='Mean Squared Error (p)^2')
axarr[1].plot(x, y_ce, label='Cross Entropy (log(p))')
axarr[1].plot(x, y_focal1, label='Focal ((1-p)^1.log(p))')
axarr[1].plot(x, y_focal2, label='Focal ((1-p)^2.log(p))')
axarr[1].plot(x, y_focal3, label='Focal ((1-p)^3.log(p))')
axarr[1].plot(x, y_dice, label='DICE (1 - 2p/(p+1))')
axarr[1].legend()
axarr[1].set_xlim([0.7,1])
axarr[1].set_ylim(bottom=0)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment