利用 python/keras/tensorflow 计算 DICE系数,评估 3D 分割结果,只挑选特定层面

之前在文章:https://blog.csdn.net/Sweety_Lin/article/details/104199580 中写的DICE计算程序,是将Nii的整个volume计算在内,现想提取T2中有lesion的特定层面来计算DICE:

import nibabel as nib
import scipy.io as io
import os
import numpy as np
import tensorflow as tf
from keras import backend as K


def dice_coefficient(y_true, y_pred, smooth=0.00001):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)


pred_dir = '/home/share/20200207_precess_lactate/DICE_NEW/SELECT/Lac_mask'
true_dir = '/home/share/20200207_precess_lactate/DICE_NEW/SELECT/T2_mask'
pred_filenames = os.listdir(pred_dir)
pred_filenames.sort(key=lambda x:x[:-2])

true_filenames = os.listdir(true_dir)
true_filenames.sort(key=lambda x:x[:-8])

dice_value = np.zeros(18)
temp = []
for f in range(18):
    pred_path = os.path.join(pred_dir, pred_filenames[f])
    img_pred = nib.load(pred_path)
    y_pred = img_pred.get_fdata()
    true_path = os.path.join(true_dir, true_filenames[f])
    img_true = nib.load(true_path)
    y_true = img_true.get_fdata()
    select = []
    for slices in range(121):
        if np.sum(y_true[:, :, slices]) > 0:
            select.append(slices)
            pass
        pass
    temp.append(dice_coefficient(y_true[:, :, select], y_pred[:, :, select]))
    print(select)
    pass


with tf.Session() as sess:
    dice_value = sess.run(temp)
    pass

mat_path = '/home/share/20200207_precess_lactate/DICE_NEW/SELECT/DICE_Lac.mat'
io.savemat(mat_path, {'DICE_value': dice_value})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值