Views
No views yet
config.json to load model from local repo rather than McGill-NLP/LLM2Vec-Meta-Llama-3-8B-Instruct-mntpmodeling_llama_encoder.py and added attn_mask_utils.py to work with newer versions of transformers
1from PIL import Image
2from transformers import AutoModel
3from transformers import CLIPImageProcessor
4import torch
5
6image_path = "CLIP.png"
7model_name_or_path = "LLM2CLIP-Openai-L-14-336" # or /path/to/local/LLM2CLIP-Openai-L-14-336
8
9processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-large-patch14-336")
10model = AutoModel.from_pretrained(
11 model_name_or_path,
12 torch_dtype=torch.float16,
13 trust_remote_code=True).to('cuda').eval()
14
15image = Image.open(image_path)
16input_pixels = processor(images=image, return_tensors="pt").pixel_values.to('cuda')
17
18with torch.no_grad(), torch.cuda.amp.autocast():
19 outputs = model.get_image_features(input_pixels)1from PIL import Image
2from transformers import AutoModel, AutoConfig, AutoTokenizer
3from transformers import CLIPImageProcessor
4import torch
5from llm2vec import LLM2Vec
6import os
7
8os.environ["CUDA_VISIBLE_DEVICES"] = "0"
9
10processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-large-patch14-336")
11model_name_or_path = "microsoft/LLM2CLIP-Openai-L-14-336" # or /path/to/local/LLM2CLIP-Openai-L-14-336
12model = AutoModel.from_pretrained(
13 model_name_or_path,
14 torch_dtype=torch.bfloat16,
15 trust_remote_code=True).to('cuda').eval()
16
17llm_model_name = 'microsoft/LLM2CLIP-Llama-3-8B-Instruct-CC-Finetuned'
18config = AutoConfig.from_pretrained(
19 llm_model_name, trust_remote_code=True
20)
21llm_model = AutoModel.from_pretrained(llm_model_name, torch_dtype=torch.bfloat16, config=config, trust_remote_code=True)
22tokenizer = AutoTokenizer.from_pretrained(llm_model_name)
23llm_model.config._name_or_path = 'meta-llama/Meta-Llama-3-8B-Instruct' # Workaround for LLM2VEC
24l2v = LLM2Vec(llm_model, tokenizer, pooling_mode="mean", max_length=512, doc_max_length=512)
25
26captions = ["a diagram", "a dog", "a cat"]
27image_path = "CLIP.png"
28
29image = Image.open(image_path)
30input_pixels = processor(images=image, return_tensors="pt").pixel_values.to('cuda')
31text_features = l2v.encode(captions, convert_to_tensor=True).to('cuda')
32
33with torch.no_grad(), torch.cuda.amp.autocast():
34 image_features = model.get_image_features(input_pixels)
35 text_features = model.get_text_features(text_features)
36
37 image_features /= image_features.norm(dim=-1, keepdim=True)
38 text_features /= text_features.norm(dim=-1, keepdim=True)
39
40 text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
41
42print("Label probs:", text_probs)
43@misc{huang2024llm2clippowerfullanguagemodel,
title={LLM2CLIP: Powerful Language Model Unlock Richer Visual Representation},
author={Weiquan Huang and Aoqi Wu and Yifan Yang and Xufang Luo and Yuqing Yang and Liang Hu and Qi Dai and Xiyang Dai and Dongdong Chen and Chong Luo and Lili Qiu},
year={2024},
eprint={2411.04997},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2411.04997},
}