-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_clip_feature.py
176 lines (159 loc) · 6.37 KB
/
extract_clip_feature.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
'''
Extract logit for CLIP
'''
import pathlib
import sys
sys.path.append('..')
import clip
import numpy as np
import torch
import tqdm
import datasets
import copy
import os
import copy
import itertools
import progmet
path_prefix=''
def extract_features_pipeline(dataset, model_file_name,split_name):
out_dir = pathlib.Path(root+model_file_name)
out_dir.mkdir(exist_ok=True)
if os.path.isfile(out_dir / f'{split_name}.pt') and os.path.isfile(out_dir / f'{split_name}_label.pt'):
print('Loaded pre-computed image feature')
return
features, labels = extract_image_features(dataset)
features_norm= features/features.norm(dim=-1, keepdim=True)
print(out_dir / f'{split_name}.pt')
torch.save(features,out_dir / f'{split_name}_unnormed.pt')
torch.save(features_norm,out_dir / f'{split_name}.pt')
torch.save(labels,out_dir / f'{split_name}_label.pt')
print()
def extract_image_features(dataset):
loader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=64,
shuffle=False,
pin_memory=False,
num_workers=8,
prefetch_factor=2)
feature_batches = []
label_batches = []
model.eval()
with torch.inference_mode():
for image_batch, label_batch in tqdm.tqdm(loader):
image_batch = image_batch.to(device)
feature_batch = model.encode_image(image_batch)
feature_batch_=copy.deepcopy(feature_batch.cpu())
feature_batches.append(feature_batch_.cpu())
# Important not to keep output of DataLoader. Perform deep copy.
# https://github.com/pytorch/pytorch/issues/11201#issuecomment-486232056
label_batch_=copy.deepcopy(label_batch.cpu())
label_batches.append(label_batch_)
features = torch.cat(feature_batches,axis=0)
labels = torch.cat(label_batches,axis=0)
return features, labels
def load_text_feature(full_text_file_path,text_name):
text_inputs = torch.cat([clip.tokenize(f"a photo of a {c}") for c in text_name]).to(device)
batch_size=256
iter_=int(np.ceil(len(text_inputs)/batch_size))
'''
extract text feature
'''
text_features_list=[]
# Calculate text features
with torch.no_grad():
for i in tqdm.tqdm(range(iter_)):
slice_=slice(int(i*batch_size),int((i+1)*batch_size))
text_features_ = model.encode_text(text_inputs[slice_,:])
text_features_list.append(text_features_)
text_features=torch.concat(text_features_list)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_features=text_features.cpu()
torch.save(text_features,full_text_file_path)
print(f'save text embedding to {full_text_file_path}')
return text_features
def zero_shot_exp(model_file_name,text_features):
print(model_file_name)
image_file_path=root+model_file_name
image_dataset= datasets.NpzDataset(image_file_path,'test',None)
image_dataloader = torch.utils.data.DataLoader(
dataset=image_dataset,
batch_size=32,
shuffle=False,
pin_memory=False,
num_workers=8,
prefetch_factor=2)
gt=torch.load(image_file_path+'/test_label.pt')
'''
calculate similarity & acc
'''
pred_list=[]
print('Running zero shot experiment...')
meter = progmet.ProgressMeter('apply', interval_time=5)
logit_list=[]
for batch_index, minibatch in enumerate(itertools.islice(meter(image_dataloader), None)):
inputs, gt_labels = minibatch
image_features=inputs
similarity = 100.0 * (image_features.cuda() @ text_features.T.float().cuda())
logit_list.append(similarity)
pred=similarity.argmax(-1)
pred_list.append(pred)
logit_list_ = torch.concat(logit_list).cpu()
os.makedirs(root+'llogit',exist_ok=True)
torch.save(logit_list_, root+'llogit/{}'.format(model_file_name))
pred_list_ = torch.concat(pred_list).cpu()
acc=np.mean((pred_list_==gt).numpy())
print(f'zero shot accuracy for {model_file_name} test split is {acc}')
device = torch.device('cuda')
print('preprocess:' +'\n')
root ='./features/'
text_root='./text_features/'
os.makedirs(root, exist_ok = True)
os.makedirs(text_root, exist_ok = True)
'''
imagenet extraction
'''
DATASETS = ['imagenet_r','imagenet','imagenetv2','imagenet_sketch','imagenet_a','objectnet']
for clip_model_name,model_name in zip(['ViT-B/32','ViT-B/16','ViT-L/14','ViT-L/14@336px','RN50','RN101','RN50x4'],['vitb32','vitb16','vitl14','vitl14_336','RN50','RN101','RN50x4']):
model, preprocess = clip.load(clip_model_name, device=device)
for dataset_name in DATASETS:
print()
split_name='test'
model_file_name=f'{path_prefix}{dataset_name}_{model_name}'
print(model_file_name)
if os.path.isdir(root+model_file_name)==True:
print(f'passing {root+model_file_name}')
continue
if 'imagenet' in dataset_name:
testset, labels, label_map = datasets.build_imagenet_dataset(dataset_name, split_name, preprocess)
text_name=labels # it's all imageNet textname
# text_name=np.array(labels)[label_map].tolist()
else:
testset, labels, _, label_map = datasets.build_objectnet_dataset(preprocess)
text_name=labels
'''
Extract image embedding
'''
os.makedirs(root+model_file_name,exist_ok=True)
torch.save(label_map, root+model_file_name+ '/label_map.pt')
print(f"dataset {dataset_name} [split {split_name}] len {len(testset)} with encode model {model_name}")
extract_features_pipeline(testset, model_file_name,split_name)
'''
Extract text embedding
'''
full_text_file_path=text_root+model_file_name
if 'imagenet' in dataset_name:
full_text_file_path=full_text_file_path.replace(dataset_name,'imagenet')
elif 'objectnet' in dataset_name:
full_text_file_path=full_text_file_path
if os.path.isfile(full_text_file_path):
print('loaded text embedding')
text_features=torch.load(full_text_file_path)
else:
text_features=load_text_feature(full_text_file_path,text_name)
if 'imagenet' in dataset_name:
text_features=text_features[label_map,:]
'''
Image/Text feature alignment
'''
zero_shot_exp(model_file_name,text_features)