Skip to main content

leakcheck

A small tool that checks whether any of your test images secretly also exist in your training set.

I started thinking about this while working on a brain tumor MRI classifier. If the same scan ends up in both train and test (even resized, renamed, or saved as a different format), the model gets tested on something it has already seen, and the accuracy number stops meaning much. Checking this by hand is impossible once you have thousands of images, so I wrote this.

A test image next to its copy in the train set

Install

pip install leakcheck

How to use it

Point it at your train and test folders:

leakcheck train/ test/

You'll get something like this:

LEAK  leaked_0.jpg  ~  train_0.png  (distance 0)
LEAK  leaked_1.jpg  ~  train_1.png  (distance 0)
...
5 of 25 test images (20.0%) also appear in train.

A few extra options:

leakcheck train/ test/ --csv leaks.csv       # save the results to a CSV file
leakcheck train/ test/ --show leak_pairs     # save side-by-side images so you can check them yourself
leakcheck train/ test/ --threshold 0         # only flag exact matches

The default threshold is 5. Raise it to catch more loosely similar images, or lower it if you're getting false alarms.

How it works

Every image gets turned into a 64-bit "fingerprint" using perceptual hashing. Unlike a normal file hash, this fingerprint depends on what the image looks like, not on the exact bytes, so a resized or recompressed copy ends up with the same (or almost the same) fingerprint.

To compare two images, I count how many bits differ between their fingerprints. Zero means they're basically identical.

Comparing every test image with every train image gets slow fast, so the train fingerprints go into a BK-tree. It's a data structure built for "find anything within distance X" searches, and it uses the triangle inequality to skip big parts of the tree that can't contain a match.

Tests

pip install pytest
pytest -v

One of the tests compares the BK-tree against a plain brute-force search on random data, to make sure the faster method never misses anything.

License

MIT

Metadata

Release files for imgleakcheck 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for imgleakcheck 0.1.0
File Size Uploaded
imgleakcheck-0.1.0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for imgleakcheck 0.1.0
File Interpreter ABI Platform
imgleakcheck-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.9 kB

Release files / imgleakcheck-0.1.0.tar.gz

Download URL imgleakcheck-0.1.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3e12c11a8d1cbd78f0a10d772becf61a2871941454771f5390d91a4e7855cfc2
BLAKE2b-256 checksum
How to use checksums
703549327c53f3de34a29686de422c753c0d4075545ff713a483c1e9fc74ac92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / imgleakcheck-0.1.0-py3-none-any.whl

Download URL imgleakcheck-0.1.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5f594c9226561ab243fe9d07faf00f10154af5a60821f72440e55722b57a328
BLAKE2b-256 checksum
How to use checksums
f8a3ccc1affe227428473ea4c44281ceb0272443fd3b46ae70ff8db2b9c58b07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page