Views
No views yet



1from PIL import Image
2import requests
3from transformers import CLIPProcessor, AutoModel, CLIPTokenizer
4
5model_path = "laion/openMaMMUT-ViT-L-14-DataComp-1.4B-s12.8B-b180K"
6tokenizer = CLIPTokenizer.from_pretrained(model_path)
7
8model = AutoModel.from_pretrained(model_path, trust_remote_code=True)
9processor = CLIPProcessor.from_pretrained(model_path)
10
11url = "http://images.cocodataset.org/val2017/000000039769.jpg"
12image = Image.open(requests.get(url, stream=True).raw)
13
14# Image captioning
15inputs = processor(images=image, return_tensors="pt", padding=True)
16outputs = model.generate(pixel_values=inputs["pixel_values"], top_p=0.1, do_sample=True).sequences
17decoded_outputs = tokenizer.batch_decode(outputs)
18print("HuggingFace outputs:", decoded_outputs) # prints: ['<|startoftext|>cats on couch']
19
20# Get image-text similarity (just like CLIP)
21text = ["a photo of a cat", "a photo of a dog"]
22inputs = processor(images=image, text=text, return_tensors="pt", padding=True)
23
24outputs = model(pixel_values=inputs["pixel_values"], input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], contrastive_only=True)
25
26logits_per_image = outputs.logits_per_image # this is the image-text similarity score
27probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
28
29print("Label probabilities:", probs)
30print("Logits per image:", logits_per_image)
31
32# Compute image and text embeddings separately
33
34text_features = model.get_text_features(inputs["input_ids"], inputs["attention_mask"])
35image_features = model.get_image_features(inputs["pixel_values"])
36
37print("Text features shape:", text_features.shape) # prints: [batch_size, feature_dim]
38print("Image features shape:", image_features.shape) # prints: [batch_size, feature_dim]
39text_features /= text_features.norm(dim=-1, keepdim=True)
40image_features /= image_features.norm(dim=-1, keepdim=True)
41text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
42print("Label probs:", text_probs) # prints: [[1., 0.]] or similar, depending on the image and text1git clone https://github.com/LAION-AI/open_clip_mammut
2cd open_clip_mammut
3python -m pip install .1import torch
2from PIL import Image
3import open_clip
4
5model, _, transform = open_clip.create_model_and_transforms('hf-hub:laion/openMaMMUT-ViT-L-14-DataComp-1.4B-s12.8B-b180K')
6model.eval() # model in train mode by default, impacts some models with BatchNorm or stochastic depth active
7tokenizer = open_clip.get_tokenizer('hf-hub:laion/openMaMMUT-ViT-L-14-DataComp-1.4B-s12.8B-b180K')
8
9image = transform(Image.open("docs/CLIP.png")).unsqueeze(0)
10text = tokenizer(["a diagram", "a dog", "a cat"])
11
12with torch.no_grad(), torch.amp.autocast('cuda'):
13 image_features = model.encode_image(image)
14 text_features = model.encode_text(text)
15 image_features /= image_features.norm(dim=-1, keepdim=True)
16 text_features /= text_features.norm(dim=-1, keepdim=True)
17
18 text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
19
20print("Label probs:", text_probs) # prints: [[1., 0., 0.]]
211import open_clip
2import torch
3from PIL import Image
4
5model, _, transform = open_clip.create_model_and_transforms('hf-hub:laion/openMaMMUT-ViT-L-14-DataComp-1.4B-s12.8B-b180K')
6
7im = Image.open("docs/CLIP.png").convert("RGB")
8im = transform(im).unsqueeze(0)
9
10with torch.no_grad(), torch.amp.autocast('cuda'):
11 generated = model.generate(im)
12
13print(open_clip.decode(generated[0]).split("<end_of_text>")[0].replace("<start_of_text>", ""))
14@inproceedings{
nezhurina2025scaling,
title={Scaling Laws for Robust Comparison of Open Foundation Language-Vision Models and Datasets},
author={Marianna Nezhurina and Tomer Porian and Giovanni Puccetti and Tommie Kerssies and Romain Beaumont and Mehdi Cherti and Jenia Jitsev},
booktitle={The Thirty-ninth Annual Conference on Neural Information Processing Systems},
volume={39},
year={2025},
url={https://openreview.net/forum?id=cWnZLIdeKn}
}
@article{gadre2023datacomp,
title={Datacomp: In search of the next generation of multimodal datasets},
author={Gadre, Samir Yitzhak and Ilharco, Gabriel and Fang, Alex and Hayase, Jonathan and Smyrnis, Georgios and Nguyen, Thao and Marten, Ryan and Wortsman, Mitchell and Ghosh, Dhruba and Zhang, Jieyu and others},
journal={Advances in Neural Information Processing Systems},
volume={36},
pages={27092--27112},
year={2023}
}@article{
kuo2023mammut,
title={Ma{MMUT}: A Simple Architecture for Joint Learning for MultiModal Tasks},
author={Weicheng Kuo and AJ Piergiovanni and Dahun Kim and xiyang luo and Benjamin Caine and Wei Li and Abhijit Ogale and Luowei Zhou and Andrew M. Dai and Zhifeng Chen and Claire Cui and Anelia Angelova},
journal={Transactions on Machine Learning Research},
issn={2835-8856},
year={2023},
url={https://openreview.net/forum?id=FqOG4osY7C},
}@inproceedings{Cherti2023,
title={Reproducible scaling laws for contrastive language-image learning},
author={Cherti, Mehdi and Beaumont, Romain and Wightman, Ross and Wortsman, Mitchell and Ilharco, Gabriel and Gordon, Cade and Schuhmann, Christoph and Schmidt, Ludwig and Jitsev, Jenia},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages={2818--2829},
year={2023}
}@software{ilharco_gabriel_2021_5143773,
author = {Ilharco, Gabriel and
Wortsman, Mitchell and
Wightman, Ross and
Gordon, Cade and
Carlini, Nicholas and
Taori, Rohan and
Dave, Achal and
Shankar, Vaishaal and
Namkoong, Hongseok and
Miller, John and
Hajishirzi, Hannaneh and
Farhadi, Ali and
Schmidt, Ludwig},
title = {OpenCLIP},
month = jul,
year = 2021,
note = {If you use this software, please cite it as below.},
publisher = {Zenodo},
version = {0.1},
doi = {10.5281/zenodo.5143773},
url = {https://doi.org/10.5281/zenodo.5143773}
}@software{cherti_2025_15403103,
author = {Cherti, Mehdi and
Beaumont, Romain},
title = {CLIP benchmark},
month = may,
year = 2025,
publisher = {Zenodo},
doi = {10.5281/zenodo.15403103},
url = {https://doi.org/10.5281/zenodo.15403103},
swhid = {swh:1:dir:8cf49a5dd06f59224844a1e767337a1d14ee56c2
;origin=https://doi.org/10.5281/zenodo.15403102;vi
sit=swh:1:snp:dd153b26f702d614346bf814f723d59fef3d
77a2;anchor=swh:1:rel:cff2aeb98f42583b44fdab5374e9
fa71793f2cff;path=CLIP\\_benchmark-main
},
}