-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
173 lines (147 loc) · 5.64 KB
/
loss.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
"""
Harmonization loss module for aligning model saliency maps with human attention maps.
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from pyramid import pyramidal_representation
from utils import compute_human_alignment
# def standardize_cut(heatmaps, axis=(2, 3), epsilon=1e-5):
# """
# Standardize the heatmaps (zero mean, unit variance) and apply ReLU.
# Parameters:
# -----------
# heatmaps : torch.Tensor
# The heatmaps to standardize. Shape: (N, 1, H, W)
# axis : tuple, optional
# The axes to compute the mean and variance. Default: (2, 3)
# epsilon : float, optional
# A small value to avoid division by zero. Default: 1e-5
# Returns:
# --------
# torch.Tensor
# The positive part of the standardized heatmaps.
# """
# means = torch.mean(heatmaps, dim=axis, keepdim=True)
# stds = torch.std(heatmaps, dim=axis, keepdim=True)
# heatmaps = (heatmaps - means) / (stds + epsilon)
# return torch.relu(heatmaps)
def mse(heatmaps_a, heatmaps_b):
"""
Compute the Mean Squared Error between two sets of heatmaps.
Parameters:
-----------
heatmaps_a : torch.Tensor
The first set of heatmaps.
heatmaps_b : torch.Tensor
The second set of heatmaps.
Returns:
--------
torch.Tensor
The Mean Squared Error.
"""
return torch.mean(torch.square(heatmaps_a - heatmaps_b))
def pyramidal_mse(true_heatmaps, predicted_heatmaps, nb_levels=5):
"""
Compute mean squared error between two sets of heatmaps on a pyramidal representation.
Parameters:
-----------
true_heatmaps : torch.Tensor
The true heatmaps. Shape: (N, 1, H, W)
predicted_heatmaps : torch.Tensor
The predicted heatmaps. Shape: (N, 1, H, W)
nb_levels : int, optional
The number of levels to use in the pyramid. Default: 5
Returns:
--------
torch.Tensor
The weighted MSE across all pyramid levels.
"""
# print("Shape of heatmaps:", true_heatmaps.shape, predicted_heatmaps.shape)
pyramid_y = pyramidal_representation(true_heatmaps, nb_levels)
pyramid_y_pred = pyramidal_representation(predicted_heatmaps, nb_levels)
loss = torch.mean(torch.stack(
[mse(pyramid_y[i], pyramid_y_pred[i]) for i in range(nb_levels)]))
return loss
def harmonizer_loss(predicted_label, true_label, saliency_maps, clickme_maps, lambda_weights=1e-5, lambda_harmonization=1.0):
"""
Compute the harmonization loss: cross entropy + pyramidal MSE of standardized-cut heatmaps.
Parameters:
-----------
model : torch.nn.Module
The model to train.
images : torch.Tensor
The batch of images to train on.
labels : torch.Tensor
The batch of labels.
clickme_maps : torch.Tensor
The batch of true heatmaps (e.g., Click-me maps) to align the model on.
criterion : torch.nn.CrossEntropyLoss
The cross entropy loss to use.
lambda_weights : float, optional
The weight decay factor. Default: 1e-5
lambda_harmonization : float, optional
The weight for the harmonization loss. Default: 1.0
Returns:
--------
harmonization_loss : torch.Tensor
The total harmonization loss.
cce_loss : torch.Tensor
The cross-entropy loss component.
"""
# Standardize and normalize heatmaps
# saliency_maps = standardize_cut(saliency_maps)
# clickme_maps = standardize_cut(clickme_maps)
# saliency_max = torch.amax(saliency_maps, (2,3), keepdims=True) + 1e-6
# clickme_max = torch.amax(clickme_maps, (2,3), keepdims=True) + 1e-6
# clickme_maps = clickme_maps / clickme_max * saliency_max
# Compute losses
pyramidal_loss = pyramidal_mse(saliency_maps, clickme_maps)
cce_loss = nn.CrossEntropyLoss()(predicted_label, true_label)
harmonization_loss = pyramidal_loss * lambda_harmonization
return harmonization_loss, cce_loss
def harmonization_eval(model, images, labels, clickme_maps, criterion):
"""
Evaluate the model's harmonization performance.
Parameters:
-----------
model : torch.nn.Module
The model to evaluate.
images : torch.Tensor
The batch of images to evaluate on.
labels : torch.Tensor
The batch of labels.
clickme_maps : torch.Tensor
The batch of true heatmaps (e.g., Click-me maps) to compare against.
criterion : torch.nn.CrossEntropyLoss
The cross entropy loss to use.
Returns:
--------
output : torch.Tensor
The model's output predictions.
cce_loss : torch.Tensor
The cross-entropy loss.
human_alignment : torch.Tensor
The score indicating the Spearman correlation between saliency maps and clickme maps.
"""
model.eval()
images.requires_grad = True
output = model(images)
cce_loss = criterion(output, labels)
# Compute saliency maps
correct_class_scores = output.gather(1, labels.view(-1, 1)).squeeze()
device = images.device
ones_tensor = torch.ones(correct_class_scores.shape).to(device)
correct_class_scores.backward(ones_tensor, retain_graph=True)
grads = torch.abs(images.grad)
saliency_maps, _ = torch.max(grads, dim=1, keepdim=True) # saliency map (N, C, H, W) -> (N, 1, H, W)
# measure human alignment
human_alignment = compute_human_alignment(saliency_maps, clickme_maps)
images.grad.zero_()
return output, cce_loss, human_alignment
if __name__ == "__main__":
# Test code
hmp_a, hmp_b = torch.rand(4, 1, 224, 224), torch.rand(4, 1, 224, 224)
print(standardize_cut(hmp_a).shape)
print(mse(hmp_a, hmp_b))
print(pyramidal_mse(hmp_a, hmp_b))