-
Notifications
You must be signed in to change notification settings - Fork 0
/
intervene.py
154 lines (117 loc) · 5.53 KB
/
intervene.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
from obiwan.new_models import FuseCBM
from obiwan.utils import recall
import torch
import torch.nn.functional as F
import wandb #noqa
try:
from rich.tqdm import tqdm
except ImportError:
from tqdm import tqdm
def get_concepts(model, dataloader, device):
model.to(device)
model.eval()
concepts = []
for batch in tqdm(dataloader):
if len(batch) == 2:
imgs, (labels, attrs) = batch
else:
imgs, attrs, labels = batch
imgs = imgs.to(device)
attrs = attrs.to(device)
labels = labels.to(device)
with torch.no_grad():
concept, _ = model(imgs)
# concept, _, _ = model(imgs)
concept = torch.cat(concept, dim=1)
concepts.append(concept)
concepts = torch.cat(concepts, dim=0)
return concepts
def evaluate_recall(model: FuseCBM, dataloader, device, values=None):
model.eval()
model.to(device)
embeddings_list = []
labels_list = []
with torch.no_grad():
for imgs, attrs, labels in tqdm(dataloader):
imgs = imgs.to(device)
attrs = attrs.to(device)
labels = labels.to(device)
if values is None:
embeddings = model.get_fused_embedding(imgs, return_concepts=False, return_extra_dim=False)
else:
embeddings = model.get_fused_embedding_with_intervention(imgs, attrs, return_concepts=False, return_extra_dim=False, intervention_values=values)
embeddings = F.normalize(embeddings, dim=1)
embeddings_list.append(embeddings)
labels_list.append(labels)
embeddings = torch.cat(embeddings_list, dim=0)
labels = torch.cat(labels_list, dim=0)
recall_list, num_rec = recall(embeddings, labels, rank=[1,5,10], ret_num=True)
return embeddings, labels, recall_list, num_rec
def collect_embeddings_with_probs(model: FuseCBM, dataloader, device, values):
model.eval()
model.to(device)
probs = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6 ,0.7, 0.8, 0.9, 1.0]
embeddings_list = []
labels_list = []
with torch.no_grad():
for prob in probs:
prob_embeddings = []
prob_labels = []
for imgs, attrs, labels in tqdm(dataloader):
imgs = imgs.to(device)
attrs = attrs.to(device)
labels = labels.to(device)
if values is None:
embeddings = model.get_fused_embedding(imgs, return_concepts=False, return_extra_dim=False)
else:
embeddings = model.get_fused_embedding_with_prob(imgs, attrs, return_concepts=False, intervention_values=values, prob_correct=prob)
# embeddings = model.get_fused_embedding_with_percentage_correction(imgs, attrs, return_concepts=False, return_extra_dim=False, intervention_values=values, percentage_correction=prob)
embeddings = F.normalize(embeddings, dim=1)
prob_embeddings.append(embeddings)
prob_labels.append(labels)
embeddings_list.append(torch.cat(prob_embeddings, dim=0))
labels_list.append(torch.cat(prob_labels, dim=0))
return embeddings_list, labels_list
def evaluate_recall_with_probs(model: FuseCBM, dataloader, device, values=None, prob=1.0):
model.eval()
model.to(device)
embeddings_list = []
labels_list = []
with torch.no_grad():
for imgs, attrs, labels in (dataloader):
imgs = imgs.to(device)
attrs = attrs.to(device)
labels = labels.to(device)
if values is None:
embeddings = model.get_fused_embedding(imgs, return_concepts=False, return_extra_dim=False)
else:
# embeddings = model.get_fused_embedding_with_prob(imgs, attrs, return_concepts=False, intervention_values=values, prob_correct=prob)
embeddings = model.get_fused_embedding_with_percentage_correction(imgs, attrs, return_concepts=False, return_extra_dim=False, intervention_values=values, percentage_correction=prob)
embeddings = F.normalize(embeddings, dim=1)
embeddings_list.append(embeddings)
labels_list.append(labels)
embeddings = torch.cat(embeddings_list, dim=0)
labels = torch.cat(labels_list, dim=0)
recall_list, num_rec = recall(embeddings, labels, rank=[1,5,10], ret_num=True)
return embeddings, labels, recall_list, num_rec
def evaluate_recall_with_probs_gallery(model: FuseCBM, dataloader, device, values=None, prob=1.0, gallery_features=None, gallery_labels=None):
model.eval()
model.to(device)
embeddings_list = []
labels_list = []
with torch.no_grad():
for imgs, attrs, labels in (dataloader):
imgs = imgs.to(device)
attrs = attrs.to(device)
labels = labels.to(device)
if values is None:
embeddings = model.get_fused_embedding(imgs, return_concepts=False, return_extra_dim=False)
else:
embeddings = model.get_fused_embedding_with_prob(imgs, attrs, return_concepts=False, intervention_values=values, prob_correct=prob)
embeddings = F.normalize(embeddings, dim=1)
embeddings_list.append(embeddings)
labels_list.append(labels)
embeddings = torch.cat(embeddings_list, dim=0)
labels = torch.cat(labels_list, dim=0)
recall_list, num_rec = recall(embeddings, labels, rank=[1,5,10], ret_num=True, gallery_features=gallery_features, gallery_labels=gallery_labels)
return embeddings, labels, recall_list, num_rec