Ali-Abdelhamid-Ali/The_Best_3D_Brain_Tumor_Segmentation_BraTS_2021
SMOOTH = 1e-6
def dicecoef(ytrue, ypred): ytruef = K.flatten(ytrue) ypredf = K.flatten(ypred) inter = K.sum(ytruef * ypredf) return (2. * inter + SMOOTH) / (K.sum(ytruef) + K.sum(ypred_f) + SMOOTH)
def diceloss(ytrue, ypred): return 1.0 - dicecoef(ytrue, ypred)
def ioucoef(ytrue, ypred, smooth=1e-6): ytrue = tf.squeeze(ytrue, axis=-1) numclasses = K.intshape(ypred)[-1] ytrueoh = tf.onehot(tf.cast(ytrue, tf.int32), depth=numclasses) ypredarg = K.argmax(ypred, axis=-1) ypredoh = tf.onehot(ypredarg, depth=numclasses) intersection = K.sum(ytrueoh * ypredoh, axis=[0,1,2,3]) union = K.sum(ytrueoh + ypredoh, axis=[0,1,2,3]) - intersection iou = (intersection + smooth) / (union + smooth) return K.mean(iou)
def combineddiceceloss(ytrue, ypred): ytrue = tf.squeeze(ytrue, axis=-1) numclasses = K.intshape(ypred)[-1] ytrueoh = tf.onehot(tf.cast(ytrue, tf.int32), depth=numclasses) dicesum = 0.0 for c in range(1, numclasses): dicesum += dicecoef(ytrueoh[..., c], ypred[..., c]) dicemean = dicesum / tf.cast(numclasses - 1, tf.float32) ce = K.categoricalcrossentropy(ytrueoh, ypred) cemean = K.mean(ce) return 0.5 (1.0 - dice_mean) + 0.5 ce_mean
def dicewholetumor(ytrue, ypred): ytrue = tf.squeeze(ytrue, axis=-1) numclasses = K.intshape(ypred)[-1] ytrueoh = tf.onehot(tf.cast(ytrue, tf.int32), depth=numclasses) ypredarg = K.argmax(y_pred, axis=-1)
ytruewt = K.cast(K.any(ytrueoh[..., 1:], axis=-1), 'float32') ypredwt = K.cast(K.any(tf.stack([tf.equal(ypredarg, 1), tf.equal(ypredarg, 2), tf.equal(ypredarg, 3)], axis=-1), axis=-1), 'float32') return dicecoef(ytruewt, ypred_wt)
def dicetumorcore(ytrue, ypred): ytrue = tf.squeeze(ytrue, axis=-1) numclasses = K.intshape(ypred)[-1] ytrueoh = tf.onehot(tf.cast(ytrue, tf.int32), depth=numclasses) ypredarg = K.argmax(y_pred, axis=-1)
ytruetc = K.cast(K.any(tf.stack([ytrueoh[..., 1], ytrueoh[..., 3]], axis=-1), axis=-1), 'float32') ypredtc = K.cast(K.any(tf.stack([tf.equal(ypredarg, 1), tf.equal(ypredarg, 3)], axis=-1), axis=-1), 'float32') return dicecoef(ytruetc, ypred_tc)
def diceenhancingtumor(ytrue, ypred): ytrue = tf.squeeze(ytrue, axis=-1) numclasses = K.intshape(ypred)[-1] ytrueoh = tf.onehot(tf.cast(ytrue, tf.int32), depth=numclasses) ypredarg = K.argmax(y_pred, axis=-1)
ytrueet = ytrueoh[..., 3] ypredet = K.cast(tf.equal(ypredarg, 3), 'float32') return dicecoef(ytrueet, ypred_et)
