-
Notifications
You must be signed in to change notification settings - Fork 7
/
eval_cam.py
49 lines (40 loc) · 1.68 KB
/
eval_cam.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
import argparse
import os
from omegaconf import OmegaConf
from utils import pyutils
parser = argparse.ArgumentParser()
parser.add_argument("--crf", default=False, type=bool, help="crf post-processing")
parser.add_argument("--type", default='npy', type=str, help="file type")
parser.add_argument("--config", default='configs/voc.yaml', type=str, help="config")
parser.add_argument("--eval_set", default='train', type=str, help="eval set")
args = parser.parse_args()
from scipy import misc
import numpy as np
from tqdm import tqdm
def load_txt(txt_name):
with open(txt_name) as f:
name_list = [x for x in f.read().split('\n') if x]
return name_list
if __name__=="__main__":
config = OmegaConf.load(args.config)
print('\nEvaluating:')
txt_name = config.cam.split
eval_list = load_txt(txt_name)
npy_dir = os.path.join(config.exp.backbone, config.exp.cam_dir)
label_dir = os.path.join(config.dataset.root_dir, 'SegmentationClassAug')
preds = []
labels = []
for i in tqdm(eval_list, total=len(eval_list), ncols=100,):
npy_name = os.path.join(npy_dir, i) + '.npy'
cam_dict = np.load(npy_name, allow_pickle=True).item()
label = misc.imread(os.path.join(label_dir, i) + '.png')
cams = cam_dict['high_res']
cams = np.pad(cams, ((1, 0), (0, 0), (0, 0)), mode='constant', constant_values=config.cam.bkgscore)
keys = np.pad(cam_dict['keys'] + 1, (1, 0), mode='constant')
cls_labels = np.argmax(cams, axis=0)
cls_labels = keys[cls_labels]
preds.append(cls_labels.copy())
labels.append(label)
scores = pyutils.scores(label_preds=preds, label_trues=labels)
print('')
print(scores)