Views
No views yet
1# %%
2from transformers import MllamaForConditionalGeneration, AutoProcessor
3from peft import PeftModel
4import torch
5from PIL import Image
6
7# Load base model
8base_model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct"
9model = MllamaForConditionalGeneration.from_pretrained(
10 base_model_name, torch_dtype=torch.bfloat16, device_map="auto"
11)
12processor = AutoProcessor.from_pretrained(base_model_name)
13
14# Load LoRA adapter
15adapter_path = "DermaVLM/DermatoLLama-full"
16model = PeftModel.from_pretrained(model, adapter_path)
17# %%
18# Load image using Pillow
19image_path = rf"IMAGE_LOCATION" # Replace with your image path
20image = Image.open(image_path)
21
22prompt_text = "Analyze the dermatological condition shown in the image and provide a detailed report including body location."
23messages = []
24content_list = []
25
26# Add the image to the content
27if image:
28 content_list.append({"type": "image"})
29
30# Add the text part of the prompt
31content_list.append({"type": "text", "text": prompt_text})
32messages.append({"role": "user", "content": content_list})
33
34input_text = processor.apply_chat_template(
35 messages,
36 add_generation_prompt=True,
37 tokenize=False,
38)
39
40# Prepare final inputs with the loaded image
41inputs = processor(
42 images=image,
43 text=input_text,
44 add_special_tokens=False,
45 return_tensors="pt",
46).to(model.device)
47
48generation_config = {
49 "max_new_tokens": 512, # be careful with this, it can cause very long inference times
50 "do_sample": True,
51 "temperature": 0.4,
52 "top_p": 0.95,
53}
54
55input_length = inputs.input_ids.shape[1]
56
57print(f"Processing image: {image_path}")
58print(f"Image size: {image.size}")
59print("Generating response...")
60
61with torch.no_grad():
62 outputs = model.generate(
63 **inputs,
64 **generation_config,
65 pad_token_id=(
66 processor.tokenizer.pad_token_id
67 if processor.tokenizer.pad_token_id is not None
68 else processor.tokenizer.eos_token_id
69 ),
70 )
71 generated_tokens = outputs[0][input_length:]
72 raw_output = processor.decode(generated_tokens, skip_special_tokens=True)
73
74print("\n" + "="*50)
75print("DERMATOLOGY ANALYSIS:")
76print("="*50)
77print(raw_output)
78print("="*50)1@article {Yilmaz2025-DermatoLlama-VLM,
2 author = {Yilmaz, Abdurrahim and Yuceyalcin, Furkan and Varol, Rahmetullah and Gokyayla, Ece and Erdem, Ozan and Choi, Donghee and Demircali, Ali Anil and Gencoglan, Gulsum and Posma, Joram M. and Temelkuran, Burak},
3 title = {Resource-efficient medical vision language model for dermatology via a synthetic data generation framework},
4 year = {2025},
5 doi = {10.1101/2025.05.17.25327785},
6 url = {https://www.medrxiv.org/content/early/2025/07/30/2025.05.17.25327785},
7 journal = {medRxiv}
8}