← all posts
project6 min·May 6, 2026

How I debugged a 220-million-pixel pipeline at 2am

a story about rasterio, off-by-one windows, and a GeoTIFF that was technically correct and visually insane

The model was right. The confusion matrix was right. But the map looked like somebody had sneezed the classes onto the district. Here's how I found the bug.

the setup

I was working on AgriBurn, classifying every pixel of a Sentinel-2 image of Sehore into {crop, residue, burnt, bare}. Model trained fine. Cross-validation was healthy. Test-set confusion matrix looked great. I ran full-district inference, opened the output GeoTIFF in QGIS, and my first thought was: 'why does this look like TV static'.

what i saw

Big blocks of the district were correctly classified. But between them were these 1-pixel-wide stripes of a completely different class, usually 'burnt' threading through healthy fields. Not a field. A stripe.

garbage classifications at chunk boundaries (every 2048 px)
visual of the bug: classes misaligned along chunk borders

the red herring

My first theory: class imbalance. 'Burnt' is rare, maybe the model is over-confident on rare classes near decision boundaries. I spent two hours rebalancing the training set. Same output.

Second theory: the ensemble was wrong. I ran inference with each single model. Same stripes. Different-coloured stripes, same pattern.

the actual bug

At 1:47am I noticed the stripes were exactly 2048 pixels apart. My chunk size. I was reading tiles with rasterio.Window(col_off, row_off, width, height), running inference, and writing them back. But I'd copied my feature-engineering function from the single-image case, and it was computing indices per chunk.

Several of my engineered features used neighbourhood statistics (a 3×3 focal mean). At chunk boundaries, the focal window was falling off the edge, getting padded with zeros, and pulling the indices into a garbage range. Model saw garbage, predicted the rarest class it knew.

the fix

Read with a 1-pixel overlap, then crop the result before writing:

pad = 1
read_win = Window(col - pad, row - pad, w + 2*pad, h + 2*pad)
patch = src.read(window=read_win, boundless=True, fill_value=0)
feats = engineer(patch)                    # neighbourhood-safe now
feats = feats[:, pad:-pad, pad:-pad]        # crop back
flat = feats.reshape(23, -1).T
cls = ensemble.predict(flat).reshape(h, w)
dst.write(cls.astype('uint8'), 1, window=Window(col, row, w, h))

what i take from this

  1. If your bug has a spatial period, the period is telling you where the bug lives
  2. Unit-testing a feature engineering fn on a 512×512 patch would have caught this in 30 seconds
  3. When the confusion matrix is great and the map is trash, the bug is between the model and the pixels
  4. Save intermediate GeoTIFFs. Always. Disk is cheap, your sanity isn't
Moral: your model isn't wrong. Your pipeline is wrong. It's almost always the pipeline.