This project performs bag-level binary classification using Multiple Instance Learning (MIL) with Vision Transformer (ViT) embeddings. Each bag contains multiple image instances but only the bag label is provided (no instance-level supervision). The model learns to identify key instances via attention and classify bags accordingly.
- Supports bag-level weak supervision
- Uses ViT (Base-16/224) as feature extractor
- Attention MIL for instance importance modeling
- Mixed-precision training (
torch.cuda.amp) for efficiency - Early stopping + checkpoint saving
- Bag-level inference for test data
.
├── preprocess_data.py # Convert raw bags into ViT embeddings
├── train_mil.py # Train MIL + classifier
├── test_mil.py # Bag-level inference / evaluation
└── requirements.txt # Dependenciespip install -r requirements.txtThis project consists of three main stages:
Run preprocessing to extract ViT CLS embeddings for each image instance within a bag:
python preprocess_data.py
This converts each raw bag into a matrix of shape (K, 768) where:
- K = number of images in the bag
- 768 = ViT CLS embedding dimension
Output saved as pickle:
bag_data: list of numpy arrays (K × 768)labels: bag-level labels (0 or 1)
Train the MIL classifier using instance attention aggregation:
python train_mil.pyTraining includes:
- Attention MIL
- BCEWithLogitsLoss
- Adam optimizer
- AMP mixed precision
- Early stopping & checkpointing
Best checkpoint saved to:
weights/mil_model_best.pth
Run inference on test bags:
python test_mil.py
Inference path:
bag → ViT embeddings → attention MIL → bag embedding → classifier → predicted label
Instance Embedding (ViT)
Each image → CLS token embedding (768-d)
Attention MIL Aggregation
where:
-
$x_i$ = instance embedding $\alpha_i = \text{softmax}(f(x_i))$
Bag Classification
MLP → sigmoid → bag label
-
Training converged stably
-
Validation loss slightly increased around epoch 13, indicating early signs of overfitting
-
Early stopping prevented performance degradation
-
Achieved 100% accuracy on the test set
-
Model successfully identified key instances within bags to determine bag labels


