Skip to main content

MLP-Mixer for TensorFlow.

Project description

N-dimensional MLP-Mixer TensorFlow

Based on MLP-Mixer [1], but NdMixerBlock is generalized to n-dimensions.

Original MLP-Mixer

To use the MLP-Mixer as described in the paper:

from nd_mlp_mixer import MLPMixer

# S/32, from table 1
mlp_mixer = MLPMixer(num_classes=1000, 
                     num_blocks=8,
                     patch_size=32, 
                     hidden_dim=512,
                     tokens_mlp_dim=256,
                     channels_mlp_dim=2048)

Or a more reasonable size model, on MNIST:

import tensorflow as tf
from tensorflow.keras import datasets, layers
from nd_mlp_mixer import MLPMixer

# Load data
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
train_images, test_images = train_images.astype("float32"), test_images.astype("float32")
height, width = train_images.shape[-2:]
num_classes = 10

# Prepare the model (add channel dimension to images)
inputs = layers.Input(shape=(height, width))
h = layers.Reshape([28, 28, 1])(inputs)
mlp_mixer = MLPMixer(num_classes=10, 
                     num_blocks=2, 
                     patch_size=4, 
                     hidden_dim=28, 
                     tokens_mlp_dim=28,
                     channels_mlp_dim=28)(h)
model = tf.keras.Model(inputs=inputs, outputs=mlp_mixer)
print(model.summary())

# Train
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
history = model.fit(train_images, train_labels, batch_size=64, epochs=10,
                    validation_data=(test_images, test_labels), verbose=2)

[1] MLP-Mixer paper:

https://arxiv.org/abs/2105.01601

@misc{tolstikhin2021mlpmixer,
      title={MLP-Mixer: An all-MLP Architecture for Vision}, 
      author={Ilya Tolstikhin and Neil Houlsby and Alexander Kolesnikov and Lucas Beyer and Xiaohua Zhai and Thomas Unterthiner and Jessica Yung and Daniel Keysers and Jakob Uszkoreit and Mario Lucic and Alexey Dosovitskiy},
      year={2021},
      eprint={2105.01601},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

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

nd_mlp_mixer-0.1.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

nd_mlp_mixer-0.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nd_mlp_mixer-0.1.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.6.0.post20210108 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for nd_mlp_mixer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 908970b7bec82b9092b11c39a608a14b1dcf0b92b46f225cf5987b6a57d43ebe
MD5 f8391e010f6607519afa704cdcc1936d
BLAKE2b-256 b7a846bfc622cd0995bb9b67e5402deb931b8756aff44e04e2ec8cb1c10d6300

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nd_mlp_mixer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.6.0.post20210108 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for nd_mlp_mixer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5976351ad3db1cb2de90e209e52ea1344f8c56f824fa78f3bdd6cd3969dfb914
MD5 113999c5a3b0cb6f3dced8c4c3dd6288
BLAKE2b-256 8efef16cc4a4702adc60e80ffd37340b053a9086058d1474fe301e4611be2dce

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