-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloss_landscape_clip.py
198 lines (166 loc) · 5.62 KB
/
loss_landscape_clip.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
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
# %%
import loss_landscapes
import numpy as np
import torch.nn.functional as F
from torch import nn
from evaluate_single_task_clip import *
# %%
fabric = L.Fabric(accelerator="gpu", devices=1)
fabric.launch()
# %%
def evaluate_loss(
*,
clip_model: CLIPModel,
clip_processor,
text: List[str],
test_loader,
) -> float:
clip_model.eval()
# precompute the text features
text_input = clip_processor(text, return_tensors="pt", padding=True)
text_embeds = clip_model.get_text_features(**text_input)
loss, count = 0, 0
with TitledLog("Evaluate accuracy", log_fn=log.info):
for batch in tqdm(test_loader):
images, labels = batch
with torch.no_grad():
image_embeds = clip_model.get_image_features(pixel_values=images)
# normalized features
image_embeds = image_embeds / image_embeds.norm(
p=2, dim=-1, keepdim=True
)
text_embeds = text_embeds.to(image_embeds.device)
text_embeds = text_embeds / text_embeds.norm(p=2, dim=-1, keepdim=True)
# cosine similarity as logits
logit_scale = clip_model.logit_scale.exp().item()
logits_per_text = (
torch.matmul(text_embeds, image_embeds.t()) * logit_scale
)
logits_per_image = logits_per_text.t()
loss += F.cross_entropy(
logits_per_image, labels, reduction="sum"
).item()
count += len(labels)
return loss / count
def evaluate_loss_for_clip_vision_model(
clip_vision_model, test_loader: DataLoader, classes: List[str]
) -> float:
# replace the `clip_model.vision_model` with `clip_vision_model`
clip_processor, clip_model = load_clip_model(
MODEL_NAME_OR_PATH, local_files_only=True
)
clip_model.vision_model = deepcopy(clip_vision_model)
# setup fabric modules
clip_model.vision_model = fabric.setup_module(clip_model.vision_model)
clip_model.visual_projection = fabric.setup_module(clip_model.visual_projection)
# compute text features
text = [f"a photo of a {c}" for c in classes]
test_loader = fabric.setup_dataloaders(test_loader)
loss = evaluate_loss(
clip_model=clip_model,
clip_processor=clip_processor,
text=text,
test_loader=test_loader,
)
return loss
# %%
from loss_landscapes.metrics import Metric
class SingleTaskLoss(Metric):
def __init__(self, task_name: str):
super().__init__()
self.task_name = task_name
self.dataloader = test_loaders[task_name]
self.classes = datamodules[task_name].classes
def __call__(self, model_wrapper: loss_landscapes.ModelWrapper) -> float:
# print(model_wrapper.modules)
loss = evaluate_loss_for_clip_vision_model(
model_wrapper.modules[0],
self.dataloader,
self.classes,
)
return loss
class MultiTaskLoss(Metric):
def __init__(self, task_names: List[str]):
super().__init__()
self.task_names = task_names
self.dataloaders = [test_loaders[t] for t in task_names]
self.classes = [datamodules[t].classes for t in task_names]
def __call__(self, model_wrapper: loss_landscapes.ModelWrapper) -> float:
loss = 0
for dataloader, classes in zip(self.dataloaders, self.classes):
loss += evaluate_loss_for_clip_vision_model(
model_wrapper.modules[0],
dataloader,
classes,
)
return loss
def dummy_loss(*args):
return 1
# %%
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(usage=str(DATASET_NAMES))
parser.add_argument(
"--method",
type=str,
default="standard",
)
parser.add_argument(
"task_one",
type=str,
)
parser.add_argument(
"task_two",
type=str,
)
args = parser.parse_args()
method = args.method
task_one = args.task_one
task_two = args.task_two
# |
# |
# --- 0 --- 2
# |
# 1
model_start: nn.Module = deepcopy(pretrained_clip_vision_models[method])
model_end_one: nn.Module = deepcopy(pretrained_clip_vision_models[method])
model_end_two: nn.Module = deepcopy(pretrained_clip_vision_models[method])
model_start.load_state_dict(
state_dict_add(
model_start.state_dict(),
state_dict_add(
finetuned_clip_vison_models_task_vectors[method][task_one],
finetuned_clip_vison_models_task_vectors[method][task_two],
),
strict=False,
),
strict=False,
)
model_end_one.load_state_dict(
state_dict_add(
model_end_one.state_dict(),
state_dict_mul(
finetuned_clip_vison_models_task_vectors[method][task_one], 2.0
),
strict=False,
),
strict=False,
)
model_end_two.load_state_dict(
state_dict_add(
model_end_two.state_dict(),
state_dict_mul(
finetuned_clip_vison_models_task_vectors[method][task_two], 2.0
),
strict=False,
),
strict=False,
)
landscape = loss_landscapes.planar_interpolation(
model_start=model_start,
model_end_one=model_end_one,
model_end_two=model_end_two,
metric=MultiTaskLoss([task_one, task_two]),
deepcopy_model=False,
)
np.save(f"results/ViT-B-16/{method}_landscape_{task_one}-{task_two}.npy", landscape)