-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
174 lines (136 loc) · 5.32 KB
/
eval.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import torch
import torch.nn.functional as F
import time
import numpy as np
from tqdm import tqdm
from ptflops import get_model_complexity_info
import argparse
import os
import cv2
from tools.eval import available_dataset, evaluate
from dataset.test_dataset import TestDataset
from givtednet.model import GIVTEDNet
def parse_arguments():
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description="Testing configuration.")
# Add arguments
parser.add_argument(
"--image_size",
type=int,
default=224,
help="Testing image size.")
parser.add_argument(
"--epsilon",
type=float,
default=1e-8,
help="Small value for numerical stability.")
parser.add_argument(
"--threshold",
type=float,
default=0.5,
help="Threshold for prediction.")
# Parse arguments from the command line
return parser.parse_args()
def eval_fn():
# Parse arguments
config = parse_arguments()
print_once_param = True
warmup_counter = 20
for _data_name in available_dataset:
pth_path = f"./experiment/{_data_name}/model_pth/GIVTEDNet_best.pth"
model = GIVTEDNet()
if torch.cuda.is_available():
model.load_state_dict(torch.load(pth_path))
model.cuda()
else:
model.load_state_dict(
torch.load(
pth_path,
map_location=torch.device('cpu')))
if print_once_param:
macs, params = get_model_complexity_info(
model,
(3, config.image_size, config.image_size),
as_strings=True,
print_per_layer_stat=True,
verbose=True,
)
print("\n\n=======================================================")
print("{:<30} {:<8}".format("Computational complexity: ", macs))
print("{:<30} {:<8}".format("Number of parameters: ", params))
print("=======================================================\n\n")
print_once_param = False
if torch.cuda.is_available():
x = torch.rand(1, 3, config.image_size, config.image_size).cuda()
else:
x = torch.rand(1, 3, config.image_size, config.image_size).cpu()
model = torch.jit.trace(model, x)
model.eval()
with torch.no_grad():
for _ in tqdm(range(warmup_counter)):
if torch.cuda.is_available():
x = torch.rand(
1, 3, config.image_size, config.image_size).cuda()
else:
x = torch.rand(
1, 3, config.image_size, config.image_size).cpu()
y = model(x)
data_path_ = f"./experiment/{config.dataset_name}/TestDataset"
save_path_ = f"./experiment/{config.dataset_name}/result"
os.makedirs(save_path_, exist_ok=True)
image_root = "{data_path_}/images/"
gt_root = "{data_path_}/masks/"
if not os.path.exists(image_root) or not os.path.exists(gt_root):
continue
N = len(os.listdir(gt_root))
test_loader = TestDataset(image_root, gt_root, config.image_size)
DSC, IoU, Recall, Precision = list(), list(), list(), list()
FPS = list()
for i in tqdm(range(N)):
image, gt, name = test_loader.load_data()
gt = np.asarray(gt, np.float32)
gt /= gt.max() + config.epsilon
if torch.cuda.is_available():
image = image.cuda()
else:
image = image.cpu()
start = time.time()
res = model(image)
end = time.time()
FPS.append(1. / (end - start))
res = F.interpolate(
res,
size=gt.shape,
mode="bilinear",
align_corners=False)
res = res.sigmoid().data.cpu().numpy().squeeze()
res = abs(res - res.min()) / \
(abs(res.max() - res.min()) + config.epsilon)
dice, iou, recall, precision = evaluate(
res, gt, config.threshold, config.epsilon)
DSC.append(dice)
IoU.append(iou)
Recall.append(recall)
Precision.append(precision)
res[res >= config.threshold] = 1
res[res < config.threshold] = 0
cv2.imwrite(os.path.join(save_path_, name), res * 255)
DSC = np.array(DSC)
IoU = np.array(IoU)
Recall = np.array(Recall)
Precision = np.array(Precision)
print(_data_name, "Finish!")
print(f"Mean DICE: {DSC.mean():.4f}")
print(f"Mean IoU: {IoU.mean():.4f}")
print(f"Recall: {Recall.mean():.4f}")
print(f"Precision: {Precision.mean():.4f}")
print("----------------------------------------------------")
image_root = ""
gt_root = ""
FPS = np.array(FPS)
print(f"FPS: {FPS.mean():.4f}")
print("----------------------------------------------------")
print()
print("Evaluation Complete")
if __name__ == "__main__":
eval_fn()