Update README.md
Browse files
README.md
CHANGED
|
@@ -28,25 +28,32 @@ GPU: 1xRTX4090 <br>
|
|
| 28 |
## Usage
|
| 29 |
|
| 30 |
```python
|
|
|
|
| 31 |
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
| 32 |
from peft import PeftModel
|
|
|
|
| 33 |
from PIL import Image
|
| 34 |
|
| 35 |
# Load base model
|
| 36 |
-
base_model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
| 37 |
-
model = MllamaForConditionalGeneration.from_pretrained(
|
|
|
|
|
|
|
| 38 |
processor = AutoProcessor.from_pretrained(base_model_name)
|
| 39 |
|
| 40 |
# Load LoRA adapter
|
| 41 |
-
adapter_path = "DermaVLM/DermatoLLama-
|
| 42 |
model = PeftModel.from_pretrained(model, adapter_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
image_path = "DERM12345.jpg"
|
| 46 |
-
image = Image.open(image_path).convert("RGB")
|
| 47 |
-
prompt_text = "Describe the image in detail."
|
| 48 |
messages = []
|
| 49 |
content_list = []
|
|
|
|
|
|
|
| 50 |
if image:
|
| 51 |
content_list.append({"type": "image"})
|
| 52 |
|
|
@@ -60,7 +67,7 @@ input_text = processor.apply_chat_template(
|
|
| 60 |
tokenize=False,
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Prepare final inputs
|
| 64 |
inputs = processor(
|
| 65 |
images=image,
|
| 66 |
text=input_text,
|
|
@@ -69,7 +76,7 @@ inputs = processor(
|
|
| 69 |
).to(model.device)
|
| 70 |
|
| 71 |
generation_config = {
|
| 72 |
-
"max_new_tokens":
|
| 73 |
"do_sample": True,
|
| 74 |
"temperature": 0.4,
|
| 75 |
"top_p": 0.95,
|
|
@@ -77,6 +84,10 @@ generation_config = {
|
|
| 77 |
|
| 78 |
input_length = inputs.input_ids.shape[1]
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
with torch.no_grad():
|
| 81 |
outputs = model.generate(
|
| 82 |
**inputs,
|
|
@@ -90,7 +101,11 @@ with torch.no_grad():
|
|
| 90 |
generated_tokens = outputs[0][input_length:]
|
| 91 |
raw_output = processor.decode(generated_tokens, skip_special_tokens=True)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
print(raw_output)
|
|
|
|
| 94 |
```
|
| 95 |
|
| 96 |
## Citation
|
|
|
|
| 28 |
## Usage
|
| 29 |
|
| 30 |
```python
|
| 31 |
+
# %%
|
| 32 |
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
| 33 |
from peft import PeftModel
|
| 34 |
+
import torch
|
| 35 |
from PIL import Image
|
| 36 |
|
| 37 |
# Load base model
|
| 38 |
+
base_model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
| 39 |
+
model = MllamaForConditionalGeneration.from_pretrained(
|
| 40 |
+
base_model_name, torch_dtype=torch.bfloat16, device_map="auto"
|
| 41 |
+
)
|
| 42 |
processor = AutoProcessor.from_pretrained(base_model_name)
|
| 43 |
|
| 44 |
# Load LoRA adapter
|
| 45 |
+
adapter_path = "DermaVLM/DermatoLLama-full"
|
| 46 |
model = PeftModel.from_pretrained(model, adapter_path)
|
| 47 |
+
# %%
|
| 48 |
+
# Load image using Pillow
|
| 49 |
+
image_path = rf"IMAGE_LOCATION" # Replace with your image path
|
| 50 |
+
image = Image.open(image_path)
|
| 51 |
|
| 52 |
+
prompt_text = "Analyze the dermatological condition shown in the image and provide a detailed report including body location."
|
|
|
|
|
|
|
|
|
|
| 53 |
messages = []
|
| 54 |
content_list = []
|
| 55 |
+
|
| 56 |
+
# Add the image to the content
|
| 57 |
if image:
|
| 58 |
content_list.append({"type": "image"})
|
| 59 |
|
|
|
|
| 67 |
tokenize=False,
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Prepare final inputs with the loaded image
|
| 71 |
inputs = processor(
|
| 72 |
images=image,
|
| 73 |
text=input_text,
|
|
|
|
| 76 |
).to(model.device)
|
| 77 |
|
| 78 |
generation_config = {
|
| 79 |
+
"max_new_tokens": 512, # be careful with this, it can cause very long inference times
|
| 80 |
"do_sample": True,
|
| 81 |
"temperature": 0.4,
|
| 82 |
"top_p": 0.95,
|
|
|
|
| 84 |
|
| 85 |
input_length = inputs.input_ids.shape[1]
|
| 86 |
|
| 87 |
+
print(f"Processing image: {image_path}")
|
| 88 |
+
print(f"Image size: {image.size}")
|
| 89 |
+
print("Generating response...")
|
| 90 |
+
|
| 91 |
with torch.no_grad():
|
| 92 |
outputs = model.generate(
|
| 93 |
**inputs,
|
|
|
|
| 101 |
generated_tokens = outputs[0][input_length:]
|
| 102 |
raw_output = processor.decode(generated_tokens, skip_special_tokens=True)
|
| 103 |
|
| 104 |
+
print("\n" + "="*50)
|
| 105 |
+
print("DERMATOLOGY ANALYSIS:")
|
| 106 |
+
print("="*50)
|
| 107 |
print(raw_output)
|
| 108 |
+
print("="*50)
|
| 109 |
```
|
| 110 |
|
| 111 |
## Citation
|