-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathload.py
336 lines (264 loc) · 14.8 KB
/
load.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import json
import numpy as np
import torch
from torch.nn import functional as F
from descriptor_strings import * # label_to_classname, wordify, modify_descriptor,
import pathlib
from torch.utils.data import DataLoader, Subset
from torchvision import transforms
from torchvision.datasets import ImageNet, ImageFolder, Places365
from imagenetv2_pytorch import ImageNetV2Dataset as ImageNetV2
from datasets import _transform, CUBDataset
from collections import OrderedDict
import clip
from loading_helpers import *
hparams = {}
# hyperparameters
hparams['model_size'] = "ViT-B/32"
# Options:
# ['RN50',
# 'RN101',
# 'RN50x4',
# 'RN50x16',
# 'RN50x64',
# 'ViT-B/32',
# 'ViT-B/16',
# 'ViT-L/14',
# 'ViT-L/14@336px']
hparams['dataset'] = 'cub'
hparams['batch_size'] = 64*10
hparams['device'] = "cuda" if torch.cuda.is_available() else "cpu"
hparams['category_name_inclusion'] = 'prepend' #'append' 'prepend'
hparams['apply_descriptor_modification'] = True
hparams['verbose'] = False
hparams['image_size'] = 224
if hparams['model_size'] == 'ViT-L/14@336px' and hparams['image_size'] != 336:
print(f'Model size is {hparams["model_size"]} but image size is {hparams["image_size"]}. Setting image size to 336.')
hparams['image_size'] = 336
elif hparams['model_size'] == 'RN50x4' and hparams['image_size'] != 288:
print(f'Model size is {hparams["model_size"]} but image size is {hparams["image_size"]}. Setting image size to 288.')
hparams['image_size'] = 288
elif hparams['model_size'] == 'RN50x16' and hparams['image_size'] != 384:
print(f'Model size is {hparams["model_size"]} but image size is {hparams["image_size"]}. Setting image size to 288.')
hparams['image_size'] = 384
elif hparams['model_size'] == 'RN50x64' and hparams['image_size'] != 448:
print(f'Model size is {hparams["model_size"]} but image size is {hparams["image_size"]}. Setting image size to 288.')
hparams['image_size'] = 448
hparams['before_text'] = ""
hparams['label_before_text'] = ""
hparams['between_text'] = ', '
# hparams['between_text'] = ' '
# hparams['between_text'] = ''
hparams['after_text'] = ''
hparams['unmodify'] = True
# hparams['after_text'] = '.'
# hparams['after_text'] = ' which is a type of bird.'
hparams['label_after_text'] = ''
# hparams['label_after_text'] = ' which is a type of bird.'
hparams['seed'] = 1
# TODO: fix this... defining global variable to be edited in a function, bad practice
# unmodify_dict = {}
# classes_to_load = openai_imagenet_classes
hparams['descriptor_fname'] = None
IMAGENET_DIR = '/proj/vondrick3/datasets/ImageNet/' # REPLACE THIS WITH YOUR OWN PATH
IMAGENETV2_DIR = '/proj/vondrick/datasets/ImageNetV2/' # REPLACE THIS WITH YOUR OWN PATH
CUB_DIR = '/proj/vondrick/datasets/Birds-200-2011/' # REPLACE THIS WITH YOUR OWN PATH
EUROSAT_DIR = ''
FOOD101_DIR = ''
PETS_DIR = ''
DTD_DIR = ''
PLACES_DIR = ''
# PyTorch datasets
tfms = _transform(hparams['image_size'])
if hparams['dataset'] == 'imagenet':
if hparams['dataset'] == 'imagenet':
dsclass = ImageNet
hparams['data_dir'] = pathlib.Path(IMAGENET_DIR)
# train_ds = ImageNet(hparams['data_dir'], split='val', transform=train_tfms)
dataset = dsclass(hparams['data_dir'], split='val', transform=tfms)
classes_to_load = None
if hparams['descriptor_fname'] is None:
hparams['descriptor_fname'] = 'descriptors_imagenet'
hparams['after_text'] = hparams['label_after_text'] = '.'
elif hparams['dataset'] == 'imagenetv2':
hparams['data_dir'] = pathlib.Path(IMAGENETV2_DIR)
dataset = ImageNetV2(location=hparams['data_dir'], transform=tfms)
classes_to_load = openai_imagenet_classes
hparams['descriptor_fname'] = 'descriptors_imagenet'
dataset.classes = classes_to_load
elif hparams['dataset'] == 'cub':
# load CUB dataset
hparams['data_dir'] = pathlib.Path(CUB_DIR)
dataset = CUBDataset(hparams['data_dir'], train=False, transform=tfms)
classes_to_load = None #dataset.classes
hparams['descriptor_fname'] = 'descriptors_cub'
# I recommend using VISSL https://github.com/facebookresearch/vissl/blob/main/extra_scripts/README.md to download these
elif hparams['dataset'] == 'eurosat':
from extra_datasets.patching.eurosat import EuroSATVal
hparams['data_dir'] = pathlib.Path(EUROSAT_DIR)
dataset = EuroSATVal(location=hparams['data_dir'], preprocess=tfms)
dataset = dataset.test_dataset
hparams['descriptor_fname'] = 'descriptors_eurosat'
classes_to_load = None
elif hparams['dataset'] == 'places365':
hparams['data_dir'] = pathlib.Path(PLACES_DIR)
# dataset = Places365(hparams['data_dir'], split='val', small=True, download=False, transform=tfms)
dsclass = ImageFolder
dataset = dsclass(hparams['data_dir'] / 'val', transform=tfms)
hparams['descriptor_fname'] = 'descriptors_places365'
elif hparams['dataset'] == 'food101':
hparams['data_dir'] = pathlib.Path(FOOD101_DIR)
dsclass = ImageFolder
dataset = dsclass(hparams['data_dir'] / 'test', transform=tfms)
hparams['descriptor_fname'] = 'descriptors_food101'
classes_to_load = None
elif hparams['dataset'] == 'pets':
hparams['data_dir'] = pathlib.Path(PETS_DIR)
dsclass = ImageFolder
dataset = dsclass(hparams['data_dir'] / 'test', transform=tfms)
hparams['descriptor_fname'] = 'descriptors_pets'
classes_to_load = None
elif hparams['dataset'] == 'dtd':
hparams['data_dir'] = pathlib.Path(DTD_DIR)
dataset = ImageFolder(hparams['data_dir'] / 'val', transform=tfms)
hparams['descriptor_fname'] = 'descriptors_dtd'
classes_to_load = None
hparams['descriptor_fname'] = './descriptors/' + hparams['descriptor_fname']
print("Creating descriptors...")
gpt_descriptions, unmodify_dict = load_gpt_descriptions(hparams, classes_to_load)
label_to_classname = list(gpt_descriptions.keys())
n_classes = len(list(gpt_descriptions.keys()))
def compute_description_encodings(model):
description_encodings = OrderedDict()
for k, v in gpt_descriptions.items():
tokens = clip.tokenize(v).to(hparams['device'])
description_encodings[k] = F.normalize(model.encode_text(tokens))
return description_encodings
def compute_label_encodings(model):
label_encodings = F.normalize(model.encode_text(clip.tokenize([hparams['label_before_text'] + wordify(l) + hparams['label_after_text'] for l in label_to_classname]).to(hparams['device'])))
return label_encodings
def aggregate_similarity(similarity_matrix_chunk, aggregation_method='mean'):
if aggregation_method == 'max': return similarity_matrix_chunk.max(dim=1)[0]
elif aggregation_method == 'sum': return similarity_matrix_chunk.sum(dim=1)
elif aggregation_method == 'mean': return similarity_matrix_chunk.mean(dim=1)
else: raise ValueError("Unknown aggregate_similarity")
def show_from_indices(indices, images, labels=None, predictions=None, predictions2 = None, n=None, image_description_similarity=None, image_labels_similarity=None):
if indices is None or (len(indices) == 0):
print("No indices provided")
return
if n is not None:
indices = indices[:n]
for index in indices:
show_single_image(images[index])
print(f"Index: {index}")
if labels is not None:
true_label = labels[index]
true_label_name = label_to_classname[true_label]
print(f"True label: {true_label_name}")
if predictions is not None:
predicted_label = predictions[index]
predicted_label_name = label_to_classname[predicted_label]
print(f"Predicted label (ours): {predicted_label_name}")
if predictions2 is not None:
predicted_label2 = predictions2[index]
predicted_label_name2 = label_to_classname[predicted_label2]
print(f"Predicted label 2 (CLIP): {predicted_label_name2}")
print("\n")
if image_labels_similarity is not None:
if labels is not None:
print(f"Total similarity to {true_label_name} (true label) labels: {image_labels_similarity[index][true_label].item()}")
if predictions is not None:
if labels is not None and true_label_name == predicted_label_name:
print("Predicted label (ours) matches true label")
else:
print(f"Total similarity to {predicted_label_name} (predicted label) labels: {image_labels_similarity[index][predicted_label].item()}")
if predictions2 is not None:
if labels is not None and true_label_name == predicted_label_name2:
print("Predicted label 2 (CLIP) matches true label")
elif predictions is not None and predicted_label_name == predicted_label_name2:
print("Predicted label 2 (CLIP) matches predicted label 1")
else:
print(f"Total similarity to {predicted_label_name2} (predicted label 2) labels: {image_labels_similarity[index][predicted_label2].item()}")
print("\n")
if image_description_similarity is not None:
if labels is not None:
print_descriptor_similarity(image_description_similarity, index, true_label, true_label_name, "true")
print("\n")
if predictions is not None:
if labels is not None and true_label_name == predicted_label_name:
print("Predicted label (ours) same as true label")
# continue
else:
print_descriptor_similarity(image_description_similarity, index, predicted_label, predicted_label_name, "descriptor")
print("\n")
if predictions2 is not None:
if labels is not None and true_label_name == predicted_label_name2:
print("Predicted label 2 (CLIP) same as true label")
# continue
elif predictions is not None and predicted_label_name == predicted_label_name2:
print("Predicted label 2 (CLIP) matches predicted label 1")
else:
print_descriptor_similarity(image_description_similarity, index, predicted_label2, predicted_label_name2, "CLIP")
print("\n")
def print_descriptor_similarity(image_description_similarity, index, label, label_name, label_type="provided"):
# print(f"Total similarity to {label_name} ({label_type} label) descriptors: {aggregate_similarity(image_description_similarity[label][index].unsqueeze(0)).item()}")
print(f"Total similarity to {label_name} ({label_type} label) descriptors:")
print(f"Average:\t\t{100.*aggregate_similarity(image_description_similarity[label][index].unsqueeze(0)).item()}")
label_descriptors = gpt_descriptions[label_name]
for k, v in sorted(zip(label_descriptors, image_description_similarity[label][index]), key = lambda x: x[1], reverse=True):
k = unmodify_dict[label_name][k]
# print("\t" + f"matched \"{k}\" with score: {v}")
print(f"{k}\t{100.*v}")
def print_max_descriptor_similarity(image_description_similarity, index, label, label_name):
max_similarity, argmax = image_description_similarity[label][index].max(dim=0)
label_descriptors = gpt_descriptions[label_name]
print(f"I saw a {label_name} because I saw {unmodify_dict[label_name][label_descriptors[argmax.item()]]} with score: {max_similarity.item()}")
def show_misclassified_images(images, labels, predictions, n=None,
image_description_similarity=None,
image_labels_similarity=None,
true_label_to_consider: int = None,
predicted_label_to_consider: int = None):
misclassified_indices = yield_misclassified_indices(images, labels=labels, predictions=predictions, true_label_to_consider=true_label_to_consider, predicted_label_to_consider=predicted_label_to_consider)
if misclassified_indices is None: return
show_from_indices(misclassified_indices, images, labels, predictions,
n=n,
image_description_similarity=image_description_similarity,
image_labels_similarity=image_labels_similarity)
def yield_misclassified_indices(images, labels, predictions, true_label_to_consider=None, predicted_label_to_consider=None):
misclassified_indicators = (predictions.cpu() != labels.cpu())
if true_label_to_consider is not None:
misclassified_indicators = misclassified_indicators & (labels.cpu() == true_label_to_consider)
if predicted_label_to_consider is not None:
misclassified_indicators = misclassified_indicators & (predictions.cpu() == predicted_label_to_consider)
if misclassified_indicators.sum() == 0:
output_string = 'No misclassified images found'
if true_label_to_consider is not None:
output_string += f' with true label {label_to_classname[true_label_to_consider]}'
if predicted_label_to_consider is not None:
output_string += f' with predicted label {label_to_classname[predicted_label_to_consider]}'
print(output_string + '.')
return
misclassified_indices = torch.arange(images.shape[0])[misclassified_indicators]
return misclassified_indices
from PIL import Image
def predict_and_show_explanations(images, model, labels=None, description_encodings=None, label_encodings=None, device=None):
if type(images) == Image:
images = tfms(images)
if images.device != device:
images = images.to(device)
labels = labels.to(device)
image_encodings = model.encode_image(images)
image_encodings = F.normalize(image_encodings)
image_labels_similarity = image_encodings @ label_encodings.T
clip_predictions = image_labels_similarity.argmax(dim=1)
n_classes = len(description_encodings)
image_description_similarity = [None]*n_classes
image_description_similarity_cumulative = [None]*n_classes
for i, (k, v) in enumerate(description_encodings.items()): # You can also vectorize this; it wasn't much faster for me
dot_product_matrix = image_encodings @ v.T
image_description_similarity[i] = dot_product_matrix
image_description_similarity_cumulative[i] = aggregate_similarity(image_description_similarity[i])
# create tensor of similarity means
cumulative_tensor = torch.stack(image_description_similarity_cumulative,dim=1)
descr_predictions = cumulative_tensor.argmax(dim=1)
show_from_indices(torch.arange(images.shape[0]), images, labels, descr_predictions, clip_predictions, image_description_similarity=image_description_similarity, image_labels_similarity=image_labels_similarity)