forked from WalBouss/LeGrad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheatmap_score.py
68 lines (52 loc) · 2 KB
/
heatmap_score.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
import torch
def load_heatmap(path):
"""
Load heatmap tensor from a given file path.
Parameters:
path (str): Path to the file containing the heatmap tensor.
Returns:
torch.Tensor: Loaded heatmap tensor.
"""
return torch.load(path)
def calculate_heatmap_strength(heatmap, bbox):
"""
Calculate the sum of heatmap values within the bounding box.
Parameters:
heatmap (torch.Tensor): 2D tensor representing the heatmap.
bbox (tuple): Bounding box coordinates (x_min, y_min, x_max, y_max).
Returns:
float: Sum of heatmap values within the bounding box.
"""
x_min, y_min, x_max, y_max = bbox
return torch.sum(heatmap[y_min:y_max, x_min:x_max]).item()
def find_strongest_bounding_box(heatmap, bounding_boxes):
"""
Find the bounding box where the heatmap is the strongest as a percentage of the bounding box size.
Parameters:
heatmap (torch.Tensor): 2D tensor representing the heatmap.
bounding_boxes (list of tuples): List of bounding box coordinates (x_min, y_min, x_max, y_max).
Returns:
tuple: The bounding box with the highest heatmap intensity percentage.
"""
max_intensity = 0
best_bbox = None
for bbox in bounding_boxes:
x_min, y_min, x_max, y_max = bbox
bbox_area = (x_max - x_min) * (y_max - y_min)
heatmap_intensity = calculate_heatmap_strength(heatmap, bbox)
intensity_percentage = heatmap_intensity / bbox_area
if intensity_percentage > max_intensity:
max_intensity = intensity_percentage
best_bbox = bbox
return best_bbox
# Example usage
if __name__ == "__main__":
heatmap_path = 'path_to_heatmap_tensor.pt'
heatmap = load_heatmap(heatmap_path)
bounding_boxes = [
(0, 0, 2, 2),
(1, 1, 3, 3),
(2, 2, 4, 4)
]
strongest_bbox = find_strongest_bounding_box(heatmap, bounding_boxes)
print(f"The bounding box with the highest heatmap intensity percentage is: {strongest_bbox}")