-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.py
More file actions
283 lines (234 loc) · 11.1 KB
/
Copy pathmain.py
File metadata and controls
283 lines (234 loc) · 11.1 KB
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
import os
import random
import argparse
import yaml
import time
import pandas as pd
from tqdm import tqdm
import requests
import torch
import torch.nn.functional as F
import torch.nn as nn
import torchvision.transforms as transforms
from datasets import build_dataset
from datasets.utils import build_data_loader
import clip
from trainers import __dict__ as all_methods
from utils import *
from open_clip.src.open_clip import create_model_from_pretrained
from clip.pmcclip import ModifiedResNet, image_transform
from transformers import AutoTokenizer, AutoModel
directory = "clip/checkpoints"
# File URLs
pubmedclip_files = {
"PubMedCLIP_ViT32.pth": "https://huggingface.co/sarahESL/PubMedCLIP/resolve/main/PubMedCLIP_ViT32.pth?download=true",
}
# File URLs
pmcclip_files = {
"text_encoder.pth": "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/text_encoder.pth",
"image_encoder(resnet50).pth": "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/image_encoder(resnet50).pth",
"text_projection_layer.pth": "https://huggingface.co/datasets/axiong/pmc_oa/resolve/main/text_projection_layer.pth",
}
# Function to download a file with a progress bar
def download_file(url, filepath):
print(f"Downloading {filepath}...")
response = requests.get(url, stream=True)
if response.status_code == 200:
total_size = int(response.headers.get('content-length', 0))
with open(filepath, "wb") as file:
# Use tqdm to show the progress bar
with tqdm(total=total_size, unit='B', unit_scale=True, desc=filepath) as pbar:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
pbar.update(len(chunk)) # Update progress bar by the chunk size
print(f"{filepath} downloaded successfully.")
else:
print(f"Failed to download {filepath}. HTTP Status Code: {response.status_code}")
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
'--base_config', default='configs/base.yaml',
help='setting of Few-shot CLIP')
parser.add_argument(
'--dataset_config', default='configs/caltech101.yaml',
help='dataset config')
parser.add_argument('--opts', default=None, nargs=argparse.REMAINDER)
args = parser.parse_args()
cfg = load_cfg_from_cfg_file(args.base_config)
cfg.update(load_cfg_from_cfg_file(args.dataset_config))
if args.opts is not None:
cfg = merge_cfg_from_list(cfg, args.opts)
return cfg
class PMCCLIP(nn.Module):
def __init__(self,image_encoder, text_encoder, projection_layer):
super().__init__()
self.image_encoder = image_encoder
self.text_encoder = text_encoder
self.text_projection_layer = projection_layer
self.logit_scale = 4.4292
self.tokenizer = AutoTokenizer.from_pretrained('microsoft/BiomedNLP-BiomedBERT-base-uncased-abstract')
def forward(self,image,text):
encoded_input = self.tokenizer(text, padding='max_length', truncation=True, max_length=77, return_tensors='pt')
input_ids = encoded_input['input_ids']
text_feature = self.text_encoder(input_ids)
last_hidden_state = text_feature.last_hidden_state
pooler_output = text_feature.pooler_output
text_feature = pooler_output @ self.text_projection_layer
image_feature = self.image_encoder(image)
if isinstance(image_feature, dict):
image_feature = image_feature['image_features']
return image_feature, text_feature
def encode_text(self, text):
text_feature = self.text_encoder(text['input_ids'].cuda(), attention_mask=text['attention_mask'].cuda())
pooler_output = text_feature.pooler_output
text_feature = pooler_output @ self.text_projection_layer
return text_feature
def encode_image(self, image):
image_feature = self.image_encoder(image)
if isinstance(image_feature, dict):
image_feature = image_feature['image_features']
return image_feature
def main():
# Load config file
cfg = get_arguments()
cache_dir = os.path.join('./caches', cfg.DATASET.NAME)
os.makedirs(cache_dir, exist_ok=True)
cfg['cache_dir'] = cache_dir
print("\nRunning configs.")
print(cfg, "\n")
method = all_methods[cfg['method']](args=cfg)
clip_model_pretrained = cfg['clip_model']
if(clip_model_pretrained == 'CLIP'):
clip_model, preprocess = clip.load(cfg['backbone'])
clip_model.eval()
elif(clip_model_pretrained == 'PubMedCLIP'):
# Check for files in the directory and download if necessary
for filename, url in pubmedclip_files.items():
filepath = os.path.join(directory, filename)
if not os.path.exists(filepath):
print(f"{filename} not found in {directory}. Downloading...")
download_file(url, filepath)
else:
print(f"{filename} already exists in {directory}.")
clip_model, preprocess = clip.load('ViT-B/32')
checkpoint = torch.load(os.path.join(directory,"PubMedCLIP_ViT32.pth"),weights_only=True)
clip_model.load_state_dict(checkpoint['state_dict'])
clip_model.eval()
elif(clip_model_pretrained == 'PMCCLIP'):
# Check for files in the directory and download if necessary
for filename, url in pmcclip_files.items():
filepath = os.path.join(directory, filename)
if not os.path.exists(filepath):
print(f"{filename} not found in {directory}. Downloading...")
download_file(url, filepath)
else:
print(f"{filename} already exists in {directory}.")
image_encoder = ModifiedResNet(layers=[3,4,6,3], output_dim=768, heads=8, image_size=224, width=64)
image_encoder.load_state_dict(torch.load(os.path.join(directory,'image_encoder(resnet50).pth'),weights_only=True))
# Load Text Encoder
text_encoder = AutoModel.from_pretrained('microsoft/BiomedNLP-BiomedBERT-base-uncased-abstract')
text_encoder.load_state_dict(torch.load(os.path.join(directory,'text_encoder.pth'),weights_only=True))
# Load Text Proj Layer
text_projection_layer = torch.load(os.path.join(directory,'text_projection_layer.pth'),weights_only=True)
text_projection_layer = nn.Parameter(text_projection_layer)
# Device
device = 'cuda' if torch.cuda.is_available() else 'cpu'
image_encoder = image_encoder.to(device).eval()
text_encoder = text_encoder.to(device).eval()
text_projection_layer = text_projection_layer.to(device)
clip_model = PMCCLIP(image_encoder, text_encoder, text_projection_layer).to(device).eval()
preprocess = image_transform(image_size=224)
elif(clip_model_pretrained == 'BiomedCLIP'):
# Load the model and config files from the Hugging Face Hub
clip_model, preprocess = create_model_from_pretrained('hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224')
clip_model = clip_model.cuda()
clip_model.eval()
# Prepare dataset
random.seed(1)
torch.manual_seed(1)
cfg.DATASET.ROOT = cfg['root_path']
cfg.SEED = 1
cfg.DATASET.SUBSAMPLE_CLASSES = "all"
cfg.DATASET.NUM_SHOTS = cfg['shots']
print("Preparing dataset.")
dataset = build_dataset(cfg)
classnames = dataset.classnames
test_loader = build_data_loader(data_source=dataset.test, batch_size=100, is_train=False, tfm=preprocess, shuffle=False)
train_tranform = transforms.Compose([
transforms.RandomResizedCrop(size=224, scale=(0.5, 1), interpolation=transforms.InterpolationMode.BICUBIC),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711))
])
template = ['a photo of a {}.']
# Textual features
print(f"Getting textual features as {clip_model_pretrained}'s classifier.")
if(clip_model_pretrained in ['CLIP', 'PubMedCLIP']):
clip_weights = clip_classifier(
dataset.classnames, template, clip_model)
elif(clip_model_pretrained == 'BiomedCLIP'):
clip_weights = biomedclip_classifier(
dataset.classnames, template, clip_model)
elif(clip_model_pretrained == 'PMCCLIP'):
clip_weights = pmcclip_classifier(
dataset.classnames, template, clip_model)
# Pre-load test features
f_test_time = time.time()
print("\nLoading visual features and labels from test set.")
test_features, test_labels = pre_load_features(
cfg, "test", clip_model, test_loader)
total_acc = 0
predictions = []
for i in range(cfg['tasks']):
random.seed(i+1)
torch.manual_seed(i+1)
print("Start Training Task:{}".format(str(i+1)))
few_shot_train_data = dataset.generate_fewshot_dataset_(cfg['shots'], split="train")
few_shot_val_data = dataset.generate_fewshot_dataset_(cfg['shots'], split="val")
if cfg['finetune']:
train_loader = build_data_loader(
data_source=few_shot_train_data, batch_size=cfg["batch_size"], tfm=train_tranform, is_train=True, shuffle=True)
else:
train_loader = build_data_loader(
data_source=few_shot_train_data, batch_size=cfg["batch_size"], tfm=train_tranform, is_train=True, shuffle=False)
val_loader = build_data_loader(
data_source=few_shot_val_data, batch_size=cfg["batch_size"], tfm=preprocess, is_train=False, shuffle=False)
loss, acc = method(train_loader=train_loader,
val_loader=val_loader,
test_features=test_features,
test_labels=test_labels,
text_weights=clip_weights,
model=clip_model,
classnames=classnames)
print('Final Accuracy on task {}: {}'.format(str(i+1), acc))
predictions.append(acc)
tasks_acc, tasks_std = compute_confidence_interval(predictions)
test_stats = {}
test_stats['acc'] = tasks_acc
test_stats['std'] = tasks_std
print('Total Accuracy and std on {} tasks: {:.4f} , {:.4f}'.format(
str(cfg['tasks']), tasks_acc, tasks_std))
if not os.path.exists(cfg['output_dir']):
os.makedirs(cfg['output_dir'])
csv_path = os.path.join(cfg['output_dir'], cfg.DATASET.NAME +".csv")
write_to_csv(cfg, csv_path, test_stats)
def write_to_csv(cfg, path, test_stats):
try:
res = pd.read_csv(path)
except:
res = pd.DataFrame()
records = res.to_dict('records')
if cfg['method'] == "TIPAdapter" and cfg["finetune"]:
test_stats['method'] = "TIPAdapter-F"
else:
test_stats['method'] = cfg['method']
test_stats['acc'] = round(test_stats['acc'],4)
test_stats['std'] = round(test_stats['std'],4)
test_stats['num_shots'] = cfg['shots']
test_stats['tasks'] = cfg['tasks']
records.append(test_stats)
# Save back to dataframe
df = pd.DataFrame.from_records(records)
df.to_csv(path, index=False)
if __name__ == '__main__':
main()