-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
73 lines (58 loc) · 2.09 KB
/
test.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
import os
import torch
import segmentation_models_pytorch as smp
from torch.utils.data import DataLoader
import config
from datasets import DRACDataset
from preprocess import get_validation_augmentation, get_preprocessing
from utils import Dice
if __name__ == '__main__':
ENCODER = config.ENCODER
ENCODER_WEIGHTS = config.ENCODER_WEIGHTS
CLASSES = config.CLASSES
ACTIVATION = config.ACTIVATION
DEVICE = config.DEVICE
NAME = config.NAME
# create segmentation model with pretrained encoder
model = smp.FPN(
encoder_name=ENCODER,
encoder_weights=ENCODER_WEIGHTS,
classes=len(CLASSES),
activation=ACTIVATION,
)
preprocessing_fn = smp.encoders.get_preprocessing_fn(ENCODER, ENCODER_WEIGHTS)
DATA_DIR = config.DATA_DIR
classes = {'IMA':'1. Intraretinal Microvascular Abnormalities', 'NA':'2. Nonperfusion Areas', 'NE':'3. Neovascularization', 'NE_nohealth':'3. Neovascularization'}
x_test_dir = os.path.join(DATA_DIR, '1. Original Images', 'val')
y_test_dir = os.path.join(DATA_DIR, '2. Groundtruths', 'val', classes[CLASSES[0]])
loss = smp.utils.losses.DiceLoss()
metrics = [
smp.utils.metrics.IoU(threshold=0.5),
Dice(threshold=0.5)
# smp.utils.metrics.Dice(threshold=0.5),
# AUC(threshold=0.5),
# smp.utils.metrics.Recall(threshold=0.5),
# Specificity(threshold=0.5)
]
optimizer = torch.optim.Adam([
dict(params=model.parameters(), lr=0.0001),
])
# create test dataset
test_dataset = DRACDataset(
x_test_dir,
y_test_dir,
augmentation=get_validation_augmentation(),
preprocessing=get_preprocessing(preprocessing_fn),
)
test_dataloader = DataLoader(test_dataset)
# load best saved checkpoint
best_model = torch.load('./models/' + CLASSES[0] + '_'+NAME+'_best_model.pth')
# evaluate model on test set
test_epoch = smp.utils.train.ValidEpoch(
model=best_model,
loss=loss,
metrics=metrics,
device=DEVICE,
)
logs = test_epoch.run(test_dataloader)
print(logs)