File size: 11,300 Bytes
5fee096 | 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 | '''
Code Reference:
Adapted from https://github.com/GT-RIPL/CODA-Prompt
'''
import os
import timm
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from functools import partial
from timm.models.vision_transformer import _cfg, PatchEmbed
from timm.models.registry import register_model
from timm.models.layers import trunc_normal_, DropPath
from timm.models.helpers import named_apply, adapt_input_conv
from .prompt import L2P, CodaPrompt, DualPrompt
from .transformer import MultiHeadAttention_LoRA, VisionTransformer, VisionTransformer_CL_LoRA
def interpolate_pos_embed(pos_embed_checkpoint, visual_encoder):
# interpolate position embedding
embedding_size = pos_embed_checkpoint.shape[-1]
num_patches = visual_encoder.patch_embed.num_patches
num_extra_tokens = visual_encoder.pos_embed.shape[-2] - num_patches
# height (== width) for the checkpoint position embedding
orig_size = int((pos_embed_checkpoint.shape[-2] - num_extra_tokens) ** 0.5)
# height (== width) for the new position embedding
new_size = int(num_patches ** 0.5)
if orig_size!=new_size:
# class_token and dist_token are kept unchanged
extra_tokens = pos_embed_checkpoint[:, :num_extra_tokens]
# only the position tokens are interpolated
pos_tokens = pos_embed_checkpoint[:, num_extra_tokens:]
pos_tokens = pos_tokens.reshape(-1, orig_size, orig_size, embedding_size).permute(0, 3, 1, 2)
pos_tokens = torch.nn.functional.interpolate(
pos_tokens, size=(new_size, new_size), mode='bicubic', align_corners=False)
pos_tokens = pos_tokens.permute(0, 2, 3, 1).flatten(1, 2)
new_pos_embed = torch.cat((extra_tokens, pos_tokens), dim=1)
print('reshape position embedding from %d to %d'%(orig_size ** 2,new_size ** 2))
return new_pos_embed
else:
return pos_embed_checkpoint
class ViTZoo(nn.Module):
def __init__(self, pretrained = False, model_name='vit_base_patch16_224', attn_layer='MultiHeadAttention', **kwargs):
super(ViTZoo, self).__init__()
self.task_id = None
self.feat_dim = 768
self.feat = VisionTransformer(img_size=224, patch_size=16, embed_dim=768, depth=12,
num_heads=12, ckpt_layer=0,
drop_path_rate=0, attn_layer=attn_layer,
**kwargs
)
if pretrained:
print(f'Using pretrained model : {model_name}')
if model_name == 'vit_base_patch16_224.augreg2_in21k_ft_in1k' and os.path.exists('/home/lvqiexuan/.cache/torch/hub/checkpoints/vit_base_patch16_224.augreg2_in21k_ft_in1k.pt'):
# Manually Loading weight
load_dict = torch.load('/home/lvqiexuan/.cache/torch/hub/checkpoints/vit_base_patch16_224.augreg2_in21k_ft_in1k.pt')
else:
load_dict = timm.create_model(model_name, pretrained = pretrained).state_dict()
key_mapping = {
".norm1.": ".ln_1.",
".norm2.": ".ln_2.",
"blocks.": "transformer.blocks."
}
modified_load_dict = {}
for key in load_dict.keys():
new_key = key
for old_key, mapped_key in key_mapping.items():
if old_key in new_key:
new_key = new_key.replace(old_key, mapped_key)
modified_load_dict[new_key] = load_dict[key]
self.feat.load_state_dict(modified_load_dict, strict = False)
self.prompt = None
self.prompt_flag = ''
def create_prompt(self, prompt_flag, **kwargs):
self.prompt_flag = prompt_flag
if self.prompt_flag == 'l2p':
self.prompt = L2P(**kwargs)
elif self.prompt_flag == 'dual':
self.prompt = DualPrompt(768, **kwargs)
elif self.prompt_flag == 'coda':
self.prompt = CodaPrompt(768, **kwargs)
# pen: get penultimate features
def forward(self, image, text=None, pen=False, train=False, **kwargs):
if self.prompt_flag == 'l2p':
with torch.no_grad():
self.eval()
cls_features = self.feat(image, prompt_flag = self.prompt_flag)
if train:
self.train()
out, reduce_sim = self.feat(
x = image,
prompt = self.prompt,
cls_features = cls_features,
prompt_flag = self.prompt_flag
)
return out, reduce_sim
elif self.prompt is not None:
with torch.no_grad():
q, _ = self.feat(image)
q = q[:,0,:]
# q?, train?, task_id?
out, prompt_loss = self.feat(image, prompt=self.prompt, q=q, train=train, task_id=self.task_id)
out = out[:,0,:]
else:
out, _ = self.feat(image, **kwargs)
if len(out.shape) == 3:
out = out[:,0,:]
out = out.view(out.size(0), -1)
if self.prompt is not None and train:
return out, prompt_loss
else:
return out
class ViT_in21k_adapter(nn.Module):
def __init__(self, pretrained=False, **kwargs):
super(ViT_in21k_adapter, self).__init__()
self.task_id = None
self.feat_dim = 768
# get feature encoder
if pretrained:
print("Using pretrained model")
from core.model.backbone.petl import vision_transformer_adapter
from easydict import EasyDict
tuning_config = EasyDict(
# AdaptFormer
ffn_adapt=True,
ffn_option="parallel",
ffn_adapter_layernorm_option="none",
ffn_adapter_init_option="lora",
ffn_adapter_scalar="0.1",
ffn_num=64,
d_model=768,
# VPT related
vpt_on=False,
vpt_num=0,
)
zoo_model = vision_transformer_adapter.vit_base_patch16_224_in21k_adapter(num_classes=0,
global_pool=False, drop_path_rate=0.0, tuning_config=tuning_config)
zoo_model.out_dim=768
zoo_model.eval()
self.prompt = None
# feature encoder changes if transformer vs resnet
self.feat = zoo_model
def create_prompt(self, prompt_flag, **kwargs):
self.prompt_flag = prompt_flag
# self.prompt_param = prompt_param
# create prompting module
if self.prompt_flag == 'l2p':
self.prompt = L2P(768, **kwargs)
elif self.prompt_flag == 'dual':
self.prompt = DualPrompt(768, **kwargs)
elif self.prompt_flag == 'coda':
self.prompt = CodaPrompt(768, **kwargs)
# pen: get penultimate features
def forward(self, x, pen=False, train=False):
if self.prompt is not None:
with torch.no_grad():
q, _ = self.feat(x)
q = q[:,0,:]
out, prompt_loss = self.feat(x, prompt=self.prompt, q=q, train=train, task_id=self.task_id)
out = out[:,0,:]
else:
out = self.feat(x) # This implementation of adapter vit doesn't return prompt loss
out = out.view(out.size(0), -1)
# if not pen:
# out = self.last(out)
if self.prompt is not None and train:
return out, prompt_loss
else:
return out
class ViT_CL_LoRA(nn.Module):
def __init__(self, pretrained = False, model_name='vit_base_patch16_224', attn_layer='MultiHeadAttention', **kwargs):
super().__init__()
self.task_id = None
self.feat_dim = 768
self.feat = VisionTransformer_CL_LoRA(img_size=224, patch_size=16, embed_dim=768, depth=12,
num_heads=12, ckpt_layer=0,
drop_path_rate=0, attn_layer=attn_layer,
**kwargs
)
if pretrained:
print(f'Using pretrained model : {model_name}')
if model_name == 'vit_base_patch16_224.augreg2_in21k_ft_in1k' and os.path.exists('/home/lvqiexuan/.cache/torch/hub/checkpoints/vit_base_patch16_224.augreg2_in21k_ft_in1k.pt'):
# Manually Loading weight
load_dict = torch.load('/home/lvqiexuan/.cache/torch/hub/checkpoints/vit_base_patch16_224.augreg2_in21k_ft_in1k.pt')
else:
load_dict = timm.create_model(model_name, pretrained = pretrained).state_dict()
key_mapping = {
".norm1.": ".ln_1.",
".norm2.": ".ln_2.",
"blocks.": "transformer.blocks."
}
modified_load_dict = {}
for key in load_dict.keys():
new_key = key
for old_key, mapped_key in key_mapping.items():
if old_key in new_key:
new_key = new_key.replace(old_key, mapped_key)
modified_load_dict[new_key] = load_dict[key]
self.feat.load_state_dict(modified_load_dict, strict = False)
self.prompt = None
self.prompt_flag = ''
# pen: get penultimate features
def forward(self, image, test, text=None, pen=False, train=False, **kwargs):
if self.prompt_flag == 'l2p':
with torch.no_grad():
self.eval()
cls_features = self.feat(image, prompt_flag = self.prompt_flag)
if train:
self.train()
out, reduce_sim = self.feat(
x = image,
prompt = self.prompt,
cls_features = cls_features,
prompt_flag = self.prompt_flag
)
return out, reduce_sim
elif self.prompt is not None:
with torch.no_grad():
q, _ = self.feat(image)
q = q[:,0,:]
# q?, train?, task_id?
out, prompt_loss = self.feat(image, prompt=self.prompt, q=q, train=train, task_id=self.task_id)
out = out[:,0,:]
else:
out, _ = self.feat(image, test, **kwargs)
if len(out.shape) == 3:
out = out[:,0,:]
out = out.view(out.size(0), -1)
if self.prompt is not None and train:
return out, prompt_loss
else:
return out
def forward_proto(self, x, adapt_index):
return self.feat.forward_proto(x, adapt_index)
def forward_general_cls(self, x, t_idx):
return self.feat.forward_general_cls(x, t_idx)
def add_adapter_to_list(self):
self.feat.add_adapter_to_list()
def vit_pt_imnet(pretrained=False, **kwargs):
return ViTZoo(pretrained, **kwargs)
def vit_pt_imnet_in21k_adapter(pretrained=False, **kwargs):
return ViT_in21k_adapter(pretrained, **kwargs)
def vit_cl_lora(pretrained=False, **kwargs):
return ViT_CL_LoRA(pretrained, **kwargs) |