Skip to main content

SGU-MLP: Pytorch Impelementation of the Spatial Gated Multilayer Perceptron

image

Pytorch implementation of the SGU-MLP Architecture from the paper "Spatial Gated Multi-Layer Perceptron for Land Use". The implementation follows the implementation of the original authors. It differs from the architecture described in the paper published on GitHub in the following aspects:

  • Input patches and DWC Block outputs are combined using a residual connection.
  • Patches are embedded using a Convolutional Layer (by default, the embedding_kernel_size is set to 1 to achieve per pixel projections).
  • Input patches can be overlapping (this is only relevant for data preprocessing, not for general model usage).

Additionally, this implementation makes the initial residual weights configurable and learnable.

An implementation of MLP Mixer with optional usage of Spatial Gated Units in the MLP Blocks is also included. See src/sgu_mlp/models.py for details.

Basic Usage

from sgu_mlp import SGUMLPMixer
import torch

height = width = 64
num_patches = height * width
channels = 3
patch_size = 11

input_dimensions = (patch_size, patch_size, channels)
patches = torch.randn(num_patches, *input_dimensions)

# feature extractor
sgu = SGUMLPMixer(
    input_dimensions=input_dimensions,
    token_features=32,
    mixer_features_channel=64,
    mixer_features_sequence=96,
)
out = sgu(patches)

# (num_patches, patch_size**2, channels)
print(out.shape) 

# classifier
num_classes = 8
sgu_clf = SGUMLPMixer(
    input_dimensions=input_dimensions,      
    token_features=32,                      
    mixer_features_channel=64,              
    mixer_features_sequence=96,
    num_classes=num_classes
)
out = sgu_clf(patches)

# (num_patches, num_classes)
print(out.shape)

Installation

If you just want to use SGU-MLP Architecture:

pip install sgu-mlp
pip install git+https://github.com/simulacrum6/sgu-mlp.git

If you want to run the experiments as well, add the [experiments] extras:

pip install "sgu-mlp[experiments]"
pip install "git+https://github.com/simulacrum6/sgu-mlp.git#egg=sgu-mlp[experiments]"

Running Experiments

To run the replication experiment, first download the benchmark datasets. You can download them from gDrive or run

python3 -m experiments.run download --out_dir='/path/to/data/dir'

To run the replication experiment:

python3 -m experiments.run experiment replication

Per default, it is assumed, that you run the script from the root of this repository (--root_dir='data/config').

To run a custom experiment:

python3 -m experiments.run run <experiment_type> <cfg_path>

Arguments

  • <experiment_type: "cv" or "ood".
  • <cfg_path>: path to the config file for the experiment.

See data/config for examples. Of the config format.

References

Citations

When using this software for your research, please cite the orginial article as well as this version of the software.

@article{10399888,
  author={Jamali, Ali and Roy, Swalpa Kumar and Hong, Danfeng and Atkinson, Peter M. and Ghamisi, Pedram},
  journal={IEEE Geoscience and Remote Sensing Letters}, 
  title={Spatial-Gated Multilayer Perceptron for Land Use and Land Cover Mapping}, 
  year={2024},
  volume={21},
  number={},
  pages={1-5},
  keywords={Feature extraction;Classification algorithms;Hyperspectral imaging;Data models;Transformers;Biological system modeling;Training data;Attention mechanism;image classification;spatial gating unit (SGU);vision transformers (ViTs)},
  doi={10.1109/LGRS.2024.3354175}}
@software{hamacher2024sgumlp,
  title = {SGU-MLP: Pytorch Implementation of the Spatial Gated Multi-Layer Perceptron},
  author = {Hamacher, Marius},
  year = {2025},
  url = {https://github.com/simulacrum6/sgu-mlp},
  version = {0.1.0},
  note = {Pytorch Implementation of the SGU-MLP Architecture from the paper "Spatial Gated Multi-Layer Perceptron for Land Use and Land Cover Mapping"}}

Metadata

Release files for sgu-mlp 0.1.1

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

Source distribution (sdist)

Source distribution for sgu-mlp 0.1.1
File Size Uploaded Provenance Actions
sgu_mlp-0.1.1.tar.gz 18.5 kB Without provenance Details for sgu_mlp-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sgu-mlp 0.1.1
File Interpreter ABI Platform Actions
sgu_mlp-0.1.1-py3-none-any.whl Python 3 none any Details for sgu_mlp-0.1.1-py3-none-any.whl

Total release size: 38.6 kB

Release files / sgu_mlp-0.1.1.tar.gz

Download URL sgu_mlp-0.1.1.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7ac8785df8cb318ca78e7fc3d235fa280292a4f0a659fa1c502a4f93bf2a7003
BLAKE2b-256 checksum
How to use checksums
49883406ce7f764adf8837256fa8bb3a4dd77229ab1a6900e55fb23e3bb9d3ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

Release files / sgu_mlp-0.1.1-py3-none-any.whl

Download URL sgu_mlp-0.1.1-py3-none-any.whl
Size 20.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
950b1f584e6151a8c20a320143b5aaf418f2a9e133e18f2be9d0d23249d1195b
BLAKE2b-256 checksum
How to use checksums
d8b70df4abeebdc99c89111720be3e4b43f562c9e78dfd1a373d1ff6b6e8e9ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

Security for sgu-mlp 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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