MCPcopy Create free account
hub / github.com/CHENGY12/PLOT / clip_classifier

Function clip_classifier

plot-adapter/utils.py:69–86  ·  view source on GitHub ↗
(classnames, template, clip_model)

Source from the content-addressed store, hash-verified

67
68
69def clip_classifier(classnames, template, clip_model):
70 with torch.no_grad():
71 clip_weights = []
72
73 for classname in classnames:
74 # Tokenize the prompts
75 classname = classname.replace('_', ' ')
76 texts = [t.format(classname) for t in template]
77 texts = clip.tokenize(texts).cuda()
78 # prompt ensemble for ImageNet
79 class_embeddings = clip_model.encode_text(texts)
80 class_embeddings /= class_embeddings.norm(dim=-1, keepdim=True)
81 class_embedding = class_embeddings.mean(dim=0)
82 class_embedding /= class_embedding.norm()
83 clip_weights.append(class_embedding)
84
85 clip_weights = torch.stack(clip_weights, dim=1).cuda()
86 return clip_weights
87
88
89def build_cache_model(cfg, clip_model, train_loader_cache):

Callers 2

mainFunction · 0.85
mainFunction · 0.85

Calls 1

encode_textMethod · 0.45

Tested by

no test coverage detected