File size: 7,876 Bytes
a942e46 eb70165 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | ---
viewer: false
tags:
- uv-script
- training
- vlm
- unsloth
- iconclass
- fine-tuning
---
# VLM Training with Unsloth
Fine-tune Vision-Language Models efficiently using [Unsloth](https://github.com/unslothai/unsloth) - get 2x faster training with lower memory usage!
## π¨ Example: Iconclass VLM
This directory contains scripts for fine-tuning VLMs to generate [Iconclass](https://iconclass.org) metadata codes from artwork images. Iconclass is a hierarchical classification system used in art history and cultural heritage.
### What You'll Train
Given an artwork image, the model outputs structured JSON:
```json
{
"iconclass-codes": ["25H213", "25H216", "25I"]
}
```
Where codes represent:
- `25H213`: river
- `25H216`: waterfall
- `25I`: city-view with man-made constructions
## π Quick Start
### Option 1: Run on HF Jobs (Recommended)
```bash
# Set your HF token
export HF_TOKEN=your_token_here
# Submit training job
python submit_training_job.py
```
That's it! Your model will train on cloud GPUs and automatically push to the Hub.
### Option 2: Run Locally (Requires GPU)
```bash
# Install UV (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Run training
uv run iconclass-vlm-sft.py \
--base-model Qwen/Qwen3-VL-8B-Instruct \
--dataset davanstrien/iconclass-vlm-sft \
--output-model your-username/iconclass-vlm
```
### Option 3: Quick Test (100 steps)
```bash
uv run iconclass-vlm-sft.py \
--base-model Qwen/Qwen3-VL-8B-Instruct \
--dataset davanstrien/iconclass-vlm-sft \
--output-model your-username/iconclass-vlm-test \
--max-steps 100
```
## π Requirements
### For HF Jobs
- Hugging Face account with Jobs access
- HF token with write permissions
### For Local Training
- CUDA-capable GPU (A100 recommended, A10G works)
- 40GB+ VRAM for 8B models (with 4-bit quantization)
- Python 3.11+
- [UV](https://docs.astral.sh/uv/) installed
## ποΈ Configuration
### Quick Config via Python Script
Edit `submit_training_job.py`:
```python
# Model and dataset
BASE_MODEL = "Qwen/Qwen3-VL-8B-Instruct"
DATASET = "davanstrien/iconclass-vlm-sft"
OUTPUT_MODEL = "your-username/iconclass-vlm"
# Training settings
BATCH_SIZE = 2
GRADIENT_ACCUMULATION = 8
LEARNING_RATE = 2e-5
MAX_STEPS = None # Auto-calculate for 1 epoch
# LoRA settings
LORA_R = 16
LORA_ALPHA = 32
# GPU
GPU_FLAVOR = "a100-large" # or "a100", "a10g-large"
```
### Full CLI Options
```bash
uv run iconclass-vlm-sft.py --help
```
Key arguments:
| Argument | Default | Description |
|----------|---------|-------------|
| `--base-model` | Required | Base VLM (e.g., Qwen/Qwen3-VL-8B-Instruct) |
| `--dataset` | Required | Training dataset on HF Hub |
| `--output-model` | Required | Where to push your model |
| `--lora-r` | 16 | LoRA rank (higher = more capacity) |
| `--lora-alpha` | 32 | LoRA alpha (usually 2Γr) |
| `--learning-rate` | 2e-5 | Learning rate |
| `--batch-size` | 2 | Per-device batch size |
| `--gradient-accumulation` | 8 | Gradient accumulation steps |
| `--max-steps` | Auto | Total training steps |
| `--num-epochs` | 1.0 | Epochs (if max-steps not set) |
## ποΈ Architecture
### What Makes This Fast?
1. **Unsloth Optimizations**: 2x faster training through:
- Optimized CUDA kernels
- Better memory management
- Efficient gradient checkpointing
2. **4-bit Quantization**:
- Loads model in 4-bit precision
- Dramatically reduces VRAM usage
- Minimal impact on quality with LoRA
3. **LoRA (Low-Rank Adaptation)**:
- Only trains 0.1-1% of parameters
- Much faster than full fine-tuning
- Easy to merge back or share
### Training Flow
```
Dataset (HF Hub)
β
FastVisionModel.from_pretrained (4-bit)
β
Apply LoRA adapters
β
SFTTrainer (Unsloth-optimized)
β
Push to Hub with model card
```
## π Expected Performance
### Training Time (Qwen3-VL-8B on A100)
| Dataset Size | Batch Config | Time | Cost (est.) |
|--------------|--------------|------|-------------|
| 44K samples | BS=2, GA=8 | ~4h | $16 |
| 10K samples | BS=2, GA=8 | ~1h | $4 |
| 1K samples | BS=2, GA=8 | ~10min | $0.70 |
*BS = Batch Size, GA = Gradient Accumulation*
### GPU Requirements
| Model Size | Min GPU | Recommended | VRAM Usage |
|------------|---------|-------------|------------|
| 3B-4B | A10G | A100 | ~20GB |
| 7B-8B | A100 | A100 | ~35GB |
| 13B+ | A100 (80GB) | A100 (80GB) | ~60GB |
## π Monitoring Your Job
### Via CLI
```bash
# Check status
hfjobs status your-job-id
# Stream logs
hfjobs logs your-job-id --follow
# List all jobs
hfjobs list
```
### Via Python
```python
from huggingface_hub import HfApi
api = HfApi()
job = api.get_job("your-job-id")
print(job.status)
print(job.logs())
```
### Via Web
Your job URL: `https://huggingface.co/jobs/your-username/your-job-id`
## π― Using Your Fine-Tuned Model
```python
from unsloth import FastVisionModel
from PIL import Image
# Load your model
model, tokenizer = FastVisionModel.from_pretrained(
model_name="your-username/iconclass-vlm",
load_in_4bit=True,
max_seq_length=2048,
)
FastVisionModel.for_inference(model)
# Prepare input
image = Image.open("artwork.jpg")
prompt = "Extract ICONCLASS labels for this image."
messages = [
{
"role": "user",
"content": [
{"type": "image"},
{"type": "text", "text": prompt},
],
}
]
# Apply chat template
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt",
).to("cuda")
# Generate
outputs = model.generate(
**inputs,
max_new_tokens=256,
temperature=0.7,
top_p=0.9,
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)
# {"iconclass-codes": ["31A235", "31A24(+1)", "61B(+54)"]}
```
## π¦ Files in This Directory
| File | Purpose |
|------|---------|
| `iconclass-vlm-sft.py` | Main training script (UV script) |
| `submit_training_job.py` | Helper to submit HF Jobs |
| `README.md` | This file |
## π οΈ Troubleshooting
### Out of Memory?
Reduce batch size or increase gradient accumulation:
```bash
--batch-size 1 --gradient-accumulation 16
```
### Training Too Slow?
Increase batch size if you have VRAM:
```bash
--batch-size 4 --gradient-accumulation 4
```
### Model Not Learning?
Try adjusting learning rate:
```bash
--learning-rate 5e-5 # Higher
--learning-rate 1e-5 # Lower
```
Or increase LoRA rank:
```bash
--lora-r 32 --lora-alpha 64
```
### Jobs Failing?
Check logs:
```bash
hfjobs logs your-job-id
```
Common issues:
- HF_TOKEN not set correctly
- Output model repo doesn't exist (create it first)
- GPU out of memory (reduce batch size)
## π Related Resources
- **Unsloth**: https://github.com/unslothai/unsloth
- **Unsloth Docs**: https://docs.unsloth.ai/
- **TRL**: https://github.com/huggingface/trl
- **HF Jobs**: https://huggingface.co/docs/hub/spaces-sdks-jobs
- **UV**: https://docs.astral.sh/uv/
- **Iconclass**: https://iconclass.org
- **Blog Post**: https://danielvanstrien.xyz/posts/2025/iconclass-vlm-sft/
## π‘ Tips
1. **Start Small**: Test with `--max-steps 100` before full training
2. **Use Wandb**: Add `--report-to wandb` for better monitoring
3. **Save Often**: Use `--save-steps 50` for checkpoints
4. **Multiple GPUs**: Script automatically uses all available GPUs
5. **Resume Training**: Load from checkpoint with `--resume-from-checkpoint`
## π Citation
If you use this training setup, please cite:
```bibtex
@misc{iconclass-vlm-training,
author = {Daniel van Strien},
title = {Efficient VLM Fine-tuning with Unsloth for Art History},
year = {2025},
publisher = {GitHub},
howpublished = {\url{https://github.com/davanstrien/uv-scripts}}
}
```
---
Made with π¦₯ [Unsloth](https://github.com/unslothai/unsloth) β’
Powered by π€ [UV Scripts](https://huggingface.co/uv-scripts)
|