-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrain.py
More file actions
380 lines (296 loc) · 13.2 KB
/
Copy pathtrain.py
File metadata and controls
380 lines (296 loc) · 13.2 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""
Project: CVS-AdaptNet
-----
Copyright (c) University of Strasbourg, All Rights Reserved.
"""
import torch.nn as nn
import argparse
import torch
import clip
import torch.distributed as dist
from PIL import Image
from collections import defaultdict
from mmengine.logging import MMLogger
from torchmetrics import AveragePrecision as AP, Precision, Recall, F1Score
from torch import Tensor
import sys
import json
from codes.datasets import build_dataset
from codes.models import build_algorithm
from mmengine.config import Config
from transformers import AutoTokenizer
import torchmetrics
import numpy as np
import gc
from torch.utils.data import ConcatDataset
import torch.optim as optim
import torchvision.transforms as transforms
from tqdm import tqdm
from codes.models.algorithms.CVS_AdaptNet import CVSAdaptNetModel, process_text
import json
import shutil
import os
import sys
def save_script(destination_folder, extra_files=None):
# Get the path of the currently running script
script_path = os.path.abspath(sys.argv[0])
script_filename = os.path.basename(script_path)
# Make sure the destination folder exists
os.makedirs(destination_folder, exist_ok=True)
# Save the training script itself
destination_script_path = os.path.join(destination_folder, script_filename)
shutil.copy(script_path, destination_script_path)
# Save extra files (configs, prompts, etc.)
if extra_files:
for f in extra_files:
if os.path.exists(f):
shutil.copy(f, os.path.join(destination_folder, os.path.basename(f)))
else:
print(f"Warning: file not found: {f}")
def test_both(val_loader, model_1, args):
class_prompt=args.class_prompt
model_1.eval()
with open(class_prompt) as f:
lines = f.readlines()
f.close()
class_texts_ = [i.replace('\n', '') for i in lines]
class_texts = process_text(class_texts_)
text_features = model_1.model(None, class_texts, mode='text')['text_emb'].cuda()
text_features /= text_features.norm(dim=-1, keepdim=True)
# init logger
logger: MMLogger = MMLogger.get_current_instance()
logger_info = []
eval_results = {}
probs_list = []
label_list = []
with torch.no_grad():
for val_loader in val_loaders:
for i, data in enumerate(val_loader):
frames = data['video'].cuda() # (1, M, T, C, H, W)
B, C, H, W = frames.shape
frames = frames.view(-1, C, H, W)
image_features = model_1.model(frames, None, mode='video')['img_emb'] # (B*M*T, D)
image_features /= image_features.norm(dim=-1, keepdim=True)
probs = (image_features @ text_features.T) # (1, classes)
## for average precision calculation
preds = probs.cpu()
gts = [[float(item) for item in json.loads(value)] for value in data['label']]
probs_list = probs_list + [p.tolist() for p in preds]
#gt_prob_list = label_list + [g for g in gts]
label_list = label_list + [g for g in gts]
torch_ap = AP(task='multilabel', num_labels=3, average='none')
ds_preds = torch.stack([Tensor(p) for p in probs_list]).sigmoid()
ds_gt = torch.stack([Tensor(g).round() for g in label_list]).long()
ds_ap = torch_ap(ds_preds, ds_gt)
#print(ds_ap)
# log overall
logger_info.append(f'ds_average_precision: {torch.nanmean(ds_ap):.4f}')
eval_results['ds_average_precision'] = torch.nanmean(ds_ap)
# log component-wise
for ind, i in enumerate(ds_ap):
logger_info.append(f'ds_average_precision_C{ind+1}: {i:.4f}')
eval_results['ds_average_precision_C{}'.format(ind+1)] = i
print('\n'.join(logger_info))
return ds_ap
def test(classifier, val_loader, model_1, args):
class_prompt=args.class_prompt
model_1.eval()
logger: MMLogger = MMLogger.get_current_instance()
logger_info = []
eval_results = {}
probs_list = []
label_list = []
with torch.no_grad():
for val_loader in val_loaders:
for i, data in enumerate(val_loader):
frames = data['video'].cuda() # (1, M, T, C, H, W)
# B, M, T, C, H, W = frames.shape
B, C, H, W = frames.shape
frames = frames.view(-1, C, H, W)
image_features = model_1.model(frames)['img_emb']
probs = nn.Sigmoid()(classifier(image_features))
#print(probs)
## for average precision calculation
preds = probs.cpu()
gts = [[float(item) for item in json.loads(value)] for value in data['label']]
probs_list = probs_list + [p.tolist() for p in preds]
label_list = label_list + [g for g in gts]
torch_ap = AP(task='multilabel', num_labels=3, average='none')
ds_preds = torch.stack([Tensor(p) for p in probs_list])
ds_gt = torch.stack([Tensor(g).round() for g in label_list]).long()
ds_ap = torch_ap(ds_preds, ds_gt.long())
#print(ds_ap)
# log overall
logger_info.append(f'ds_average_precision: {torch.nanmean(ds_ap):.4f}')
eval_results['ds_average_precision'] = torch.nanmean(ds_ap)
# log component-wise
for ind, i in enumerate(ds_ap):
logger_info.append(f'ds_average_precision_C{ind+1}: {i:.4f}')
eval_results['ds_average_precision_C{}'.format(ind+1)] = i
print('\n'.join(logger_info))
return ds_ap
def fine_tune_image(
train_loader: torch.utils.data.DataLoader,
val_loader: torch.utils.data.DataLoader,
model: torch.nn.Module,
num_classes: int
) -> torch.nn.Module:
model_clip = CVSAdaptNetModel(args, configs).cuda()
model_clip.model = model
for param in model_clip.model.parameters():
param.requires_grad = True
optimizer = torch.optim.Adam([
{'params': model_clip.model.parameters(), 'lr': 0.00001}, # Fine-tune the pre-trained model
], weight_decay=0.0005)
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=40)
best_map = -1
num_epochs = 20
#epoch = 0
best_map = -1
json_write = {}
for epoch in range(num_epochs):
#epoch+=1
model_clip.train()
for batch in tqdm(train_loader):
optimizer.zero_grad()
classifier, total_loss = model_clip(batch,return_image=True)
#Backward and optimize
total_loss.backward()
optimizer.step()
print('epoch', epoch)
print(total_loss.item())
# Learning rate scheduling
scheduler.step()
print("_____________________________Before Cache clearance_________________________")
print(torch.cuda.memory_summary(device=None, abbreviated=False))
torch.cuda.empty_cache()
print("_____________________________After Cache clearance_________________________")
print(torch.cuda.memory_summary(device=None, abbreviated=False))
gc.collect()
with torch.no_grad():
ds_ap = test(classifier, val_loader, model_clip, args)
json_write[epoch] = str(ds_ap.detach().cpu().numpy())
with open(args.save_dir +'/ds_ap_test_updated.json', 'w', encoding ='utf8') as json_file:
json.dump(json_write, json_file)
map_value = torch.nanmean(ds_ap)
if map_value > best_map:
# Update the best MAP value
best_map = map_value
#Save the model checkpoint
torch.save({
'epoch': epoch,
'model_state_dict': model_clip.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'best_map': best_map
}, args.save_dir +'/best_model_checkpoint_'+str(epoch)+'.pth')
torch.save(model_clip, args.save_dir + "/best_model_checkpoint_"+str(epoch)+".pkl")
return model_clip, classifier # Return the fine-tuned model and classifier
def fine_tune(
train_loader: torch.utils.data.DataLoader,
val_loader: torch.utils.data.DataLoader,
model: torch.nn.Module,
num_classes: int
) -> torch.nn.Module:
model_clip = CVSAdaptNetModel(args, configs).cuda()
model_clip.model = model
# Freeze the pre-trained model's text parameters
for param in model_clip.model.parameters():
param.requires_grad = True
optimizer = torch.optim.Adam([
{'params': model_clip.model.parameters(), 'lr': 0.00001}, # Fine-tune the pre-trained model
{'params': model_clip.logit_scale, 'lr': 0.001}, # logit scale is learnable
], weight_decay=0.0005)
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=40)
best_map = -1
num_epochs = 20
#epoch = 0
ap_epoch = {}
for epoch in range(num_epochs):
model_clip.train()
for batch in tqdm(train_loader):
optimizer.zero_grad() # Clear gradients
total_loss = model_clip(batch,return_image=False)
total_loss.backward()
optimizer.step()
print('epoch', epoch)
print(total_loss.item())
# Learning rate scheduling
scheduler.step()
print("_____________________________Before Cache clearance_________________________")
print(torch.cuda.memory_summary(device=None, abbreviated=False))
torch.cuda.empty_cache()
print("_____________________________After Cache clearance_________________________")
print(torch.cuda.memory_summary(device=None, abbreviated=False))
gc.collect()
# # Validation using both text and image
with torch.no_grad():
ds_ap = test_both(val_loader, model_clip, args)
ap_epoch[epoch] = str(ds_ap.detach().cpu().numpy())
with open(args.save_dir + '/ds_ap_both.json', 'w', encoding ='utf8') as json_file_both:
json.dump(ap_epoch, json_file_both)
map_value = torch.nanmean(ds_ap)
if map_value > best_map:
# Update the best MAP value
best_map = map_value
#Save the model checkpoint
torch.save({
'epoch': epoch,
'model_state_dict': model_clip.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'best_map': best_map
}, args.save_dir +'/best_model_checkpoint_both.pth')
torch.save(model_clip, args.save_dir + "/best_model_checkpoint_both.pkl")
return model_clip
def get_args(description='CVSAdaptNet'):
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--class_prompt', default='../class_prompt.txt', type=str, help='prompt for categories')
parser.add_argument('--cvs_config', default='config/config_cvsadaptnet.py', type=str, help='config for dataset and cvsadaptnet')
parser.add_argument('--checkpoint', default='./checkpoint.pkl', type=str, help='checkpoint of pre-trained weights')
parser.add_argument('--batch_size', default=1, type=int, help='batch for training')
parser.add_argument('--data_mode', default="both", type=str, help='mode for finetuning')
parser.add_argument('--save_dir', default="save", type=str, help='directory for saving weights')
parser.add_argument('--num_classes', default="3", type=int, help='number of classes')
args = parser.parse_args()
return args, parser
if __name__ == "__main__":
args, _ = get_args()
save_script(args.save_dir, extra_files=[args.cvs_config, args.class_prompt])
device = "cuda" if torch.cuda.is_available() else "cpu"
configs = Config.fromfile(args.cvs_config)['config']
num_classes = args.num_classes
model = build_algorithm(configs.model_config).cuda()
print("Configuration loaded.")
# Load model
state_dict = torch.load(args.checkpoint)
a, b = model.load_state_dict(state_dict, strict=True)
train_datasets = [build_dataset(c) for c in configs.train_config]
train_dataset = ConcatDataset(train_datasets)
val_datasets = [build_dataset(c) for c in configs.val_config]
val_dataset = ConcatDataset(val_datasets)
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=4,
pin_memory=True,
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=4
)
val_loaders = [torch.utils.data.DataLoader(
val_dataset,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=0
) for val_dataset in val_datasets]
print(args)
if args.data_mode == "both":
model_1 = fine_tune(train_loader, val_loader, model, num_classes)
elif args.data_mode == "image":
classifier, model_1 = fine_tune_image(train_loader, val_loader, model, num_classes)