Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some questions #96

Open
ruti0823 opened this issue Nov 6, 2024 · 2 comments
Open

some questions #96

ruti0823 opened this issue Nov 6, 2024 · 2 comments

Comments

@ruti0823
Copy link

ruti0823 commented Nov 6, 2024

请问有友友成功复现这篇代码了吗?我复现的结果,除了第一个指标外,其余的都是零,这是什么问题呀?

@xiaoyu-s-alt
Copy link

请问有友友成功复现这篇代码了吗?我复现的结果,除了第一个指标外,其余的都是零,这是什么问题呀?
我也碰到了这个问题,很难受,请问您解决这个问题了吗

@zhen-guang
Copy link

I made some changes to the function for calculating metrics to adapt to the latest version of PyTorch, but I cannot guarantee that my changes are completely correct.

def get_sensitivity(SR, GT, threshold=0.5):
    # Sensitivity == Recall
    SR = SR > threshold
    GT = GT == torch.max(GT)

    # TP : True Positive
    # FN : False Negative
    TP = (SR == 1) & (GT == 1) 
    FN = (SR == 0) & (GT == 1)

    SE = float(torch.sum(TP)) / (float(torch.sum(TP) + torch.sum(FN)) + 1e-6)

    return SE


def get_specificity(SR, GT, threshold=0.5):
    SR = SR > threshold
    GT = GT == torch.max(GT)

    # TN : True Negative
    # FP : False Positive
    TN = (SR == 0) & (GT == 0)
    FP = (SR == 1) & (GT == 0)

    SP = float(torch.sum(TN)) / (float(torch.sum(TN) + torch.sum(FP)) + 1e-6)

    return SP


def get_precision(SR, GT, threshold=0.5):
    SR = SR > threshold
    GT = GT == torch.max(GT)

    # TP : True Positive
    # FP : False Positive
    TP = (SR == 1) & (GT == 1)
    FP = (SR == 1) & (GT == 0)

    PC = float(torch.sum(TP)) / (float(torch.sum(TP) + torch.sum(FP)) + 1e-6)

    return PC


def get_F1(SR, GT, threshold=0.5):
    # Sensitivity == Recall
    SE = get_sensitivity(SR, GT, threshold=threshold)
    PC = get_precision(SR, GT, threshold=threshold)

    F1 = 2 * SE * PC / (SE + PC + 1e-6)

    return F1


def get_JS(SR, GT, threshold=0.5):
    # JS : Jaccard similarity
    SR = SR > threshold
    GT = GT == torch.max(GT)

    Inter = torch.sum(SR & GT)
    Union = torch.sum(SR | GT)

    JS = float(Inter) / (float(Union) + 1e-6)

    return JS


def get_DC(SR, GT, threshold=0.5):
    # DC : Dice Coefficient
    SR = SR > threshold
    GT = GT == torch.max(GT)

    Inter = torch.sum(SR & GT)
    DC = float(2 * Inter) / (float(torch.sum(SR) + torch.sum(GT)) + 1e-6)

    return DC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants