forked from WalBouss/LeGrad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_iou.py
170 lines (121 loc) · 5.96 KB
/
calc_iou.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
# Modified script to handle maximum IoU values and classify correctly, misclassified, and hallucinations
import json
from typing import List, Dict
threshold = 0.7
# Function to calculate IoU
def calculate_iou(box1, box2):
x1, y1, w1, h1 = box1
x2, y2, w2, h2 = box2
# Calculate the (x, y)-coordinates of the intersection rectangle
xA = max(x1, x2)
yA = max(y1, y2)
xB = min(x1 + w1, x2 + w2)
yB = min(y1 + h1, y2 + h2)
# Compute the area of intersection rectangle
interArea = max(0, xB - xA) * max(0, yB - yA)
# Compute the area of both the prediction and ground-truth rectangles
box1Area = w1 * h1
box2Area = w2 * h2
# Compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the intersection area
iou = interArea / float(box1Area + box2Area - interArea)
return iou
# Function to parse bounding box from string
def parse_bounding_box(bbox_str):
bbox_str = bbox_str.split('[')[-1].split(']')[0] # Get the part inside the square brackets
bbox = list(map(float, bbox_str.split(',')))
return bbox
# Load JSON files
with open('../data/bboxes_objects/synonyms/adjusted_bboxes_objects.json', 'r') as f:
predicted_data = json.load(f)
# with open('../data/bboxes_objects/adjusted_bboxes__original_objects.json', 'r') as f:
# predicted_data = json.load(f)
# with open(f'../data/bboxes_objects/map_classes/bboxes_objects_{threshold}.json', 'r') as f:
# predicted_data = json.load(f)
with open('../data/gt_bboxes/gt_bboxes.json', 'r') as f:
ground_truth_data = json.load(f)
hallucinations = []
correctly_classified = []
misclassified = []
wrong_object = []
instances = 0
#############
# PSEUDOCODE COGVLMCHAT IMPLEMENTATION
# for image in test_dataset
# for heatmap in heatmap_predictions
# for gt_bounding_box in gt_bounding_boxes
# compute heatmap score for gt_bounding_box
# collect gt_bounding_box for which the heatmap has the highest score
# if score>threshhold:
# if label==pred:
# correctly classifed
# else:
# missclassified
# else:
#hallucination
# if label==pred:
# hallucination
# ATTENTION IS NOT POINING TO AN OBJECT BUT THE MODEL IS SAYING THE OBJECT IS SOMEWHERE
# else:
# wrong_object
# if max_iou < 0.5:
# # If IoU is less than 0.5, classify as a hallucination
# if matched_gt_label == pred_label:
# hallucinations.append({"image_id": image_id, "pred_box": pred_box, "iou": max_iou})
# else:
# wrong_object.append({"image_id": image_id, "pred_box": pred_box, "iou": max_iou})
# else:
# # Check if the labels match
# if matched_gt_label == pred_label:
# correctly_classified.append({"image_id": image_id, "pred_box": pred_box, "iou": max_iou, "label": pred_label})
# else:
# misclassified.append({"image_id": image_id, "pred_box": pred_box, "iou": max_iou, "pred_label": pred_label, "gt_label": matched_gt_label})
#############
# LOOP OVER ALL PREDICTIONS (BOUNDING BOX + CLASS) -> SHOULD BECOME????
for pred in predicted_data:
image_id = pred['image']
pred_boxes = pred['bbx with object']
instances += len(pred_boxes)
# Find the corresponding ground truth boxes
gt = next((item for item in ground_truth_data if item['image'] == image_id), None)
if gt is not None:
gt_boxes = gt['bbx with object']
for pred_box in pred_boxes:
pred_box_parsed = parse_bounding_box(pred_box)
max_iou = 0
matched_gt_idx = -1
for idx, gt_box in enumerate(gt_boxes):
gt_box_parsed = parse_bounding_box(gt_box)
iou = calculate_iou(pred_box_parsed, gt_box_parsed)
if iou > max_iou:
max_iou = iou
matched_gt_idx = idx
pred_label = pred_box.split('[')[0].strip()
matched_gt_label = gt_boxes[matched_gt_idx].split('[')[0].strip() if matched_gt_idx >= 0 else None
if max_iou < 0.5:
# If IoU is less than 0.5, classify as a hallucination
if matched_gt_label == pred_label:
hallucinations.append({"image_id": image_id, "pred_box": pred_box, "iou": max_iou})
else:
wrong_object.append({"image_id": image_id, "pred_box": pred_box, "iou": max_iou})
else:
# Check if the labels match
if matched_gt_label == pred_label:
correctly_classified.append({"image_id": image_id, "pred_box": pred_box, "iou": max_iou, "label": pred_label})
else:
misclassified.append({"image_id": image_id, "pred_box": pred_box, "iou": max_iou, "pred_label": pred_label, "gt_label": matched_gt_label})
print(f"Total instances: {instances}")
print(f"Number of hallucinations: {len(hallucinations)}")
print(f"Number of correct classifications: {len(correctly_classified)}")
print(f"Number of misclassifications: {len(misclassified)}")
print(f"Number of wrong objects: {len(wrong_object)}")
# Save the results to JSON files
with open(f'../data/output/synonyms/hallucinations.json', 'w') as f:
json.dump(hallucinations, f, indent=4)
with open(f'../data/output/synonyms/correctly_classified.json', 'w') as f:
json.dump(correctly_classified, f, indent=4)
with open(f'../data/output/synonyms/misclassified.json', 'w') as f:
json.dump(misclassified, f, indent=4)
with open(f'../data/output/synonyms/wrong_object.json', 'w') as f:
json.dump(wrong_object, f, indent=4)