Prototype: votes, saves, and profiles aren't stored on this hosted preview yet. No real accounts, and no emails or texts. Privacy

Recipes / Document & invoice OCR

Fine-tune Gemma 4 E4B to read images (Unsloth)

Gemma 4 E4B fine-tuned on images paired with text, using LaTeX OCR as the example.

  • No license flags found

Source: Unsloth notebooks ↗ · by Unsloth · last updated Oct 12, 2026

What a local business could build with it

One small model that reads photos of invoices, job-site forms or menus. Swap in your own labeled images.

How to run it

Commands are copied from the source, unchanged except where a notebook cell is cut at 40 lines (marked in the code). Placeholders like <you> are the source's own. Credit goes to the authors named on each block.

Install (as written in the notebook)

%%capture import os, re if "COLAB_" not in "".join(os.environ.keys()): !pip install unsloth # Do this in local & cloud setups else: !pip install sentencepiece protobuf "datasets==4.3.0" "huggingface_hub>=0.34.0" hf_transfer !pip install --no-deps unsloth_zoo bitsandbytes accelerate peft trl triton unsloth !unsloth install-kernels !pip install --no-deps --upgrade "torchao>=0.16.0" !pip install --no-deps transformers==5.5.0 "tokenizers>=0.22.0,<=0.23.0" !pip install "huggingface_hub>=1.5.0,<2.0" import importlib.metadata as _md try: _tv = tuple(int(x) for x in _md.version("torch").split("+")[0].split(".")[:2]) except Exception: _tv = (2, 11) _torchcodec = {(2, 11): "torchcodec>=0.11.0", (2, 10): "torchcodec==0.10.*", (2, 9): "torchcodec==0.9.*", (2, 8): "torchcodec==0.7.*", (2, 7): "torchcodec==0.5.*"}.get(_tv, "torchcodec>=0.12.0" if _tv >= (2, 12) else "torchcodec") !pip install "{_torchcodec}" import torch; torch._dynamo.config.recompile_limit = 64;

From Unsloth notebook, Gemma4_(E4B)-Vision ↗

Load the base model

from unsloth import FastVisionModel # FastLanguageModel for LLMs import torch gemma4_models = [ # Gemma-4 instruct models: "unsloth/gemma-4-E2B-it", "unsloth/gemma-4-E4B-it", "unsloth/gemma-4-31B-it", "unsloth/gemma-4-26B-A4B-it", # Gemma-4 base models: "unsloth/gemma-4-E2B", "unsloth/gemma-4-E4B", "unsloth/gemma-4-31B", "unsloth/gemma-4-26B-A4B", ] # More models at https://huggingface.co/unsloth model, processor = FastVisionModel.from_pretrained( "unsloth/gemma-4-E4B-it", load_in_4bit = True, # Use 4bit to reduce memory use. False for 16bit LoRA. use_gradient_checkpointing = "unsloth", # True or "unsloth" for long context )

From Unsloth notebook, Gemma4_(E4B)-Vision ↗

Set up training

from unsloth.trainer import UnslothVisionDataCollator from trl import SFTTrainer, SFTConfig trainer = SFTTrainer( model = model, train_dataset = converted_dataset, processing_class = processor.tokenizer, data_collator = UnslothVisionDataCollator(model, processor), args = SFTConfig( per_device_train_batch_size = 1, gradient_accumulation_steps = 4, max_grad_norm = 0.3, warmup_ratio = 0.03, max_steps = 60, # num_train_epochs = 2, # Set this instead of max_steps for full training runs learning_rate = 2e-4, logging_steps = 1, save_strategy = "steps", optim = "adamw_8bit", weight_decay = 0.001, lr_scheduler_type = "cosine", seed = 3407, output_dir = "outputs", report_to = "none", # For Weights and Biases or others # You MUST put the below items for vision finetuning: remove_unused_columns = False, dataset_text_field = "", dataset_kwargs = {"skip_prepare_dataset": True}, max_length = 2048, ) )

From Unsloth notebook, Gemma4_(E4B)-Vision ↗

Train

trainer_stats = trainer.train()

From Unsloth notebook, Gemma4_(E4B)-Vision ↗

Licenses

No flags: every license we found for this recipe is permissive (Apache-2.0, MIT, BSD, CC BY or similar). Still read each one.

PartLicense, as the source or Hub states it
CodeLGPL-3.0 (Unsloth notebooks)
Base modelApache-2.0
DatasetApache-2.0

Checked against the Hugging Face API and each repo on December 4, 2026. A grouping like “$10M revenue cap” is our shorthand, not legal advice.