MCPcopy Index your code
hub / github.com/DanielShalam/BPA

github.com/DanielShalam/BPA @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
222 symbols 561 edges 31 files 35 documented · 16%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

BPA: The balanced-pairwise-affinities Feature Transform (ICML 2024)

This repository contains the official PyTorch implementation of BPA (formerly SOT) — the Balanced-Pairwise-Affinities feature transform — as described in our paper The Balanced-Pairwise-Affinities Feature Transform, presented at ICML 2024.

BPA

BPA enhances the representation of a set of input features to support downstream tasks such as matching or grouping.

The transformed features capture both direct pairwise similarity and third-party agreement — how other instances in the set influence similarity between a given pair. This enables BPA to model higher-order relations effectively.


PWC PWC PWC PWC PWC PWC


Few-Shot Classification Results

Dataset Method 5-Way 1-Shot 5-Way 5-Shot
MiniImagenet PTMAP-BPAp 83.19 89.56
PTMAP-BPAt 84.18 90.51
PTMAP-SF-BPA 85.59 91.34
CIFAR-FS PTMAP-BPAp 87.37 91.12
PTMAP-SF-BPA 89.94 92.83
CUB PTMAP-BPAp 91.90 94.63
PTMAP-SF-BPA 95.80 97.12

Setup

Datasets

Instructions for downloading and preparing the few-shot classification datasets are available in the datasets directory.

Pretrained Models

Most results in the paper use fine-tuned models:

BPA Checkpoint

We provide a checkpoint for PTMAP-BPAt, which yields:

  • 84.69% accuracy for 5-way 1-shot (vs. 84.18% in the paper)
  • 90.30% accuracy for 5-way 5-shot (vs. 90.51% in the paper)

Usage

Quick Start

BPA can be applied in just two lines of code:

import torch
from bpa import BPA

x = torch.randn(100, 128)  # [n_samples, dim]
x = BPA()(x)               # Output shape: [n_samples, n_samples]

Training PT-MAP-BPAt on MiniImagenet

  1. Download the pretrained WRN feature extractor.
  2. Create an empty checkpoints directory.
  3. Extract the downloaded file into the checkpoints folder.

Run the following command to train BPA with PT-MAP:

python train.py \
  --sink_iters 5 \
  --distance_metric cosine \
  --ot_reg 0.2 \
  --method pt_map_bpa \
  --backbone wrn \
  --augment false \
  --lr 5e-5 \
  --weight_decay 0. \
  --max_epochs 50 \
  --train_way 10 \
  --val_way 5 \
  --num_shot 5 \
  --num_query 15 \
  --train_episodes 200 \
  --eval_episodes 400 \
  --checkpoint_dir ./checkpoints/pt_map_bpa \
  --data_path <your_dataset_path/miniimagenet/> \
  --pretrained_path ./checkpoints/miniImagenet/WideResNet28_10_S2M2_R/470.tar

Alternatively, use the trained checkpoint.


Evaluation

You can evaluate using the original PT-MAP repository:

  1. Clone the PT-MAP repository.
  2. Navigate to method/pt_map/evaluation/ and replace the files with the ones from this repository.
  3. Edit the following:
  4. Set checkpoint_dir in save_plk.py to the location of your BPA checkpoint.
  5. Update _datasetFeaturesFiles in FSLTask.py to point to your feature files.
  6. Create feature files by running:
python save_plk.py --dataset miniImagenet --method S2M2_R --model WideResNet28_10
  1. Then run the evaluation:
   python test_standard.py

Alternatively, evaluate directly within this repository

You can run evaluation directly using the same script as training, with a few extra flags:

python train.py \
  --eval true \
  --pretrained_path <path_to_checkpoint> \
  --backbone <backbone_name> \
  --test_episodes 2000 ```

Make sure to include the same arguments you used during training (e.g., --method, --data_path, etc.) so the evaluation runs consistently.


Logging

We support logging and visualization via Weights & Biases (wandb). This helps track your training and evaluation metrics in real-time.

To enable logging:

  1. Install the wandb package:
pip install wandb
  1. Add the following flags to your command:
--wandb true --project <project_name> --entity <wandb_entity>

Replace <project_name> with your wandb project name, and <wandb_entity> with your wandb username or team name.


Citation

If you find this repository useful in your research, please cite:

@article{shalam2024balanced,
  title={The Balanced-Pairwise-Affinities Feature Transform},
  author={Shalam, Daniel and Korman, Simon},
  journal={arXiv preprint arXiv:2407.01467},
  year={2024}
}

Acknowledgment

Leveraging the Feature Distribution in Transfer-based Few-Shot Learning

S2M2 Charting the Right Manifold: Manifold Mixup for Few-shot Learning

Few-Shot Learning via Embedding Adaptation with Set-to-Set Functions

Core symbols most depended-on inside this repo

cuda
called by 10
methods/pt_map/pt_map_loss.py
max
called by 8
utils.py
update
called by 7
utils.py
mixup_data
called by 6
models/res_mixup_model.py
_make_layer
called by 4
models/res_mixup_model.py
mixup_data
called by 4
models/wrn_mixup_model.py
_make_layer
called by 4
models/resnet12.py
eval_one_epoch
called by 3
train.py

Shape

Method 124
Function 60
Class 38

Languages

Python100%

Modules by API surface

utils.py32 symbols
methods/pt_map/test_standard.py22 symbols
methods/pt_map/evaluation/test_standard.py19 symbols
methods/pt_map/pt_map_loss.py18 symbols
methods/pt_map/data/dataset.py17 symbols
models/wrn_mixup_model.py16 symbols
models/res_mixup_model.py14 symbols
models/resnet12.py9 symbols
methods/pt_map/data/datamgr.py9 symbols
methods/pt_map/save_plk.py7 symbols
methods/pt_map/evaluation/save_plk.py6 symbols
bpa/balanced_pairwise_affinities.py6 symbols

For agents

$ claude mcp add BPA \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact