Spaces:
Sleeping
Sleeping
Uploading FoodExtract-Vision demo folder
Browse files
README.md
CHANGED
|
@@ -1,26 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# ๐๐ FoodExtract-Vision v1: Fine-tuned SmolVLM2-500M for Structured Food Tag Extraction
|
| 2 |
|
| 3 |
[](https://huggingface.co/berkeruveyik/FoodExtract-Vision-SmolVLM2-500M-fine-tune-v3)
|
| 4 |
[](https://huggingface.co/datasets/berkeruveyik/vlm-food-4k-not-food-dataset)
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
|
|
|
| 12 |
|
|
|
|
| 13 |
|
| 14 |
-
##
|
| 15 |
|
| 16 |
-
|
| 17 |
-
-
|
| 18 |
-
|
| 19 |
-
- `image_title` โ short food-related caption
|
| 20 |
-
- `food_items` โ list of visible food item nouns
|
| 21 |
-
- `drink_items` โ list of visible drink item nouns
|
| 22 |
|
| 23 |
-
###
|
| 24 |
|
| 25 |
```json
|
| 26 |
{
|
|
@@ -31,58 +46,130 @@
|
|
| 31 |
}
|
| 32 |
```
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
---
|
| 35 |
|
| 36 |
-
##
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
- **Parameters:** ~500M
|
| 42 |
-
- **Precision:** `bfloat16`
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
- **Size:** ~3,698 image-JSON pairs
|
| 48 |
-
- **Split:** 80% train / 20% validation
|
| 49 |
-
- **Content:**
|
| 50 |
-
- ๐ Food images from the Food270 dataset (various cuisines, ingredients, prepared dishes)
|
| 51 |
-
- ๐ผ๏ธ Non-food images (random internet images) to teach correct negative classification
|
| 52 |
|
| 53 |
-
###
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
###
|
| 58 |
|
| 59 |
-
|
| 60 |
-
- **Frozen:** Vision encoder parameters
|
| 61 |
-
- **Trainable:** LLM + connector layers
|
| 62 |
-
- **Learning Rate:** `2e-4`
|
| 63 |
-
- **Epochs:** 2
|
| 64 |
-
- **Batch Size:** 8 (with gradient accumulation of 4)
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
-
|
| 69 |
-
-
|
| 70 |
-
- **Learning Rate:** `2e-6` (much lower to prevent catastrophic forgetting)
|
| 71 |
-
- **Epochs:** 2
|
| 72 |
-
- **Batch Size:** 8 (with gradient accumulation of 4)
|
| 73 |
|
| 74 |
-
###
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
---
|
| 88 |
|
|
@@ -91,7 +178,7 @@ Inspired by the [SmolVLM Docling paper](https://arxiv.org/pdf/2503.11576), the f
|
|
| 91 |
### ๐ฆ Installation
|
| 92 |
|
| 93 |
```bash
|
| 94 |
-
pip install transformers torch gradio spaces
|
| 95 |
```
|
| 96 |
|
| 97 |
### ๐ฎ Inference with Pipeline
|
|
@@ -99,6 +186,7 @@ pip install transformers torch gradio spaces
|
|
| 99 |
```python
|
| 100 |
import torch
|
| 101 |
from transformers import pipeline
|
|
|
|
| 102 |
|
| 103 |
FINE_TUNED_MODEL_ID = "berkeruveyik/FoodExtraqt-Vision-SmoLVLM2-500M-fine-tune-v3"
|
| 104 |
|
|
@@ -113,12 +201,14 @@ prompt = """Classify the given input image into food or not and if edible food o
|
|
| 113 |
|
| 114 |
Only return valid JSON in the following form:
|
| 115 |
|
|
|
|
| 116 |
{
|
| 117 |
"is_food": 0,
|
| 118 |
"image_title": "",
|
| 119 |
"food_items": [],
|
| 120 |
"drink_items": []
|
| 121 |
}
|
|
|
|
| 122 |
"""
|
| 123 |
|
| 124 |
messages = [
|
|
@@ -185,31 +275,54 @@ print(decoded)
|
|
| 185 |
|
| 186 |
## ๐ฎ Gradio Demo
|
| 187 |
|
|
|
|
|
|
|
| 188 |
### โถ๏ธ Running Locally
|
| 189 |
|
| 190 |
```bash
|
| 191 |
-
cd demos/FoodExtract-Vision
|
|
|
|
| 192 |
python app.py
|
| 193 |
```
|
| 194 |
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
| 198 |
-
2. ๐ Compare outputs from the **base model** vs. the **fine-tuned model** side-by-side
|
| 199 |
-
3. ๐ See structured JSON extraction in real-time
|
| 200 |
|
| 201 |
---
|
| 202 |
|
| 203 |
## ๐ Project Structure
|
| 204 |
|
| 205 |
```
|
| 206 |
-
|
| 207 |
-
โโโ
|
| 208 |
-
โโโ
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
```
|
| 214 |
|
| 215 |
---
|
|
@@ -218,16 +331,27 @@ demos/FoodExtract-Vision-v1/
|
|
| 218 |
|
| 219 |
### โ
What Worked
|
| 220 |
|
| 221 |
-
- ๐๏ธ **Two-stage training** significantly improved output quality compared to single-stage
|
| 222 |
-
- ๐ง **Freezing the vision encoder first**
|
| 223 |
-
- ๐ข **
|
| 224 |
- ๐ค Even a **500M parameter model** can learn reliable structured output generation
|
|
|
|
|
|
|
| 225 |
|
| 226 |
### โ ๏ธ Important Notes
|
| 227 |
|
| 228 |
-
- **Dtype consistency:**
|
| 229 |
-
- **System prompt handling:** When not using `transformers.pipeline`, the system prompt may need to be folded into the user prompt
|
| 230 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
---
|
| 233 |
|
|
@@ -241,11 +365,14 @@ demos/FoodExtract-Vision-v1/
|
|
| 241 |
| ๐ SmolVLM Docling Paper | [arxiv.org/pdf/2503.11576](https://arxiv.org/pdf/2503.11576) |
|
| 242 |
| ๐ TRL Documentation | [huggingface.co/docs/trl](https://huggingface.co/docs/trl/main/en/index) |
|
| 243 |
| ๐ PEFT GitHub | [github.com/huggingface/peft](https://github.com/huggingface/peft) |
|
|
|
|
| 244 |
|
| 245 |
---
|
| 246 |
|
| 247 |
## ๐ License
|
| 248 |
|
| 249 |
-
Please refer to the respective model and dataset cards for licensing information.
|
|
|
|
| 250 |
---
|
| 251 |
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: FoodExtract-Vision
|
| 3 |
+
emoji: ๐
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: yellow
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "5.50.0"
|
| 8 |
+
python_version: "3.12"
|
| 9 |
+
app_file: app.py
|
| 10 |
+
pinned: false
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
# ๐๐ FoodExtract-Vision v1: Fine-tuned SmolVLM2-500M for Structured Food Tag Extraction
|
| 14 |
|
| 15 |
[](https://huggingface.co/berkeruveyik/FoodExtract-Vision-SmolVLM2-500M-fine-tune-v3)
|
| 16 |
[](https://huggingface.co/datasets/berkeruveyik/vlm-food-4k-not-food-dataset)
|
| 17 |
+
[](https://huggingface.co/HuggingFaceTB/SmolVLM2-500M-Video-Instruct)
|
| 18 |
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
| 19 |
|
| 20 |
+
---
|
| 21 |
|
| 22 |
+
## ๐ Overview
|
| 23 |
|
| 24 |
+
**FoodExtract-Vision** is a fine-tuned Vision-Language Model (VLM) that takes any image as input and produces **structured JSON output** classifying whether food/drink items are visible and extracting them into organized lists.
|
| 25 |
|
| 26 |
+
Built on top of [SmolVLM2-500M-Video-Instruct](https://huggingface.co/HuggingFaceTB/SmolVLM2-500M-Video-Instruct), this project demonstrates that even **small (~500M parameter) VLMs** can be fine-tuned to reliably produce structured outputs for domain-specific tasks โ without needing PEFT/LoRA adapters.
|
| 27 |
|
| 28 |
+
> ๐ก **Key Insight:** The base model often fails to follow the required JSON output structure, producing inconsistent or unstructured responses. After two-stage fine-tuning, the model **reliably generates valid JSON** matching the specified schema.
|
| 29 |
|
| 30 |
+
---
|
| 31 |
|
| 32 |
+
## ๐ฏ What Does It Do?
|
| 33 |
|
| 34 |
+
| | Input | Output |
|
| 35 |
+
|---|---|---|
|
| 36 |
+
| ๐ธ | Any image (food or non-food) | Structured JSON |
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
### Output Schema
|
| 39 |
|
| 40 |
```json
|
| 41 |
{
|
|
|
|
| 46 |
}
|
| 47 |
```
|
| 48 |
|
| 49 |
+
| Field | Type | Description |
|
| 50 |
+
|---|---|---|
|
| 51 |
+
| `is_food` | `int` | `0` = no food/drink visible, `1` = food/drink visible |
|
| 52 |
+
| `image_title` | `str` | Short food-related caption (blank if no food) |
|
| 53 |
+
| `food_items` | `list[str]` | List of visible edible food item nouns |
|
| 54 |
+
| `drink_items` | `list[str]` | List of visible edible drink item nouns |
|
| 55 |
+
|
| 56 |
---
|
| 57 |
|
| 58 |
+
## ๐ ๏ธ What Was Done โ End-to-End Pipeline
|
| 59 |
|
| 60 |
+
This project covers the **full ML lifecycle** from dataset creation to deployment:
|
| 61 |
|
| 62 |
+
### Step 1: ๐ Dataset Creation (`00_create_vlm_dataset.ipynb`)
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
1. ๐ท๏ธ Loaded food labels from `data/food_dataset-2.jsonl` (generated via Qwen3-VL-8B inference on Food270 images)
|
| 65 |
+
2. ๐ Added metadata fields (`image_id`, `image_name`, `food270_class_name`, `image_source`)
|
| 66 |
+
3. ๐ผ๏ธ Sampled **not-food images** from `data/not_food/` and created empty labels with `is_food = 0`
|
| 67 |
+
4. ๐ Merged food + not-food labels into a unified dataset
|
| 68 |
+
5. ๐ Copied all images into `data/food_all/` and wrote `metadata.jsonl` for HuggingFace `imagefolder` format
|
| 69 |
+
6. ๐ Pushed to HuggingFace Hub as [`berkeruveyik/vlm-food-4k-not-food-dataset`](https://huggingface.co/datasets/berkeruveyik/vlm-food-4k-not-food-dataset)
|
| 70 |
|
| 71 |
+
**Final dataset:** ~3,698 image-JSON pairs across **270 food categories** + not-food images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
### Step 2: ๐งช Base Model Evaluation (`01_fine_tune_vlm_v3_smolVLM_500m.ipynb`)
|
| 74 |
|
| 75 |
+
- Tested `SmolVLM2-500M-Video-Instruct` on the food extraction task
|
| 76 |
+
- **Result:** The base model produced unstructured text like *"The given image is a food or drink item."* instead of valid JSON
|
| 77 |
+
- โ Base model **cannot** follow the structured output format
|
| 78 |
|
| 79 |
+
### Step 3: ๐ Data Formatting for SFT
|
| 80 |
|
| 81 |
+
Converted each sample to a **conversational message format** with three roles:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
```
|
| 84 |
+
[SYSTEM] โ Expert food extractor persona
|
| 85 |
+
[USER] โ Image + JSON extraction prompt
|
| 86 |
+
[ASSISTANT] โ Ground truth JSON output
|
| 87 |
+
```
|
| 88 |
|
| 89 |
+
- Used `PIL.Image` objects directly (not bytes) to preserve image quality
|
| 90 |
+
- 80/20 train/validation split with `random.seed(42)` for reproducibility
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
### Step 4: ๐ง Stage 1 Training โ Frozen Vision Encoder
|
| 93 |
|
| 94 |
+
- **Froze** the vision encoder (`model.model.vision_model`)
|
| 95 |
+
- **Trained** only the LLM + connector layers
|
| 96 |
+
- **Goal:** Teach the language model to output valid JSON structure
|
| 97 |
+
- Used `SFTTrainer` from TRL with custom `collate_fn` for image-text batching
|
| 98 |
+
|
| 99 |
+
### Step 5: ๐ฅ Stage 2 Training โ Full Model Fine-tuning
|
| 100 |
+
|
| 101 |
+
- **Unfroze** the vision encoder
|
| 102 |
+
- **Trained** all parameters with a **100x lower learning rate** (`2e-6` vs `2e-4`)
|
| 103 |
+
- **Goal:** Allow the vision encoder to adapt for better food recognition without catastrophic forgetting
|
| 104 |
+
|
| 105 |
+
### Step 6: ๐ Evaluation & Comparison
|
| 106 |
+
|
| 107 |
+
- Compared outputs from 3 models side-by-side:
|
| 108 |
+
- ๐ด **Pre-trained** (base model) โ fails at structured output
|
| 109 |
+
- ๐ก **Stage 1** (frozen vision) โ learns JSON format
|
| 110 |
+
- ๐ข **Stage 2** (full fine-tune) โ best food recognition + JSON format
|
| 111 |
+
|
| 112 |
+
### Step 7: ๐ Deployment
|
| 113 |
+
|
| 114 |
+
- Uploaded fine-tuned model to HuggingFace Hub
|
| 115 |
+
- Built Gradio demo with side-by-side comparison
|
| 116 |
+
- Deployed as a HuggingFace Space
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
## ๐๏ธ Architecture & Training Details
|
| 121 |
+
|
| 122 |
+
### ๐ง Base Model
|
| 123 |
+
|
| 124 |
+
| Property | Value |
|
| 125 |
+
|---|---|
|
| 126 |
+
| Model | `HuggingFaceTB/SmolVLM2-500M-Video-Instruct` |
|
| 127 |
+
| Parameters | ~500M |
|
| 128 |
+
| Precision | `bfloat16` |
|
| 129 |
+
| Attention | `eager` |
|
| 130 |
+
|
| 131 |
+
### ๐ Dataset
|
| 132 |
+
|
| 133 |
+
| Property | Value |
|
| 134 |
+
|---|---|
|
| 135 |
+
| Source | [`berkeruveyik/vlm-food-4k-not-food-dataset`](https://huggingface.co/datasets/berkeruveyik/vlm-food-4k-not-food-dataset) |
|
| 136 |
+
| Total Samples | ~3,698 image-JSON pairs |
|
| 137 |
+
| Train / Val Split | 80% / 20% |
|
| 138 |
+
| Food Categories | 270 (from Food270 dataset) |
|
| 139 |
+
| Non-food Images | Random internet images |
|
| 140 |
+
| Label Source | Qwen3-VL-8B inference outputs |
|
| 141 |
+
|
| 142 |
+
### ๐ง Two-Stage Training Strategy
|
| 143 |
+
|
| 144 |
+
Inspired by the [SmolVLM Docling paper](https://arxiv.org/pdf/2503.11576):
|
| 145 |
+
|
| 146 |
+
#### ๐ง Stage 1: LLM Alignment (Frozen Vision Encoder)
|
| 147 |
+
|
| 148 |
+
| Parameter | Value |
|
| 149 |
+
|---|---|
|
| 150 |
+
| Vision Encoder | โ๏ธ Frozen |
|
| 151 |
+
| Trainable | LLM + connector layers |
|
| 152 |
+
| Learning Rate | `2e-4` |
|
| 153 |
+
| Epochs | 2 |
|
| 154 |
+
| Batch Size | 8 ร 4 gradient accumulation = effective 32 |
|
| 155 |
+
| Optimizer | `adamw_torch_fused` |
|
| 156 |
+
| LR Scheduler | `constant` |
|
| 157 |
+
| Warmup Ratio | `0.03` |
|
| 158 |
+
| Precision | `bf16` |
|
| 159 |
+
|
| 160 |
+
#### ๐ฅ Stage 2: Full Model Fine-tuning (Unfrozen Vision Encoder)
|
| 161 |
+
|
| 162 |
+
| Parameter | Value |
|
| 163 |
+
|---|---|
|
| 164 |
+
| Vision Encoder | ๐ฅ Unfrozen |
|
| 165 |
+
| Trainable | All parameters |
|
| 166 |
+
| Learning Rate | `2e-6` (100x lower than Stage 1) |
|
| 167 |
+
| Epochs | 2 |
|
| 168 |
+
| Batch Size | 8 ร 4 gradient accumulation = effective 32 |
|
| 169 |
+
| Optimizer | `adamw_torch_fused` |
|
| 170 |
+
| LR Scheduler | `constant` |
|
| 171 |
+
| Warmup Ratio | `0.03` |
|
| 172 |
+
| Precision | `bf16` |
|
| 173 |
|
| 174 |
---
|
| 175 |
|
|
|
|
| 178 |
### ๐ฆ Installation
|
| 179 |
|
| 180 |
```bash
|
| 181 |
+
pip install transformers torch gradio spaces accelerate
|
| 182 |
```
|
| 183 |
|
| 184 |
### ๐ฎ Inference with Pipeline
|
|
|
|
| 186 |
```python
|
| 187 |
import torch
|
| 188 |
from transformers import pipeline
|
| 189 |
+
from PIL import Image
|
| 190 |
|
| 191 |
FINE_TUNED_MODEL_ID = "berkeruveyik/FoodExtraqt-Vision-SmoLVLM2-500M-fine-tune-v3"
|
| 192 |
|
|
|
|
| 201 |
|
| 202 |
Only return valid JSON in the following form:
|
| 203 |
|
| 204 |
+
```json
|
| 205 |
{
|
| 206 |
"is_food": 0,
|
| 207 |
"image_title": "",
|
| 208 |
"food_items": [],
|
| 209 |
"drink_items": []
|
| 210 |
}
|
| 211 |
+
```
|
| 212 |
"""
|
| 213 |
|
| 214 |
messages = [
|
|
|
|
| 275 |
|
| 276 |
## ๐ฎ Gradio Demo
|
| 277 |
|
| 278 |
+
This Space runs a **side-by-side comparison** between the base model and the fine-tuned model.
|
| 279 |
+
|
| 280 |
### โถ๏ธ Running Locally
|
| 281 |
|
| 282 |
```bash
|
| 283 |
+
cd demos/FoodExtract-Vision
|
| 284 |
+
pip install -r requirements.txt
|
| 285 |
python app.py
|
| 286 |
```
|
| 287 |
|
| 288 |
+
### ๐ฅ๏ธ What the Demo Shows
|
| 289 |
+
|
| 290 |
+
1. ๐ค **Upload** any image
|
| 291 |
+
2. ๐ **Compare** outputs from the base model vs. the fine-tuned model side-by-side
|
| 292 |
+
3. ๐ See how fine-tuning enables **reliable structured JSON extraction**
|
| 293 |
+
|
| 294 |
+
### ๐ธ Example Images Included
|
| 295 |
|
| 296 |
+
The demo comes with pre-loaded examples to try instantly.
|
|
|
|
|
|
|
| 297 |
|
| 298 |
---
|
| 299 |
|
| 300 |
## ๐ Project Structure
|
| 301 |
|
| 302 |
```
|
| 303 |
+
vlm_finetune/
|
| 304 |
+
โโโ ๐ 00_create_vlm_dataset.ipynb # Dataset creation pipeline
|
| 305 |
+
โโโ ๐ 01-fine_tune_vlm.ipynb # First fine-tuning experiment (Gemma-3n)
|
| 306 |
+
โโโ ๐ 01-fine_tune_vlm-v2-smolVLM.ipynb # SmolVLM 256M experiment
|
| 307 |
+
โโโ ๐ 01_fine_tune_vlm_v3_smolVLM_500m.ipynb # โ
Final: SmolVLM 500M two-stage training
|
| 308 |
+
โโโ ๐ qwen3-food270-inference-viewer.ipynb # Dataset visualization tool
|
| 309 |
+
โโโ ๐ README.md # Root project README
|
| 310 |
+
โโโ ๐ data/
|
| 311 |
+
โ โโโ food_dataset-2.jsonl # Qwen3-VL-8B inference outputs
|
| 312 |
+
โ โโโ food_labels_updated.json # Processed food labels
|
| 313 |
+
โ โโโ ๐ 10_images_270_class/ # 10 sample images per category
|
| 314 |
+
โ โโโ ๐ food_all/ # Merged dataset (food + not-food)
|
| 315 |
+
โ โ โโโ metadata.jsonl # HuggingFace imagefolder metadata
|
| 316 |
+
โ โโโ ๐ not_food/ # Non-food images
|
| 317 |
+
โโโ ๐ demos/
|
| 318 |
+
โโโ ๐ FoodExtract-Vision/
|
| 319 |
+
โโโ app.py # ๐ Gradio demo application
|
| 320 |
+
โโโ README.md # ๐ This file
|
| 321 |
+
โโโ requirements.txt # ๐ฆ Python dependencies
|
| 322 |
+
โโโ ๐ examples/ # ๐ผ๏ธ Example images
|
| 323 |
+
โโโ 36741.jpg
|
| 324 |
+
โโโ IMG_3808.JPG
|
| 325 |
+
โโโ istockphoto-175500494-612x612.jpg
|
| 326 |
```
|
| 327 |
|
| 328 |
---
|
|
|
|
| 331 |
|
| 332 |
### โ
What Worked
|
| 333 |
|
| 334 |
+
- ๐๏ธ **Two-stage training** significantly improved output quality compared to single-stage
|
| 335 |
+
- ๐ง **Freezing the vision encoder first** let the LLM learn JSON format without vision interference
|
| 336 |
+
- ๐ข **100x lower learning rate in Stage 2** (`2e-6` vs `2e-4`) prevented catastrophic forgetting
|
| 337 |
- ๐ค Even a **500M parameter model** can learn reliable structured output generation
|
| 338 |
+
- ๐ **Custom `collate_fn`** with proper label masking (pad tokens + image tokens โ `-100`) was essential
|
| 339 |
+
- ๐ **`remove_unused_columns = False`** is critical when using a custom data collator with `SFTTrainer`
|
| 340 |
|
| 341 |
### โ ๏ธ Important Notes
|
| 342 |
|
| 343 |
+
- **Dtype consistency:** Model inputs must match the model's dtype (e.g., `bfloat16` inputs for a `bfloat16` model)
|
| 344 |
+
- **System prompt handling:** When not using `transformers.pipeline`, the system prompt may need to be folded into the user prompt
|
| 345 |
+
- **PIL images over bytes:** Using `format_data()` as a list comprehension instead of `dataset.map()` preserves PIL image types
|
| 346 |
+
- **Gradient checkpointing:** Set `use_reentrant=False` to avoid warnings and ensure compatibility
|
| 347 |
+
|
| 348 |
+
### ๐งช Experiments Tried
|
| 349 |
+
|
| 350 |
+
| Notebook | Model | Approach | Result |
|
| 351 |
+
|---|---|---|---|
|
| 352 |
+
| `01-fine_tune_vlm.ipynb` | Gemma-3n-E2B | QLoRA + PEFT | โ
Works but larger model |
|
| 353 |
+
| `01-fine_tune_vlm-v2-smolVLM.ipynb` | SmolVLM2-256M | Full fine-tune | ๐ก Limited capacity |
|
| 354 |
+
| `01_fine_tune_vlm_v3_smolVLM_500m.ipynb` | SmolVLM2-500M | **Two-stage full fine-tune** | โ
**Best results** |
|
| 355 |
|
| 356 |
---
|
| 357 |
|
|
|
|
| 365 |
| ๐ SmolVLM Docling Paper | [arxiv.org/pdf/2503.11576](https://arxiv.org/pdf/2503.11576) |
|
| 366 |
| ๐ TRL Documentation | [huggingface.co/docs/trl](https://huggingface.co/docs/trl/main/en/index) |
|
| 367 |
| ๐ PEFT GitHub | [github.com/huggingface/peft](https://github.com/huggingface/peft) |
|
| 368 |
+
| ๐ HF Vision Fine-tune Guide | [ai.google.dev/gemma/docs](https://ai.google.dev/gemma/docs/core/huggingface_vision_finetune_qlora?hl=tr) |
|
| 369 |
|
| 370 |
---
|
| 371 |
|
| 372 |
## ๐ License
|
| 373 |
|
| 374 |
+
This project uses Apache 2.0 license. Please refer to the respective model and dataset cards for additional licensing information.
|
| 375 |
+
|
| 376 |
---
|
| 377 |
|
| 378 |
+
*Built with โค๏ธ using ๐ค Transformers, TRL, and Gradio โ by [Berker รveyik](https://huggingface.co/berkeruveyik)*
|