-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassessment.py
More file actions
69 lines (47 loc) · 2.1 KB
/
Copy pathassessment.py
File metadata and controls
69 lines (47 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from scipy.spatial.distance import directed_hausdorff
from medpy.metric.binary import hd, hd95, dc, jc,positive_predictive_value
def dice(pred, gt, class_label):
# im1 = im1 == tid
# im2 = im2 == tid
# im1 = np.asarray(im1).astype(np.bool_)
# im2 = np.asarray(im2).astype(np.bool_)
# if im1.shape != im2.shape:
# raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
pred_binary = (pred == class_label).astype(np.uint8)
gt_binary = (gt == class_label).astype(np.uint8)
assert pred_binary.shape == gt_binary.shape, "预测和真实标注的尺寸必须相同"
# Compute Dice coefficient
dsc = dc(pred_binary, gt_binary)
return dsc
def jaccard(pred, gt, class_label):
# im1 = im1 == tid
# im2 = im2 == tid
# im1 = np.asarray(im1, dtype=np.bool_)
# im2 = np.asarray(im2, dtype=np.bool_)
#
# if im1.shape != im2.shape:
# raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
pred_binary = (pred == class_label).astype(np.uint8)
gt_binary = (gt == class_label).astype(np.uint8)
assert pred_binary.shape == gt_binary.shape, "预测和真实标注的尺寸必须相同"
jac = jc(pred_binary, gt_binary)
return jac
def calculate_ppv(pred, gt, class_label):
# 将预测和真实标注二值化,只保留当前类别的点
pred_binary = (pred == class_label).astype(np.uint8)
gt_binary = (gt == class_label).astype(np.uint8)
assert pred_binary.shape == gt_binary.shape, "预测和真实标注的尺寸必须相同"
ppv = positive_predictive_value(pred_binary, gt_binary)
return ppv
def calculate_hd95(pred, gt, class_label):
# 计算豪斯多夫距离
pred_binary = (pred == class_label).astype(np.uint8)
gt_binary = (gt == class_label).astype(np.uint8)
assert pred_binary.shape == gt_binary.shape, "预测和真实标注的尺寸必须相同"
#hausdorff_distance = hd(pred, gt)
hausdorff_distance95 = hd95(pred_binary,gt_binary)
return hausdorff_distance95