from dataclasses import dataclass, field from typing import Tuple, Type import torch import torchvision try: import clip except ImportError: assert False, "clip is not installed, install it with `pip install clip`" from lerf.encoders.image_encoder import BaseImageEncoder, BaseImageEncoderConfig @dataclass class CLIPNetworkConfig(BaseImageEncoderConfig): _target: Type = field(default_factory=lambda: CLIPNetwork) clip_model_type: str = "ViT-B/16" clip_n_dims: int = 512 negatives: Tuple[str] = ("object", "things", "stuff", "texture") class CLIPNetwork(BaseImageEncoder): def __init__(self, config: CLIPNetworkConfig): self.config = config self.process = torchvision.transforms.Compose( [ torchvision.transforms.Resize((224, 224)), torchvision.transforms.Normalize( mean=[0.48145466, 0.4578275, 0.40821073], std=[0.26862954, 0.26130258, 0.27577711], ), ] ) model, _ = clip.load(self.config.clip_model_type) model.eval() self.tokenizer = clip.tokenize self.model = model.to("cuda") self.clip_n_dims = self.config.clip_n_dims self.positives = ["hand sanitizer"] self.negatives = self.config.negatives with torch.no_grad(): tok_phrases = torch.cat([self.tokenizer(phrase) for phrase in self.positives]).to("cuda") self.pos_embeds = model.encode_text(tok_phrases) tok_phrases = torch.cat([self.tokenizer(phrase) for phrase in self.negatives]).to("cuda") self.neg_embeds = model.encode_text(tok_phrases) self.pos_embeds /= self.pos_embeds.norm(dim=-1, keepdim=True) self.neg_embeds /= self.neg_embeds.norm(dim=-1, keepdim=True) assert ( self.pos_embeds.shape[1] == self.neg_embeds.shape[1] ), "Positive and negative embeddings must have the same dimensionality" assert ( self.pos_embeds.shape[1] == self.clip_n_dims ), "Embedding dimensionality must match the model dimensionality" @property def name(self) -> str: return "clip_openai_{}".format(self.config.clip_model_type) @property def embedding_dim(self) -> int: return self.config.clip_n_dims def set_positives(self, text_list): self.positives = text_list with torch.no_grad(): tok_phrases = torch.cat([self.tokenizer(phrase) for phrase in self.positives]).to("cuda") self.pos_embeds = self.model.encode_text(tok_phrases) self.pos_embeds /= self.pos_embeds.norm(dim=-1, keepdim=True) def get_relevancy(self, embed: torch.Tensor, positive_id: int) -> torch.Tensor: phrases_embeds = torch.cat([self.pos_embeds, self.neg_embeds], dim=0) p = phrases_embeds.to(embed.dtype) # phrases x 512 output = torch.mm(embed, p.T) # rays x phrases positive_vals = output[..., positive_id : positive_id + 1] # rays x 1 negative_vals = output[..., len(self.positives) :] # rays x N_phrase repeated_pos = positive_vals.repeat(1, len(self.negatives)) # rays x N_phrase sims = torch.stack((repeated_pos, negative_vals), dim=-1) # rays x N-phrase x 2 softmax = torch.softmax(10 * sims, dim=-1) # rays x n-phrase x 2 best_id = softmax[..., 0].argmin(dim=1) # rays x 2 return torch.gather(softmax, 1, best_id[..., None, None].expand(best_id.shape[0], len(self.negatives), 2))[ :, 0, : ] def encode_image(self, input): processed_input = self.process(input).half() return self.model.encode_image(processed_input)