-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_runner.py
290 lines (247 loc) · 12.8 KB
/
method_runner.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
import torch
import torch.nn as nn
import os
import time
from torch.optim import Adam
from VisionTransformer.custom import VisionTransformer, CLIPClassifier
# from VisionTransformer.logger import create_logger
import clip
from models import network
from timm.loss import LabelSmoothingCrossEntropy, SoftTargetCrossEntropy
from timm.scheduler.cosine_lr import CosineLRScheduler
import util.trainval as TV
from util import train_gan
def get_sentence_embeddings():
# Define label dictionary and sentence template
label_dict = {
0: "start comm",
1: "end comm",
2: "up",
3: "down",
4: "photo",
5: "backwards",
6: "carry",
7: "boat",
8: "here",
9: "mosaic",
10: "num delimiter",
11: "one",
12: "two",
13: "three",
14: "four",
15: "five"
}
sentence_template = "A photo of a diver gesturing {}"
# Create dictionary to store sentence features
sentence_features = {}
# Load the CLIP model
device = 'cuda' if torch.cuda.is_available() else 'cpu'
clip_model, _ = clip.load('ViT-B/32', device=device) # take from arguments
# Extract features for each sentence
for idx, label_name in label_dict.items():
sentence = sentence_template.format(label_name)
# Tokenize the sentence
tokenized_text = clip.tokenize([sentence])
# Get the features (embeddings) for the sentence
with torch.no_grad():
features = clip_model.encode_text(tokenized_text.to(device=device))
# Store the features for the sentence
sentence_features[idx] = features
# Stack features into a single tensor
sentence_features_tensor = torch.cat(list(sentence_features.values()), dim=0)
return sentence_features_tensor.float().detach()
def load_ckpt(args, ckpt_path, optimizer, lr_scheduler):
checkpoint = torch.load(ckpt_path, map_location='cpu')
if 'optimizer' in checkpoint and 'lr_scheduler' in checkpoint:
optimizer.load_state_dict(checkpoint['optimizer'])
lr_scheduler.load_state_dict(checkpoint['lr_scheduler'])
# change SS: do this part if you want to add more lr drops while your code has already run for k times
# if args.extend_lr_drop_list:
# lr_drops = args.lr_drop_list
# # make sure you have added the lr drop epochs in the script to run
# lr_scheduler.milestones = Counter(lr_drops)
# # if lr scheduler has to make a drop at epoch k, it checks for it at epoch k-1
# # print(lr_scheduler.milestones)
return checkpoint
def our_method(datasets,dataloaders,args):
if args.method == 'ours':
dir = args.dirs[f'{args.our_method_type}_{args.split_type}_{args.split_number}']
elif args.method == 'clip_linear_probe':
dir = args.dirs[f'{args.clip_version}_{args.split_type}_{args.split_number}']
elif args.method == 'pretrained_cnn':
dir = args.dirs[f'{args.pretrained_cnn_type}_{args.split_type}_{args.split_number}']
elif args.method == 'existing_zsl':
dir = args.dirs[f'{args.existing_zsl_type}_{args.split_type}_{args.split_number}']
else:
raise ValueError(f'Method {args.method} not supported')
# logger = create_logger(output_dir=dir, eval=args.eval, setting_name=args.setting) # take from arguments
output_file_path = os.path.join(dir,'log_train_'+args.setting+'.txt')
f = open(output_file_path,'a')
f.write(str(args))
if args.our_method_type == 'base-ViT':
f.write('\n = = = = = = = = Vision Transformer = = = = = = = = \n' )
elif args.our_method_type == 'GCAT':
f.write('\n = = = = = = = = GCAT model = = = = = = = = \n' )
elif args.method == 'clip_linear_probe':
f.write('\n = = = = = = = = clip_linear_probe model = = = = = = = = \n' )
dataset_sizes = {x: len(datasets[x]) for x in ['train', 'test_seen', 'test_unseen']}
sentence_features = get_sentence_embeddings()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# print(f'device: {device}')
# device=torch.device("cpu")
num_epochs = args.epochs # take from args
if args.method == 'ours':
print('###########################')
print(args.our_method_type)
print('###########################')
if args.our_method_type == 'base-ViT':
model = VisionTransformer(
img_width=224,
img_height=224,
patch_size=16, # subject to change
in_chans=3,
n_classes=16, # Change this based on your dataset
embed_dim=768,
depth=12,
n_heads=12,
mlp_ratio=4.,
qkv_bias=True,
p=0.,
attn_p=0.,
).to(device)
# Define optimizer and scheduler
optimizer = Adam(model.parameters(), lr=2e-6)
n_iter_per_epoch = len(dataloaders['train'])
num_steps = int(num_epochs * n_iter_per_epoch)
warmup_steps = int(5 * n_iter_per_epoch)
lr_scheduler = CosineLRScheduler(
optimizer,
t_initial=num_steps,
lr_min=8.e-6 / 100,
warmup_lr_init=0,
warmup_t=warmup_steps,
cycle_limit=1,
t_in_epochs=False,
)
elif args.our_method_type == 'GCAT':
model = network.build(args, sentence_features).to(device)
# no need to compute gradients through this linear layer during test time
for name, p in model.named_parameters():
if 'eval_visual_projection' in name:
p.requires_grad = False
# no need to compute gradients for CLIP components
if args.fix_clip:
for name, p in model.named_parameters():
if 'visual_projection' in name or 'clip_model' in name:
p.requires_grad = False
param_dicts = [
{"params": [p for n, p in model.named_parameters() if "backbone" not in n and p.requires_grad]},
{
"params": [p for n, p in model.named_parameters() if "backbone" in n and p.requires_grad],
"lr": args.lr_backbone,
},
]
optimizer = torch.optim.AdamW(param_dicts, lr=args.lr, weight_decay=args.weight_decay)
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
print('Number of params:', n_parameters)
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[args.lr_drop], gamma=args.lr_drop_gamma)
ckpt_path = os.path.join(dir, args.setting+'_'+args.resume)
best_ckpt_path = os.path.join(dir, args.setting+'_'+args.best_ckpt)
if args.resume and os.path.exists(ckpt_path):
checkpoint = load_ckpt(args, ckpt_path, optimizer, lr_scheduler)
print(f'Loaded checkpoint from {ckpt_path}')
model.load_state_dict(checkpoint['model'])
start_epoch = checkpoint['epoch'] + 1
else:
start_epoch = 0
if os.path.exists(best_ckpt_path):
best_checkpoint = load_ckpt(args, best_ckpt_path, optimizer, lr_scheduler)
best_performance = best_checkpoint['performance_stats']
else:
# TODO: make best scores for acc_seen, acc_unseen, acc_H, too
# best_performance = {
# 'acc_novel': 0.0,
# 'acc_seen': 0.0,
# 'acc_unseen': 0.0,
# 'HM': 0.0
# }
best_performance = {
'acc_seen': 0.0,
'acc_unseen': 0.0,
'HM': 0.0
}
since = time.time()
args.unseen_labels = args.split_labels['test_unseen']
# best_acc = 0.0
# best_H = 0.0
# best_seen_acc = 0.0
# best_unseen_acc = 0.0
for epoch in range(start_epoch, num_epochs):
epoch_since = time.time()
# f.write('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train', 'test_unseen']:
if phase == 'train':
# return avg loss
avg_loss = TV.train_one_epoch(epoch,device, model, dataloaders[phase],optimizer,lr_scheduler,f,sentence_features, args) # Set model to training mode
else:
# acc1_avg, _ = TV.val_czsl(device, dataloaders[phase], model) # Set model to evaluate mode
H, acc_seen, _, acc_unseen, _ = TV.val_gzsl(device, dataloaders, model)
epoch_time = time.time() - epoch_since
f.write(f'Epoch {epoch+1}/{num_epochs} ')
# f.write(f'acc_czsl = {acc1_avg*100} ')
f.write(f'loss={avg_loss} H={H*100} acc_gzsl: acc_seen={acc_seen*100} acc_unseen={acc_unseen*100}\n')
print(f'Epoch [{epoch+1}/{num_epochs}] loss: {avg_loss} H: {H*100: .6f} acc_seen: {acc_seen*100: .6f} acc_unseen: {acc_unseen*100: .6f} time: {epoch_time // 60:.0f}m {epoch_time % 60:.0f}s')
# print(f'Epoch [{epoch+1}/{num_epochs}] loss: {avg_loss} acc_czsl: {acc1_avg*100: .6f} H: {H*100: .6f} acc_seen: {acc_seen*100: .6f} acc_unseen: {acc_unseen*100: .6f} time: {epoch_time // 60:.0f}m {epoch_time % 60:.0f}s')
lr_scheduler.step()
if phase == 'test_unseen' and H > best_performance['HM']:
best_performance['acc_seen']=acc_seen
best_performance['acc_unseen']=acc_unseen
best_performance['HM'] = H
deep_copy = {'avg_loss': avg_loss,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'lr_scheduler': lr_scheduler.state_dict(),
'epoch': epoch,
'performance_stats': best_performance,
'args': args
}
torch.save(deep_copy, best_ckpt_path)
# save current epoch ckpt
if phase == 'test_unseen':
torch.save({
'avg_loss': avg_loss,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'lr_scheduler': lr_scheduler.state_dict(),
'epoch': epoch,
# 'performance_stats': {
# 'acc_novel': acc1_avg,
# 'acc_seen': acc_seen,
# 'acc_unseen': acc_unseen,
# 'HM': H
# },
'performance_stats': {
'acc_seen': acc_seen,
'acc_unseen': acc_unseen,
'HM': H
},
'args': args
}, ckpt_path)
time_elapsed = time.time() - since
print(f"\nBest GZSL: H={best_performance['HM']*100} acc_seen={best_performance['acc_seen']*100} acc_unseen={best_performance['acc_unseen']*100}")
# print(f"Best CZSL: acc_czsl={best_performance['acc_novel']*100}")
print(f"\nTraining complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s")
f.write(f"\n\nBest GZSL: H={best_performance['HM']*100} acc_seen={best_performance['acc_seen']*100} acc_unseen={best_performance['acc_unseen']*100}")
# f.write(f"\nBest CZSL: acc_czsl={best_performance['acc_novel']*100}")
f.write(f"\nTraining complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s")
f.close()
elif args.method == 'clip_linear_probe':
model = network.build(args, sentence_features).to(device)
H, acc_seen, _, acc_unseen, _ = TV.val_gzsl_clip(device, dataloaders, model)
print(f"\nBest GZSL: H={H*100} acc_seen={acc_seen*100} acc_unseen={acc_unseen*100}")
# print(f"Best CZSL: acc_czsl={best_performance['acc_novel']*100}")
f.write(f"\n\nBest GZSL: H={H*100} acc_seen={acc_seen*100} acc_unseen={acc_unseen*100}")
# f.write(f"\nBest CZSL: acc_czsl={best_performance['acc_novel']*100}")
f.close()
#-----------