Views
No views yet
1conda env create -f environment.yml
2conda activate dragonfly_envpip install flash-attn --no-build-isolationpip install --upgrade -e .
1import torch
2from PIL import Image
3from transformers import AutoProcessor, AutoTokenizer
4
5from dragonfly.models.modeling_dragonfly import DragonflyForCausalLM
6from dragonfly.models.processing_dragonfly import DragonflyProcessor
7from pipeline.train.train_utils import random_seed1device = torch.device("cuda:0")
2
3tokenizer = AutoTokenizer.from_pretrained("togethercomputer/Llama-3.1-8B-Dragonfly-v2")
4clip_processor = AutoProcessor.from_pretrained("openai/clip-vit-large-patch14-336")
5image_processor = clip_processor.image_processor
6processor = DragonflyProcessor(image_processor=image_processor, tokenizer=tokenizer, image_encoding_style="llava-hd")
7
8model = DragonflyForCausalLM.from_pretrained("togethercomputer/Llama-3.1-8B-Dragonfly-v2")
9model = model.to(torch.bfloat16)
10model = model.to(device)1image = Image.open("./test_images/skateboard.png")
2image = image.convert("RGB")
3images = [image]
4# images = [None] # if you do not want to pass any images
5
6text_prompt = "<|start_header_id|>user<|end_header_id|>\n\nWhat is so funny about this image?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
7
8inputs = processor(text=[text_prompt], images=images, max_length=4096, return_tensors="pt", is_generate=True)
9inputs = inputs.to(device)1temperature = 0
2
3with torch.inference_mode():
4 generation_output = model.generate(**inputs, max_new_tokens=1024, eos_token_id=tokenizer.encode("<|eot_id|>"), do_sample=temperature > 0, temperature=temperature, use_cache=True)
5
6generation_text = processor.batch_decode(generation_output, skip_special_tokens=False)1The humor in this image comes from the surreal juxtaposition of a dog's face with the body of the Mona Lisa, a famous painting by Leonardo da Vinci.
2The Mona Lisa is known for her enigmatic smile and is often considered one of the most famous paintings in the world. By combining the dog's face with
3the body of the Mona Lisa, the artist has created a whimsical and amusing image that plays on the viewer 's expectations and familiarity with the
4original paintings. The contrast between the dog's natural, expressive features and the serene, mysterious expression of the Mona Lisa creates a
5humerous effect that is likely to elicit laughter<|eot_id|>1@misc{thapa2024dragonfly,
2 title={Dragonfly: Multi-Resolution Zoom-In Encoding Enhances Vision-Language Models},
3 author={Rahul Thapa and Kezhen Chen and Ian Covert and Rahul Chalamala and Ben Athiwaratkun and Shuaiwen Leon Song and James Zou},
4 year={2024},
5 eprint={2406.00977},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV}
8}