MCPcopy Create free account
hub / github.com/Yuan-Li-FNLP/R3-RAG / encode_image

Method encode_image

tool/FlashRAG/flashrag/retriever/encoder.py:207–221  ·  view source on GitHub ↗
(self, image_list: List)

Source from the content-addressed store, hash-verified

205
206 @torch.inference_mode()
207 def encode_image(self, image_list: List) -> np.ndarray:
208 # Each item in image_list: PIL Image, local path, or URL
209 if self.model_type == "CLIPModel" or self.model_type == 'ChineseCLIPModel':
210 # need handle image
211 image_list = [parse_image(image) for image in image_list]
212 inputs = self.processor(images=image_list, return_tensors="pt")
213 inputs = {k: v.cuda() for k, v in inputs.items()}
214 image_emb = self.model.get_image_features(**inputs)
215 image_emb = image_emb / image_emb.norm(p=2, dim=-1, keepdim=True)
216 image_emb = image_emb.detach().cpu().numpy().astype(np.float32)
217 elif self.model_type.endswith("CLIPModel"):
218 image_emb = self.model.encode_image(image_list)
219 else:
220 raise NotImplementedError(f"Unsupported model type: {self.model_type}")
221 return image_emb
222
223 @torch.inference_mode()
224 def encode_text(self, text_list: List[str]) -> np.ndarray:

Callers

nothing calls this directly

Calls 1

parse_imageFunction · 0.90

Tested by

no test coverage detected