|
a |
|
b/evaluation/stress_test.py |
|
|
1 |
import random |
|
|
2 |
from os import listdir |
|
|
3 |
from os.path import join |
|
|
4 |
|
|
|
5 |
import matplotlib.pyplot as plt |
|
|
6 |
import torch |
|
|
7 |
import torch.nn as nn |
|
|
8 |
import torchvision.transforms as transforms |
|
|
9 |
from torch.utils.data import DataLoader |
|
|
10 |
from tqdm import tqdm |
|
|
11 |
|
|
|
12 |
from data.etis import EtisDataset |
|
|
13 |
from data.hyperkvasir import KvasirSegmentationDataset |
|
|
14 |
from models.segmentation_models import DeepLab |
|
|
15 |
from evaluation.metrics import iou |
|
|
16 |
from utils.logging import log_iou |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
class AdditiveNoise(nn.Module): |
|
|
20 |
def __init__(self, noise_factor): |
|
|
21 |
super().__init__() |
|
|
22 |
self.noise_factor = noise_factor |
|
|
23 |
|
|
|
24 |
def forward(self, img): |
|
|
25 |
return img + torch.randn(img.shape).to("cuda") * random.randint(0, 5) * self.noise_factor |
|
|
26 |
|
|
|
27 |
def __repr__(self): |
|
|
28 |
return self.__class__.__name__ + '(noise_factor={})'.format(self.noise_factor) |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
class segmentation_stressors(nn.Module): |
|
|
32 |
def __init__(self): |
|
|
33 |
super().__init__() |
|
|
34 |
|
|
|
35 |
def forward(self, img, mask): |
|
|
36 |
return img, mask |
|
|
37 |
|
|
|
38 |
|
|
|
39 |
def apply_stressors(image, mask): |
|
|
40 |
img_augs = transforms.Compose([AdditiveNoise(0.05), |
|
|
41 |
transforms.RandomErasing(0.5, scale=(0.02, 0.08)), |
|
|
42 |
transforms.RandomInvert(p=0.1)]) |
|
|
43 |
img_and_mask_augs = segmentation_stressors() |
|
|
44 |
image = img_augs(image) |
|
|
45 |
image, mask = img_and_mask_augs(image, mask) |
|
|
46 |
return image, mask |
|
|
47 |
|
|
|
48 |
|
|
|
49 |
def stresstesttest(): |
|
|
50 |
dataset = KvasirSegmentationDataset("Datasets/HyperKvasir") |
|
|
51 |
for x, y, fname in DataLoader(dataset): |
|
|
52 |
image, mask = apply_stressors(x.to("cuda"), y.to("cuda")) |
|
|
53 |
plt.imshow(image[0].permute(1, 2, 0).cpu().numpy()) |
|
|
54 |
# plt.imshow(mask.cpu().numpy().squeeze().T, alpha=0.5) |
|
|
55 |
plt.show() |
|
|
56 |
input() |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
def perform_stresstest(modelpath, stressors=True): |
|
|
60 |
dataset = EtisDataset("Datasets/ETIS-LaribPolypDB") |
|
|
61 |
# dataset = KvasirSegmentationDataset("Datasets/HyperKvasir") |
|
|
62 |
for predictor_name in listdir(modelpath): |
|
|
63 |
if len(predictor_name.split("-")) == 3: |
|
|
64 |
model = DeepLab(1).to("cuda") |
|
|
65 |
model = torch.nn.Sequential(model, torch.nn.Sigmoid()) |
|
|
66 |
|
|
|
67 |
model.eval() |
|
|
68 |
test = torch.load(join(modelpath, predictor_name)) |
|
|
69 |
# print(test) |
|
|
70 |
model.load_state_dict(test) |
|
|
71 |
ious = torch.empty((0,)) |
|
|
72 |
|
|
|
73 |
for x, y, fname in tqdm(DataLoader(dataset)): |
|
|
74 |
if stressors: |
|
|
75 |
image, mask = apply_stressors(x.to("cuda"), y.to("cuda")) |
|
|
76 |
else: |
|
|
77 |
image, mask = x.to("cuda"), y.to("cuda") |
|
|
78 |
with torch.no_grad(): |
|
|
79 |
output = model(image) |
|
|
80 |
batch_ious = torch.Tensor([iou(output_i, mask_j) for output_i, mask_j in zip(output, mask)]) |
|
|
81 |
ious = torch.cat((ious, batch_ious.flatten())) |
|
|
82 |
log_iou("logs/kvasir-no-pretrain-baseline.log", -1, predictor_name.split("-")[-1], ious) |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
if __name__ == '__main__': |
|
|
86 |
perform_stresstest("Predictors/BaselineDeepLab", stressors=False) |
|
|
87 |
# EtisDataset("") |