Skip to main content

Implementation of the SGU-MLP Architecture (mostly) as described in the paper "Spatial Gated Multi-Layer Perceptron for Land Use and Land Cover Mapping".

Project description

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"}}

Download files

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

Source Distribution

sgu_mlp-0.1.1.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

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

sgu_mlp-0.1.1-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file sgu_mlp-0.1.1.tar.gz.

File metadata

  • Download URL: sgu_mlp-0.1.1.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for sgu_mlp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7ac8785df8cb318ca78e7fc3d235fa280292a4f0a659fa1c502a4f93bf2a7003
MD5 8a35a33733f998b5bb3f847fdcaf8186
BLAKE2b-256 49883406ce7f764adf8837256fa8bb3a4dd77229ab1a6900e55fb23e3bb9d3ee

See more details on using hashes here.

File details

Details for the file sgu_mlp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sgu_mlp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for sgu_mlp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 950b1f584e6151a8c20a320143b5aaf418f2a9e133e18f2be9d0d23249d1195b
MD5 0343653fe9ff209a3024e36b150e992c
BLAKE2b-256 d8b70df4abeebdc99c89111720be3e4b43f562c9e78dfd1a373d1ff6b6e8e9ce

See more details on using hashes here.

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