-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathevalAnomaly.py
More file actions
146 lines (120 loc) · 5.01 KB
/
evalAnomaly.py
File metadata and controls
146 lines (120 loc) · 5.01 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
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
# Copyright (c) OpenMMLab. All rights reserved.
import os
import cv2
import glob
import torch
import random
from PIL import Image
import numpy as np
from erfnet import ERFNet
import os.path as osp
from argparse import ArgumentParser
from ood_metrics import fpr_at_95_tpr, calc_metrics, plot_roc, plot_pr,plot_barcode
from sklearn.metrics import roc_auc_score, roc_curve, auc, precision_recall_curve, average_precision_score
seed = 42
# general reproducibility
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
NUM_CHANNELS = 3
NUM_CLASSES = 20
# gpu training specific
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def main():
parser = ArgumentParser()
parser.add_argument(
"--input",
default="/home/shyam/Mask2Former/unk-eval/RoadObsticle21/images/*.webp",
nargs="+",
help="A list of space separated input images; "
"or a single glob pattern such as 'directory/*.jpg'",
)
parser.add_argument('--loadDir',default="../trained_models/")
parser.add_argument('--loadWeights', default="erfnet_pretrained.pth")
parser.add_argument('--loadModel', default="erfnet.py")
parser.add_argument('--subset', default="val") #can be val or train (must have labels)
parser.add_argument('--datadir', default="/home/shyam/ViT-Adapter/segmentation/data/cityscapes/")
parser.add_argument('--num-workers', type=int, default=4)
parser.add_argument('--batch-size', type=int, default=1)
parser.add_argument('--cpu', action='store_true')
args = parser.parse_args()
anomaly_score_list = []
ood_gts_list = []
if not os.path.exists('results.txt'):
open('results.txt', 'w').close()
file = open('results.txt', 'a')
modelpath = args.loadDir + args.loadModel
weightspath = args.loadDir + args.loadWeights
print ("Loading model: " + modelpath)
print ("Loading weights: " + weightspath)
model = ERFNet(NUM_CLASSES)
if (not args.cpu):
model = torch.nn.DataParallel(model).cuda()
def load_my_state_dict(model, state_dict): #custom function to load model when not all dict elements
own_state = model.state_dict()
for name, param in state_dict.items():
if name not in own_state:
if name.startswith("module."):
own_state[name.split("module.")[-1]].copy_(param)
else:
print(name, " not loaded")
continue
else:
own_state[name].copy_(param)
return model
model = load_my_state_dict(model, torch.load(weightspath, map_location=lambda storage, loc: storage))
print ("Model and weights LOADED successfully")
model.eval()
for path in glob.glob(os.path.expanduser(str(args.input[0]))):
print(path)
images = torch.from_numpy(np.array(Image.open(path).convert('RGB'))).unsqueeze(0).float()
images = images.permute(0,3,1,2)
with torch.no_grad():
result = model(images)
anomaly_result = 1.0 - np.max(result.squeeze(0).data.cpu().numpy(), axis=0)
pathGT = path.replace("images", "labels_masks")
if "RoadObsticle21" in pathGT:
pathGT = pathGT.replace("webp", "png")
if "fs_static" in pathGT:
pathGT = pathGT.replace("jpg", "png")
if "RoadAnomaly" in pathGT:
pathGT = pathGT.replace("jpg", "png")
mask = Image.open(pathGT)
ood_gts = np.array(mask)
if "RoadAnomaly" in pathGT:
ood_gts = np.where((ood_gts==2), 1, ood_gts)
if "LostAndFound" in pathGT:
ood_gts = np.where((ood_gts==0), 255, ood_gts)
ood_gts = np.where((ood_gts==1), 0, ood_gts)
ood_gts = np.where((ood_gts>1)&(ood_gts<201), 1, ood_gts)
if "Streethazard" in pathGT:
ood_gts = np.where((ood_gts==14), 255, ood_gts)
ood_gts = np.where((ood_gts<20), 0, ood_gts)
ood_gts = np.where((ood_gts==255), 1, ood_gts)
if 1 not in np.unique(ood_gts):
continue
else:
ood_gts_list.append(ood_gts)
anomaly_score_list.append(anomaly_result)
del result, anomaly_result, ood_gts, mask
torch.cuda.empty_cache()
file.write( "\n")
ood_gts = np.array(ood_gts_list)
anomaly_scores = np.array(anomaly_score_list)
ood_mask = (ood_gts == 1)
ind_mask = (ood_gts == 0)
ood_out = anomaly_scores[ood_mask]
ind_out = anomaly_scores[ind_mask]
ood_label = np.ones(len(ood_out))
ind_label = np.zeros(len(ind_out))
val_out = np.concatenate((ind_out, ood_out))
val_label = np.concatenate((ind_label, ood_label))
prc_auc = average_precision_score(val_label, val_out)
fpr = fpr_at_95_tpr(val_out, val_label)
print(f'AUPRC score: {prc_auc*100.0}')
print(f'FPR@TPR95: {fpr*100.0}')
file.write((' AUPRC score:' + str(prc_auc*100.0) + ' FPR@TPR95:' + str(fpr*100.0) ))
file.close()
if __name__ == '__main__':
main()