Repository navigation
Expand file tree
/
Copy pathscoring.py
More file actions
339 lines (256 loc) · 11.1 KB
/
Copy pathscoring.py
File metadata and controls
339 lines (256 loc) · 11.1 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import numpy as np
import torch
import matplotlib.pyplot as plt
import os
import cv2
import json
import torch
import torchvision
import numpy as np
import pandas as pd
import torch.nn as nn
import albumentations as A
import matplotlib.pyplot as plt
import torch.nn.functional as F
from PIL import Image
from tqdm import tqdm
from collections import defaultdict
from albumentations.pytorch import ToTensorV2
from torch.utils.data import Dataset, DataLoader
from torchvision.models.detection import MaskRCNN
from sklearn.model_selection import train_test_split
from torchvision.models.detection.rpn import AnchorGenerator
from torchvision.transforms import functional as F_transforms
from torch.utils.data import Subset
from torchvision.ops import box_iou
from dataloader import *
from edarnn import *
def to_numpy(mask):
"""Convert torch tensor to 2D NumPy bool array."""
if torch.is_tensor(mask):
mask = mask.detach().cpu().numpy()
return mask.astype(bool)
def plot_masks(true_mask, pred_mask, title_prefix="", save_path=None):
true_mask = np.squeeze(true_mask)
pred_mask = np.squeeze(pred_mask)
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(true_mask, cmap="gray")
ax[0].set_title(f"{title_prefix} True Mask")
ax[1].imshow(pred_mask, cmap="gray")
ax[1].set_title(f"{title_prefix} Predicted Mask")
for a in ax:
a.axis("off")
plt.tight_layout()
if save_path:
plt.savefig(save_path)
print(f"Plot saved to {save_path}")
else:
plt.show(block=True) # keep window open in scripts
import torch
def soft_dice(pred_mask, target, verbose=False):
"""
Computes soft Dice score between predicted mask and ground truth.
pred_mask: torch.Tensor, shape [H, W] or [1, H, W], float values in [0, 1]
target: dict from Mask R-CNN containing 'masks': [N, H, W]
verbose: bool, print intermediate values
Returns:
dice: float
"""
if target.ndim == 2: # single mask
true_mask = target.unsqueeze(0).cpu() # [1, H, W]
else:
true_mask = target.float().cpu() # [N, H, W]
# Flatten prediction
if pred_mask.ndim == 3 and pred_mask.shape[0] == 1:
pred_mask = pred_mask.squeeze(0).cpu()
pred_flat = pred_mask.contiguous().view(-1).cpu()
full_true_mask = (true_mask.sum(dim=0) > 0).float()
true_flat = full_true_mask.contiguous().view(-1)
# Compute soft dice
intersection = (pred_flat * true_flat).sum()
denominator = pred_flat.sum() + true_flat.sum()
dice = (2.0 * intersection + 1e-6) / (denominator + 1e-6)
return dice.item()
def binary_iou(pred_mask, true_mask, debug=False):
pred_mask = to_numpy(pred_mask)
true_mask = to_numpy(true_mask)
print(f" Masks shape {pred_mask.shape}, {true_mask.shape}")
intersection = np.logical_and(pred_mask, true_mask).sum()
union = np.logical_or(pred_mask, true_mask).sum()
iou = intersection / union if union != 0 else (1.0 if pred_mask.sum() == 0 else 0.0)
return iou
def binary_dice(pred_mask, true_mask, debug=True):
pred_mask = to_numpy(pred_mask)
true_mask = to_numpy(true_mask)
intersection = np.logical_and(pred_mask, true_mask).sum()
total = pred_mask.sum() + true_mask.sum()
dice = (2 * intersection / total) if total != 0 else (1.0 if pred_mask.sum() == 0 else 0.0)
return dice
def of1_score(pred_mask: np.ndarray, gt_mask: np.ndarray, n_pred: int, n_gt: int):
pred_flat = pred_mask.cpu().numpy().flatten()
gt_flat = gt_mask.cpu().numpy().flatten()
tp = np.sum((pred_flat == 1) & (gt_flat == 1))
fp = np.sum((pred_flat == 1) & (gt_flat == 0))
fn = np.sum((pred_flat == 0) & (gt_flat == 1))
precision = tp / (tp + fp) if (tp + fp) > 0 else 0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0
penalty = n_gt / max(n_pred, n_gt) # Penalty for predicting more submasks than there are gt masks
if (precision + recall) > 0:
return 2 * penalty * (precision * recall) / (precision + recall), recall, precision
else:
return 0, recall, precision
from torchvision.ops import box_iou
def debug_box_scores(pred, gt, scores, iou_thr=0.8, score_thr=0.8):
pred_boxes = pred["boxes"].cpu()
gt_boxes = gt["boxes"].cpu()
if len(gt_boxes) == 0 or len(pred_boxes) == 0:
return None
ious = box_iou(gt_boxes, pred_boxes) # len(gt) X len(pred)
""" max iou and idx along dim1 """
best_iou, best_idx = ious.max(dim=1)
best_scores = scores[best_idx]
gt_covered = (best_iou >= iou_thr) & (best_scores >= score_thr)
""" Boolean array of the GT boxes covered by high scoring pred box """
high_score_gt_recall = gt_covered.float().mean().item()
# False positives = preds not matched to any GT
valid_gt = best_iou >= iou_thr # [True if IoU>thresh, False] of len(scores)
matched_preds = best_idx[valid_gt].unique() # Grabs only indexes with True
fp_mask = torch.ones(len(scores), dtype=torch.bool)
fp_mask[matched_preds] = False
""" with length=len(scores), its True if not match GT, False if match GT (TP) """
mean_fp_score = scores[fp_mask].mean().item() if fp_mask.any() else 0.0
tp_mask = best_iou >= iou_thr
mean_tp_score = best_scores[tp_mask].mean().item() if tp_mask.any() else 0.0
return {
"gt_high_score_recall": high_score_gt_recall,
"mean_tp_score": mean_tp_score,
"mean_fp_score": mean_fp_score,
"num_preds": (scores >= score_thr).sum().item(),
"num_gt": len(gt["boxes"])
}
def evaluate_allauthentic(loader):
oF1s = []
img_size = []
avg_boxsize = []
N_boxes = []
true_forged_pixels = 0
pred_forged_pixels = 0
for idx, (image, target, filename, _) in enumerate(tqdm(loader)):
image = image[0] # take first item from batch
target = target[0]
raw_img, raw_mask = full_dataset.get_raw_img_mask(idx)
with torch.no_grad():
if ((len(target["boxes"]) == 0)):
oF1s.append(1)
print("Correct: Authentic")
elif((len(target["boxes"]) != 0)):
oF1s.append(0)
print("Wrong: Forged")
return np.asarray(oF1s)
def evaluate(loader, pixel_thresh, mask_thresh):
oF1s = []
img_size = []
avg_boxsize = []
N_boxes = []
recalls = []
precisions = []
true_forged_pixels = 0
pred_forged_pixels = 0
box_overlap_scores = {
"gt_high_score_recall": [],
"mean_tp_score": [],
"mean_fp_score": [],
"num_preds": [],
"num_gt": [],
}
for image, target, filename, idxs in tqdm(loader):
image = image[0] # take first item from batch
target = target[0]
raw_img, raw_mask = full_dataset.get_raw_img_mask(idxs[0])
with torch.no_grad():
outputs = model(image.unsqueeze(0).to(device)) # forward pass
if ((len(outputs[0]["boxes"]) == 0) and (len(target["boxes"]) == 0)):
oF1s.append(1)
elif((len(target["boxes"]) == 0) and (len(outputs[0]["boxes"]) != 0)):
oF1s.append(0)
elif((len(target["boxes"]) != 0) and (len(outputs[0]["boxes"]) == 0)):
oF1s.append(0)
else:
# Force submasks into place, gets rid of the box errors. If oF1 improves, box regression is largest ERROR
#outputs[0]["boxes"] = target["boxes"]
#outputs[0]["labels"] = target["labels"]
#scores = outputs[0]['scores']
pred_mask, mask_confs = combine_resize_submasks(outputs[0], raw_img, pixel_thresh, mask_thresh)
gt_mask, _ = combine_resize_submasks(target, raw_img, pixel_thresh, mask_thresh, already_binary = True)
print(f"Avg mask_conf: {mask_confs}")
out = debug_box_scores(outputs[0], target, mask_confs, iou_thr = 0.6)
if out is None:
continue
else:
for (k,v) in out.items():
box_overlap_scores[k].append(v)
oF1, recall, precision = of1_score(pred_mask, gt_mask, len(outputs[0]["boxes"]), len(target["boxes"]))
boxes_size = []
for box in target["boxes"]:
size = (box[2]-box[0])*(box[3]-box[1])
boxes_size.append(size)
N_boxes.append(len(target["boxes"]))
img_size.append(raw_img.shape[0]*raw_img.shape[1])
avg_boxsize.append(np.mean(boxes_size))
oF1s.append(oF1)
recalls.append(recall)
precisions.append(precision)
return box_overlap_scores, np.asarray(oF1s), np.asarray(recalls), np.asarray(precisions)
def corr_oF1_avgboxsize(oF1s, avg_boxsize):
# align lengths FIRST
n = min(len(avg_boxsize), len(oF1s))
avg_boxsize = avg_boxsize[:n]
oF1s = oF1s[:n]
# now remove NaNs
mask = ~np.isnan(avg_boxsize) & ~np.isnan(oF1s)
corr = np.corrcoef(avg_boxsize[mask], oF1s[mask])[0, 1]
if __name__ == "__main__":
# Make sure device is consistent
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load model and weights
model = get_coco_initialized_model(num_classes=2)
#state = torch.load("./images/frozen_natural_300/natural_frozen_300epochs.pth", map_location=device) # load directly to device
state = torch.load("../pretrained_final.pth", map_location=device)
model.load_state_dict(state)
model.to(device) # ensure model is on same device as inputs
model.eval()
""" ONLY PLOTS THE FIRST ELEM IN BATCH """
""" mean DICE w.r.t threshold
histogram oF1s w.r.t image size
histogram oF1s w.r.t avg submask size
histogram oF1s w.r.t Number submasks
"""
plot = False
# For all authentic prediction, compute the oF1 scores in the train and test dataset
#oF1s = evaluate_allauthentic(train_loader)
#print(" Baseline oF1s TRAIN: ")
#print(np.mean(oF1s), np.std(oF1s))
"""
Baseline oF1s TRAIN:
0.404 +- 0.490
"""
#oF1s = evaluate_allauthentic(train_loader)
#print(" Baseline oF1s TEST: ")
#print(np.mean(oF1s), np.std(oF1s))
for thresh in [0.1, 0.2, 0.8]:
"""# MEAN oF1s:
0.210 0.241
RECALL:
higher/stricter pixel_thresh -> Lower mask_confs"""
box_overlap_scores, oF1s, recalls, precisions = evaluate(train_loader, pixel_thresh = 0.7, mask_thresh = 0.9)
print(" MEAN oF1s: ")
print(f"{np.mean(oF1s):.2f} +- {np.std(oF1s):.2f}")
print(" RECALL:")
print(f"{np.mean(recalls):.2f} +- {np.std(recalls):.2f}")
print(" PRECISION:")
print(f"{np.mean(precisions):.2f} +- {np.std(precisions):.2f}")
for (k, v) in box_overlap_scores.items():
print(f"{k}: {np.mean(v):.2f} +- {np.std(v):.2f}")
# corr = corr_oF1_avgboxsize(oF1s, avg_boxsize)
#print(f"Corr oF1 w avg_boxsize: {corr:.4f} \n oF1 w img_size: {np.corrcoef(img_size, oF1s)[0,1]:.4f} \n oF1 w N_boxes: {np.corrcoef(N_boxes, oF1s)[0,1]:.4f}")
#print(f"FP: {FP_forged_imgs}, FN: {FN_forged_imgs}, TP: {TP_forged_imgs}, TN: {TN_forged_imgs}")