|
|
--- |
|
|
license: apache-2.0 |
|
|
base_model: |
|
|
- timm/ViT-SO400M-14-SigLIP |
|
|
pipeline_tag: zero-shot-image-classification |
|
|
tags: |
|
|
- causal |
|
|
- clip |
|
|
- siglip |
|
|
--- |
|
|
|
|
|
Model card for `sii-research/CausalRobot-400M` (based on SigLIP) |
|
|
|
|
|
Model Details |
|
|
|
|
|
- Model Type: Contrastive Image-Text, Zero-Shot Image Classification. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Usage |
|
|
|
|
|
```shell |
|
|
pip install open_clip_torch |
|
|
``` |
|
|
|
|
|
Download the model from [sii-research/CausalRobot-400M](https://huggingface.co/sii-research/CausalRobot-400M) |
|
|
|
|
|
```python |
|
|
import torch |
|
|
import torch.nn.functional as F |
|
|
from urllib.request import urlopen |
|
|
from PIL import Image |
|
|
from open_clip import create_model_from_pretrained, get_tokenizer # works on open-clip-torch>=2.23.0, timm>=0.9.8 |
|
|
|
|
|
model, preprocess = create_model_from_pretrained('hf-hub:timm/ViT-SO400M-14-SigLIP') |
|
|
checkpoint = torch.load(ckpt_path, map_location="cpu") |
|
|
msg = clip_model.load_state_dict("/path/to/pytorch_model.bin", strict=False) |
|
|
tokenizer = get_tokenizer('hf-hub:timm/ViT-SO400M-14-SigLIP') |
|
|
|
|
|
image = Image.open(urlopen( |
|
|
'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' |
|
|
)) |
|
|
image = preprocess(image).unsqueeze(0) |
|
|
|
|
|
labels_list = ["a dog", "a cat", "a donut", "a beignet"] |
|
|
text = tokenizer(labels_list, context_length=model.context_length) |
|
|
|
|
|
with torch.no_grad(), torch.cuda.amp.autocast(): |
|
|
image_features = model.encode_image(image) |
|
|
text_features = model.encode_text(text) |
|
|
image_features = F.normalize(image_features, dim=-1) |
|
|
text_features = F.normalize(text_features, dim=-1) |
|
|
|
|
|
text_probs = torch.sigmoid(image_features @ text_features.T * model.logit_scale.exp() + model.logit_bias) |
|
|
|
|
|
zipped_list = list(zip(labels_list, [round(p.item(), 3) for p in text_probs[0]])) |
|
|
print("Label probabilities: ", zipped_list) |
|
|
|
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|