MCPcopy Create free account
hub / github.com/pytorch/executorch / encode_images

Method encode_images

examples/models/llava/model.py:163–187  ·  view source on GitHub ↗
(self, images: torch.Tensor)

Source from the content-addressed store, hash-verified

161 return self.model_.language_model.embed_tokens(tokens)
162
163 def encode_images(self, images: torch.Tensor) -> torch.Tensor:
164 # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_pt_objects.LlavaForConditionalGeneration` has no attribute `dtype`.
165 images = images.to(dtype=self.model_.dtype)
166 if type(images) is list:
167 image_features = []
168 for image in images:
169 image_forward_out = self.model_.vision_tower(
170 image.to(
171 device=self.model_.device, dtype=self.model_.dtype
172 ).unsqueeze(0),
173 output_hidden_states=True,
174 )
175 image_feature = self._feature_select(image_forward_out).to(image.dtype)
176 image_features.append(image_feature)
177 else:
178 # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_pt_objects.LlavaForConditionalGeneration` has no attribute `vision_tower`.
179 image_forward_outs = self.model_.vision_tower(
180 # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_pt_objects.LlavaForConditionalGeneration` has no attribute `device`.
181 images.to(device=self.model_.device, dtype=self.model_.dtype),
182 output_hidden_states=True,
183 )
184 image_features = self._feature_select(image_forward_outs).to(images.dtype)
185 # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_pt_objects.LlavaForConditionalGeneration` has no attribute `multi_modal_projector`.
186 image_features = self.model_.multi_modal_projector(image_features)
187 return image_features
188
189 def image_preprocess(self, img: torch.Tensor) -> torch.Tensor:
190 # pyre-ignore: Undefined attribute [16]: `transformers.utils.dummy_vision_objects.CLIPImageProcessor` has no attribute `crop_size`.

Callers 1

image_embeddingMethod · 0.95

Calls 3

_feature_selectMethod · 0.95
toMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected