Downloads · 30 days
19
6% of all-time downloads
lordChipotle/nutrition-label-detector
nutrition-label-detector is a image-text-to-text model from lordChipotle. Use it for the image-text-to-text task on the model card, and read the license before you ship it in a product. The card lists the license as apache-2.0.
This model card describes a fine-tuned version of the Qwen/Qwen2-VL-7B-Instruct model, specifically adapted for detecting nutrition tables in product images.
Downloads · 30 days
19
6% of all-time downloads
All-time downloads
295
Public
Parameters
8.5B
5.9 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors5.9 GB · 100%
How the weights are stored.
U87.4B · 87%
From the Hugging Face model README
This model card describes a fine-tuned version of the Qwen/Qwen2-VL-7B-Instruct model, specifically adapted for detecting nutrition tables in product images.
The entire fine-tuning walkthrough can be accessed at: https://colab.research.google.com/drive/1EkF4arAYcxfi2fugO1B3bfohr9gZa8Ly?usp=sharing
For serving on vLLM/Nvidia Triton, the code can be found on: https://colab.research.google.com/drive/1furnMbQmD7beK5Z35KnJb2lQCIzQ-dpB?usp=sharing
This model is intended for identifying and localizing nutrition tables within images of food products. The primary output is the bounding box coordinates of the detected nutrition table.
Primary Intended Uses:
Out-of-Scope Uses:
<|object_ref_start|>nutrition table<|object_ref_end|><|box_start|>(x0,y0),(x1,y1)<|box_end|>), where coordinates are scaled to the [0,1000) integer space.SFTTrainer (or a custom QwenVLSFTTrainer to handle specific model inputs).bitsandbytes.
bnb_4bit_quant_type: "nf4"bnb_4bit_compute_dtype: torch.bfloat16bnb_4bit_use_double_quant: Truer: 64lora_alpha: 16lora_dropout: 0.05bias: "none"task_type: "CAUSAL_LM"target_modules: ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj", "qkv", "proj"] (covering both transformer decoder and vision encoder ViT blocks).SFTConfig):
dataset_text_field: "messages"learning_rate: 2e-4per_device_train_batch_size: 4 (PD_BATCH)gradient_accumulation_steps: 16 (GA_STEPS) - effective batch size of 64.num_train_epochs: 3lr_scheduler_type: "cosine"warmup_ratio: 0.05bf16: Truetf32: Truegradient_checkpointing: Trueoptim: "paged_adamw_32bit"max_grad_norm: 1.0eval_strategy: "steps"eval_steps: 500save_strategy: "steps"save_steps: 500save_total_limit: 2logging_steps: 25report_to: "wandb"load_best_model_at_end: Truemetric_for_best_model: "eval_loss"remove_unused_columns: Falsepacking: Falsedataloader_pin_memory: Trueoutput_dir: "qwen2vl_qlora_sft"seed: 42transformers: 4.52.0.dev0 (or similar dev version, potentially 4.47.0.dev0 as initially installed)trl: 0.12.0.dev0datasets: 3.0.2bitsandbytes: 0.44.1peft: 0.13.2qwen-vl-utils: 0.0.8accelerate: 1.0.1torch: 2.4.1+cu121 (specific older version due to compatibility issues with latest PyTorch at the time of notebook creation)torchvision: 0.19.1+cu121torchaudio: 2.4.1+cu121wandb for logging.openfoodfacts/nutrition-table-detection dataset. Performance on other types of objects or significantly different image styles may vary.<|object_ref_start|>object_name<|object_ref_end|><|box_start|>(x0,y0),(x1,y1)<|box_end|>) with coordinates scaled to [0,1000). Parsing this output is necessary for downstream tasks.The fine-tuned LoRA adapters are available in the qwen2vl_qlora_sft/checkpoint-51 directory (or your specified output directory).
The notebook demonstrates merging these adapters with the base Qwen/Qwen2-VL-7B-Instruct model and saving the merged model to /content/qwen2vl_merged-bf16. This merged model can then be pushed to a Hugging Face Hub repository (e.g., lordChipotle/nutrition-label-detector).
Inference with the merged model:
Use the AutoModelForVision2Seq and AutoProcessor from the Hugging Face transformers library, loading the model from your Hub repository or the saved OUTPUT_DIR.
from transformers import AutoModelForVision2Seq, AutoProcessor
from PIL import Image
import torch
# Example: Load from local merged directory
MODEL_PATH = "/content/qwen2vl_merged-bf16" # Or your Hugging Face Hub repo name
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
DTYPE = torch.bfloat16
model = AutoModelForVision2Seq.from_pretrained(
MODEL_PATH,
torch_dtype=DTYPE,
device_map="auto",
trust_remote_code=True # Qwen models may require this
)
processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)
# Prepare image and prompt
# img = Image.open("path/to/your/image.jpg").convert("RGB")
# prompt_text = "Detect the bounding box of the nutrition table."
# vision_inputs = [{"type": "image", "image": img}]
# messages = [{"role": "user", "content": vision_inputs + [{"type": "text", "text": prompt_text}]}]
# prompt_chatml = processor.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# inputs = processor(
# text=prompt_chatml,
# images=[img], # Assuming single image for simplicity
# return_tensors="pt"
# ).to(DEVICE)
# with torch.no_grad():
# output_ids = model.generate(**inputs, max_new_tokens=64)
# response = processor.batch_decode(output_ids, skip_special_tokens=True)[0]
# print(response)
# # Parse bounding box from response