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 git+https://github.com/simulacrum6/sgu-mlp.git

If you want to run the experiments as well:

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.0.tar.gz (14.0 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.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sgu_mlp-0.1.0.tar.gz
  • Upload date:
  • Size: 14.0 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.0.tar.gz
Algorithm Hash digest
SHA256 4db835a2ee4516c60675a827358d48f8c6b9a5784e6792227709c34c649f755d
MD5 0d0329e2aecc2c005e498d0eeeb3dfbc
BLAKE2b-256 613fee36be8497d84cd99caf77e502712bd791869136c715a12aa1cb36be87ad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sgu_mlp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24244d11943ace86c7156889be496a0de6bbaaee601661c41fb66aa1986f394f
MD5 d4e67f7092d5ae26d8a83a27980fbb21
BLAKE2b-256 4d7c00fedb91d51bf5ae7bf7f96759925af7130ed796f4dbbfd55c3982fba612

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