Release of PromptSRC with pretrained models.
This commit is contained in:
0
trainers/__init__.py
Normal file
0
trainers/__init__.py
Normal file
318
trainers/cocoop.py
Normal file
318
trainers/cocoop.py
Normal file
@@ -0,0 +1,318 @@
|
||||
import os.path as osp
|
||||
from collections import OrderedDict
|
||||
import math
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.nn import functional as F
|
||||
from torch.cuda.amp import GradScaler, autocast
|
||||
|
||||
from dassl.engine import TRAINER_REGISTRY, TrainerX
|
||||
from dassl.metrics import compute_accuracy
|
||||
from dassl.utils import load_pretrained_weights, load_checkpoint
|
||||
from dassl.optim import build_optimizer, build_lr_scheduler
|
||||
|
||||
from clip import clip
|
||||
from clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
|
||||
|
||||
_tokenizer = _Tokenizer()
|
||||
|
||||
|
||||
def load_clip_to_cpu(cfg):
|
||||
backbone_name = cfg.MODEL.BACKBONE.NAME
|
||||
url = clip._MODELS[backbone_name]
|
||||
model_path = clip._download(url)
|
||||
|
||||
try:
|
||||
# loading JIT archive
|
||||
model = torch.jit.load(model_path, map_location="cpu").eval()
|
||||
state_dict = None
|
||||
|
||||
except RuntimeError:
|
||||
state_dict = torch.load(model_path, map_location="cpu")
|
||||
design_details = {"trainer": 'CoCoOp',
|
||||
"vision_depth": 0,
|
||||
"language_depth": 0, "vision_ctx": 0,
|
||||
"language_ctx": 0}
|
||||
model = clip.build_model(state_dict or model.state_dict(), design_details)
|
||||
|
||||
return model
|
||||
|
||||
|
||||
class TextEncoder(nn.Module):
|
||||
def __init__(self, clip_model):
|
||||
super().__init__()
|
||||
self.transformer = clip_model.transformer
|
||||
self.positional_embedding = clip_model.positional_embedding
|
||||
self.ln_final = clip_model.ln_final
|
||||
self.text_projection = clip_model.text_projection
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, prompts, tokenized_prompts):
|
||||
x = prompts + self.positional_embedding.type(self.dtype)
|
||||
x = x.permute(1, 0, 2) # NLD -> LND
|
||||
x = self.transformer(x)
|
||||
x = x.permute(1, 0, 2) # LND -> NLD
|
||||
x = self.ln_final(x).type(self.dtype)
|
||||
|
||||
# x.shape = [batch_size, n_ctx, transformer.width]
|
||||
# take features from the eot embedding (eot_token is the highest number in each sequence)
|
||||
x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class PromptLearner(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
n_cls = len(classnames)
|
||||
n_ctx = cfg.TRAINER.COCOOP.N_CTX
|
||||
ctx_init = cfg.TRAINER.COCOOP.CTX_INIT
|
||||
dtype = clip_model.dtype
|
||||
ctx_dim = clip_model.ln_final.weight.shape[0]
|
||||
vis_dim = clip_model.visual.output_dim
|
||||
clip_imsize = clip_model.visual.input_resolution
|
||||
cfg_imsize = cfg.INPUT.SIZE[0]
|
||||
assert cfg_imsize == clip_imsize, f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"
|
||||
|
||||
if ctx_init:
|
||||
# use given words to initialize context vectors
|
||||
ctx_init = ctx_init.replace("_", " ")
|
||||
n_ctx = len(ctx_init.split(" "))
|
||||
prompt = clip.tokenize(ctx_init)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(prompt).type(dtype)
|
||||
ctx_vectors = embedding[0, 1: 1 + n_ctx, :]
|
||||
prompt_prefix = ctx_init
|
||||
else:
|
||||
# random initialization
|
||||
ctx_vectors = torch.empty(n_ctx, ctx_dim, dtype=dtype)
|
||||
nn.init.normal_(ctx_vectors, std=0.02)
|
||||
prompt_prefix = " ".join(["X"] * n_ctx)
|
||||
|
||||
print(f'Initial context: "{prompt_prefix}"')
|
||||
print(f"Number of context words (tokens): {n_ctx}")
|
||||
|
||||
self.ctx = nn.Parameter(ctx_vectors)
|
||||
|
||||
self.meta_net = nn.Sequential(OrderedDict([
|
||||
("linear1", nn.Linear(vis_dim, vis_dim // 16)),
|
||||
("relu", nn.ReLU(inplace=True)),
|
||||
("linear2", nn.Linear(vis_dim // 16, ctx_dim))
|
||||
]))
|
||||
|
||||
if cfg.TRAINER.COCOOP.PREC == "fp16":
|
||||
self.meta_net.half()
|
||||
|
||||
classnames = [name.replace("_", " ") for name in classnames]
|
||||
name_lens = [len(_tokenizer.encode(name)) for name in classnames]
|
||||
prompts = [prompt_prefix + " " + name + "." for name in classnames]
|
||||
|
||||
tokenized_prompts = torch.cat([clip.tokenize(p) for p in prompts]) # (n_cls, n_tkn)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(tokenized_prompts).type(dtype)
|
||||
|
||||
# These token vectors will be saved when in save_model(),
|
||||
# but they should be ignored in load_model() as we want to use
|
||||
# those computed using the current class names
|
||||
self.register_buffer("token_prefix", embedding[:, :1, :]) # SOS
|
||||
self.register_buffer("token_suffix", embedding[:, 1 + n_ctx:, :]) # CLS, EOS
|
||||
|
||||
self.n_cls = n_cls
|
||||
self.n_ctx = n_ctx
|
||||
self.tokenized_prompts = tokenized_prompts # torch.Tensor
|
||||
self.name_lens = name_lens
|
||||
|
||||
def construct_prompts(self, ctx, prefix, suffix, label=None):
|
||||
# dim0 is either batch_size (during training) or n_cls (during testing)
|
||||
# ctx: context tokens, with shape of (dim0, n_ctx, ctx_dim)
|
||||
# prefix: the sos token, with shape of (n_cls, 1, ctx_dim)
|
||||
# suffix: remaining tokens, with shape of (n_cls, *, ctx_dim)
|
||||
|
||||
if label is not None:
|
||||
prefix = prefix[label]
|
||||
suffix = suffix[label]
|
||||
|
||||
prompts = torch.cat(
|
||||
[
|
||||
prefix, # (dim0, 1, dim)
|
||||
ctx, # (dim0, n_ctx, dim)
|
||||
suffix, # (dim0, *, dim)
|
||||
],
|
||||
dim=1,
|
||||
)
|
||||
|
||||
return prompts
|
||||
|
||||
def forward(self, im_features):
|
||||
prefix = self.token_prefix
|
||||
suffix = self.token_suffix
|
||||
ctx = self.ctx # (n_ctx, ctx_dim)
|
||||
bias = self.meta_net(im_features) # (batch, ctx_dim)
|
||||
bias = bias.unsqueeze(1) # (batch, 1, ctx_dim)
|
||||
ctx = ctx.unsqueeze(0) # (1, n_ctx, ctx_dim)
|
||||
ctx_shifted = ctx + bias # (batch, n_ctx, ctx_dim)
|
||||
|
||||
# Use instance-conditioned context tokens for all classes
|
||||
prompts = []
|
||||
for ctx_shifted_i in ctx_shifted:
|
||||
ctx_i = ctx_shifted_i.unsqueeze(0).expand(self.n_cls, -1, -1)
|
||||
pts_i = self.construct_prompts(ctx_i, prefix, suffix) # (n_cls, n_tkn, ctx_dim)
|
||||
prompts.append(pts_i)
|
||||
prompts = torch.stack(prompts)
|
||||
|
||||
return prompts
|
||||
|
||||
|
||||
class CustomCLIP(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
self.prompt_learner = PromptLearner(cfg, classnames, clip_model)
|
||||
self.tokenized_prompts = self.prompt_learner.tokenized_prompts
|
||||
self.image_encoder = clip_model.visual
|
||||
self.text_encoder = TextEncoder(clip_model)
|
||||
self.logit_scale = clip_model.logit_scale
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, image, label=None):
|
||||
tokenized_prompts = self.tokenized_prompts
|
||||
logit_scale = self.logit_scale.exp()
|
||||
|
||||
image_features = self.image_encoder(image.type(self.dtype))
|
||||
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
||||
|
||||
prompts = self.prompt_learner(image_features)
|
||||
|
||||
logits = []
|
||||
for pts_i, imf_i in zip(prompts, image_features):
|
||||
text_features = self.text_encoder(pts_i, tokenized_prompts)
|
||||
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
||||
l_i = logit_scale * imf_i @ text_features.t()
|
||||
logits.append(l_i)
|
||||
logits = torch.stack(logits)
|
||||
|
||||
if self.prompt_learner.training:
|
||||
return F.cross_entropy(logits, label)
|
||||
|
||||
return logits
|
||||
|
||||
|
||||
@TRAINER_REGISTRY.register()
|
||||
class CoCoOp(TrainerX):
|
||||
def check_cfg(self, cfg):
|
||||
assert cfg.TRAINER.COCOOP.PREC in ["fp16", "fp32", "amp"]
|
||||
|
||||
def build_model(self):
|
||||
cfg = self.cfg
|
||||
classnames = self.dm.dataset.classnames
|
||||
|
||||
print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})")
|
||||
clip_model = load_clip_to_cpu(cfg)
|
||||
|
||||
if cfg.TRAINER.COCOOP.PREC == "fp32" or cfg.TRAINER.COCOOP.PREC == "amp":
|
||||
# CLIP's default precision is fp16
|
||||
clip_model.float()
|
||||
|
||||
print("Building custom CLIP")
|
||||
self.model = CustomCLIP(cfg, classnames, clip_model)
|
||||
|
||||
print("Turning off gradients in both the image and the text encoder")
|
||||
name_to_update = "prompt_learner"
|
||||
|
||||
for name, param in self.model.named_parameters():
|
||||
if name_to_update not in name:
|
||||
param.requires_grad_(False)
|
||||
|
||||
# Double check
|
||||
enabled = set()
|
||||
for name, param in self.model.named_parameters():
|
||||
if param.requires_grad:
|
||||
enabled.add(name)
|
||||
print(f"Parameters to be updated: {enabled}")
|
||||
|
||||
if cfg.MODEL.INIT_WEIGHTS:
|
||||
load_pretrained_weights(self.model.prompt_learner, cfg.MODEL.INIT_WEIGHTS)
|
||||
|
||||
self.model.to(self.device)
|
||||
# NOTE: only give prompt_learner to the optimizer
|
||||
self.optim = build_optimizer(self.model.prompt_learner, cfg.OPTIM)
|
||||
self.sched = build_lr_scheduler(self.optim, cfg.OPTIM)
|
||||
self.register_model("prompt_learner", self.model.prompt_learner, self.optim, self.sched)
|
||||
|
||||
self.scaler = GradScaler() if cfg.TRAINER.COCOOP.PREC == "amp" else None
|
||||
|
||||
# Note that multi-gpu training could be slow because CLIP's size is
|
||||
# big, which slows down the copy operation in DataParallel
|
||||
device_count = torch.cuda.device_count()
|
||||
if device_count > 1:
|
||||
print(f"Multiple GPUs detected (n_gpus={device_count}), use all of them!")
|
||||
self.model = nn.DataParallel(self.model)
|
||||
|
||||
def forward_backward(self, batch):
|
||||
image, label = self.parse_batch_train(batch)
|
||||
|
||||
model = self.model
|
||||
optim = self.optim
|
||||
scaler = self.scaler
|
||||
|
||||
prec = self.cfg.TRAINER.COCOOP.PREC
|
||||
if prec == "amp":
|
||||
with autocast():
|
||||
loss = model(image, label)
|
||||
optim.zero_grad()
|
||||
scaler.scale(loss).backward()
|
||||
scaler.step(optim)
|
||||
scaler.update()
|
||||
else:
|
||||
loss = model(image, label)
|
||||
optim.zero_grad()
|
||||
loss.backward()
|
||||
optim.step()
|
||||
|
||||
loss_summary = {"loss": loss.item()}
|
||||
|
||||
if (self.batch_idx + 1) == self.num_batches:
|
||||
self.update_lr()
|
||||
|
||||
return loss_summary
|
||||
|
||||
def parse_batch_train(self, batch):
|
||||
input = batch["img"]
|
||||
label = batch["label"]
|
||||
input = input.to(self.device)
|
||||
label = label.to(self.device)
|
||||
return input, label
|
||||
|
||||
def load_model(self, directory, epoch=None):
|
||||
if not directory:
|
||||
print("Note that load_model() is skipped as no pretrained model is given")
|
||||
return
|
||||
|
||||
names = self.get_model_names()
|
||||
|
||||
# By default, the best model is loaded
|
||||
model_file = "model-best.pth.tar"
|
||||
|
||||
if epoch is not None:
|
||||
model_file = "model.pth.tar-" + str(epoch)
|
||||
|
||||
for name in names:
|
||||
model_path = osp.join(directory, name, model_file)
|
||||
|
||||
if not osp.exists(model_path):
|
||||
raise FileNotFoundError('Model not found at "{}"'.format(model_path))
|
||||
|
||||
checkpoint = load_checkpoint(model_path)
|
||||
state_dict = checkpoint["state_dict"]
|
||||
epoch = checkpoint["epoch"]
|
||||
|
||||
# Ignore fixed token vectors
|
||||
if "token_prefix" in state_dict:
|
||||
del state_dict["token_prefix"]
|
||||
|
||||
if "token_suffix" in state_dict:
|
||||
del state_dict["token_suffix"]
|
||||
|
||||
print("Loading weights to {} " 'from "{}" (epoch = {})'.format(name, model_path, epoch))
|
||||
# set strict=False
|
||||
self._models[name].load_state_dict(state_dict, strict=False)
|
||||
328
trainers/coop.py
Normal file
328
trainers/coop.py
Normal file
@@ -0,0 +1,328 @@
|
||||
import os.path as osp
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.nn import functional as F
|
||||
from torch.cuda.amp import GradScaler, autocast
|
||||
|
||||
from dassl.engine import TRAINER_REGISTRY, TrainerX
|
||||
from dassl.metrics import compute_accuracy
|
||||
from dassl.utils import load_pretrained_weights, load_checkpoint
|
||||
from dassl.optim import build_optimizer, build_lr_scheduler
|
||||
|
||||
from clip import clip
|
||||
from clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
|
||||
|
||||
_tokenizer = _Tokenizer()
|
||||
|
||||
|
||||
def load_clip_to_cpu(cfg):
|
||||
backbone_name = cfg.MODEL.BACKBONE.NAME
|
||||
url = clip._MODELS[backbone_name]
|
||||
model_path = clip._download(url)
|
||||
|
||||
try:
|
||||
# loading JIT archive
|
||||
model = torch.jit.load(model_path, map_location="cpu").eval()
|
||||
state_dict = None
|
||||
|
||||
except RuntimeError:
|
||||
state_dict = torch.load(model_path, map_location="cpu")
|
||||
design_details = {"trainer": 'CoOp',
|
||||
"vision_depth": 0,
|
||||
"language_depth": 0, "vision_ctx": 0,
|
||||
"language_ctx": 0}
|
||||
model = clip.build_model(state_dict or model.state_dict(), design_details)
|
||||
|
||||
return model
|
||||
|
||||
|
||||
class TextEncoder(nn.Module):
|
||||
def __init__(self, clip_model):
|
||||
super().__init__()
|
||||
self.transformer = clip_model.transformer
|
||||
self.positional_embedding = clip_model.positional_embedding
|
||||
self.ln_final = clip_model.ln_final
|
||||
self.text_projection = clip_model.text_projection
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, prompts, tokenized_prompts):
|
||||
x = prompts + self.positional_embedding.type(self.dtype)
|
||||
x = x.permute(1, 0, 2) # NLD -> LND
|
||||
x = self.transformer(x)
|
||||
x = x.permute(1, 0, 2) # LND -> NLD
|
||||
x = self.ln_final(x).type(self.dtype)
|
||||
|
||||
# x.shape = [batch_size, n_ctx, transformer.width]
|
||||
# take features from the eot embedding (eot_token is the highest number in each sequence)
|
||||
x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class PromptLearner(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
n_cls = len(classnames)
|
||||
n_ctx = cfg.TRAINER.COOP.N_CTX
|
||||
ctx_init = cfg.TRAINER.COOP.CTX_INIT
|
||||
dtype = clip_model.dtype
|
||||
ctx_dim = clip_model.ln_final.weight.shape[0]
|
||||
clip_imsize = clip_model.visual.input_resolution
|
||||
cfg_imsize = cfg.INPUT.SIZE[0]
|
||||
assert cfg_imsize == clip_imsize, f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"
|
||||
|
||||
if ctx_init:
|
||||
# use given words to initialize context vectors
|
||||
ctx_init = ctx_init.replace("_", " ")
|
||||
n_ctx = len(ctx_init.split(" "))
|
||||
prompt = clip.tokenize(ctx_init)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(prompt).type(dtype)
|
||||
ctx_vectors = embedding[0, 1 : 1 + n_ctx, :]
|
||||
prompt_prefix = ctx_init
|
||||
|
||||
else:
|
||||
# random initialization
|
||||
if cfg.TRAINER.COOP.CSC:
|
||||
print("Initializing class-specific contexts")
|
||||
ctx_vectors = torch.empty(n_cls, n_ctx, ctx_dim, dtype=dtype)
|
||||
else:
|
||||
print("Initializing a generic context")
|
||||
ctx_vectors = torch.empty(n_ctx, ctx_dim, dtype=dtype)
|
||||
nn.init.normal_(ctx_vectors, std=0.02)
|
||||
prompt_prefix = " ".join(["X"] * n_ctx)
|
||||
|
||||
print(f'Initial context: "{prompt_prefix}"')
|
||||
print(f"Number of context words (tokens): {n_ctx}")
|
||||
|
||||
self.ctx = nn.Parameter(ctx_vectors) # to be optimized
|
||||
|
||||
classnames = [name.replace("_", " ") for name in classnames]
|
||||
name_lens = [len(_tokenizer.encode(name)) for name in classnames]
|
||||
prompts = [prompt_prefix + " " + name + "." for name in classnames]
|
||||
|
||||
tokenized_prompts = torch.cat([clip.tokenize(p) for p in prompts])
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(tokenized_prompts).type(dtype)
|
||||
|
||||
# These token vectors will be saved when in save_model(),
|
||||
# but they should be ignored in load_model() as we want to use
|
||||
# those computed using the current class names
|
||||
self.register_buffer("token_prefix", embedding[:, :1, :]) # SOS
|
||||
self.register_buffer("token_suffix", embedding[:, 1 + n_ctx :, :]) # CLS, EOS
|
||||
|
||||
self.n_cls = n_cls
|
||||
self.n_ctx = n_ctx
|
||||
self.tokenized_prompts = tokenized_prompts # torch.Tensor
|
||||
self.name_lens = name_lens
|
||||
self.class_token_position = cfg.TRAINER.COOP.CLASS_TOKEN_POSITION
|
||||
|
||||
def forward(self):
|
||||
ctx = self.ctx
|
||||
if ctx.dim() == 2:
|
||||
ctx = ctx.unsqueeze(0).expand(self.n_cls, -1, -1)
|
||||
|
||||
prefix = self.token_prefix
|
||||
suffix = self.token_suffix
|
||||
|
||||
if self.class_token_position == "end":
|
||||
prompts = torch.cat(
|
||||
[
|
||||
prefix, # (n_cls, 1, dim)
|
||||
ctx, # (n_cls, n_ctx, dim)
|
||||
suffix, # (n_cls, *, dim)
|
||||
],
|
||||
dim=1,
|
||||
)
|
||||
|
||||
elif self.class_token_position == "middle":
|
||||
half_n_ctx = self.n_ctx // 2
|
||||
prompts = []
|
||||
for i in range(self.n_cls):
|
||||
name_len = self.name_lens[i]
|
||||
prefix_i = prefix[i : i + 1, :, :]
|
||||
class_i = suffix[i : i + 1, :name_len, :]
|
||||
suffix_i = suffix[i : i + 1, name_len:, :]
|
||||
ctx_i_half1 = ctx[i : i + 1, :half_n_ctx, :]
|
||||
ctx_i_half2 = ctx[i : i + 1, half_n_ctx:, :]
|
||||
prompt = torch.cat(
|
||||
[
|
||||
prefix_i, # (1, 1, dim)
|
||||
ctx_i_half1, # (1, n_ctx//2, dim)
|
||||
class_i, # (1, name_len, dim)
|
||||
ctx_i_half2, # (1, n_ctx//2, dim)
|
||||
suffix_i, # (1, *, dim)
|
||||
],
|
||||
dim=1,
|
||||
)
|
||||
prompts.append(prompt)
|
||||
prompts = torch.cat(prompts, dim=0)
|
||||
|
||||
elif self.class_token_position == "front":
|
||||
prompts = []
|
||||
for i in range(self.n_cls):
|
||||
name_len = self.name_lens[i]
|
||||
prefix_i = prefix[i : i + 1, :, :]
|
||||
class_i = suffix[i : i + 1, :name_len, :]
|
||||
suffix_i = suffix[i : i + 1, name_len:, :]
|
||||
ctx_i = ctx[i : i + 1, :, :]
|
||||
prompt = torch.cat(
|
||||
[
|
||||
prefix_i, # (1, 1, dim)
|
||||
class_i, # (1, name_len, dim)
|
||||
ctx_i, # (1, n_ctx, dim)
|
||||
suffix_i, # (1, *, dim)
|
||||
],
|
||||
dim=1,
|
||||
)
|
||||
prompts.append(prompt)
|
||||
prompts = torch.cat(prompts, dim=0)
|
||||
|
||||
else:
|
||||
raise ValueError
|
||||
|
||||
return prompts
|
||||
|
||||
|
||||
class CustomCLIP(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
self.prompt_learner = PromptLearner(cfg, classnames, clip_model)
|
||||
self.tokenized_prompts = self.prompt_learner.tokenized_prompts
|
||||
self.image_encoder = clip_model.visual
|
||||
self.text_encoder = TextEncoder(clip_model)
|
||||
self.logit_scale = clip_model.logit_scale
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, image):
|
||||
image_features = self.image_encoder(image.type(self.dtype))
|
||||
|
||||
prompts = self.prompt_learner()
|
||||
tokenized_prompts = self.tokenized_prompts
|
||||
text_features = self.text_encoder(prompts, tokenized_prompts)
|
||||
|
||||
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
||||
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
||||
|
||||
logit_scale = self.logit_scale.exp()
|
||||
logits = logit_scale * image_features @ text_features.t()
|
||||
|
||||
return logits
|
||||
|
||||
|
||||
@TRAINER_REGISTRY.register()
|
||||
class CoOp(TrainerX):
|
||||
"""Context Optimization (CoOp).
|
||||
|
||||
Learning to Prompt for Vision-Language Models
|
||||
https://arxiv.org/abs/2109.01134
|
||||
"""
|
||||
|
||||
def check_cfg(self, cfg):
|
||||
assert cfg.TRAINER.COOP.PREC in ["fp16", "fp32", "amp"]
|
||||
|
||||
def build_model(self):
|
||||
cfg = self.cfg
|
||||
classnames = self.dm.dataset.classnames
|
||||
|
||||
print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})")
|
||||
clip_model = load_clip_to_cpu(cfg)
|
||||
|
||||
if cfg.TRAINER.COOP.PREC == "fp32" or cfg.TRAINER.COOP.PREC == "amp":
|
||||
# CLIP's default precision is fp16
|
||||
clip_model.float()
|
||||
|
||||
print("Building custom CLIP")
|
||||
self.model = CustomCLIP(cfg, classnames, clip_model)
|
||||
|
||||
print("Turning off gradients in both the image and the text encoder")
|
||||
for name, param in self.model.named_parameters():
|
||||
if "prompt_learner" not in name:
|
||||
param.requires_grad_(False)
|
||||
|
||||
if cfg.MODEL.INIT_WEIGHTS:
|
||||
load_pretrained_weights(self.model.prompt_learner, cfg.MODEL.INIT_WEIGHTS)
|
||||
|
||||
self.model.to(self.device)
|
||||
# NOTE: only give prompt_learner to the optimizer
|
||||
self.optim = build_optimizer(self.model.prompt_learner, cfg.OPTIM)
|
||||
self.sched = build_lr_scheduler(self.optim, cfg.OPTIM)
|
||||
self.register_model("prompt_learner", self.model.prompt_learner, self.optim, self.sched)
|
||||
|
||||
self.scaler = GradScaler() if cfg.TRAINER.COOP.PREC == "amp" else None
|
||||
|
||||
# Note that multi-gpu training could be slow because CLIP's size is
|
||||
# big, which slows down the copy operation in DataParallel
|
||||
device_count = torch.cuda.device_count()
|
||||
if device_count > 1:
|
||||
print(f"Multiple GPUs detected (n_gpus={device_count}), use all of them!")
|
||||
self.model = nn.DataParallel(self.model)
|
||||
|
||||
def forward_backward(self, batch):
|
||||
image, label = self.parse_batch_train(batch)
|
||||
|
||||
prec = self.cfg.TRAINER.COOP.PREC
|
||||
if prec == "amp":
|
||||
with autocast():
|
||||
output = self.model(image)
|
||||
loss = F.cross_entropy(output, label)
|
||||
self.optim.zero_grad()
|
||||
self.scaler.scale(loss).backward()
|
||||
self.scaler.step(self.optim)
|
||||
self.scaler.update()
|
||||
else:
|
||||
output = self.model(image)
|
||||
loss = F.cross_entropy(output, label)
|
||||
self.model_backward_and_update(loss)
|
||||
|
||||
loss_summary = {
|
||||
"loss": loss.item(),
|
||||
"acc": compute_accuracy(output, label)[0].item(),
|
||||
}
|
||||
|
||||
if (self.batch_idx + 1) == self.num_batches:
|
||||
self.update_lr()
|
||||
|
||||
return loss_summary
|
||||
|
||||
def parse_batch_train(self, batch):
|
||||
input = batch["img"]
|
||||
label = batch["label"]
|
||||
input = input.to(self.device)
|
||||
label = label.to(self.device)
|
||||
return input, label
|
||||
|
||||
def load_model(self, directory, epoch=None):
|
||||
if not directory:
|
||||
print("Note that load_model() is skipped as no pretrained model is given")
|
||||
return
|
||||
|
||||
names = self.get_model_names()
|
||||
|
||||
# By default, the best model is loaded
|
||||
model_file = "model-best.pth.tar"
|
||||
|
||||
if epoch is not None:
|
||||
model_file = "model.pth.tar-" + str(epoch)
|
||||
|
||||
for name in names:
|
||||
model_path = osp.join(directory, name, model_file)
|
||||
|
||||
if not osp.exists(model_path):
|
||||
raise FileNotFoundError('Model not found at "{}"'.format(model_path))
|
||||
|
||||
checkpoint = load_checkpoint(model_path)
|
||||
state_dict = checkpoint["state_dict"]
|
||||
epoch = checkpoint["epoch"]
|
||||
|
||||
# Ignore fixed token vectors
|
||||
if "token_prefix" in state_dict:
|
||||
del state_dict["token_prefix"]
|
||||
|
||||
if "token_suffix" in state_dict:
|
||||
del state_dict["token_suffix"]
|
||||
|
||||
print("Loading weights to {} " 'from "{}" (epoch = {})'.format(name, model_path, epoch))
|
||||
# set strict=False
|
||||
self._models[name].load_state_dict(state_dict, strict=False)
|
||||
74
trainers/imagenet_templates.py
Normal file
74
trainers/imagenet_templates.py
Normal file
@@ -0,0 +1,74 @@
|
||||
# source: https://github.com/openai/CLIP/blob/main/notebooks/Prompt_Engineering_for_ImageNet.ipynb
|
||||
|
||||
IMAGENET_TEMPLATES = [
|
||||
"a photo of a {}.",
|
||||
"a bad photo of a {}.",
|
||||
"a photo of many {}.",
|
||||
"a sculpture of a {}.",
|
||||
"a photo of the hard to see {}.",
|
||||
"a low resolution photo of the {}.",
|
||||
"a rendering of a {}.",
|
||||
"graffiti of a {}.",
|
||||
"a bad photo of the {}.",
|
||||
"a cropped photo of the {}.",
|
||||
"a tattoo of a {}.",
|
||||
"the embroidered {}.",
|
||||
"a photo of a hard to see {}.",
|
||||
"a bright photo of a {}.",
|
||||
"a photo of a clean {}.",
|
||||
"a photo of a dirty {}.",
|
||||
"a dark photo of the {}.",
|
||||
"a drawing of a {}.",
|
||||
"a photo of my {}.",
|
||||
"the plastic {}.",
|
||||
"a photo of the cool {}.",
|
||||
"a close-up photo of a {}.",
|
||||
"a black and white photo of the {}.",
|
||||
"a painting of the {}.",
|
||||
"a painting of a {}.",
|
||||
"a pixelated photo of the {}.",
|
||||
"a sculpture of the {}.",
|
||||
"a bright photo of the {}.",
|
||||
"a cropped photo of a {}.",
|
||||
"a plastic {}.",
|
||||
"a photo of the dirty {}.",
|
||||
"a jpeg corrupted photo of a {}.",
|
||||
"a blurry photo of the {}.",
|
||||
"a photo of the {}.",
|
||||
"a good photo of the {}.",
|
||||
"a rendering of the {}.",
|
||||
"a {} in a video game.",
|
||||
"a photo of one {}.",
|
||||
"a doodle of a {}.",
|
||||
"a close-up photo of the {}.",
|
||||
"the origami {}.",
|
||||
"the {} in a video game.",
|
||||
"a sketch of a {}.",
|
||||
"a doodle of the {}.",
|
||||
"a origami {}.",
|
||||
"a low resolution photo of a {}.",
|
||||
"the toy {}.",
|
||||
"a rendition of the {}.",
|
||||
"a photo of the clean {}.",
|
||||
"a photo of a large {}.",
|
||||
"a rendition of a {}.",
|
||||
"a photo of a nice {}.",
|
||||
"a photo of a weird {}.",
|
||||
"a blurry photo of a {}.",
|
||||
"a cartoon {}.",
|
||||
"art of a {}.",
|
||||
"a sketch of the {}.",
|
||||
"a embroidered {}.",
|
||||
"a pixelated photo of a {}.",
|
||||
"itap of the {}.",
|
||||
]
|
||||
|
||||
IMAGENET_TEMPLATES_SELECT = [
|
||||
"itap of a {}.",
|
||||
"a bad photo of the {}.",
|
||||
"a origami {}.",
|
||||
"a photo of the large {}.",
|
||||
"a {} in a video game.",
|
||||
"art of the {}.",
|
||||
"a photo of the small {}.",
|
||||
]
|
||||
301
trainers/independentVL.py
Normal file
301
trainers/independentVL.py
Normal file
@@ -0,0 +1,301 @@
|
||||
import os.path as osp
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.nn import functional as F
|
||||
from torch.cuda.amp import GradScaler, autocast
|
||||
|
||||
from dassl.engine import TRAINER_REGISTRY, TrainerX
|
||||
from dassl.utils import load_pretrained_weights, load_checkpoint
|
||||
from dassl.optim import build_optimizer, build_lr_scheduler
|
||||
|
||||
from clip import clip
|
||||
from clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
|
||||
|
||||
_tokenizer = _Tokenizer()
|
||||
|
||||
|
||||
def load_clip_to_cpu(cfg):
|
||||
backbone_name = cfg.MODEL.BACKBONE.NAME
|
||||
url = clip._MODELS[backbone_name]
|
||||
model_path = clip._download(url)
|
||||
|
||||
try:
|
||||
# loading JIT archive
|
||||
model = torch.jit.load(model_path, map_location="cpu").eval()
|
||||
state_dict = None
|
||||
|
||||
except RuntimeError:
|
||||
state_dict = torch.load(model_path, map_location="cpu")
|
||||
design_details = {"trainer": 'IVLP',
|
||||
"vision_depth": cfg.TRAINER.IVLP.PROMPT_DEPTH_VISION,
|
||||
"language_depth": cfg.TRAINER.IVLP.PROMPT_DEPTH_TEXT, "vision_ctx": cfg.TRAINER.IVLP.N_CTX_VISION,
|
||||
"language_ctx": cfg.TRAINER.IVLP.N_CTX_TEXT}
|
||||
model = clip.build_model(state_dict or model.state_dict(), design_details)
|
||||
|
||||
return model
|
||||
|
||||
|
||||
class TextEncoder(nn.Module):
|
||||
def __init__(self, clip_model):
|
||||
super().__init__()
|
||||
self.transformer = clip_model.transformer
|
||||
self.positional_embedding = clip_model.positional_embedding
|
||||
self.ln_final = clip_model.ln_final
|
||||
self.text_projection = clip_model.text_projection
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, prompts, tokenized_prompts):
|
||||
x = prompts + self.positional_embedding.type(self.dtype)
|
||||
x = x.permute(1, 0, 2) # NLD -> LND
|
||||
x = self.transformer(x)
|
||||
x = x.permute(1, 0, 2) # LND -> NLD
|
||||
x = self.ln_final(x).type(self.dtype)
|
||||
|
||||
# x.shape = [batch_size, n_ctx, transformer.width]
|
||||
# take features from the eot embedding (eot_token is the highest number in each sequence)
|
||||
x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class VLPromptLearner(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
n_cls = len(classnames)
|
||||
# Make sure Language depth >= 1
|
||||
assert cfg.TRAINER.IVLP.PROMPT_DEPTH_TEXT >= 1, "In Independent VL prompting, Language prompt depth should be >=1" \
|
||||
"\nPlease use VPT trainer if you want to learn only vision " \
|
||||
"branch "
|
||||
n_ctx = cfg.TRAINER.IVLP.N_CTX_TEXT
|
||||
ctx_init = cfg.TRAINER.IVLP.CTX_INIT
|
||||
dtype = clip_model.dtype
|
||||
ctx_dim = clip_model.ln_final.weight.shape[0]
|
||||
vis_dim = clip_model.visual.output_dim
|
||||
clip_imsize = clip_model.visual.input_resolution
|
||||
cfg_imsize = cfg.INPUT.SIZE[0]
|
||||
assert cfg_imsize == clip_imsize, f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"
|
||||
|
||||
if ctx_init and (n_ctx) <= 4:
|
||||
# Use given words to initialize context vectors
|
||||
ctx_init = ctx_init.replace("_", " ")
|
||||
n_ctx = n_ctx
|
||||
prompt = clip.tokenize(ctx_init)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(prompt).type(dtype)
|
||||
ctx_vectors = embedding[0, 1: 1 + n_ctx, :]
|
||||
prompt_prefix = ctx_init
|
||||
else:
|
||||
# Random initialization
|
||||
ctx_vectors = torch.empty(n_ctx, ctx_dim, dtype=dtype)
|
||||
nn.init.normal_(ctx_vectors, std=0.02)
|
||||
prompt_prefix = " ".join(["X"] * n_ctx)
|
||||
print(f"Independent V-L design")
|
||||
print(f'Initial text context: "{prompt_prefix}"')
|
||||
print(f"Number of context words (tokens) for Language prompting: {n_ctx}")
|
||||
print(f"Number of context words (tokens) for Vision prompting: {cfg.TRAINER.IVLP.N_CTX_VISION}")
|
||||
self.ctx = nn.Parameter(ctx_vectors)
|
||||
|
||||
classnames = [name.replace("_", " ") for name in classnames]
|
||||
name_lens = [len(_tokenizer.encode(name)) for name in classnames]
|
||||
prompts = [prompt_prefix + " " + name + "." for name in classnames]
|
||||
|
||||
tokenized_prompts = torch.cat([clip.tokenize(p) for p in prompts]) # (n_cls, n_tkn)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(tokenized_prompts).type(dtype)
|
||||
|
||||
# These token vectors will be saved when in save_model(),
|
||||
# but they should be ignored in load_model() as we want to use
|
||||
# those computed using the current class names
|
||||
self.register_buffer("token_prefix", embedding[:, :1, :]) # SOS
|
||||
self.register_buffer("token_suffix", embedding[:, 1 + n_ctx:, :]) # CLS, EOS
|
||||
|
||||
self.n_cls = n_cls
|
||||
self.n_ctx = n_ctx
|
||||
self.tokenized_prompts = tokenized_prompts # torch.Tensor
|
||||
self.name_lens = name_lens
|
||||
|
||||
def construct_prompts(self, ctx, prefix, suffix, label=None):
|
||||
# dim0 is either batch_size (during training) or n_cls (during testing)
|
||||
# ctx: context tokens, with shape of (dim0, n_ctx, ctx_dim)
|
||||
# prefix: the sos token, with shape of (n_cls, 1, ctx_dim)
|
||||
# suffix: remaining tokens, with shape of (n_cls, *, ctx_dim)
|
||||
|
||||
if label is not None:
|
||||
prefix = prefix[label]
|
||||
suffix = suffix[label]
|
||||
|
||||
prompts = torch.cat(
|
||||
[
|
||||
prefix, # (dim0, 1, dim)
|
||||
ctx, # (dim0, n_ctx, dim)
|
||||
suffix, # (dim0, *, dim)
|
||||
],
|
||||
dim=1,
|
||||
)
|
||||
|
||||
return prompts
|
||||
|
||||
def forward(self):
|
||||
ctx = self.ctx
|
||||
if ctx.dim() == 2:
|
||||
ctx = ctx.unsqueeze(0).expand(self.n_cls, -1, -1)
|
||||
|
||||
prefix = self.token_prefix
|
||||
suffix = self.token_suffix
|
||||
prompts = self.construct_prompts(ctx, prefix, suffix)
|
||||
|
||||
return prompts
|
||||
|
||||
|
||||
class CustomCLIP(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
self.prompt_learner = VLPromptLearner(cfg, classnames, clip_model)
|
||||
self.tokenized_prompts = self.prompt_learner.tokenized_prompts
|
||||
self.image_encoder = clip_model.visual
|
||||
self.text_encoder = TextEncoder(clip_model)
|
||||
self.logit_scale = clip_model.logit_scale
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, image, label=None):
|
||||
tokenized_prompts = self.tokenized_prompts
|
||||
logit_scale = self.logit_scale.exp()
|
||||
|
||||
prompts = self.prompt_learner()
|
||||
text_features = self.text_encoder(prompts, tokenized_prompts)
|
||||
image_features = self.image_encoder(image.type(self.dtype))
|
||||
|
||||
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
||||
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
||||
logits = logit_scale * image_features @ text_features.t()
|
||||
|
||||
if self.prompt_learner.training:
|
||||
return F.cross_entropy(logits, label)
|
||||
|
||||
return logits
|
||||
|
||||
|
||||
@TRAINER_REGISTRY.register()
|
||||
class IVLP(TrainerX):
|
||||
def check_cfg(self, cfg):
|
||||
assert cfg.TRAINER.IVLP.PREC in ["fp16", "fp32", "amp"]
|
||||
|
||||
def build_model(self):
|
||||
cfg = self.cfg
|
||||
classnames = self.dm.dataset.classnames
|
||||
|
||||
print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})")
|
||||
clip_model = load_clip_to_cpu(cfg)
|
||||
|
||||
if cfg.TRAINER.IVLP.PREC == "fp32" or cfg.TRAINER.IVLP.PREC == "amp":
|
||||
# CLIP's default precision is fp16
|
||||
clip_model.float()
|
||||
|
||||
print("Building custom CLIP")
|
||||
self.model = CustomCLIP(cfg, classnames, clip_model)
|
||||
|
||||
print("Turning off gradients in both the image and the text encoder")
|
||||
name_to_update = "prompt_learner"
|
||||
|
||||
for name, param in self.model.named_parameters():
|
||||
if name_to_update not in name:
|
||||
# Make sure that VPT prompts are updated
|
||||
if "VPT" in name:
|
||||
param.requires_grad_(True)
|
||||
else:
|
||||
param.requires_grad_(False)
|
||||
|
||||
# Double check
|
||||
enabled = set()
|
||||
for name, param in self.model.named_parameters():
|
||||
if param.requires_grad:
|
||||
enabled.add(name)
|
||||
print(f"Parameters to be updated: {enabled}")
|
||||
|
||||
if cfg.MODEL.INIT_WEIGHTS:
|
||||
load_pretrained_weights(self.model, cfg.MODEL.INIT_WEIGHTS)
|
||||
|
||||
self.model.to(self.device)
|
||||
# NOTE: only give prompt_learner to the optimizer
|
||||
self.optim = build_optimizer(self.model, cfg.OPTIM)
|
||||
self.sched = build_lr_scheduler(self.optim, cfg.OPTIM)
|
||||
self.register_model("VLPromptLearner", self.model, self.optim, self.sched)
|
||||
|
||||
self.scaler = GradScaler() if cfg.TRAINER.IVLP.PREC == "amp" else None
|
||||
|
||||
# Note that multi-gpu training could be slow because CLIP's size is
|
||||
# big, which slows down the copy operation in DataParallel
|
||||
device_count = torch.cuda.device_count()
|
||||
if device_count > 1:
|
||||
print(f"Multiple GPUs detected (n_gpus={device_count}), use all of them!")
|
||||
self.model = nn.DataParallel(self.model)
|
||||
|
||||
def forward_backward(self, batch):
|
||||
image, label = self.parse_batch_train(batch)
|
||||
|
||||
model = self.model
|
||||
optim = self.optim
|
||||
scaler = self.scaler
|
||||
|
||||
prec = self.cfg.TRAINER.IVLP.PREC
|
||||
if prec == "amp":
|
||||
with autocast():
|
||||
loss = model(image, label)
|
||||
optim.zero_grad()
|
||||
scaler.scale(loss).backward()
|
||||
scaler.step(optim)
|
||||
scaler.update()
|
||||
else:
|
||||
loss = model(image, label)
|
||||
optim.zero_grad()
|
||||
loss.backward()
|
||||
optim.step()
|
||||
|
||||
loss_summary = {"loss": loss.item()}
|
||||
|
||||
if (self.batch_idx + 1) == self.num_batches:
|
||||
self.update_lr()
|
||||
|
||||
return loss_summary
|
||||
|
||||
def parse_batch_train(self, batch):
|
||||
input = batch["img"]
|
||||
label = batch["label"]
|
||||
input = input.to(self.device)
|
||||
label = label.to(self.device)
|
||||
return input, label
|
||||
|
||||
def load_model(self, directory, epoch=None):
|
||||
if not directory:
|
||||
print("Note that load_model() is skipped as no pretrained model is given")
|
||||
return
|
||||
|
||||
names = self.get_model_names()
|
||||
|
||||
# By default, the best model is loaded
|
||||
model_file = "model-best.pth.tar"
|
||||
|
||||
if epoch is not None:
|
||||
model_file = "model.pth.tar-" + str(epoch)
|
||||
|
||||
for name in names:
|
||||
model_path = osp.join(directory, name, model_file)
|
||||
|
||||
if not osp.exists(model_path):
|
||||
raise FileNotFoundError('Model not found at "{}"'.format(model_path))
|
||||
|
||||
checkpoint = load_checkpoint(model_path)
|
||||
state_dict = checkpoint["state_dict"]
|
||||
epoch = checkpoint["epoch"]
|
||||
|
||||
# Ignore fixed token vectors
|
||||
if "prompt_learner.token_prefix" in state_dict:
|
||||
del state_dict["prompt_learner.token_prefix"]
|
||||
|
||||
if "prompt_learner.token_suffix" in state_dict:
|
||||
del state_dict["prompt_learner.token_suffix"]
|
||||
|
||||
print("Loading weights to {} " 'from "{}" (epoch = {})'.format(name, model_path, epoch))
|
||||
# set strict=False
|
||||
self._models[name].load_state_dict(state_dict, strict=False)
|
||||
333
trainers/maple.py
Normal file
333
trainers/maple.py
Normal file
@@ -0,0 +1,333 @@
|
||||
import os.path as osp
|
||||
from collections import OrderedDict
|
||||
import math
|
||||
import copy
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.nn import functional as F
|
||||
from torch.cuda.amp import GradScaler, autocast
|
||||
|
||||
from dassl.engine import TRAINER_REGISTRY, TrainerX
|
||||
from dassl.metrics import compute_accuracy
|
||||
from dassl.utils import load_pretrained_weights, load_checkpoint
|
||||
from dassl.optim import build_optimizer, build_lr_scheduler
|
||||
|
||||
from clip import clip
|
||||
from clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
|
||||
|
||||
_tokenizer = _Tokenizer()
|
||||
|
||||
|
||||
def load_clip_to_cpu(cfg):
|
||||
backbone_name = cfg.MODEL.BACKBONE.NAME
|
||||
url = clip._MODELS[backbone_name]
|
||||
model_path = clip._download(url)
|
||||
|
||||
try:
|
||||
# loading JIT archive
|
||||
model = torch.jit.load(model_path, map_location="cpu").eval()
|
||||
state_dict = None
|
||||
|
||||
except RuntimeError:
|
||||
state_dict = torch.load(model_path, map_location="cpu")
|
||||
design_details = {"trainer": 'MaPLe',
|
||||
"vision_depth": 0,
|
||||
"language_depth": 0, "vision_ctx": 0,
|
||||
"language_ctx": 0,
|
||||
"maple_length": cfg.TRAINER.MAPLE.N_CTX}
|
||||
model = clip.build_model(state_dict or model.state_dict(), design_details)
|
||||
|
||||
return model
|
||||
|
||||
|
||||
class TextEncoder(nn.Module):
|
||||
def __init__(self, clip_model):
|
||||
super().__init__()
|
||||
self.transformer = clip_model.transformer
|
||||
self.positional_embedding = clip_model.positional_embedding
|
||||
self.ln_final = clip_model.ln_final
|
||||
self.text_projection = clip_model.text_projection
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, prompts, tokenized_prompts, compound_prompts_deeper_text):
|
||||
x = prompts + self.positional_embedding.type(self.dtype)
|
||||
x = x.permute(1, 0, 2) # NLD -> LND
|
||||
# Pass as the list, as nn.sequential cannot process multiple arguments in the forward pass
|
||||
combined = [x, compound_prompts_deeper_text, 0] # third argument is the counter which denotes depth of prompt
|
||||
outputs = self.transformer(combined)
|
||||
x = outputs[0] # extract the x back from here
|
||||
x = x.permute(1, 0, 2) # LND -> NLD
|
||||
x = self.ln_final(x).type(self.dtype)
|
||||
|
||||
# x.shape = [batch_size, n_ctx, transformer.width]
|
||||
# take features from the eot embedding (eot_token is the highest number in each sequence)
|
||||
x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class MultiModalPromptLearner(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
n_cls = len(classnames)
|
||||
n_ctx = cfg.TRAINER.MAPLE.N_CTX
|
||||
ctx_init = cfg.TRAINER.MAPLE.CTX_INIT
|
||||
dtype = clip_model.dtype
|
||||
ctx_dim = clip_model.ln_final.weight.shape[0]
|
||||
clip_imsize = clip_model.visual.input_resolution
|
||||
cfg_imsize = cfg.INPUT.SIZE[0]
|
||||
# Default is 1, which is compound shallow prompting
|
||||
assert cfg.TRAINER.MAPLE.PROMPT_DEPTH >= 1, "For MaPLe, PROMPT_DEPTH should be >= 1"
|
||||
self.compound_prompts_depth = cfg.TRAINER.MAPLE.PROMPT_DEPTH # max=12, but will create 11 such shared prompts
|
||||
assert cfg_imsize == clip_imsize, f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"
|
||||
|
||||
if ctx_init and (n_ctx) <= 4:
|
||||
# use given words to initialize context vectors
|
||||
ctx_init = ctx_init.replace("_", " ")
|
||||
n_ctx = n_ctx
|
||||
prompt = clip.tokenize(ctx_init)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(prompt).type(dtype)
|
||||
ctx_vectors = embedding[0, 1: 1 + n_ctx, :]
|
||||
prompt_prefix = ctx_init
|
||||
else:
|
||||
# random initialization
|
||||
ctx_vectors = torch.empty(n_ctx, ctx_dim, dtype=dtype)
|
||||
nn.init.normal_(ctx_vectors, std=0.02)
|
||||
prompt_prefix = " ".join(["X"] * n_ctx)
|
||||
print('MaPLe design: Multi-modal Prompt Learning')
|
||||
print(f'Initial context: "{prompt_prefix}"')
|
||||
print(f"Number of MaPLe context words (tokens): {n_ctx}")
|
||||
# These below, related to the shallow prompts
|
||||
# Linear layer so that the tokens will project to 512 and will be initialized from 768
|
||||
self.proj = nn.Linear(ctx_dim, 768)
|
||||
self.proj.half()
|
||||
self.ctx = nn.Parameter(ctx_vectors)
|
||||
# These below parameters related to the shared prompts
|
||||
# Define the compound prompts for the deeper layers
|
||||
|
||||
# Minimum can be 1, which defaults to shallow MaPLe
|
||||
# compound prompts
|
||||
self.compound_prompts_text = nn.ParameterList([nn.Parameter(torch.empty(n_ctx, 512))
|
||||
for _ in range(self.compound_prompts_depth - 1)])
|
||||
for single_para in self.compound_prompts_text:
|
||||
nn.init.normal_(single_para, std=0.02)
|
||||
# Also make corresponding projection layers, for each prompt
|
||||
single_layer = nn.Linear(ctx_dim, 768)
|
||||
self.compound_prompt_projections = _get_clones(single_layer, self.compound_prompts_depth - 1)
|
||||
|
||||
classnames = [name.replace("_", " ") for name in classnames]
|
||||
name_lens = [len(_tokenizer.encode(name)) for name in classnames]
|
||||
prompts = [prompt_prefix + " " + name + "." for name in classnames]
|
||||
|
||||
tokenized_prompts = torch.cat([clip.tokenize(p) for p in prompts]) # (n_cls, n_tkn)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(tokenized_prompts).type(dtype)
|
||||
|
||||
# These token vectors will be saved when in save_model(),
|
||||
# but they should be ignored in load_model() as we want to use
|
||||
# those computed using the current class names
|
||||
self.register_buffer("token_prefix", embedding[:, :1, :]) # SOS
|
||||
self.register_buffer("token_suffix", embedding[:, 1 + n_ctx:, :]) # CLS, EOS
|
||||
|
||||
self.n_cls = n_cls
|
||||
self.n_ctx = n_ctx
|
||||
self.tokenized_prompts = tokenized_prompts # torch.Tensor
|
||||
self.name_lens = name_lens
|
||||
|
||||
def construct_prompts(self, ctx, prefix, suffix, label=None):
|
||||
# dim0 is either batch_size (during training) or n_cls (during testing)
|
||||
# ctx: context tokens, with shape of (dim0, n_ctx, ctx_dim)
|
||||
# prefix: the sos token, with shape of (n_cls, 1, ctx_dim)
|
||||
# suffix: remaining tokens, with shape of (n_cls, *, ctx_dim)
|
||||
|
||||
if label is not None:
|
||||
prefix = prefix[label]
|
||||
suffix = suffix[label]
|
||||
|
||||
prompts = torch.cat(
|
||||
[
|
||||
prefix, # (dim0, 1, dim)
|
||||
ctx, # (dim0, n_ctx, dim)
|
||||
suffix, # (dim0, *, dim)
|
||||
],
|
||||
dim=1,
|
||||
)
|
||||
|
||||
return prompts
|
||||
|
||||
def forward(self):
|
||||
ctx = self.ctx
|
||||
|
||||
if ctx.dim() == 2:
|
||||
ctx = ctx.unsqueeze(0).expand(self.n_cls, -1, -1)
|
||||
|
||||
prefix = self.token_prefix
|
||||
suffix = self.token_suffix
|
||||
prompts = self.construct_prompts(ctx, prefix, suffix)
|
||||
|
||||
# Before returning, need to transform
|
||||
# prompts to 768 for the visual side
|
||||
visual_deep_prompts = []
|
||||
for index, layer in enumerate(self.compound_prompt_projections):
|
||||
visual_deep_prompts.append(layer(self.compound_prompts_text[index]))
|
||||
# Now the other way around
|
||||
# We will project the textual prompts from 512 to 768
|
||||
return prompts, self.proj(self.ctx), self.compound_prompts_text, visual_deep_prompts # pass here original, as for visual 768 is required
|
||||
|
||||
|
||||
class CustomCLIP(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
self.prompt_learner = MultiModalPromptLearner(cfg, classnames, clip_model)
|
||||
self.tokenized_prompts = self.prompt_learner.tokenized_prompts
|
||||
self.image_encoder = clip_model.visual
|
||||
self.text_encoder = TextEncoder(clip_model)
|
||||
self.logit_scale = clip_model.logit_scale
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, image, label=None):
|
||||
tokenized_prompts = self.tokenized_prompts
|
||||
logit_scale = self.logit_scale.exp()
|
||||
|
||||
prompts, shared_ctx, deep_compound_prompts_text, deep_compound_prompts_vision = self.prompt_learner()
|
||||
text_features = self.text_encoder(prompts, tokenized_prompts, deep_compound_prompts_text)
|
||||
image_features = self.image_encoder(image.type(self.dtype), shared_ctx, deep_compound_prompts_vision)
|
||||
|
||||
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
||||
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
||||
logits = logit_scale * image_features @ text_features.t()
|
||||
|
||||
if self.prompt_learner.training:
|
||||
return F.cross_entropy(logits, label)
|
||||
|
||||
return logits
|
||||
|
||||
|
||||
def _get_clones(module, N):
|
||||
return nn.ModuleList([copy.deepcopy(module) for i in range(N)])
|
||||
|
||||
|
||||
@TRAINER_REGISTRY.register()
|
||||
class MaPLe(TrainerX):
|
||||
def check_cfg(self, cfg):
|
||||
assert cfg.TRAINER.MAPLE.PREC in ["fp16", "fp32", "amp"]
|
||||
|
||||
def build_model(self):
|
||||
cfg = self.cfg
|
||||
classnames = self.dm.dataset.classnames
|
||||
|
||||
print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})")
|
||||
clip_model = load_clip_to_cpu(cfg)
|
||||
|
||||
if cfg.TRAINER.MAPLE.PREC == "fp32" or cfg.TRAINER.MAPLE.PREC == "amp":
|
||||
# CLIP's default precision is fp16
|
||||
clip_model.float()
|
||||
|
||||
print("Building custom CLIP")
|
||||
self.model = CustomCLIP(cfg, classnames, clip_model)
|
||||
|
||||
print("Turning off gradients in both the image and the text encoder")
|
||||
name_to_update = "prompt_learner"
|
||||
|
||||
for name, param in self.model.named_parameters():
|
||||
if name_to_update not in name:
|
||||
# Make sure that VPT prompts are updated
|
||||
if "VPT" in name:
|
||||
param.requires_grad_(True)
|
||||
else:
|
||||
param.requires_grad_(False)
|
||||
|
||||
# Double check
|
||||
enabled = set()
|
||||
for name, param in self.model.named_parameters():
|
||||
if param.requires_grad:
|
||||
enabled.add(name)
|
||||
print(f"Parameters to be updated: {enabled}")
|
||||
|
||||
if cfg.MODEL.INIT_WEIGHTS:
|
||||
load_pretrained_weights(self.model, cfg.MODEL.INIT_WEIGHTS)
|
||||
|
||||
self.model.to(self.device)
|
||||
# NOTE: only give prompt_learner to the optimizer
|
||||
self.optim = build_optimizer(self.model, cfg.OPTIM)
|
||||
self.sched = build_lr_scheduler(self.optim, cfg.OPTIM)
|
||||
self.register_model("MultiModalPromptLearner", self.model, self.optim, self.sched)
|
||||
|
||||
self.scaler = GradScaler() if cfg.TRAINER.MAPLE.PREC == "amp" else None
|
||||
|
||||
# Note that multi-gpu training could be slow because CLIP's size is
|
||||
# big, which slows down the copy operation in DataParallel
|
||||
device_count = torch.cuda.device_count()
|
||||
if device_count > 1:
|
||||
print(f"Multiple GPUs detected (n_gpus={device_count}), use all of them!")
|
||||
self.model = nn.DataParallel(self.model)
|
||||
|
||||
def forward_backward(self, batch):
|
||||
image, label = self.parse_batch_train(batch)
|
||||
|
||||
model = self.model
|
||||
optim = self.optim
|
||||
scaler = self.scaler
|
||||
|
||||
prec = self.cfg.TRAINER.MAPLE.PREC
|
||||
if prec == "amp":
|
||||
with autocast():
|
||||
loss = model(image, label)
|
||||
optim.zero_grad()
|
||||
scaler.scale(loss).backward()
|
||||
scaler.step(optim)
|
||||
scaler.update()
|
||||
else:
|
||||
loss = model(image, label)
|
||||
optim.zero_grad()
|
||||
loss.backward()
|
||||
optim.step()
|
||||
|
||||
loss_summary = {"loss": loss.item()}
|
||||
|
||||
if (self.batch_idx + 1) == self.num_batches:
|
||||
self.update_lr()
|
||||
|
||||
return loss_summary
|
||||
|
||||
def parse_batch_train(self, batch):
|
||||
input = batch["img"]
|
||||
label = batch["label"]
|
||||
input = input.to(self.device)
|
||||
label = label.to(self.device)
|
||||
return input, label
|
||||
|
||||
def load_model(self, directory, epoch=None):
|
||||
if not directory:
|
||||
print("Note that load_model() is skipped as no pretrained model is given")
|
||||
return
|
||||
|
||||
names = self.get_model_names()
|
||||
|
||||
# By default, the best model is loaded
|
||||
model_file = "model-best.pth.tar"
|
||||
|
||||
if epoch is not None:
|
||||
model_file = "model.pth.tar-" + str(epoch)
|
||||
|
||||
for name in names:
|
||||
model_path = osp.join(directory, name, model_file)
|
||||
|
||||
if not osp.exists(model_path):
|
||||
raise FileNotFoundError('Model not found at "{}"'.format(model_path))
|
||||
|
||||
checkpoint = load_checkpoint(model_path)
|
||||
state_dict = checkpoint["state_dict"]
|
||||
epoch = checkpoint["epoch"]
|
||||
|
||||
# Ignore fixed token vectors
|
||||
if "prompt_learner.token_prefix" in state_dict:
|
||||
del state_dict["prompt_learner.token_prefix"]
|
||||
|
||||
if "prompt_learner.token_suffix" in state_dict:
|
||||
del state_dict["prompt_learner.token_suffix"]
|
||||
|
||||
print("Loading weights to {} " 'from "{}" (epoch = {})'.format(name, model_path, epoch))
|
||||
# set strict=False
|
||||
self._models[name].load_state_dict(state_dict, strict=False)
|
||||
401
trainers/promptsrc.py
Normal file
401
trainers/promptsrc.py
Normal file
@@ -0,0 +1,401 @@
|
||||
import copy
|
||||
import os.path as osp
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.nn import functional as F
|
||||
from torch.cuda.amp import GradScaler, autocast
|
||||
|
||||
from dassl.engine import TRAINER_REGISTRY, TrainerX
|
||||
from dassl.utils import load_pretrained_weights, load_checkpoint
|
||||
from dassl.optim import build_optimizer, build_lr_scheduler
|
||||
from clip import clip
|
||||
from clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
|
||||
from .imagenet_templates import IMAGENET_TEMPLATES
|
||||
|
||||
_tokenizer = _Tokenizer()
|
||||
|
||||
|
||||
def load_clip_to_cpu(cfg, zero_shot_model=False):
|
||||
backbone_name = cfg.MODEL.BACKBONE.NAME
|
||||
url = clip._MODELS[backbone_name]
|
||||
model_path = clip._download(url)
|
||||
|
||||
try:
|
||||
# loading JIT archive
|
||||
model = torch.jit.load(model_path, map_location="cpu").eval()
|
||||
state_dict = None
|
||||
|
||||
except RuntimeError:
|
||||
state_dict = torch.load(model_path, map_location="cpu")
|
||||
if not zero_shot_model:
|
||||
design_details = {"trainer": 'IVLP',
|
||||
"vision_depth": cfg.TRAINER.PROMPTSRC.PROMPT_DEPTH_VISION,
|
||||
"language_depth": cfg.TRAINER.PROMPTSRC.PROMPT_DEPTH_TEXT,
|
||||
"vision_ctx": cfg.TRAINER.PROMPTSRC.N_CTX_VISION,
|
||||
"language_ctx": cfg.TRAINER.PROMPTSRC.N_CTX_TEXT}
|
||||
model = clip.build_model(state_dict or model.state_dict(), design_details)
|
||||
else:
|
||||
# Return original CLIP model for generating frozen VL features
|
||||
design_details = {"trainer": 'IVLP',
|
||||
"vision_depth": 0,
|
||||
"language_depth": 0, "vision_ctx": 0,
|
||||
"language_ctx": 0}
|
||||
model = clip.build_model(state_dict or model.state_dict(), design_details)
|
||||
return model
|
||||
return model
|
||||
|
||||
|
||||
class TextEncoder(nn.Module):
|
||||
def __init__(self, clip_model):
|
||||
super().__init__()
|
||||
self.transformer = clip_model.transformer
|
||||
self.positional_embedding = clip_model.positional_embedding
|
||||
self.ln_final = clip_model.ln_final
|
||||
self.text_projection = clip_model.text_projection
|
||||
self.dtype = clip_model.dtype
|
||||
|
||||
def forward(self, prompts, tokenized_prompts):
|
||||
x = prompts + self.positional_embedding.type(self.dtype)
|
||||
x = x.permute(1, 0, 2) # NLD -> LND
|
||||
x = self.transformer(x)
|
||||
x = x.permute(1, 0, 2) # LND -> NLD
|
||||
x = self.ln_final(x).type(self.dtype)
|
||||
|
||||
# x.shape = [batch_size, n_ctx, transformer.width]
|
||||
# take features from the eot embedding (eot_token is the highest number in each sequence)
|
||||
x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class VLPromptLearner(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
n_cls = len(classnames)
|
||||
# Make sure Language depth >= 1
|
||||
assert cfg.TRAINER.PROMPTSRC.PROMPT_DEPTH_TEXT >= 1, "In Independent VL prompting, Language prompt depth should be >=1" \
|
||||
"\nPlease use VPT trainer if you want to learn only vision " \
|
||||
"branch"
|
||||
n_ctx = cfg.TRAINER.PROMPTSRC.N_CTX_TEXT
|
||||
ctx_init = cfg.TRAINER.PROMPTSRC.CTX_INIT
|
||||
dtype = clip_model.dtype
|
||||
ctx_dim = clip_model.ln_final.weight.shape[0]
|
||||
clip_imsize = clip_model.visual.input_resolution
|
||||
cfg_imsize = cfg.INPUT.SIZE[0]
|
||||
assert cfg_imsize == clip_imsize, f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"
|
||||
|
||||
if ctx_init and n_ctx <= 4:
|
||||
# use given words to initialize context vectors
|
||||
ctx_init = ctx_init.replace("_", " ")
|
||||
n_ctx = n_ctx
|
||||
prompt = clip.tokenize(ctx_init)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(prompt).type(dtype)
|
||||
ctx_vectors = embedding[0, 1: 1 + n_ctx, :]
|
||||
prompt_prefix = ctx_init
|
||||
else:
|
||||
# random initialization
|
||||
ctx_vectors = torch.empty(n_ctx, ctx_dim, dtype=dtype)
|
||||
nn.init.normal_(ctx_vectors, std=0.02)
|
||||
prompt_prefix = " ".join(["X"] * n_ctx)
|
||||
print(f"Independent V-L design")
|
||||
print(f'Initial text context: "{prompt_prefix}"')
|
||||
print(f"Number of context words (tokens) for Language prompting: {n_ctx}")
|
||||
print(f"Number of context words (tokens) for Vision prompting: {cfg.TRAINER.PROMPTSRC.N_CTX_VISION}")
|
||||
self.ctx = nn.Parameter(ctx_vectors)
|
||||
|
||||
classnames = [name.replace("_", " ") for name in classnames]
|
||||
name_lens = [len(_tokenizer.encode(name)) for name in classnames]
|
||||
prompts = [prompt_prefix + " " + name + "." for name in classnames]
|
||||
|
||||
tokenized_prompts = torch.cat([clip.tokenize(p) for p in prompts]) # (n_cls, n_tkn)
|
||||
# Also create frozen CLIP
|
||||
clip_model_temp = load_clip_to_cpu(cfg, True).float().cuda()
|
||||
clip_model_temp_image = load_clip_to_cpu(cfg, True)
|
||||
with torch.no_grad():
|
||||
embedding = clip_model.token_embedding(tokenized_prompts).type(dtype)
|
||||
self.ZS_image_encoder = clip_model_temp_image.visual
|
||||
# Now pre-compute the frozen VL embeddings
|
||||
all_teacher_features = []
|
||||
# Using multiple text templates to ensure textual diversity during training
|
||||
for single_template in IMAGENET_TEMPLATES:
|
||||
x = [single_template.replace("{}", name) for name in classnames]
|
||||
x_tokenized = torch.cat([clip.tokenize(p) for p in x])
|
||||
text_features = clip_model_temp.encode_text(x_tokenized.cuda())
|
||||
all_teacher_features.append(text_features.unsqueeze(1))
|
||||
|
||||
self.fixed_embeddings = torch.cat(all_teacher_features, dim=1).mean(dim=1)
|
||||
# These token vectors will be saved when in save_model(),
|
||||
# but they should be ignored in load_model() as we want to use
|
||||
# those computed using the current class names
|
||||
self.register_buffer("token_prefix", embedding[:, :1, :]) # SOS
|
||||
self.register_buffer("token_suffix", embedding[:, 1 + n_ctx:, :]) # CLS, EOS
|
||||
|
||||
self.n_cls = n_cls
|
||||
self.n_ctx = n_ctx
|
||||
self.tokenized_prompts = tokenized_prompts # torch.Tensor
|
||||
self.name_lens = name_lens
|
||||
|
||||
def construct_prompts(self, ctx, prefix, suffix, label=None):
|
||||
# dim0 is either batch_size (during training) or n_cls (during testing)
|
||||
# ctx: context tokens, with shape of (dim0, n_ctx, ctx_dim)
|
||||
# prefix: the sos token, with shape of (n_cls, 1, ctx_dim)
|
||||
# suffix: remaining tokens, with shape of (n_cls, *, ctx_dim)
|
||||
|
||||
if label is not None:
|
||||
prefix = prefix[label]
|
||||
suffix = suffix[label]
|
||||
|
||||
prompts = torch.cat(
|
||||
[
|
||||
prefix, # (dim0, 1, dim)
|
||||
ctx, # (dim0, n_ctx, dim)
|
||||
suffix, # (dim0, *, dim)
|
||||
],
|
||||
dim=1,
|
||||
)
|
||||
|
||||
return prompts
|
||||
|
||||
def forward(self):
|
||||
ctx = self.ctx
|
||||
if ctx.dim() == 2:
|
||||
ctx = ctx.unsqueeze(0).expand(self.n_cls, -1, -1)
|
||||
|
||||
prefix = self.token_prefix
|
||||
suffix = self.token_suffix
|
||||
prompts = self.construct_prompts(ctx, prefix, suffix)
|
||||
|
||||
return prompts
|
||||
|
||||
|
||||
class CustomCLIP(nn.Module):
|
||||
def __init__(self, cfg, classnames, clip_model):
|
||||
super().__init__()
|
||||
self.prompt_learner = VLPromptLearner(cfg, classnames, clip_model)
|
||||
self.tokenized_prompts = self.prompt_learner.tokenized_prompts
|
||||
self.image_encoder = clip_model.visual
|
||||
self.text_encoder = TextEncoder(clip_model)
|
||||
self.logit_scale = clip_model.logit_scale
|
||||
self.dtype = clip_model.dtype
|
||||
self.total_epochs = cfg.OPTIM.MAX_EPOCH
|
||||
self.n_cls = len(classnames)
|
||||
|
||||
def forward(self, image, label=None):
|
||||
tokenized_prompts = self.tokenized_prompts
|
||||
logit_scale = self.logit_scale.exp()
|
||||
|
||||
prompts = self.prompt_learner()
|
||||
# Compute the prompted image and text features
|
||||
text_features = self.text_encoder(prompts, tokenized_prompts)
|
||||
image_features = self.image_encoder(image.type(self.dtype))
|
||||
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
||||
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
||||
# Compute the prompted logits
|
||||
logits = logit_scale * image_features @ text_features.t()
|
||||
if self.prompt_learner.training:
|
||||
# Now calculate the frozen pre-trained features
|
||||
fixed_embeddings = self.prompt_learner.fixed_embeddings # precomputed pre-trained frozen textual features
|
||||
fixed_embeddings = fixed_embeddings / fixed_embeddings.norm(dim=-1, keepdim=True)
|
||||
with torch.no_grad():
|
||||
zero_shot_features = self.prompt_learner.ZS_image_encoder(image.type(self.dtype))
|
||||
zero_shot_features = zero_shot_features / zero_shot_features.norm(dim=-1, keepdim=True)
|
||||
# Compute pre-trained frozen visual features
|
||||
zero_shot_logits = logit_scale * zero_shot_features.cuda() @ fixed_embeddings.half().cuda().t()
|
||||
|
||||
return F.cross_entropy(logits,
|
||||
label), text_features, fixed_embeddings, zero_shot_features, \
|
||||
image_features, zero_shot_logits, logits
|
||||
else:
|
||||
return logits
|
||||
|
||||
|
||||
@TRAINER_REGISTRY.register()
|
||||
class PromptSRC(TrainerX):
|
||||
def check_cfg(self, cfg):
|
||||
assert cfg.TRAINER.PROMPTSRC.PREC in ["fp16", "fp32", "amp"]
|
||||
|
||||
def build_model(self):
|
||||
cfg = self.cfg
|
||||
classnames = self.dm.dataset.classnames
|
||||
|
||||
print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})")
|
||||
clip_model = load_clip_to_cpu(cfg)
|
||||
|
||||
if cfg.TRAINER.PROMPTSRC.PREC == "fp32" or cfg.TRAINER.PROMPTSRC.PREC == "amp":
|
||||
# CLIP's default precision is fp16
|
||||
clip_model.float()
|
||||
|
||||
print("Building custom CLIP")
|
||||
self.model = CustomCLIP(cfg, classnames, clip_model)
|
||||
|
||||
print("Turning off gradients in both the image and the text encoder")
|
||||
name_to_update = "prompt_learner"
|
||||
|
||||
for name, param in self.model.named_parameters():
|
||||
if name_to_update not in name:
|
||||
# Make sure that VPT prompts are updated
|
||||
if "VPT" in name:
|
||||
param.requires_grad_(True)
|
||||
else:
|
||||
param.requires_grad_(False)
|
||||
else:
|
||||
if "ZS_image_encoder" in name:
|
||||
param.requires_grad_(False)
|
||||
|
||||
# Double check
|
||||
enabled = set()
|
||||
for name, param in self.model.named_parameters():
|
||||
if param.requires_grad:
|
||||
enabled.add(name)
|
||||
print(f"Parameters to be updated: {enabled}")
|
||||
print(f"Parameters count: {len(enabled)}")
|
||||
if cfg.MODEL.INIT_WEIGHTS:
|
||||
load_pretrained_weights(self.model, cfg.MODEL.INIT_WEIGHTS)
|
||||
|
||||
self.model.to(self.device)
|
||||
# NOTE: only give prompt_learner to the optimizer
|
||||
self.optim = build_optimizer(self.model, cfg.OPTIM)
|
||||
self.sched = build_lr_scheduler(self.optim, cfg.OPTIM)
|
||||
self.register_model("VLPromptLearner", self.model, self.optim, self.sched)
|
||||
# Cosine scheduler
|
||||
self.total_epochs = cfg.OPTIM.MAX_EPOCH
|
||||
self.step_counter = 1
|
||||
N = cfg.OPTIM.MAX_EPOCH
|
||||
mean = cfg.TRAINER.PROMPTSRC.GPA_MEAN
|
||||
stdev = cfg.TRAINER.PROMPTSRC.GPA_STD
|
||||
gauss = self.get_gauss(mean, stdev)
|
||||
self.gauss = np.array([gauss(a) for a in range(1, N + 1)])
|
||||
self.gauss = self.gauss / sum(self.gauss)
|
||||
self.scaler = GradScaler() if cfg.TRAINER.PROMPTSRC.PREC == "amp" else None
|
||||
# Note that multi-gpu training could be slow because CLIP's size is
|
||||
# big, which slows down the copy operation in DataParallel
|
||||
device_count = torch.cuda.device_count()
|
||||
if device_count > 1:
|
||||
print(f"Multiple GPUs detected (n_gpus={device_count}), use all of them!")
|
||||
self.model = nn.DataParallel(self.model)
|
||||
# Keep model with GPA
|
||||
self.previous_model_gpa = None
|
||||
|
||||
def forward_backward(self, batch):
|
||||
image, label = self.parse_batch_train(batch)
|
||||
|
||||
model = self.model
|
||||
optim = self.optim
|
||||
scaler = self.scaler
|
||||
|
||||
prec = self.cfg.TRAINER.PROMPTSRC.PREC
|
||||
if prec == "amp":
|
||||
with autocast():
|
||||
loss = model(image, label)
|
||||
optim.zero_grad()
|
||||
scaler.scale(loss).backward()
|
||||
scaler.step(optim)
|
||||
scaler.update()
|
||||
else:
|
||||
loss_ce, normalized_text_features, zs_clip_text_embeddings, zs_image_embedd, image_ft, \
|
||||
zero_shot_logits, logits = model(image, label)
|
||||
# Calculate the L_SCL_text loss
|
||||
loss_scl_text = F.l1_loss(normalized_text_features, zs_clip_text_embeddings.cuda(),
|
||||
reduction='mean') * self.cfg.TRAINER.PROMPTSRC.TEXT_LOSS_WEIGHT
|
||||
# Calculate the L_SCL_image loss
|
||||
loss_scl_image = F.l1_loss(image_ft, zs_image_embedd.cuda(),
|
||||
reduction='mean') * self.cfg.TRAINER.PROMPTSRC.IMAGE_LOSS_WEIGHT
|
||||
# Now calculate L_SCL_logits
|
||||
L_SCL_logits = F.kl_div(
|
||||
F.log_softmax(logits / 1, dim=1),
|
||||
F.log_softmax(zero_shot_logits / 1, dim=1),
|
||||
reduction='sum',
|
||||
log_target=True
|
||||
) * (1 * 1) / logits.numel()
|
||||
L_SCL = (L_SCL_logits + loss_scl_text + loss_scl_image)
|
||||
loss = (loss_ce + L_SCL)
|
||||
optim.zero_grad()
|
||||
loss.backward()
|
||||
optim.step()
|
||||
|
||||
loss_summary = {"loss": loss.item()}
|
||||
|
||||
if (self.batch_idx + 1) == self.num_batches:
|
||||
self.update_lr()
|
||||
# Means one epoch is completed, perform GPA
|
||||
self.step_counter = self.step_counter + 1
|
||||
current_epoch_weight = self.gauss[self.step_counter - 2]
|
||||
current_model_weights = copy.deepcopy(model.state_dict())
|
||||
weighted_state_dict = self.state_dict_weighting(current_model_weights, current_epoch_weight)
|
||||
if self.previous_model_gpa is None:
|
||||
self.previous_model_gpa = weighted_state_dict
|
||||
else:
|
||||
self.previous_model_gpa = self.state_dict_add(weighted_state_dict, self.previous_model_gpa)
|
||||
|
||||
if self.step_counter == self.model.total_epochs + 1:
|
||||
print("Using GPA model for final inference...")
|
||||
model.load_state_dict(self.previous_model_gpa)
|
||||
self.model.load_state_dict(self.previous_model_gpa)
|
||||
return loss_summary
|
||||
|
||||
def state_dict_weighting(self, main_dict, weightage, prompt_only=False):
|
||||
# Average all parameters
|
||||
updated_dict = copy.deepcopy(main_dict)
|
||||
if not prompt_only:
|
||||
for key in main_dict:
|
||||
updated_dict[key] = main_dict[key] * weightage
|
||||
return updated_dict
|
||||
else:
|
||||
return main_dict * weightage
|
||||
|
||||
def state_dict_add(self, dict1, dict2, prompt_only=False):
|
||||
# Average all parameters
|
||||
if not prompt_only:
|
||||
modified_dict = dict2
|
||||
for key in dict1:
|
||||
modified_dict[key] = (modified_dict[key] + dict1[key])
|
||||
return modified_dict
|
||||
else:
|
||||
return dict1 + dict2
|
||||
|
||||
def get_gauss(self, mu, sigma):
|
||||
gauss = lambda x: (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - mu) / sigma) ** 2)
|
||||
return gauss
|
||||
|
||||
def parse_batch_train(self, batch):
|
||||
input = batch["img"]
|
||||
label = batch["label"]
|
||||
input = input.to(self.device)
|
||||
label = label.to(self.device)
|
||||
return input, label
|
||||
|
||||
def load_model(self, directory, epoch=None):
|
||||
if not directory:
|
||||
print("Note that load_model() is skipped as no pretrained model is given")
|
||||
return
|
||||
|
||||
names = self.get_model_names()
|
||||
|
||||
# By default, the best model is loaded
|
||||
model_file = "model-best.pth.tar"
|
||||
|
||||
if epoch is not None:
|
||||
model_file = "model.pth.tar-" + str(epoch)
|
||||
|
||||
for name in names:
|
||||
model_path = osp.join(directory, name, model_file)
|
||||
|
||||
if not osp.exists(model_path):
|
||||
raise FileNotFoundError('Model not found at "{}"'.format(model_path))
|
||||
|
||||
checkpoint = load_checkpoint(model_path)
|
||||
state_dict = checkpoint["state_dict"]
|
||||
epoch = checkpoint["epoch"]
|
||||
|
||||
# Ignore fixed token vectors
|
||||
if "prompt_learner.token_prefix" in state_dict:
|
||||
del state_dict["prompt_learner.token_prefix"]
|
||||
|
||||
if "prompt_learner.token_suffix" in state_dict:
|
||||
del state_dict["prompt_learner.token_suffix"]
|
||||
|
||||
print("Loading weights to {} " 'from "{}" (epoch = {})'.format(name, model_path, epoch))
|
||||
# set strict=False
|
||||
self._models[name].load_state_dict(state_dict, strict=False)
|
||||
99
trainers/zsclip.py
Normal file
99
trainers/zsclip.py
Normal file
@@ -0,0 +1,99 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
from dassl.engine import TRAINER_REGISTRY, TrainerX
|
||||
from dassl.optim import build_optimizer, build_lr_scheduler
|
||||
|
||||
from clip import clip
|
||||
from clip.model import convert_weights
|
||||
|
||||
from .coop import load_clip_to_cpu
|
||||
from .imagenet_templates import IMAGENET_TEMPLATES, IMAGENET_TEMPLATES_SELECT
|
||||
|
||||
CUSTOM_TEMPLATES = {
|
||||
"OxfordPets": "a photo of a {}, a type of pet.",
|
||||
"OxfordFlowers": "a photo of a {}, a type of flower.",
|
||||
"FGVCAircraft": "a photo of a {}, a type of aircraft.",
|
||||
"DescribableTextures": "{} texture.",
|
||||
"EuroSAT": "a centered satellite photo of {}.",
|
||||
"StanfordCars": "a photo of a {}.",
|
||||
"Food101": "a photo of {}, a type of food.",
|
||||
"SUN397": "a photo of a {}.",
|
||||
"Caltech101": "a photo of a {}.",
|
||||
"UCF101": "a photo of a person doing {}.",
|
||||
"ImageNet": "a photo of a {}.",
|
||||
"ImageNetSketch": "a photo of a {}.",
|
||||
"ImageNetV2": "a photo of a {}.",
|
||||
"ImageNetA": "a photo of a {}.",
|
||||
"ImageNetR": "a photo of a {}.",
|
||||
}
|
||||
|
||||
|
||||
@TRAINER_REGISTRY.register()
|
||||
class ZeroshotCLIP(TrainerX):
|
||||
def build_model(self):
|
||||
cfg = self.cfg
|
||||
classnames = self.dm.dataset.classnames
|
||||
|
||||
print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})")
|
||||
clip_model = load_clip_to_cpu(cfg)
|
||||
clip_model.to(self.device)
|
||||
|
||||
temp = CUSTOM_TEMPLATES[cfg.DATASET.NAME]
|
||||
prompts = [temp.format(c.replace("_", " ")) for c in classnames]
|
||||
print(f"Prompts: {prompts}")
|
||||
prompts = torch.cat([clip.tokenize(p) for p in prompts])
|
||||
prompts = prompts.to(self.device)
|
||||
|
||||
with torch.no_grad():
|
||||
text_features = clip_model.encode_text(prompts)
|
||||
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
||||
|
||||
self.text_features = text_features
|
||||
self.clip_model = clip_model
|
||||
|
||||
def model_inference(self, image):
|
||||
image_features = self.clip_model.encode_image(image)
|
||||
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
||||
logit_scale = self.clip_model.logit_scale.exp()
|
||||
logits = logit_scale * image_features @ self.text_features.t()
|
||||
return logits
|
||||
|
||||
|
||||
@TRAINER_REGISTRY.register()
|
||||
class ZeroshotCLIP2(ZeroshotCLIP):
|
||||
"""Prompt ensembling."""
|
||||
|
||||
# templates = IMAGENET_TEMPLATES
|
||||
templates = IMAGENET_TEMPLATES_SELECT
|
||||
|
||||
def build_model(self):
|
||||
cfg = self.cfg
|
||||
classnames = self.dm.dataset.classnames
|
||||
|
||||
print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})")
|
||||
clip_model = load_clip_to_cpu(cfg)
|
||||
clip_model.to(self.device)
|
||||
|
||||
for params in clip_model.parameters():
|
||||
params.requires_grad_(False)
|
||||
|
||||
# add custom-made prompt
|
||||
if cfg.DATASET.NAME != "ImageNet":
|
||||
self.templates += [CUSTOM_TEMPLATES[cfg.DATASET.NAME]]
|
||||
|
||||
num_temp = len(self.templates)
|
||||
print(f"Prompt ensembling (n={num_temp})")
|
||||
|
||||
mean_text_features = 0
|
||||
for i, temp in enumerate(self.templates):
|
||||
prompts = [temp.format(c.replace("_", " ")) for c in classnames]
|
||||
prompts = torch.cat([clip.tokenize(p) for p in prompts]).to(self.device)
|
||||
text_features = clip_model.encode_text(prompts)
|
||||
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
||||
mean_text_features = mean_text_features + text_features
|
||||
mean_text_features = mean_text_features / num_temp
|
||||
mean_text_features = mean_text_features / mean_text_features.norm(dim=-1, keepdim=True)
|
||||
|
||||
self.text_features = mean_text_features
|
||||
self.clip_model = clip_model
|
||||
Reference in New Issue
Block a user