Skip to content

About

Unsupervised concept mining and concept-based explanations for medical images, with accuracy, ablation, stability and randomisation tests (research use only)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

conceptmine

tests DOI License: MIT Python

Unsupervised concept mining and concept-based explanations for medical image classifiers, with tests of accuracy, ablation, stability and encoder randomisation.

Research and education use only. conceptmine is not a medical device and must not be used to interpret patient images or to support clinical decisions.

conceptmine follows the idea of Vats, Pedersen and Mohammed, Concept-based reasoning in medical imaging, International Journal of Computer Assisted Radiology and Surgery 18:1335-1339 (2023), doi:10.1007/s11548-023-02920-3: mine candidate concepts from unlabelled images, let an expert name the task-relevant ones, and explain a classifier's "abnormal" prediction by retrieving the concept whose direction best matches the image (their Concept Mapping Module, CMM). The paper shows qualitatively which concepts appear in capsule endoscopy. conceptmine measures an analogue of the idea: how often the retrieved concept is the correct finding, whether removing it changes an abnormality classifier, how stable the mined concepts are, and how retrieval behaves when the encoder is randomised.

It is an independent implementation (no public code from the authors was found) and it is not a reproduction: the paper factorises the latent space of a StyleGAN2 trained on about 200,000 capsule-endoscopy images (from three sources); here concepts are mined in the 512-d feature space of a frozen ImageNet ResNet-18, which runs on a CPU. The authors have not reviewed or endorsed this package.

What it does

Part Module
Concept candidates from unlabelled embeddings: PCA on embeddings (as in GANSpace; SeFa instead factorises generator weights), FastICA, NMF (as in CRAFT), k-means prototypes (as in ACE); both poles for PCA/ICA conceptmine.concepts.mine_concepts
References: supervised concept activation vectors (TCAV-style CAVs) and random directions cav_concepts, random_concepts
Naming (stands in for the expert): each candidate gets the label it separates best in a small labelled naming set (one-vs-rest AUROC >= 0.75), else it stays unnamed conceptmine.explain.name_concepts
CMM-style retrieval: cosine between the centred embedding and each named concept; label margin (top-1 minus top-2) as an explanation-confidence score conceptmine.explain.Explainer
Model to explain: small MLP head on frozen embeddings with analytic input gradients conceptmine.classifier.MLPHead
Evaluation: explanation accuracy with bootstrap CIs, risk-coverage, concept ablation (logit drop) with matched controls, TCAV-style directional sensitivity (no significance test), bootstrap stability (Hungarian matching), cascading encoder randomisation in the spirit of Adebayo et al. (2018) conceptmine.evaluate, conceptmine.features
CLI: demo, embed, mine, name, explain conceptmine.cli

Install and quick start

git clone https://github.com/Decadentry/conceptmine && cd conceptmine
pip install -e ".[dev]"          # core: numpy, scipy, scikit-learn
python -m pytest -q              # 30 tests, no downloads needed
conceptmine demo                 # synthetic embeddings with 5 planted concepts

With your own images (needs pip install -e ".[images]" for torch/torchvision):

conceptmine embed path/to/pool_images --out pool.npz        # labels = parent folder names
conceptmine embed path/to/naming_images --out naming.npz
conceptmine mine pool.npz --method nmf --k 20 --out candidates.npz
conceptmine name candidates.npz naming.npz --min-auc 0.75 --min-positives 3 --out named.npz
conceptmine explain named.npz naming.npz --index 0
from conceptmine import mine_concepts, name_concepts, Explainer
cs = mine_concepts(X_pool, "kmeans", k=20)
naming, aucs = name_concepts(cs, X_naming, y_naming, sorted(set(y_naming)), min_auc=0.75, min_positives=3)
labels, margin = Explainer(cs, naming).predict(X_test)

Experiment: HyperKvasir

Data: HyperKvasir labelled images (Borgli et al., Sci Data 2020; CC BY 4.0), a seeded class-balanced subset of 2,547 images (at most 150 per class, 23 classes, 1,057 with a pathological finding), downloaded by scripts/download_hyperkvasir.py and never redistributed. Splits per class: 50% mining pool (finding labels unused for mining; the abnormality classifier is trained on its normal/abnormal labels), 20% naming set, 30% test; three split seeds. k = 20 factors per method. Naming uses 17 labels (the 23 classes with finding sub-grades grouped) and ignores labels with fewer than 3 naming images. Retrieval does not use the classifier: explanations are scored on all test images with a pathological finding, grouped into five finding concepts (Barrett's, oesophagitis, polyp, mild ulcerative colitis = grades 0-1 to 1-2, severe = grades 2 to 3; hemorrhoids excluded, 6 images). Reproduce with python scripts/download_hyperkvasir.py && python experiments/run_hyperkvasir.py && python experiments/make_figures.py (about 15 minutes on 2 CPU cores).

Results (all numbers from results/*.json)

Explanation accuracy (retrieved concept = true finding; mean ± sample SD over 3 splits; n = 315 test images per split; 95% bootstrap CIs per split in hyperkvasir_accuracy.json)

Concepts Labels needed Accuracy Per-split 95% CIs (lowest - highest bound)
ICA (unsupervised, then named) naming set only 0.631 ± 0.040 0.543 - 0.727
k-means naming set only 0.577 ± 0.076 0.444 - 0.705
NMF naming set only 0.520 ± 0.136 0.381 - 0.727
PCA naming set only 0.498 ± 0.127 0.298 - 0.638
CAV (supervised, one probe per concept) naming set 0.707 ± 0.023 0.632 - 0.771
Random directions, named the same way naming set 0.310 ± 0.045 0.222 - 0.413
Always the most frequent finding - 0.296 -
  • Mined concepts scored above random directions in every split except PCA in split 1 (0.352 vs 0.359), and below supervised CAVs trained on the same naming images in every split. Not every finding was named in every split (PCA named 2-3 of 5); a finding without a named candidate counts as an error. Per-split CIs of the mined methods overlap, so their order is uncertain (no paired tests were run). The confidence, ablation and randomisation analyses below use split 0 and PCA, NMF, k-means and CAV only.
  • Label efficiency (hyperkvasir_label_efficiency.json, mean of 3 splits): with 3, 5, 10 or 20 labelled images per concept, CAVs reached 0.549, 0.550, 0.579 and 0.637; the mined methods reached at most 0.448, 0.452, 0.463 and 0.563 at the same budgets (best of ICA, NMF, k-means and PCA chosen after the fact, which favours them). In this setting, naming mined concepts did not save labels compared with training CAVs directly.
  • What was mined (hyperkvasir_inventory.json, split 0): most named candidates align with anatomy, bowel preparation or dye rather than findings (PCA/ICA give 40 candidates, two poles of 20 axes; NMF/k-means 20. Findings among named candidates: k-means 6 of 20; NMF 5 of 20; ICA 9 of 34; PCA 5 of 22), echoing the paper's report of non-pathological concepts (anatomy, debris). Random directions also passed the AUROC >= 0.75 naming rule for 24 of 40 directions, so that rule alone is weak evidence that a direction is a concept.
  • Confidence (hyperkvasir_risk_coverage.json, split 0): keeping the 20% of explanations with the largest margin (63 images) raised accuracy from 0.498 to 0.810 (k-means), 0.584 to 0.746 (PCA), 0.448 to 0.651 (NMF) and 0.711 to 0.937 (CAV).
  • Ablation (hyperkvasir_faithfulness.json, split 0; abnormality classifier trained on the pool, test AUROC 0.964): projecting each image's retrieved concept out of its embedding lowered the abnormality logit by 2.648 (k-means), 1.664 (PCA), 1.475 (NMF) and 0.503 (CAV) on average. Controls: the second-ranked finding concept 0.654, 0.212, 0.468 and 0.189; the best-aligned non-pathology concept 0.615, 0.114, 0.242 and -0.167 (it removed a similar share of the embedding on average for the mined methods, about 30% less for CAV); random directions 0.010 or less (they remove very little in 512 dimensions, so they are not a magnitude-matched control). Directions chosen because they separate findings are expected to move an abnormality logit, so this shows consistency with the classifier rather than proof that the classifier uses the concept.
  • Stability (hyperkvasir_stability.json, 5 bootstrap resamples of one pool): mean matched cosine to the full-pool concepts 0.826 (PCA, |cos|), 0.813 (NMF, signed), 0.803 (k-means, signed), 0.740 (ICA, |cos|).
  • Encoder randomisation (hyperkvasir_sanity.json, split 0, 3 random initialisations; concepts re-mined and re-named, CAVs re-trained on each randomised feature space, so labelled data re-enters at every level; this is a linear probe on random features, not the classic test of a fixed explanation): retrieval accuracy did not fall to the majority rate for most methods. Re-initialising layer4 raised it for every method and initialisation (mined 0.629-0.702 vs 0.448-0.584 pretrained; CAV 0.740-0.775 vs 0.711), although split 0 is the lowest split for k-means. A fully random encoder gave 0.165-0.644 (PCA 0.165-0.578; CAV 0.537-0.644). Retrieval accuracy therefore mostly measures how well a feature space separates findings (random convolutional features keep colour and texture), not whether an explanation reflects what a trained model learned; model-dependent checks are needed alongside it.

Accuracy and label efficiency Sanity check and risk-coverage

docs/figures/naming_sheet_nmf.png shows the kind of sheet an expert would name: the six test images that activate each candidate most, taken from the mining pool (images: HyperKvasir, CC BY 4.0).

Comparison with the source paper

The paper reports no quantitative explanation metrics, so there is nothing numeric to reproduce. What agrees: mined directions include task-relevant pathology concepts and many non-pathology ones (anatomy, bowel preparation, dye), so a naming step (an expert in the paper, labels here) decides which enter the dictionary. What differs: concept space (ImageNet ResNet-18 features, not a StyleGAN2 latent space), data (conventional endoscopy, not capsule endoscopy; Kvasir-Capsule was not reachable from the build environment), and naming (labels from a naming set, not a gastroenterologist). Results may be better with a generative latent space trained on the target domain; the library accepts any embedding, so that can be tested.

Limitations

  • Labels stand in for expert-named concepts; five finding groups are coarse, and ulcerative-colitis grades are ordinal and overlap.
  • HyperKvasir labelled images have no patient identifiers, so splits are by image; near-duplicate frames from one examination may fall in different splits and inflate all methods alike.
  • Some HyperKvasir images carry a green scope-position overlay and text; concepts may pick up such shortcuts (visible on the naming sheet).
  • One frozen encoder, k = 20, and an AUROC naming threshold of 0.75; small-class findings (Barrett's, 94 images) have wide uncertainty. Split-to-split SD is large for PCA and NMF.
  • Explanation accuracy is a proxy and, as the randomisation experiment shows, not a test of faithfulness by itself.
  • TCAV-style sensitivities are computed in hyperkvasir_faithfulness.json but without the random-CAV significance test of Kim et al. (2018), so they are not interpreted here.
  • Stability uses 5 bootstrap resamples of one pool; the randomisation experiment uses one split.

Intended use

Research on concept-based explainability, teaching, and benchmarking of new concept-mining methods on public data. Not for clinical use. No model weights trained on HyperKvasir are distributed.

How to cite

Please cite the software (see CITATION.cff): Boustani Hezarani H. conceptmine (v1.0.0). Zenodo. doi:10.5281/zenodo.24694788 (all versions; v1.0.0: 10.5281/zenodo.24694789) and the paper whose idea it follows:

Vats A, Pedersen M, Mohammed A. Concept-based reasoning in medical imaging. Int J Comput Assist Radiol Surg. 2023;18(7):1335-1339. doi:10.1007/s11548-023-02920-3

and the dataset: Borgli H, et al. HyperKvasir, a comprehensive multi-class image and video dataset for gastrointestinal endoscopy. Sci Data. 2020;7:283. doi:10.1038/s41597-020-00622-y

A short technical note is in docs/technical_note.pdf.

Licence

Code: MIT, (c) 2026 Hossein Boustani Hezarani / Decadentry. HyperKvasir images are CC BY 4.0 and are not included; figures that show them carry the attribution.

About

Unsupervised concept mining and concept-based explanations for medical images, with accuracy, ablation, stability and randomisation tests (research use only)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages