textvqa-lance-colab / README.md
prrao87's picture
Update README.md
b43c359 verified
|
Raw
History Blame Contribute Delete
13.4 kB
metadata
pretty_name: TextVQA Lance Colab
task_categories:
  - visual-question-answering
language:
  - en
tags:
  - lance
  - textvqa
  - visual-question-answering
  - multimodal
  - image
  - text
  - colab
size_categories:
  - 1K<n<10K
configs:
  - config_name: default
    data_files:
      - split: train
        path: textvqa_colab_train.lance/**
      - split: validation
        path: textvqa_colab_val.lance/**

TextVQA VLM Fine-Tuning Demo

A small Lance-formatted subset of TextVQA, source via pre-baked operations from this repo and fine-tuning a VLM on the subset. Each row is one visual question-answering example over an image that contains scene text: inline image bytes, a natural-language question, 10 reference answers, OCR tokens, image-class labels, and paired 512-dimensional image/question embeddings are stored together in a Lance table.

The train split also includes precomputed fixed-shape tensors for a Colab fine-tuning pipeline, so the same repository can be used both for retrieval-oriented LanceDB examples and for small end-to-end model-input experiments without preparing the full TextVQA corpus.

The dataset is available directly from the Hub at:

hf://datasets/lance-format/textvqa-lance-colab

Key Features

  • Small, notebook-friendly slice: 1,000 rows total, with 600 train examples and 400 validation examples.
  • Self-contained multimodal rows: image bytes, question text, answers, OCR tokens, image classes, and embeddings live in the same Lance row.
  • Text-centric VQA examples: questions often require reading labels, signs, packaging, book covers, or other text visible in the image.
  • Paired embeddings: image_emb and question_emb are 512-dimensional vectors suitable for lightweight semantic retrieval examples.
  • Colab training tensors: the train table includes vision_tower_hiddens, input_ids, attention_mask, and labels for fixed-shape tutorial pipelines.
  • Versioned Lance storage: the dataset can be opened directly with Lance or LanceDB, downloaded locally, subsetted, and evolved without rewriting the full table.

Splits

Hugging Face split Lance table Rows Distinct images Notes
train textvqa_colab_train 600 373 Includes raw multimodal columns plus precomputed Colab tensors
validation textvqa_colab_val 400 244 Raw multimodal/retrieval columns

The repository also includes slice_info.json, which identifies this subset as the text_dense slice.

Schema

Column Type Notes
id int64 Row id from the sampled split
image large_binary Inline image bytes
image_id string TextVQA image id
question_id string TextVQA question id
question string Natural-language visual question
answers list<string> 10 reference answers from TextVQA annotations
answer string Convenience answer, corresponding to the first reference answer
image_emb fixed_size_list<float32, 512> Image embedding
question_emb fixed_size_list<float32, 512> Question-text embedding
ocr_tokens list<string> OCR tokens associated with the image
image_classes list<string> Image-level class labels from the upstream formatted dataset
set_name string Original split label, train or val
vision_tower_hiddens fixed_size_list<float16, 819200> Train table only; precomputed visual hidden-state tensor
input_ids fixed_size_list<int32, 512> Train table only; token ids for the Colab training pipeline
attention_mask fixed_size_list<int8, 512> Train table only; token attention mask
labels fixed_size_list<int32, 512> Train table only; supervised training labels

When loaded through datasets.load_dataset, Hugging Face may present a unioned feature schema across splits; validation rows can therefore expose the train-only tensor columns as None. When opened directly as Lance tables, textvqa_colab_val contains only the raw multimodal/retrieval columns.

Index Status

This Colab subset does not ship with pre-built Lance indices. Because the dataset is intentionally small, exact scans and vector search are already practical for examples and notebooks. For heavier local experiments, download the dataset and add the indices you need:

import lancedb

db = lancedb.connect("./textvqa-lance-colab")
tbl = db.open_table("textvqa_colab_train")

tbl.create_index(vector_column_name="image_emb", metric="cosine", index_type="IVF_FLAT")
tbl.create_index(vector_column_name="question_emb", metric="cosine", index_type="IVF_FLAT")
tbl.create_fts_index("question", replace=True)

Load with datasets.load_dataset

Use the standard Hugging Face datasets interface when you want familiar Dataset objects:

import datasets

ds = datasets.load_dataset("lance-format/textvqa-lance-colab", split="train")

for row in ds.select(range(3)):
    print(row["question"], "->", row["answer"])

Load with LanceDB

LanceDB opens the repository as a small database with two tables:

import lancedb

db = lancedb.connect("hf://datasets/lance-format/textvqa-lance-colab")
print(db.list_tables().tables)

tbl = db.open_table("textvqa_colab_train")
print(len(tbl))

Use textvqa_colab_val for the validation split:

val_tbl = db.open_table("textvqa_colab_val")
print(len(val_tbl))

Load with Lance

Use pylance when you want lower-level access to schema, versions, fragments, or scans:

import lance

ds = lance.dataset(
    "hf://datasets/lance-format/textvqa-lance-colab/textvqa_colab_train.lance"
)

print(ds.count_rows())
print(ds.schema.names)
print(ds.list_indices())

For production use or repeated experiments, download locally first:

hf download lance-format/textvqa-lance-colab --repo-type dataset --local-dir ./textvqa-lance-colab

Then open ./textvqa-lance-colab with LanceDB or ./textvqa-lance-colab/textvqa_colab_train.lance with Lance.

Search

The stored image_emb and question_emb columns make it easy to demonstrate vector retrieval without running an embedding model. The example below uses one validation question embedding as a query vector and searches the image embeddings:

import lancedb

db = lancedb.connect("hf://datasets/lance-format/textvqa-lance-colab")
tbl = db.open_table("textvqa_colab_val")

seed = (
    tbl.search()
    .select(["question_emb", "question", "answer"])
    .limit(1)
    .offset(4)
    .to_list()[0]
)

hits = (
    tbl.search(seed["question_emb"], vector_column_name="image_emb")
    .metric("cosine")
    .select(["image_id", "question", "answer"])
    .limit(10)
    .to_list()
)

print("query:", seed["question"], "->", seed["answer"])
for row in hits:
    print(row["image_id"], row["question"], "->", row["answer"])

Swap vector_column_name="image_emb" for question_emb to retrieve nearby questions instead of visually similar examples.

Curate

Because the image and embedding columns are stored separately from the text columns, you can inspect and curate small slices without reading every image byte. This query collects validation examples whose question or answer mentions a sign:

import lancedb

db = lancedb.connect("hf://datasets/lance-format/textvqa-lance-colab")
tbl = db.open_table("textvqa_colab_val")

candidates = (
    tbl.search()
    .where("question LIKE '%sign%' OR answer LIKE '%sign%'")
    .select(["question_id", "image_id", "question", "answer", "ocr_tokens"])
    .limit(25)
    .to_list()
)

for row in candidates:
    print(row["question_id"], row["question"], "->", row["answer"])

The result is a plain list of dictionaries that can be reviewed, saved as a manifest, or materialized into a new local Lance table.

Evolve

Lance stores each column independently, so local copies can be extended with derived columns without rewriting all image bytes or embeddings. For example, add simple answer/question features to the validation table:

import lancedb

db = lancedb.connect("./textvqa-lance-colab")  # local copy required for writes
tbl = db.open_table("textvqa_colab_val")

tbl.add_columns({
    "question_length": "length(question)",
    "answer_length": "length(answer)",
    "num_ocr_tokens": "array_length(ocr_tokens)",
})

If you run a model over this subset, merge predictions back by question_id:

import pyarrow as pa

predictions = pa.table({
    "question_id": pa.array(["34605", "34606"]),
    "model_answer": pa.array(["bowmore", "10 years"]),
    "model_confidence": pa.array([0.92, 0.88]),
})

tbl.merge(predictions, on="question_id")

The original columns remain unchanged, and every mutation creates a new Lance version.

Train

Projection lets a training loop read only the columns each step needs. For a lightweight VQA loop, project image bytes, questions, and answers:

import lancedb
from lancedb.permutation import Permutation
from torch.utils.data import DataLoader

db = lancedb.connect("hf://datasets/lance-format/textvqa-lance-colab")
tbl = db.open_table("textvqa_colab_train")

train_ds = Permutation.identity(tbl).select_columns(["image", "question", "answer"])
loader = DataLoader(train_ds, batch_size=16, shuffle=True, num_workers=2)

for batch in loader:
    # Decode the image bytes, tokenize question/answer text, and run a VLM step.
    ...

For pipelines that use the precomputed Colab tensors, project those columns directly:

train_ds = Permutation.identity(tbl).select_columns([
    "vision_tower_hiddens",
    "input_ids",
    "attention_mask",
    "labels",
])

This avoids reading image bytes during training when the visual features have already been prepared.

Versioning

Every mutation to a Lance dataset commits a new version. You can inspect version history directly from the Hub copy; creating new tags requires a local copy because tags are writes:

import lancedb

db = lancedb.connect("hf://datasets/lance-format/textvqa-lance-colab")
tbl = db.open_table("textvqa_colab_train")

print("Current version:", tbl.version)
print("History:", tbl.list_versions())
print("Tags:", tbl.tags.list())

Once downloaded locally, tag a stable snapshot for reproducibility:

local_db = lancedb.connect("./textvqa-lance-colab")
local_tbl = local_db.open_table("textvqa_colab_train")
local_tbl.tags.create("colab-v1", local_tbl.version)

Tagged versions can be reopened by name, and numeric versions can be reopened by version number:

tbl_v1 = local_db.open_table("textvqa_colab_train", version="colab-v1")
tbl_v2 = local_db.open_table("textvqa_colab_train", version=2)

Materialize a Subset

Reads from the Hub are lazy, so exploratory queries transfer only the columns and row groups they touch. To create a local subset, stream batches from a filtered query into a new LanceDB table:

import lancedb

remote_db = lancedb.connect("hf://datasets/lance-format/textvqa-lance-colab")
remote_tbl = remote_db.open_table("textvqa_colab_val")

batches = (
    remote_tbl.search()
    .where("question LIKE '%sign%' OR answer LIKE '%sign%'")
    .select(["id", "image", "image_id", "question_id", "question", "answers",
             "answer", "ocr_tokens", "image_emb", "question_emb"])
    .to_batches()
)

local_db = lancedb.connect("./textvqa-sign-subset")
local_db.create_table("validation", batches)

The resulting ./textvqa-sign-subset is a first-class LanceDB database.

Limitations and Considerations

  • This is a small 1,000-row subset intended for examples, demos, and smoke tests. It is not a replacement for the full TextVQA benchmark.
  • The subset is text-dense by construction, so it should not be treated as representative of all TextVQA questions or image types.
  • The answer column is a convenience field; use the full answers list when evaluating model predictions against the original annotation set.
  • Real-world images can contain brand names, signs, labels, personal names, and noisy OCR. Review samples before using the data in public demos or downstream applications.
  • The validation Lance table does not include the train-only Colab tensor columns.

Source and License

This dataset was converted from lmms-lab/textvqa, which is a formatted Hugging Face redistribution of TextVQA. TextVQA was introduced in "Towards VQA Models That Can Read" and contains questions that require reading and reasoning over text in images.

The upstream TextVQA data includes image content and annotations from the original TextVQA release. Please review the upstream TextVQA terms and the source image licensing before redistribution or commercial use. This Lance-formatted subset does not assign a new license to upstream media or annotations.

Citation

@inproceedings{singh2019towards,
  title={Towards VQA Models That Can Read},
  author={Singh, Amanpreet and Natarajan, Vivek and Shah, Meet and Jiang, Yu and Chen, Xinlei and Batra, Dhruv and Parikh, Devi and Rohrbach, Marcus},
  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  pages={8317--8326},
  year={2019}
}