Code for Efficient Graph-Based Metric Learning for Medical Image Classification (Aparco-Cardenas and Falcão, SIPAIM 2026).
OPFNet is a small CNN encoder + projector trained with a two-stage loss (triplet loss, then an Optimum-Path Forest (OPF) loss) and classified with a single-model OPF at inference time.
main.py # CLI: builds data/model and runs the two-stage training
opfnet/
model.py # encoder + projector
data.py # Lab image dataset, loading from train/test file lists
losses.py # triplet mining and OPF-based loss
sampling.py # stratified sampling, OPF prototype selection
trainer.py # triplet-stage and OPF-stage epochs
evaluate.py # single-model OPF inference (balanced acc., macro F1)
opforch/ # bundled PyTorch OPF implementation (Apache 2.0, G. de Rosa)
data/ # train/test file lists; eggs.tar.gz = eggs lists + images
Python >= 3.12 and pip install -r requirements.txt. opforch is bundled and
imported directly, so no installation is needed. Run everything from the repository root.
Each dataset is read from data/<dataset>/<dataset>.{train,test}.csv, plain text lists with one
image path per line.
eggs(Helminth eggs) is included asdata/eggs.tar.gz(train/test lists + images). Extract it from the repository root:tar -xzf data/eggs.tar.gz -C data
larvae,protozoan: download the images from the intestinal parasite datasets of Suzuki et al. (2013) and place them indata/<dataset>/orig/.brisc2025: download BRISC 2025 (Fateh et al., Scientific Data, 2026) and place the images indata/brisc2025/{train,test}/.
Pass the dataset name. The train and test sets are read from
data/<dataset>/<dataset>.train.csv and data/<dataset>/<dataset>.test.csv:
python main.py --dataset eggs # eggs, larvae, protozoan or brisc2025Main options: --transition (last epoch of the triplet stage, default 30),
--lr-tri / --lr (learning rate of each stage), --batch-size-opf,
--embedding-size, --num-epochs. The model is evaluated on the test set after the last
epoch only; use --test-interval N for additional evaluations every N epochs.
Metrics go to TensorBoard (--tensorboard-root).
We thank Gustavo de Rosa for creating and sharing
OPForch, the PyTorch implementation of Optimum-Path Forest
on which our OPF decision layer is built. A minimal copy of it is bundled in opforch/ under its
original Apache 2.0 license.
@inproceedings{aparco2026efficient,
title = {Efficient Graph-Based Metric Learning for Medical Image Classification},
author = {Aparco-Cardenas, David and Falc{\~a}o, Alexandre X.},
booktitle = {SIPAIM},
year = {2026}
}