(self, text_list: List[str])
| 222 | |
| 223 | @torch.inference_mode() |
| 224 | def encode_text(self, text_list: List[str]) -> np.ndarray: |
| 225 | # Each item in image_list: PIL Image, local path, or URL |
| 226 | if self.model_type == "CLIPModel" or self.model_type == 'ChineseCLIPModel': |
| 227 | inputs = self.processor( |
| 228 | text=text_list, |
| 229 | padding=True, |
| 230 | truncation=True, |
| 231 | return_tensors="pt", |
| 232 | ) |
| 233 | inputs = {k: v.cuda() for k, v in inputs.items()} |
| 234 | text_emb = self.model.get_text_features(**inputs) |
| 235 | text_emb = text_emb / text_emb.norm(p=2, dim=-1, keepdim=True) |
| 236 | text_emb = text_emb.detach().cpu().numpy().astype(np.float32) |
| 237 | elif self.model_type.endswith("CLIPModel"): |
| 238 | text_emb = self.model.encode_text( |
| 239 | text_list, |
| 240 | padding=True, |
| 241 | truncation=True, |
| 242 | ) |
| 243 | else: |
| 244 | raise NotImplementedError(f"Unsupported model type: {self.model_type}") |
| 245 | return text_emb |
nothing calls this directly
no outgoing calls
no test coverage detected