Score the images based on the prompt. Args: images (Union[str, List[str], Image.Image, List[Image.Image]]): Path(s) to the image(s) or PIL image(s). prompt (str): The prompt text. Returns: List[float]: List of reward scores for the images.
(self, images: Union[str, List[str], Image.Image, List[Image.Image]], prompt: str)
| 97 | |
| 98 | @torch.no_grad() |
| 99 | def score(self, images: Union[str, List[str], Image.Image, List[Image.Image]], prompt: str) -> List[float]: |
| 100 | """Score the images based on the prompt. |
| 101 | |
| 102 | Args: |
| 103 | images (Union[str, List[str], Image.Image, List[Image.Image]]): Path(s) to the image(s) or PIL image(s). |
| 104 | prompt (str): The prompt text. |
| 105 | |
| 106 | Returns: |
| 107 | List[float]: List of reward scores for the images. |
| 108 | """ |
| 109 | if isinstance(images, (str, Image.Image)): |
| 110 | # Single image |
| 111 | if isinstance(images, str): |
| 112 | image = self.image_processor(Image.open(images), return_tensors="pt")["pixel_values"].to(self.device) |
| 113 | else: |
| 114 | image = self.image_processor(images, return_tensors="pt")["pixel_values"].to(self.device) |
| 115 | return [self._calculate_score(image, prompt)] |
| 116 | elif isinstance(images, list): |
| 117 | # Multiple images |
| 118 | scores = [] |
| 119 | for one_images in images: |
| 120 | if isinstance(one_images, str): |
| 121 | image = self.image_processor(Image.open(one_images), return_tensors="pt")["pixel_values"].to(self.device) |
| 122 | elif isinstance(one_images, Image.Image): |
| 123 | image = self.image_processor(one_images, return_tensors="pt")["pixel_values"].to(self.device) |
| 124 | else: |
| 125 | raise TypeError("The type of parameter images is illegal.") |
| 126 | scores.append(self._calculate_score(image, prompt)) |
| 127 | return scores |
| 128 | else: |
| 129 | raise TypeError("The type of parameter images is illegal.") |
nothing calls this directly
no test coverage detected