dice,准确率(Accuracy)、精确率(Precision)、召回率(Recall)、F值(F-Measure),PCC计算pytorch代码

具体的原理就不讲解了,直接上代码
label和predict都是one-hot 型

def compute_PCC(predicts,label):
    assert(predicts.shape==label.shape)
    PCC=[]
    for i in range(predicts.shape[1]):
        n = predicts.shape[0]*predicts.shape[2]*predicts.shape[3]
        TP = ((predicts[:,i,:,:]==1)*(label[:,i,:,:]==1)).sum()
        sum_x = predicts[:,i,:,:].sum()
        sum_y = label[:,i,:,:].sum()
        sum_x2 = predicts[:,i,:,:].pow(2).sum()
        sum_y2 = label[:,i,:,:].pow(2).sum()
        molecular = TP-(float(sum_x)*float(sum_y)/n)
        denominator = ((sum_x2-float(sum_x2**2)/n)*(sum_y2-float(sum_y2**2)/n)).sqrt()
        PCC.append( molecular/denominator)

    return PCC
def compute_score(predicts, label,n_class=2):

    (predicts.shape == label.shape)

    PC=[]
    SE=[]
    Jaccard=[]
    SP=[]
    F1=[]
    overlap = ((predicts == 1) * (label == 1)).sum().float()  # TP
    # print('overlap:',overlap)
    if (overlap > 0):
        for i in range(predicts.shape[1]):
            overlap = ((predicts[:, i, :, :] == 1) * (label[:, i, :, :] == 1)).sum().float()  # TP
            dice = 2 * overlap / ((predicts[:,i,:,:] == 1).sum() + (label[:,i,:,:] == 1).sum()).float()
            PC.append(overlap / (predicts[:,i,:,:] == 1).sum().float())
            SE .append(overlap / (label[:,i,:,:] == 1).sum().float())
            Jaccard.append(overlap / ((predicts[:,i,:,:] == 1).sum() + (label[:,i,:,:] == 1).sum() - overlap).float())
            SP.append(((predicts[:,i,:,:] == 0) * (label[:,i,:,:] == 0)).sum().float()/(label[:,i,:,:] == 0).sum().float())
            F1 .append(2*overlap / (label[:,i,:,:] == 1).sum().float()*overlap / (predicts[:,i,:,:] == 1).sum().float()/(overlap / (label[:,i,:,:] == 1).sum() + overlap / (predicts[:,i,:,:] == 1).sum().float() + 1e-6))
        return PC, SE, Jaccard, SP
    else:
        return 0, 0, 0, 0

你可能感兴趣的:(dice,准确率(Accuracy)、精确率(Precision)、召回率(Recall)、F值(F-Measure),PCC计算pytorch代码)