Skip to main content

A PyTorch implementation of "MetaFormer Baselines" with optional extensions.

Project description

🥞 x-Metaformer

A PyTorch implementation of "MetaFormer Baselines" with optional extensions.
We support various self-supervised pretraining approaches such as BarlowTwins, MoCoV3 or VICReg (see x_metaformer.pretraining).

Setup

Simply run: pip install x-metaformer

Example

import torch
from x_metaformer import CAFormer, ConvFormer


my_metaformer = CAFormer(
    in_channels=3,
    depths=(3, 3, 9, 3),
    dims=(64, 128, 320, 512),
    multi_query_attention=False,  # share keys and values across query heads
    use_seqpool=True,  # use sequence pooling vom CCT
    init_kernel_size=3,
    init_stride=2,
    drop_path_rate=0.4,
    norm='ln',  # ln, bn, rms (layernorm, batchnorm, rmsnorm)
    use_grn_mlp=True,  # use global response norm in mlps
    use_dual_patchnorm=False,  # norm on both sides for the patch embedding
    use_pos_emb=True,  # use 2d sinusodial positional embeddings
    head_dim=32,
    num_heads=4,
    attn_dropout=0.1,
    proj_dropout=0.1,
    patchmasking_prob=0.05,  # replace 5% of the initial tokens with a </mask> token
    scale_value=1.0, # scale attention logits by this value
    trainable_scale=False, # if scale can be trained
    num_mem_vecs=0, # additional memory vectors (in the attention layers)
    sparse_topk=0,  # sparsify - keep only top k values (in the attention layers)
    l2=False,   # l2 norm on tokens (in the attention layers) 
    improve_locality=False,  # remove attention on own token
    use_starreglu=False  # use gated StarReLU
)

x   = torch.randn(64, 3, 64, 64)  # B C H W
out = my_metaformer(x, return_embeddings=False)  # returns average pooled tokens

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

x-Metaformer-0.3.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

x_Metaformer-0.3.1-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file x-Metaformer-0.3.1.tar.gz.

File metadata

  • Download URL: x-Metaformer-0.3.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for x-Metaformer-0.3.1.tar.gz
Algorithm Hash digest
SHA256 bed386ba8dd0ce155866c0504a0ed63406fa5bf05a586ca8370ffe3d6cc2075f
MD5 29535d1d7976cea10f962144d412d1eb
BLAKE2b-256 9e94ee62ac48ceb19d7bbf501d200f56f7f893d6a36e5fd9617343bf99a77190

See more details on using hashes here.

File details

Details for the file x_Metaformer-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: x_Metaformer-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for x_Metaformer-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 44d719f025113328ed382dfcd005d490e1c87f20cc3e6a8960055f1ef9c9284d
MD5 8624d87e6763942147939495c0695787
BLAKE2b-256 8d559f411fddde3c702192e8c843354ac73f1ed22d6b6a36585c4d2e13829083

See more details on using hashes here.

Supported by

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