Skip to content

How do I replace the evaclip? #55

@zxqing01

Description

@zxqing01

If I want to use your model to replace evaclip, how should the following code be modified?

`
elif clip_pretrained=="EVA02-CLIP-B-16" or clip_pretrained=="EVA02-CLIP-L-14-336":
clip_model = eva_clip.create_model(model_name=clip_pretrained,
pretrained=cache_dir,
force_custom_clip=True,
precision="amp",
device=device)
self.tokenizer = open_clip.get_tokenizer(clip_pretrained)
clip_preprocess=None

def get_text_embeds(self, classnames, templates, clip_model, prompt=None):
if self.cache is not None and not self.training:
return self.cache

    if self.tokens is None or prompt is not None:
        tokens = []
        for classname in classnames:
            if ', ' in classname:
                classname_splits = classname.split(', ')
                texts = [template.format(classname_splits[0]) for template in templates]
            else:
                texts = [template.format(classname) for template in templates]  # format with class
            if self.tokenizer is not None:
                texts = self.tokenizer(texts).cuda()
            else: 
                texts = clip.tokenize(texts).cuda()
            tokens.append(texts)
        tokens = torch.stack(tokens, dim=0).squeeze(1)
        if prompt is None:
            self.tokens = tokens
    elif self.tokens is not None and prompt is None:
        tokens = self.tokens

    class_embeddings = clip_model.encode_text(tokens, prompt)
    class_embeddings = class_embeddings / class_embeddings.norm(dim=-1, keepdim=True)
    
    
    class_embeddings = class_embeddings.unsqueeze(1)
    
    if not self.training:
        self.cache = class_embeddings
        
    return class_embeddings`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions