"code to calculate dice score" Code Answer's

You're definitely familiar with the best coding language Python that developers use to develop their projects and they get all their queries like "code to calculate dice score" answered properly. Developers are finding an appropriate answer about code to calculate dice score related to the Python coding language. By visiting this online portal developers get answers concerning Python codes question like code to calculate dice score. Enter your desired code related query in the search bar and get every piece of information about Python code related question on code to calculate dice score. 

code to calculate dice score

By Embarrassed EmuEmbarrassed Emu on May 07, 2020
import numpy as np
np.random.seed(0)
true = np.random.rand(10, 5, 5, 4)>0.5
pred = np.random.rand(10, 5, 5, 4)>0.5

def single_dice_coef(y_true, y_pred_bin):
    # shape of y_true and y_pred_bin: (height, width)
    intersection = np.sum(y_true * y_pred_bin)
    if (np.sum(y_true)==0) and (np.sum(y_pred_bin)==0):
        return 1
    return (2*intersection) / (np.sum(y_true) + np.sum(y_pred_bin))

def mean_dice_coef(y_true, y_pred_bin):
    # shape of y_true and y_pred_bin: (n_samples, height, width, n_channels)
    batch_size = y_true.shape[0]
    channel_num = y_true.shape[-1]
    mean_dice_channel = 0.
    for i in range(batch_size):
        for j in range(channel_num):
            channel_dice = single_dice_coef(y_true[i, :, :, j], y_pred_bin[i, :, :, j])
            mean_dice_channel += channel_dice/(channel_num*batch_size)
    return mean_dice_channel

def dice_coef2(y_true, y_pred):
    y_true_f = y_true.flatten()
    y_pred_f = y_pred.flatten()
    union = np.sum(y_true_f) + np.sum(y_pred_f)
    if union==0: return 1
    intersection = np.sum(y_true_f * y_pred_f)
    return 2. * intersection / union

print(mean_dice_coef(true, pred))
print(dice_coef2(true, pred))

# 0.4884357140842496
# 0.499001996007984

Source: www.kaggle.com

Add Comment

1

All those coders who are working on the Python based application and are stuck on code to calculate dice score can get a collection of related answers to their query. Programmers need to enter their query on code to calculate dice score related to Python code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about code to calculate dice score for the programmers working on Python code while coding their module. Coders are also allowed to rectify already present answers of code to calculate dice score while working on the Python language code. Developers can add up suggestions if they deem fit any other answer relating to "code to calculate dice score". Visit this developer's friendly online web community, CodeProZone, and get your queries like code to calculate dice score resolved professionally and stay updated to the latest Python updates. 

Python answers related to "code to calculate dice score"

View All Python queries

Python queries related to "code to calculate dice score"

Browse Other Code Languages

CodeProZone