Skip to main content

torchmil

License Contributors Release

torchmil is a PyTorch-based library for deep Multiple Instance Learning (MIL). It provides a simple, flexible, and extensible framework for working with MIL models and data.

It includes:

Installation

pip install torchmil

Quick start

You can load a MIL dataset and train a MIL model in just a few lines of code:

from torchmil.datasets import Camelyon16MIL
from torchmil.models import ABMIL
from torchmil.utils import Trainer
from torchmil.data import collate_fn
from torch.utils.data import DataLoader

# Load the Camelyon16 dataset
dataset = Camelyon16MIL(root='data', features='UNI')
dataloader = DataLoader(dataset, batch_size=4, shuffle=True, collate_fn=collate_fn)

# Instantiate the ABMIL model and optimizer
model = ABMIL(in_shape=(2048,), criterion=torch.nn.BCEWithLogitsLoss()) # each model has its own criterion
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)

# Instantiate the Trainer
trainer = Trainer(model, optimizer, device='cuda')

# Train the model
trainer.train(dataloader, epochs=10)

# Save the model
torch.save(model.state_dict(), 'model.pth')

Next steps

You can take a look at the examples to see how to use torchmil in practice. To see the full list of available models, datasets, and modules, check the API reference.

Contributing to torchmil

We welcome contributions to torchmil! There are several ways you can contribute:

  • Reporting bugs or issues you encounter while using the library, asking questions, or requesting new features: use the Github issues.
  • Improving the documentation: if you find any part of the documentation unclear or incomplete, feel free to submit a pull request with improvements.
  • If you have a new model, dataset, or utility that you think would be useful for the community, please consider submitting a pull request to add it to the library.

Take a look at CONTRIBUTING.md for more details on how to contribute.

Citation

If you find this library useful, please consider citing it:

@article{castro2025torchmil,
  title={torchmil: A PyTorch-based library for deep Multiple Instance Learning},
  author={Castro-Mac{\'\i}as, Francisco M and S{\'a}ez-Maldonado, Francisco J and Morales-{\'A}lvarez, Pablo and Molina, Rafael},
  journal={arXiv preprint arXiv:2509.08129},
  year={2025}
}

Metadata

Release files for torchmil 1.0.2

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

Source distribution (sdist)

Source distribution for torchmil 1.0.2
File Size Uploaded
torchmil-1.0.2.tar.gz 148.6 kB Details

Built distribution (wheel)

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

Total release size: 380.1 kB

Release files / torchmil-1.0.2.tar.gz

Download URL torchmil-1.0.2.tar.gz
Size 148.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b3bd6a122f0952ca6eaefda0064d7d1f8cc463ba93a4ad32f2ab0558c39b0a1e
BLAKE2b-256 checksum
How to use checksums
468d96519cb87f8f2eea81cc456a3fe32f552b642da44b72b8b48c9e41544af5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / torchmil-1.0.2-py3-none-any.whl

Download URL torchmil-1.0.2-py3-none-any.whl
Size 231.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b28b733c1143260fedbafa365c063cfaf8c7fd97398b2b6249cfd4f64bfb7ea
BLAKE2b-256 checksum
How to use checksums
b92362b050b7c16878ca2aa1b59bc0b9221e355807809d2b13f26c763e9da675
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

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