-
Notifications
You must be signed in to change notification settings - Fork 104
[Add] transformers text and image models
#132
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||
| from torch import nn | ||||
| from transformers import AutoModel, AutoProcessor | ||||
| from functools import partial | ||||
|
|
||||
| class TransformerWrapper(nn.Module): | ||||
| def __init__(self, model): | ||||
| super().__init__() | ||||
| self.model = model | ||||
|
|
||||
| def encode_text(self, text): | ||||
| return self.model.get_text_features(**text) | ||||
|
|
||||
| def encode_image(self, image): | ||||
| return self.model.get_image_features(image["pixel_values"].squeeze(1)) | ||||
|
||||
|
|
||||
| def load_transformers_clip(model_name, pretrained, cache_dir, device): | ||||
| ckpt = f"{model_name}/{pretrained}" | ||||
|
Comment on lines
+18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
model_name = "openai"
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to choose this option for better verbosity. CLIP_benchmark/clip_benchmark/cli.py Line 247 in a230282
|
||||
| model = AutoModel.from_pretrained(ckpt, cache_dir=cache_dir, device_map=device) | ||||
| model = TransformerWrapper(model) | ||||
| processor = AutoProcessor.from_pretrained(ckpt) | ||||
|
|
||||
| transforms = partial(processor.image_processor, return_tensors="pt") | ||||
| tokenizer = partial(processor.tokenizer, return_tensors="pt", padding="max_length") | ||||
|
||||
| return model, transforms, tokenizer | ||||
Uh oh!
There was an error while loading. Please reload this page.