Fine-tune Llama 3.2 11B Vision Instruct on a radiology image-caption dataset using Unsloth, 4-bit quantization, and LoRA.
The notebook covers the following workflow:
- Loads the Llama 3.2 11B Vision model in 4-bit mode.
- Adds trainable LoRA adapters to vision and language components.
- Loads the
unsloth/Radiology_minidataset. - Converts each image-caption sample into a multimodal chat conversation.
- Runs inference before fine-tuning.
- Fine-tunes the model with supervised fine-tuning (SFT).
- Runs inference after fine-tuning.
- Saves the trained LoRA adapter and tokenizer locally.
Llama3_2_(11B)_Vision.ipynb
- Python notebook environment (e.g., Google Colab)
- CUDA-compatible NVIDIA GPU
- Sufficient GPU memory for an 11B vision-language model in 4-bit mode
- Internet access for installing packages and downloading the model/dataset
The notebook installs the main dependencies automatically:
unsloth
unsloth_zoo
transformers==4.56.2
trl==0.22.2
datasets==4.3.0
bitsandbytes
accelerate
peft
xformers
sentencepiece
protobuf
- Base model:
unsloth/Llama-3.2-11B-Vision-Instruct
The model is loaded using 4-bit quantization to significantly reduce GPU memory usage:
model, tokenizer = FastVisionModel.from_pretrained(
"unsloth/Llama-3.2-11B-Vision-Instruct",
load_in_4bit=True,
use_gradient_checkpointing="unsloth",
)LoRA adapters are applied to both the visual and language components:
model = FastVisionModel.get_peft_model(
model,
finetune_vision_layers=True,
finetune_language_layers=True,
finetune_attention_modules=True,
finetune_mlp_modules=True,
r=16,
lora_alpha=16,
lora_dropout=0,
bias="none",
random_state=3407,
use_rslora=False,
)The notebook utilizes the unsloth/Radiology_mini dataset and loads the training split:
dataset = load_dataset("unsloth/Radiology_mini", split="train")Each sample contains:
image: A radiology imagecaption: The expected image description
Each dataset sample is structured into a multimodal conversation format:
[
{
"role": "user",
"content": [
{
"type": "text",
"text": "You are an expert radiographer. Describe accurately what you see in this image."
},
{
"type": "image",
"image": sample["image"]
}
]
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": sample["caption"]
}
]
}
]
The notebook uses SFTTrainer with the following hyperparameters:
| Parameter | Value |
|---|---|
| Per-device batch size | 2 |
| Gradient accumulation steps | 4 |
| Effective batch size | 8 |
| Training steps | 30 |
| Warmup steps | 5 |
| Learning rate | 2e-4 |
| Optimizer | adamw_8bit |
| Weight decay | 0.001 |
| Scheduler | Linear |
| Maximum sequence length | 2048 |
| Random seed | 3407 |
| Output directory | outputs |
Training is initiated with:
trainer_stats = trainer.train()- Open
Llama3_2_(11B)_Vision.ipynbin a GPU-enabled notebook environment. - Select a CUDA-compatible GPU runtime.
- Run the installation cell.
- Run the remaining cells in order.
- Review the generated description before training.
- Run the training cell.
- Review the generated description after training.
- Save or download the generated
llama_loradirectory.
Inference is performed using a radiology image and the instruction prompt:
"You are an expert radiographer. Describe accurately what you see in this image."
model.generate(
**inputs,
streamer=text_streamer,
max_new_tokens=128,
use_cache=True,
temperature=1.5,
min_p=0.1,
)Note: The same dataset sample is used for both the pre-training and post-training demonstrations.
The trained LoRA adapter and tokenizer are saved locally:
model.save_pretrained("llama_lora")
tokenizer.save_pretrained("llama_lora")This saves the adapter weights rather than a fully merged standalone model in the directory llama_lora/.
To load the adapter in a new session:
from unsloth import FastVisionModel
model, tokenizer = FastVisionModel.from_pretrained(
model_name="llama_lora",
load_in_4bit=True,
)
FastVisionModel.for_inference(model)(Note: Change the block condition from if False: or move the code outside that block when running in a new session.)
The notebook reports the following metrics:
- GPU name & Total GPU memory
- Reserved memory before training
- Training runtime
- Peak reserved memory
- Approximate memory used by LoRA training
- Percentage of total GPU memory used
- Demonstration only: Training runs for only 30 steps.
- No validation: No validation/test splits or quantitative evaluation metrics are used.
- Data leakage: Inference demonstration uses an image from the training set.
- Non-medical grade: Generated output must not be treated as a medical diagnosis.
- Adapters only: Saves LoRA adapter weights, not a merged full model.
This project is for experimentation and research purposes only. It is not a medical device and must not be used as a substitute for professional evaluation by a qualified healthcare professional.