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-Med-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-Med-v2")
9model = model.to(torch.bfloat16)
10model = model.to(device)1image = Image.open("ROCO_04197.jpg")
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\nProvide a brief description of the given image.<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
7
8inputs = processor(text=[text_prompt], images=images, max_length=1024, 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)Computed tomography scan showing a large heterogenous mass in the pelvis<|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}