Views
No views yet
Ingredients detected: grilled chicken breast, steamed rice, broccoli.
JSON Summary: {"calories_kcal": 520, "protein_g": 42, "carbs_g": 38, "fat_g": 14, "fibre_g": 5}| Hyperparameter | Value |
|---|---|
| LoRA rank (r) | 16 |
| LoRA alpha | 32 |
| LoRA dropout | 0.05 |
| Target modules | q_proj, v_proj, k_proj |
| Learning rate | 2e-4 |
| Batch size | 2 |
| Gradient accumulation | 4 (effective batch: 8) |
| Epochs | 5 |
| Optimizer | paged_adamw_8bit |
| LR scheduler | cosine |
| Quantisation | 4-bit NF4 (bitsandbytes) |
| Hardware | Google Colab T4 GPU |
1from transformers import AutoProcessor, AutoModelForVision2Seq, BitsAndBytesConfig
2from transformers.models.smolvlm.configuration_smolvlm import SmolVLMConfig
3from transformers.models.smolvlm.modeling_smolvlm import SmolVLMForConditionalGeneration
4from peft import PeftModel
5from PIL import Image
6import torch
7
8MODEL_ID = "HuggingFaceTB/SmolVLM2-500M-Instruct"
9ADAPTER_ID = "Unnatrathi/caloraify-lora-adapter"
10
11# Registry patch for transformers 4.51.3
12AutoModelForVision2Seq.register(SmolVLMConfig, SmolVLMForConditionalGeneration, exist_ok=True)
13
14# Load processor
15processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
16
17# Load base model in 4-bit
18bnb_config = BitsAndBytesConfig(
19 load_in_4bit=True,
20 bnb_4bit_quant_type="nf4",
21 bnb_4bit_compute_dtype=torch.bfloat16,
22 bnb_4bit_use_double_quant=True,
23)
24base_model = AutoModelForVision2Seq.from_pretrained(
25 MODEL_ID,
26 quantization_config=bnb_config,
27 torch_dtype=torch.bfloat16,
28 device_map="auto",
29 trust_remote_code=True,
30)
31
32# Load LoRA adapter
33model = PeftModel.from_pretrained(base_model, ADAPTER_ID)
34model.eval()
35
36# Run inference
37image = Image.open("your_food_photo.jpg").convert("RGB")
38conversation = [
39 {
40 "role": "user",
41 "content": [
42 {"type": "image"},
43 {"type": "text", "text": "What food is in this image? Reply: Ingredients detected: [list]"},
44 ],
45 }
46]
47prompt = processor.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
48inputs = processor(images=[[image]], text=[prompt], return_tensors="pt", truncation=False)
49inputs["pixel_values"] = inputs["pixel_values"].to(torch.bfloat16)
50inputs = {k: v.to(model.device) for k, v in inputs.items()}
51
52with torch.inference_mode():
53 out = model.generate(**inputs, max_new_tokens=200, repetition_penalty=1.3)
54
55new_tokens = out[:, inputs["input_ids"].shape[-1]:]
56print(processor.batch_decode(new_tokens, skip_special_tokens=True)[0])