Find similar Sentinel-2 chips from L1B data#

This sample turns Sentinel-2 L1B granules into surface reflectance, encodes 128-pixel chips with Clay v1.5, makes the vectors searchable in the GeoNeMo server, and finds the chips that look most like a chosen one. It then records the result in the GeoNeMo knowledge base (GKB).

Step

Who runs it

Needs

1. L1B to surface reflectance and Clay vectors

Data producer

NVIDIA GPU with a CUDA 13 driver on an x86-64 or ARM64 host, Docker with the NVIDIA Container Toolkit, read access to the cuGeo repository, L1B auxiliary data

2. Pin the vectors as a dataset

Data producer

Python 3.12 with pyarrow

3. Serve the dataset

Services operator

A development stack (Docker, access to the GeoNeMo image registry), or an existing deployment with Milvus

4. Find similar chips and record the finding

Any user

The console, or geonemo-client

New to the Toolkit? Tutorial: find ships off a port runs the same search from L2A products, which need no auxiliary data.

To try steps 2 to 4 on one machine without a GPU or granules, make a synthetic table in step 1 and use the development stack in step 3. To set up your own L1B data for both embeddings and on-demand ortho images, follow Serve your L1B data as ortho images and embeddings: it covers the data layout, preparation, the embedding runs and one stack serving both.

Step 1: L1B to surface reflectance and Clay vectors#

The pipeline calibrates each band, orthorectifies it, applies atmospheric correction with Sen2Cor tables and a fixed visibility, cuts 128-pixel chips on a 10 m grid and encodes them with Clay v1.5. Each chip stacks ten bands: B02, B03, B04, B05, B06, B07, B08, B8A, B11 and B12. The result is surface reflectance from GeoNeMo’s own L1B workflow; it is not equivalent to ESA’s operational L2A product and has no cloud mask.

  1. Build the L1B worker image once. GeoNeMo does not publish it, so build-l1b-image.sh builds it on your machine: first the ordinary pipeline image, then adds cuGeo’s L1B native libraries. It needs Docker with BuildKit and access to the cuGeo repository. Both images use the latest cuGeo main at build time and record the resolved revision. The build targets GPU 0’s architecture unless you pass --arch. Compilation can run without a GPU.

    ./build-l1b-image.sh
    

    The default repository is https://github.com/NVIDIA/cugeo.git. Set CUGEO_REPOSITORY to use a mirror.

  2. Prepare the granules with packs/sentinel/scripts/prepare_l1b_ortho.py. It writes one cuGeo input.json per band and the l1b.json index the pipeline reads, from a corpus of L1B granules, datastrips, matching L1C products, DEM tiles, a geoid, the GIPP and the Sen2Cor tables. Serve your L1B data as ortho images and embeddings describes that layout. To write the inputs by hand instead, see Sentinel-2 ingestion pipeline and start from l1b-index.example.json.

  3. Run the pipeline once per granule in l1b.json, on one GPU. Below, $DATA is the folder holding the corpus, the preparation output and the Sen2Cor tables, and $WORK is the preparation output (--work). The prepared inputs name absolute host paths, so mount $DATA at the same path. The output, work and cache folders must be writable by UID 10001, the image’s user; the cache keeps the Clay checkpoint between runs. Don’t pass the whole granules/ folder: it also holds granules that preparation skipped, and the pipeline stops on those.

    docker run --rm --gpus device=0 --shm-size=2g \
      --mount type=bind,src=$DATA,dst=$DATA,readonly \
      --mount type=bind,src=/work/l1b-vectors,dst=/out \
      --mount type=bind,src=/absolute/pipeline-work,dst=/work \
      --mount type=bind,src=/absolute/cache,dst=/home/pipeline/.cache \
      geonemo-l1b-pipeline:local pipeline $WORK/granules/GRANULE \
        --l1b-manifest $WORK/l1b.json --size 128 --run-mode inprocess \
        --db-uri /out --table sentinel2_l1b --limit 16
    
  4. The pipeline writes the table readable only by UID 10001. Make it readable before step 2:

    docker run --rm --mount type=bind,src=/work/l1b-vectors,dst=/out \
      --entrypoint chmod geonemo-l1b-pipeline:local -R a+rX /out
    

The table directory /work/l1b-vectors/sentinel2_l1b now holds manifest.json and parts/*.parquet: one row per chip with its 1024-value Clay vector, pixel position, center coordinates, sensing time and processing recipe. Keep --clay-batch-size at 256 or less (the default is 64), because Services imports one Parquet fragment at a time.

Without a GPU: a synthetic table#

synthetic_table.py writes a table in the same layout: 12 chips of one granule in three groups, with random vectors instead of Clay embeddings. Its embedding contract names it as synthetic. It needs pyarrow.

python packs/sentinel/examples/l1b-similarity/synthetic_table.py /work/l1b-vectors

Step 2: Pin the vectors as a dataset#

Pinning fixes the exact manifest.json that Services will read. After this step, do not append to the table or run cugeo-pipeline search on it: both rewrite manifest.json, and Services then refuses the changed dataset.

python packs/sentinel/examples/l1b-similarity/retrieval_config.py \
  /work/l1b-vectors/sentinel2_l1b \
  --dataset-id s2-l1b-sample \
  --producer-revision "$(git rev-parse HEAD)" > /work/retrieval.json

The output is a complete retrieval configuration: one dataset and one import-only Clay space bound to that manifest, and the development stack’s Milvus endpoint and artifact root. With fewer than 1024 chips it selects a FLAT index, which Services builds on CPU. --owner-id ACCOUNT_ID limits the dataset to those accounts. When geonemo.knowledge is importable, the script also prints the space ID that step 3 needs.

Step 3: Serve the dataset#

On a development stack#

The development stack (Run the local stack) serves the dataset with its own Milvus, retrieval worker and console. From the repository root:

deploy/development/dev.sh up --build \
  --retrieval /work/retrieval.json --retrieval-data /work/l1b-vectors

Option

Effect

--retrieval FILE

Adds a standalone Milvus and the retrieval worker, and mounts FILE as the retrieval configuration

--retrieval-data DIR

Mounts DIR read-only at the same path. Every dataset root in FILE must be inside it, and readable by UID 10001

The development stack runs without sign-in, so the console and the scripts work without signing in. The stack keeps these options until dev.sh down. Milvus is the upstream image from Docker Hub. It runs without a GPU and has no host port. Then provision, import and index the dataset:

deploy/development/dev.sh retrieval dataset-provision s2-l1b-sample s2-l1b-sample SPACE_ID
deploy/development/dev.sh retrieval dataset-import s2-l1b-sample
deploy/development/dev.sh retrieval dataset-build s2-l1b-sample
deploy/development/dev.sh retrieval dataset-status s2-l1b-sample

The first argument is the corpus ID that users search; the sample reuses the dataset ID. Search works once dataset-status reports "state": "ready". To replace the dataset, run dev.sh down -v and start again: the pinned manifest cannot change.

On an existing deployment#

An operator merges the datasets, external_spaces and allow_vector_imports keys into the private file named by GEONEMO_RETRIEVAL_CONFIG_FILE, keeping its own milvus_uri and artifact_root. The dataset root must be readable at the same path by the API and the retrieval worker. Restart both, then run the same four commands with geonemo-services retrieval on the host. A new registry needs geonemo-services database migrate-retrieval first, with the tools migration role.

A release of 1024 chips or more needs an AISAQ index, built on an admitted GPU (build_gpu_uuid and build_receipts_root in the same configuration file). The development stack’s Milvus has no GPU, so it serves FLAT releases only.

Step 4: Find similar chips and record the finding#

In the console#

On the development stack, open the console at http://127.0.0.1:3101 and choose Embeddings. Open s2-l1b-sample, then Explore: select a chip and choose Find similar to this embedding. The inspector shows each match’s chip position, center and sensing time. The console does not record a GKB finding; the script below does.

With the script#

From the repository root:

pip install ./contracts/client
python packs/sentinel/examples/l1b-similarity/find_similar_chips.py \
  --api http://127.0.0.1:8101 --corpus s2-l1b-sample

On a deployment with sign-in, set GEONEMO_SERVICES_TOKEN to an API token that includes gkb:publish and pass that deployment’s --api.

The script:

  1. reads the corpus summary to learn its embedding space and search profile;

  2. picks a stored chip as the query (the first, or --chip-row and --chip-col together, plus --product when several granules have a chip at that position);

  3. searches with that chip’s stored vector, so no encoder runs;

  4. publishes a GKB observation that cites the query chip and its closest matches;

  5. reads the finding back, then searches the GKB for every finding about that granule.

Example output from the development stack with the synthetic table, query --chip-row 128 --chip-col 256 --limit 3:

s2-l1b-sample: 12 chips, 12 searchable, model clay-v1.5
Query chip: S2B_OPER_MSI_L1B_GR_SAMPLE r128 c256 (37.7885, -122.3565) 2026-08-01T18:55:19Z
Most similar chips (score is negative_distance; higher is closer):
   2   -0.57888  S2B_OPER_MSI_L1B_GR_SAMPLE r0 c256 (37.8000, -122.3565) 2026-08-01T18:55:19Z
   3   -0.59357  S2B_OPER_MSI_L1B_GR_SAMPLE r128 c384 (37.7885, -122.3419) 2026-08-01T18:55:19Z
   4   -0.60184  S2B_OPER_MSI_L1B_GR_SAMPLE r0 c384 (37.8000, -122.3419) 2026-08-01T18:55:19Z
Recorded finding gkb_01M7V7VN8CBJWEREXVYKWEGSAX (committed)
Read back: Chips similar to S2B_OPER_MSI_L1B_GR_SAMPLE
GKB search for S2B_OPER_MSI_L1B_GR_SAMPLE: gkb_01M7V7VN8CBJWEREXVYKWEGSAX Chips similar to S2B_OPER_MSI_L1B_GR_SAMPLE

The three matches are the other chips in columns 256 and 384, the query’s group.

Rank 1 is the query chip itself, so the list starts at 2.

Limits#

  • Search takes a stored chip or a supplied vector. Text and image queries are not available, because Services does not run the L1B encoder for this space.

  • A dataset corpus returns at most 240 results and does not support filters. The script reserves one for the query chip, so --limit accepts 1 to 239.

  • The script stops without recording a finding when the search is partial, which means the index build has not finished.

  • Similarity in Clay space is not a classification. Check matches against the imagery.