Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix open-clip-torch model inference #5395

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions fiftyone/utils/open_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| `voxel51.com <https://voxel51.com/>`_
|
"""
import contextlib
import logging

import fiftyone.core.models as fom
Expand Down Expand Up @@ -57,6 +58,7 @@ class TorchOpenClipModel(fout.TorchImageModel, fom.PromptMixin):
def __init__(self, config):
super().__init__(config)
self._text_features = None
self.preprocess = self._preprocess_aux

@property
def can_embed_prompts(self):
Expand Down Expand Up @@ -88,7 +90,7 @@ def _load_model(self, config):
(
self._model,
_,
self.preprocess,
self._preprocess_aux,
) = open_clip.create_model_and_transforms(
config.clip_model,
pretrained=config.pretrained,
Expand Down Expand Up @@ -134,20 +136,23 @@ def _get_class_logits(self, text_features, image_features):

def _predict_all(self, imgs):
if self._preprocess:
imgs = [self._preprocess(img).unsqueeze(0) for img in imgs]
imgs = [self._preprocess(img) for img in imgs]

if isinstance(imgs, (list, tuple)):
imgs = torch.stack(imgs)

height, width = imgs.size()[-2:]
frame_size = (width, height)

if self._using_gpu:
imgs = imgs.to(self.device)
with torch.no_grad(), contextlib.ExitStack() as ctx:
if self._using_gpu:
imgs = imgs.to(self.device)

# https://github.com/voxel51/fiftyone/pull/5395#issuecomment-2601055784
ctx.enter_context(
torch.amp.autocast(device_type=self.device.type)
)

with torch.no_grad(), torch.amp.autocast(
device_type=self.device.type if self._using_gpu else "cpu"
):
image_features = self._model.encode_image(imgs)
text_features = self._get_text_features()

Expand Down
Loading