Classify images with a vision-language model
Source on GitHub: the workflows/classify-images folder, with its README and its workflow file.
Labels every image in a dataset with one of a fixed set of labels by asking a vision-language model about it, through vLLM's offline batch inference. Each image travels inside its request as a base64 data URL, and vLLM's structured outputs hold each answer to one of the labels. When the images already carry labels, a last step measures agreement and prints a confusion table.
| Step | Task definition | Compute |
|---|---|---|
| Fetch the model | swaleio/hf-download | cpu-4 |
| Write one request per image, with the image inline | swaleio/duckdb | cpu-4 |
| Label | swaleio/vllm-batch | a100-1 |
| Join labels to the images' other columns, count each label, write Parquet | swaleio/duckdb | cpu-4 |
Measure agreement with existing labels, when gold_column is set | swaleio/duckdb | cpu-4 |
Upload to the Hugging Face Hub, when hub_repo is set | swaleio/hf-upload | cpu-4 |
The Compute column gives each step's compute type, the compute its tasks are allocated.
Every step is its own task; only labeling holds a GPU.
For a specialized task, a small model asked cold may label poorly. Use a larger model, or fine-tune one with the image classification workflow.
Inputs
| Input | Default | Description |
|---|---|---|
model | Qwen/Qwen2-VL-2B-Instruct | Vision-language model on the Hugging Face Hub that vLLM serves. |
images | the test split of AI-Lab-Makerere/beans | Parquet file or glob with a Hugging Face Image column, as a DuckDB path. |
image_column | image | The Image column: a struct whose bytes field holds the encoded image. |
image_type | image/jpeg | Media type of the encoded images. |
labels | angular_leaf_spot,bean_rust,healthy | Labels the model chooses from, separated by commas. |
instruction | a question about bean leaves | What the model is asked about each image. |
gold_column | labels | Column of existing labels, as positions in labels from 0; empty skips the comparison. |
limit | 1000 | Number of images to label. |
hub_repo | empty | Hugging Face dataset repo to upload labels.parquet to. |
The model download and the upload read the secret hf_token.
Output
labels.parquet on the workspace: every column but the image, a custom_id
and the model's llm_label.
Run it
You need the CLI, logged in to your account, and a project to add the workflow to; Getting started sets them up.
-
Save the file under The workflow file as
workflow.yaml, and add it to a project:swale workflow create --project-name my-project --file workflow.yamlThe command prints
Workflow created:followed by the workflow's ID. -
The workflow reads the secret
hf_token. Create it as a project secret, or as an account secret that allows the project, as Use secrets in workflows shows. -
Start a run with the workflow's ID:
swale workflow run start --project-name my-project --workflow-id <workflow-id>Each input you leave out takes its default from Inputs, and
--input <name>=<value>gives it another value.
Create and manage workflows covers the other ways to add a workflow, Start and manage workflow runs how to follow a run, and View and stream task logs how to read its tasks' logs.
The workflow file
Workflow definition syntax describes each key in it.
name: Classify images with a vision-language model
description: Labels each image with one of a fixed set of labels, using a vision-language model through vLLM batch inference, and measures agreement with existing labels.
compute_type: cpu-4
inputs:
model:
description: Hugging Face repo id of the vision-language model that labels.
default: Qwen/Qwen2-VL-2B-Instruct
images:
description: Parquet file or glob holding the images in a Hugging Face Image column, as a DuckDB path such as hf://datasets/<owner>/<dataset>/<file>.
default: hf://datasets/AI-Lab-Makerere/beans/data/test-00000-of-00001.parquet
image_column:
description: Image column, a struct holding the encoded image in its bytes field.
default: image
image_type:
description: Media type of the encoded images.
default: image/jpeg
labels:
description: The labels to choose from, separated by commas. The model can answer only with one of them.
default: angular_leaf_spot,bean_rust,healthy
instruction:
description: What the model is asked about each image.
default: This is a photo of a bean plant leaf. Is it healthy, or does it show angular leaf spot or bean rust? Answer with angular_leaf_spot, bean_rust or healthy only.
gold_column:
description: Column holding existing labels, as each label's position in labels counting from 0, to measure agreement against. Leave empty when the images have none.
default: labels
limit:
description: Number of images to label.
default: "1000"
hub_repo:
description: Hugging Face dataset repo to upload the labels to, as namespace/name. Leave empty to keep them on the workspace only.
default: ""
entry_point: main
blocks:
main:
tasks:
model:
name: Fetch model
uses: swaleio/hf-download
args:
repo: ${{inputs.model}}
dest: ${{env.WORKFLOW_STORAGE}}/model
token: ${{secrets.hf_token}}
requests:
name: Write requests
uses: swaleio/duckdb
args:
sql: |
CREATE TABLE images AS
SELECT 'row-' || lpad((row_number() OVER () - 1)::VARCHAR, 6, '0') AS custom_id, *
FROM (SELECT * FROM '${{inputs.images}}' LIMIT ${{inputs.limit}});
COPY (SELECT * EXCLUDE ("${{inputs.image_column}}") FROM images)
TO '${{env.WORKFLOW_STORAGE}}/images.parquet' (FORMAT parquet);
COPY (
SELECT custom_id,
'POST' AS method,
'/v1/chat/completions' AS url,
{'model': '${{env.WORKFLOW_STORAGE}}/model',
'messages': [{'role': 'user', 'content': [
{'type': 'image_url',
'image_url': {'url': 'data:${{inputs.image_type}};base64,' || base64("${{inputs.image_column}}".bytes)}},
{'type': 'text', 'text': $$${{inputs.instruction}}$$}]}],
'max_tokens': 16,
'temperature': 0,
'structured_outputs': {'choice': string_split($$${{inputs.labels}}$$, ',')}} AS body
FROM images
) TO '${{env.WORKFLOW_STORAGE}}/requests.jsonl' (FORMAT json);
SELECT count(*) AS requests FROM images;
label:
name: Label
uses: swaleio/vllm-batch
compute_type: a100-1
start_on:
- model
- requests
args:
model: ${{env.WORKFLOW_STORAGE}}/model
input_file: ${{env.WORKFLOW_STORAGE}}/requests.jsonl
output_file: ${{env.WORKFLOW_STORAGE}}/results.jsonl
collect:
name: Collect labels
uses: swaleio/duckdb
start_on:
- label
args:
sql: |
CREATE TABLE labeled AS
SELECT i.*, trim(r.response.body.choices[1].message.content) AS llm_label
FROM '${{env.WORKFLOW_STORAGE}}/images.parquet' i
JOIN read_json('${{env.WORKFLOW_STORAGE}}/results.jsonl') r USING (custom_id)
ORDER BY custom_id;
SELECT llm_label, count(*) AS images FROM labeled GROUP BY ALL ORDER BY images DESC;
COPY labeled TO '${{env.WORKFLOW_STORAGE}}/labels.parquet' (FORMAT parquet);
compare:
name: Compare when existing labels are given
start_on:
- collect
if:
condition: ${{inputs.gold_column}}
true: agreement
publish:
name: Upload when asked
start_on:
- compare
if:
condition: ${{inputs.hub_repo}}
true: upload
agreement:
tasks:
measure:
name: Measure agreement
uses: swaleio/duckdb
args:
sql: |
CREATE TABLE pairs AS
SELECT string_split($$${{inputs.labels}}$$, ',')["${{inputs.gold_column}}" + 1] AS gold, llm_label
FROM '${{env.WORKFLOW_STORAGE}}/labels.parquet';
SELECT count(*) AS images,
round(avg((gold = llm_label)::INT), 3) AS agreement
FROM pairs;
PIVOT pairs ON llm_label USING count(*) GROUP BY gold ORDER BY gold;
upload:
tasks:
upload_labels:
name: Upload to the Hugging Face Hub
uses: swaleio/hf-upload
args:
repo: ${{inputs.hub_repo}}
repo_type: dataset
source: ${{env.WORKFLOW_STORAGE}}/labels.parquet
path_in_repo: data/labels.parquet
token: ${{secrets.hf_token}}
Licenses of the defaults
- Model: Qwen/Qwen2-VL-2B-Instruct, Apache-2.0.
- Dataset: AI-Lab-Makerere/beans, MIT.