Views
No views yet
shibatch/tinygemma3ocr2m is a tiny Gemma3-style multimodal validation checkpoint.image -> vision tower -> multimodal projector -> image tokens -> text decoder -> generated text1Repository: shibatch/tinygemma3ocr2m
2Model class: Gemma3ForConditionalGeneration
3Task: synthetic digit OCR + text-generation sanity
4Image size: 128 x 128
5Patch size: 16 x 16
6Image tokens per image: 64
7Image token: <image>
8Image token id: 1003
9Approximate scale: about 2M parameters
10OCR prompt: Read the digits.
11OCR target format: digit string onlyGemma3ForConditionalGeneration.1Input image: synthetic 128x128 image containing centered digits
2Prompt: Read the digits.
3Output: the digit string6235317 should generate:6235317Gemma3ForCausalLM text model was trained on TinyStories. The resulting text weights were transplanted into a Gemma3ForConditionalGeneration checkpoint.Read the digits.1OCR ratio: 0.90
2Text ratio: 0.10
3OCR loss weight: 1.0
4Text loss weight: 0.2
5OCR style: fixed synthetic digits
6Font size: 30
7Offset: 0
8Rotation: 0
9Noise: 0
10Prompt: Read the digits.
11Digit length: 1 to 8pip install torch transformers pillow huggingface_hub numpy1import torch, numpy as np
2from PIL import Image
3from huggingface_hub import hf_hub_download
4from transformers import PreTrainedTokenizerFast, Gemma3ForConditionalGeneration
5
6repo = "shibatch/tinygemma3ocr2m"
7
8tok = PreTrainedTokenizerFast.from_pretrained(repo, subfolder="hf")
9model = Gemma3ForConditionalGeneration.from_pretrained(
10 repo, subfolder="hf", torch_dtype=torch.bfloat16
11).cuda().eval()
12
13path = hf_hub_download(repo, "sample_images/sample_00_6235317.png")
14img = Image.open(path).convert("RGB")
15pix = torch.from_numpy(np.asarray(img, dtype=np.float32) / 127.5 - 1).permute(2, 0, 1)[None].cuda()
16
17ids = [tok.bos_token_id] + [model.config.image_token_index] * model.config.mm_tokens_per_image
18ids += tok.encode("\nRead the digits.\n", add_special_tokens=False)
19
20input_ids = torch.tensor([ids], device="cuda")
21attention_mask = torch.ones_like(input_ids)
22
23out = model.generate(
24 input_ids=input_ids,
25 attention_mask=attention_mask,
26 pixel_values=pix,
27 max_new_tokens=12,
28 do_sample=False,
29 pad_token_id=tok.bos_token_id,
30 eos_token_id=tok.eos_token_id,
31)
32
33print(tok.decode(out[0][len(ids):], skip_special_tokens=True))6235317pixel_values. It directly calls:1model.model.language_model
2model.lm_head1import torch
2from transformers import PreTrainedTokenizerFast, Gemma3ForConditionalGeneration
3
4repo = "shibatch/tinygemma3ocr2m"
5tok = PreTrainedTokenizerFast.from_pretrained(repo, subfolder="hf")
6model = Gemma3ForConditionalGeneration.from_pretrained(
7 repo, subfolder="hf", torch_dtype=torch.bfloat16
8).cuda().eval()
9
10ids = [tok.bos_token_id] + tok.encode("Once upon", add_special_tokens=False)
11x = torch.tensor([ids], device="cuda")
12
13for _ in range(50):
14 h = model.model.language_model(input_ids=x, use_cache=False, return_dict=True).last_hidden_state
15 nxt = model.lm_head(h)[0, -1].argmax().view(1, 1)
16 x = torch.cat([x, nxt], dim=1)
17 if int(nxt) == tok.eos_token_id:
18 break
19
20print(tok.decode(x[0], skip_special_tokens=True))Once upon a time, there was a little girl named Lily...1malformed names
2repetition
3awkward TinyStories-like phrasing
4occasional non-word fragments1exact_match: 0.87
2digit_accuracy: 0.976
3length_accuracy: 0.9981python test_inference_tinygemma3ocr2m.py \
2 --model-dir shibatch/tinygemma3ocr2m \
3 --text 1677216 \
4 --font-size 30 \
5 --prompt "Read the digits."1Prediction:
2 target: 1677216
3 prediction: 1677216
4 raw_text: '1677216'1python test_inference_tinygemma3ocr2m.py \
2 --model-dir shibatch/tinygemma3ocr2m \
3 --eval-synthetic 500 \
4 --min-digits 1 \
5 --max-digits 8 \
6 --font-size 30 \
7 --prompt "Read the digits."1python test_text_generation_tinygemma3ocr2m.py \
2 --model-dir shibatch/tinygemma3ocr2m \
3 --prompt "Once upon" \
4 --max-new-tokens 501image_token_index: 1003
2mm_tokens_per_image: 64
3vision image_size: 128
4vision patch_size: 16mm_tokens_per_image is not 64, or if the vision image size is not 128, the checkpoint is not the intended 128x128 model.1shibatch/tinygemma3ocr2m
2 README.md
3 config.json
4 model.safetensors
5 tokenizer.json
6 tokenizer_config.json
7 special_tokens_map.json
8 sample_images/
9 sample_00_6235317.png
10 sample_images.json
11 eval_ocr_augmented.json
12 eval_text_generation.json
13 safetensors_keys.json
14 artifact_metadata.json1Gemma3ForConditionalGeneration.from_pretrained("shibatch/tinygemma3ocr2m")
2PreTrainedTokenizerFast.from_pretrained("shibatch/tinygemma3ocr2m")hf/ subdirectory, pass subfolder="hf" to from_pretrained().<image> tokens in the text input. The number of inserted image tokens must match mm_tokens_per_image.1image_size = 128
2patch_size = 16
3patch grid = 8 x 8
4mm_tokens_per_image = 641<bos>
2<image> repeated 64 times
3\nRead the digits.\n