Skip to main content

Reusable PyTorch modules for pathology-focused feature gating and guided token reweighting.

Project description

PFD–GSTE

Reusable PyTorch modules for pathology-focused feature gating and guided token reweighting in CNN, Transformer and hybrid image classifiers.

PFD–GSTE was developed as part of the research project Mitigating Shortcut Learning in Brain Tumour MRI Classification. This package isolates the reusable guidance components from the complete experimental repository.

Included modules

  • PathologyFocusedGate learns a soft spatial guidance mask from a CNN feature map.
  • FeatureTokenGuidance applies a spatial mask to CNN-derived transformer tokens.
  • PatchEmbed2d provides lightweight two-dimensional patch embedding.
  • PatchTokenGuidance guides image patch tokens and can optionally reduce the token grid.
  • PFDGSTEVariantA combines pathology-focused gating with feature-token guidance.
  • PFDGSTEVariantB combines pathology-focused gating with image patch-token guidance.
  • enable_mc_dropout enables dropout layers during inference.
  • mc_dropout_predict performs repeated stochastic inference for MC-dropout estimation.

Installation

After publication to PyPI:

pip install pfd-gste

Basic import

from pfd_gste import (
    PathologyFocusedGate,
    PFDGSTEVariantA,
    PFDGSTEVariantB,
    mc_dropout_predict,
)

Variant A

Variant A is intended for models whose transformer tokens are produced from a CNN feature map.

import torch

from pfd_gste import PFDGSTEVariantA

guidance = PFDGSTEVariantA(
    in_channels=2048,
    embed_dim=128,
)

features = torch.randn(2, 2048, 7, 7)
tokens, mask, alpha = guidance(features)

print(tokens.shape)
print(mask.shape)
print(alpha.shape)

Variant B

Variant B combines a CNN-derived pathology mask with image patch tokens.

import torch

from pfd_gste import PFDGSTEVariantB

guidance = PFDGSTEVariantB(
    in_channels=2048,
    embed_dim=128,
    image_channels=3,
    patch_size=16,
    min_side=7,
    max_shrink=0.50,
)

images = torch.randn(2, 3, 224, 224)
features = torch.randn(2, 2048, 7, 7)

gated_features, tokens, mask, alpha, token_hw = guidance(
    images,
    features,
    shrink=True,
)

print(gated_features.shape)
print(tokens.shape)
print(mask.shape)
print(alpha.shape)
print(token_hw)

Complete research repository

The complete repository contains the preprocessing pipeline, four matched model variants, training and held-out evaluation workflows, explainability scripts, recorded results, trained checkpoints and local Flask prototype:

https://github.com/AnnyaB/HybridResNet50V2-RViT

The Python package contains only the reusable PFD–GSTE guidance components. It does not contain datasets, trained checkpoints, complete classifiers, experimental results or clinical software.

Research-use notice

This package is provided for research and educational use only. It is not a certified medical device and must not be used for clinical diagnosis, patient management or treatment decisions.

Licence

MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pfd_gste-0.1.0.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pfd_gste-0.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file pfd_gste-0.1.0.tar.gz.

File metadata

  • Download URL: pfd_gste-0.1.0.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pfd_gste-0.1.0.tar.gz
Algorithm Hash digest
SHA256 35746248cc068f1f6696850cb33bf606050e130590c503d08445454703aa5e7f
MD5 a65a9c691c794c2d473643f25234e70d
BLAKE2b-256 3cd81fa5e5f0dbd27c20bcfb43724ce6dd724e220ca20831d8d6eb78f486c5e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pfd_gste-0.1.0.tar.gz:

Publisher: publish-pypi.yml on AnnyaB/HybridResNet50V2-RViT

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pfd_gste-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pfd_gste-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pfd_gste-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa0a3f4b860a74b850881a3e3d165803ea921cefa12f674f4034b73c0f65cb03
MD5 c941c37de63fd244ef6361dd81627e33
BLAKE2b-256 3c4f46fba3d62fccf84ae9e298f1ea2f260d456fa3830c3bd31365cdb56f4302

See more details on using hashes here.

Provenance

The following attestation bundles were made for pfd_gste-0.1.0-py3-none-any.whl:

Publisher: publish-pypi.yml on AnnyaB/HybridResNet50V2-RViT

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page