-
Notifications
You must be signed in to change notification settings - Fork 6
/
eval_cue_location.py
290 lines (235 loc) · 10.4 KB
/
eval_cue_location.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
CALM
Copyright (c) 2021-present NAVER Corp.
MIT license
"""
import cv2
import os
import numpy as np
import torch
from os.path import join as ospj
from data_loaders import configure_metadata
from data_loaders import get_data_loader
from data_loaders import get_mask_paths
from util import check_scoremap_validity
from util import resize_scoremaps
from util import get_scoremaps
from util_cub_trait import CUBTrait
from main import Trainer
from util import normalize_scoremap
from util import get_threshold_list
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
NUM_DIFF_LIST = [1, 2, 3]
_IMAGENET_MEAN = [0.485, .456, .406]
_IMAGENET_STDDEV = [.229, .224, .225]
_RESIZE_LENGTH = 224
class LocalizationEvaluator(object):
""" Abstract class for localization evaluation over score maps.
The class is designed to operate in a for loop (e.g. batch-wise cam
score map computation). At initialization, __init__ registers paths to
annotations and data containers for evaluation. At each iteration,
each score map is passed to the accumulate() method along with its image_id.
After the for loop is finalized, compute() is called to compute the final
localization performance.
"""
def __init__(self, metadata, dataset_name, split,
iou_thresholds, threshold_list,
mask_root):
self.metadata = metadata
self.threshold_list = threshold_list
self.dataset_name = dataset_name
self.split = split
self.mask_root = mask_root
self.iou_thresholds = [50] if iou_thresholds is None else iou_thresholds
def accumulate(self, scoremap, image_id):
raise NotImplementedError
def compute(self):
raise NotImplementedError
def load_mask_image(file_path, resize_size):
"""
Args:
file_path: string.
resize_size: tuple of ints (height, width)
Returns:
mask: numpy.ndarray(dtype=numpy.float32, shape=(height, width))
"""
mask = np.float32(cv2.imread(file_path, cv2.IMREAD_GRAYSCALE))
mask = cv2.resize(mask, resize_size, interpolation=cv2.INTER_NEAREST)
return mask
def get_mask(mask_root, mask_paths, ignore_path, use_ignore=True):
"""
Ignore mask is set as the ignore box region \setminus the ground truth
foreground region.
Args:
mask_root: string.
mask_paths: iterable of strings.
ignore_path: string.
Returns:
mask: numpy.ndarray(size=(224, 224), dtype=np.uint8)
"""
mask_all_instances = []
for mask_path in mask_paths:
mask_file = os.path.join(mask_root, mask_path)
mask = load_mask_image(mask_file, (_RESIZE_LENGTH, _RESIZE_LENGTH))
mask_all_instances.append(mask > 0.5)
mask_all_instances = np.stack(mask_all_instances, axis=0).any(axis=0)
ignore_file = os.path.join(mask_root, ignore_path)
ignore_box_mask = load_mask_image(ignore_file,
(_RESIZE_LENGTH, _RESIZE_LENGTH))
ignore_box_mask = ignore_box_mask > 0.5
ignore_mask = np.logical_and(ignore_box_mask,
np.logical_not(mask_all_instances))
if np.logical_and(ignore_mask, mask_all_instances).any():
raise RuntimeError("Ignore and foreground masks intersect.")
if use_ignore:
return (mask_all_instances.astype(np.uint8) +
255 * ignore_mask.astype(np.uint8))
else:
return mask_all_instances.astype(np.uint8)
class MaskEvaluator(LocalizationEvaluator):
def __init__(self, **kwargs):
super(MaskEvaluator, self).__init__(**kwargs)
if self.dataset_name not in ["OpenImages", "CUB"]:
raise ValueError(
"Mask evaluation must be performed on OpenImages or CUB.")
if self.dataset_name == "OpenImages":
self.mask_paths, self.ignore_paths = get_mask_paths(self.metadata)
# threshold_list is given as [0, bw, 2bw, ..., 1-bw]
# Set bins as [0, bw), [bw, 2bw), ..., [1-bw, 1), [1, 2), [2, 3)
self.num_bins = len(self.threshold_list) + 2
self.threshold_list_right_edge = np.append(self.threshold_list,
[1.0, 2.0, 3.0])
self.gt_true_score_hist = np.zeros(self.num_bins, dtype=np.float)
self.gt_false_score_hist = np.zeros(self.num_bins, dtype=np.float)
def accumulate(self, scoremap, image_id, gt_mask=None):
"""
Score histograms over the score map values at GT positive and negative
pixels are computed.
Args:
scoremap: numpy.ndarray(size=(H, W), dtype=np.float)
image_id: string.
"""
check_scoremap_validity(scoremap)
if gt_mask is None:
gt_mask = get_mask(self.mask_root,
self.mask_paths[image_id],
self.ignore_paths[image_id])
gt_true_scores = scoremap[gt_mask == 1]
gt_false_scores = scoremap[gt_mask == 0]
# histograms in ascending order
gt_true_hist, _ = np.histogram(gt_true_scores,
bins=self.threshold_list_right_edge)
self.gt_true_score_hist += gt_true_hist.astype(np.float)
gt_false_hist, _ = np.histogram(gt_false_scores,
bins=self.threshold_list_right_edge)
self.gt_false_score_hist += gt_false_hist.astype(np.float)
def compute(self):
"""
Arrays are arranged in the following convention (bin edges):
gt_true_score_hist: [0.0, eps), ..., [1.0, 2.0), [2.0, 3.0)
gt_false_score_hist: [0.0, eps), ..., [1.0, 2.0), [2.0, 3.0)
tp, fn, tn, fp: >=2.0, >=1.0, ..., >=0.0
Returns:
auc: float. The area-under-curve of the precision-recall curve.
Also known as average precision (AP).
"""
num_gt_true = self.gt_true_score_hist.sum()
tp = self.gt_true_score_hist[::-1].cumsum()
fn = num_gt_true - tp
num_gt_false = self.gt_false_score_hist.sum()
fp = self.gt_false_score_hist[::-1].cumsum()
tn = num_gt_false - fp
if ((tp + fn) <= 0).all():
raise RuntimeError("No positive ground truth in the eval set.")
if ((tp + fp) <= 0).all():
raise RuntimeError("No positive prediction in the eval set.")
non_zero_indices = (tp + fp) != 0
precision = tp / (tp + fp)
recall = tp / (tp + fn)
auc = (precision[1:] * np.diff(recall))[non_zero_indices[1:]].sum()
auc *= 100
pr_curve = [recall, precision]
print("Mask AUC on split {}: {}".format(self.split, auc))
return auc, pr_curve
def _get_score_map_diff(tester, inputs, class_pair, dataset_name='CUB'):
""" actiavtion map and its difference """
id1, id2 = class_pair
activation_maps1 = get_scoremaps(tester, inputs.to(tester.device),
torch.tensor([id1] * inputs.size(0)),
dataset_name)
activation_maps2 = get_scoremaps(tester, inputs.to(tester.device),
torch.tensor([id2] * inputs.size(0)),
dataset_name)
maps_diff = activation_maps1 - activation_maps2
maps_diff = resize_scoremaps(maps_diff, 'numpy')
return maps_diff
def _cub_eval_inner_loop(tester, data_loader, class_pair, trait,
class_pair_list, evaluator):
for inputs, targets, image_ids in data_loader:
maps_diff = _get_score_map_diff(tester, inputs, class_pair)
for map_diff, image_id in zip(maps_diff, image_ids):
scoremap_abs = normalize_scoremap(
abs(map_diff), tester.args.norm_type).astype(np.float)
gt_mask = trait.get_pseudo_segment_mask(image_id, class_pair_list)
if gt_mask is None:
continue
evaluator.accumulate(scoremap_abs, '', gt_mask)
def _get_evaluator(tester):
metadata = configure_metadata(ospj(tester.args.metadata_root, 'test'))
threshold_list = get_threshold_list(
tester.args.cam_curve_interval,
tester.args.threshold_type)
evaluator = MaskEvaluator(metadata=metadata,
dataset_name=tester.args.dataset_name,
split='test',
threshold_list=threshold_list,
mask_root='',
iou_thresholds=None)
return evaluator
def _get_data_loader(tester, superclass_labels):
data_loader = get_data_loader(
data_roots=tester.args.data_paths,
metadata_root=tester.args.metadata_root,
batch_size=tester.args.batch_size,
workers=tester.args.workers,
resize_size=tester.args.resize_size,
crop_size=tester.args.crop_size,
proxy_training_set=tester.args.proxy_training_set,
superclass_labels=superclass_labels,
)['test']
return data_loader
def main():
tester = Trainer()
tester.model.eval()
if tester.args.dataset_name != 'CUB':
raise ValueError(
"Evaluation of cue location is only possible on CUB dataset.")
architecture = tester.args.architecture
process = tester.args.score_map_process
if tester.args.attribution_method == 'CALM_EM':
process += '_EM'
elif tester.args.attribution_method == 'CALM_ML':
process += '_ML'
data_path = ospj(tester.args.data_root, tester.args.dataset_name)
trait = CUBTrait(data_path)
evaluator = _get_evaluator(tester)
pxap = {i: [] for i in NUM_DIFF_LIST}
for num_diff in NUM_DIFF_LIST:
print(f"Start class pairs with the number of "
f"different parts to be {num_diff}")
for class_pair_list in trait.diff2class_pair_dict[num_diff]:
class_pair = [class_id - 1 for class_id in class_pair_list]
data_loader = _get_data_loader(tester, class_pair)
_cub_eval_inner_loop(tester, data_loader, class_pair, trait,
class_pair_list, evaluator)
pxap[num_diff].append(evaluator.compute()[0])
mpxap = [float(f'{np.mean(pxap[i]):.02f}') for i in NUM_DIFF_LIST]
print(f'\n{architecture}, {process} : {mpxap}\n')
tester.logger.report(msg_dict={'step': 0,
'test/cue_1': mpxap[0],
'test/cue_2': mpxap[1],
'test/cue_3': mpxap[2],
})
tester.logger.finalize_log()
if __name__ == '__main__':
main()