-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmae.py
More file actions
258 lines (205 loc) · 9.52 KB
/
vmae.py
File metadata and controls
258 lines (205 loc) · 9.52 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
from torch.utils.data import DataLoader, Dataset
from transformers import AutoImageProcessor
import torchvision.transforms as T
import numpy as np
import random
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import WandbLogger
import segmentation_models_pytorch as smp
from torch.optim import AdamW
from warmup_scheduler import GradualWarmupScheduler
import utils
from torchvision.models.video import swin_transformer
import albumentations as A
from transformers import VideoMAEConfig, VideoMAEForPreTraining
from transformers import VideoMAEModel
class VideoMaeModel(pl.LightningModule):
def __init__(self, pred_shape, size, lr, scheduler=None, wandb_logger=None, freeze=False):
super(VideoMaeModel, self).__init__()
self.save_hyperparameters()
self.mask_pred = np.zeros(self.hparams.pred_shape)
self.mask_count = np.zeros(self.hparams.pred_shape)
self.IGNORE_INDEX = 127
self.loss_func1 = smp.losses.DiceLoss(mode='binary',ignore_index=self.IGNORE_INDEX)
self.loss_func2= smp.losses.SoftBCEWithLogitsLoss(smooth_factor=0.15,ignore_index=self.IGNORE_INDEX)
self.loss_func= lambda x,y: 0.6 * self.loss_func1(x,y)+0.4*self.loss_func2(x,y)
videomae_config = VideoMAEConfig(
image_size=64,
patch_size=16,
num_channels=1,
num_frames=8,
tubelet_size=8,
hidden_size=768,
num_hidden_layers=12,
num_attention_heads=12,
intermediate_size=1536,
decoder_num_hidden_layers=4,
decoder_hidden_size=512,
decoder_num_attention_heads=8,
decoder_intermediate_size=768,
norm_pix_loss=True,
)
self.encoder = VideoMAEModel(videomae_config)
try:
ckpt = torch.load(
'checkpoints/videomae_epoch=069_val_loss=0.6117.ckpt',
map_location="cpu",
weights_only=False
)
state_dict = ckpt["state_dict"]
# Extract only the encoder weights from the Lightning checkpoint
encoder_state = {}
for k, v in state_dict.items():
if k.startswith("videomae.videomae"): # your encoder path inside LightningModule
new_k = k.replace("videomae.videomae.", "") # strip the prefix
encoder_state[new_k] = v
# Load into your fresh VideoMAEModel
self.encoder.load_state_dict(encoder_state, strict=False)
print("✅ VideoMAE checkpoint loaded successfully!")
except Exception as e:
print("❌ Failed to load checkpoint:")
print(e)
# Remove classifier head, keep norm
self.encoder.head = nn.Identity()
embed_dim = 768
self.classifier = nn.Sequential(
nn.Linear(embed_dim, embed_dim // 2),
nn.GELU(),
nn.Dropout(0.2),
nn.Linear(embed_dim // 2, embed_dim // 4),
nn.GELU(),
nn.Dropout(0.2),
nn.Linear(embed_dim // 4, (self.hparams.size // 16) ** 2)
)
def forward(self, x):
features = self.encoder(x) # BaseModelOutput
tokens = features.last_hidden_state # (B, N, D)
cls = tokens[:, 0]
out = self.classifier(cls)
out = out.view(-1, 1, self.hparams.size // 16, self.hparams.size // 16)
return out
def training_step(self, batch, batch_idx):
x, y = batch
outputs = self(x)
# print(outputs.shape)
# print(y.shape)
loss = self.loss_func(outputs, y)
if torch.isnan(loss):
print("Loss nan encountered")
self.log("train/total_loss", loss.item(),on_step=True, on_epoch=True, prog_bar=True)
opt = self.optimizers()
current_lr = opt.param_groups[0]['lr']
self.log("train/lr", current_lr, on_step=True, on_epoch=True, prog_bar=True)
return {"loss": loss}
def validation_step(self, batch, batch_idx):
x,y,xyxys= batch
outputs = self(x)
loss1 = self.loss_func(outputs, y)
y_preds = torch.sigmoid(outputs).to('cpu')
for i, (x1, y1, x2, y2) in enumerate(xyxys):
self.mask_pred[y1:y2, x1:x2] += F.interpolate(y_preds[i].unsqueeze(0).float(),scale_factor=16,mode='bilinear').squeeze(0).squeeze(0).numpy()
self.mask_count[y1:y2, x1:x2] += np.ones((self.hparams.size, self.hparams.size))
self.log("val/total_loss", loss1.item(),on_step=True, on_epoch=True, prog_bar=True)
return {"loss": loss1}
def configure_optimizers(self):
weight_decay = 0.
base_lr = self.hparams.lr
# 1️⃣ Backbone parameters in their own group
backbone_params = list(self.parameters())
# 5 4Optimizer
optimizer = AdamW(backbone_params, lr=base_lr, weight_decay=weight_decay)
# 6 Scheduler
return [optimizer]
def on_validation_epoch_end(self):
mask_pred_tensor = torch.tensor(self.mask_pred, dtype=torch.float32, device=self.device)
mask_count_tensor = torch.tensor(self.mask_count, dtype=torch.float32, device=self.device)
if dist.is_available() and dist.is_initialized():
dist.all_reduce(mask_pred_tensor, op=dist.ReduceOp.SUM)
dist.all_reduce(mask_count_tensor, op=dist.ReduceOp.SUM)
if self.trainer.is_global_zero:
mask_pred_np = mask_pred_tensor.cpu().numpy()
mask_count_np = mask_count_tensor.cpu().numpy()
final_mask = np.divide(
mask_pred_np,
mask_count_np,
out=np.zeros_like(mask_pred_np),
where=mask_count_np != 0
)
self.hparams.wandb_logger.log_image(key="masks", images=[np.clip(final_mask, 0, 1)], caption=["probs"])
self.mask_pred = np.zeros(self.hparams.pred_shape)
self.mask_count = np.zeros(self.hparams.pred_shape)
def load_weights(model, ckpt_path, strict=True, map_location='cpu'):
"""
Loads weights from a checkpoint into the model.
Args:
model: An instance of TimesfomerModel.
ckpt_path: Path to the .ckpt file saved by PyTorch Lightning.
strict: Whether to strictly enforce that the keys in state_dict match.
map_location: Where to load the checkpoint (e.g., 'cpu', 'cuda').
Returns:
model: The model with loaded weights.
"""
ckpt = torch.load(ckpt_path, map_location=map_location, weights_only=False)
# For Lightning checkpoints, weights are under 'state_dict'
if 'state_dict' in ckpt:
state_dict = ckpt['state_dict']
else:
state_dict = ckpt
# Strip 'model.' prefix if saved with Lightning
new_state_dict = {}
for k, v in state_dict.items():
new_key = k.replace("model.", "") if k.startswith("model.") else k
new_state_dict[new_key] = v
missing_keys, unexpected_keys = model.load_state_dict(new_state_dict, strict=strict)
print("Loaded checkpoint from:", ckpt_path)
print("Missing keys:", missing_keys)
print("Unexpected keys:", unexpected_keys)
return model
class GradualWarmupSchedulerV2(GradualWarmupScheduler):
"""
https://www.kaggle.com/code/underwearfitting/single-fold-training-of-resnet200d-lb0-965
"""
def __init__(self, optimizer, multiplier, total_epoch, after_scheduler=None):
super(GradualWarmupSchedulerV2, self).__init__(
optimizer, multiplier, total_epoch, after_scheduler)
def get_lr(self):
if self.last_epoch > self.total_epoch:
if self.after_scheduler:
if not self.finished:
self.after_scheduler.base_lrs = [
base_lr * self.multiplier for base_lr in self.base_lrs]
self.finished = True
return self.after_scheduler.get_lr()
return [base_lr * self.multiplier for base_lr in self.base_lrs]
if self.multiplier == 1.0:
return [base_lr * (float(self.last_epoch) / self.total_epoch) for base_lr in self.base_lrs]
else:
return [base_lr * ((self.multiplier - 1.) * self.last_epoch / self.total_epoch + 1.) for base_lr in self.base_lrs]
def get_scheduler(optimizer, scheduler=None, epochs=15, steps_per_epoch=10):
if scheduler == "onecycle":
scheduler = torch.optim.lr_scheduler.OneCycleLR(
optimizer,
max_lr=[group['lr'] for group in optimizer.param_groups],
total_steps=epochs * 50,
pct_start=0.1, # 10% warmup
anneal_strategy='cos', # Cosine decay after warmup
cycle_momentum=False # Turn off momentum scheduling (common for AdamW)
)
elif scheduler == 'cosine':
scheduler_after = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, epochs, eta_min=1e-6)
scheduler = GradualWarmupSchedulerV2(
optimizer, multiplier=1.0, total_epoch=4, after_scheduler=scheduler_after)
elif scheduler == 'linear':
scheduler_after = torch.optim.lr_scheduler.LinearLR(
optimizer, start_factor=1.0, end_factor=0.05, total_iters=epochs)
scheduler = GradualWarmupSchedulerV2(
optimizer, multiplier=5.0, total_epoch=4, after_scheduler=scheduler_after)
return scheduler
def scheduler_step(scheduler, avg_val_loss, epoch):
scheduler.step(epoch)