forked from google-research/vision_transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b476038
commit fca1235
Showing
10 changed files
with
233 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import ml_collections | ||
from vit_jax.configs import common | ||
from vit_jax.configs import models | ||
|
||
|
||
def get_config(): | ||
"""Returns config for training Mixer-B/16 on cifar10.""" | ||
config = common.get_config() | ||
config.model_type = 'Mixer' | ||
config.model = models.get_mixer_b16_config() | ||
config.dataset = 'cifar10' | ||
config.total_steps = 10_000 | ||
config.pp = ml_collections.ConfigDict( | ||
{'train': 'train[:98%]', 'test': 'test', 'resize': 256, 'crop': 224}) | ||
return config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright 2021 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Any | ||
|
||
import einops | ||
import flax.linen as nn | ||
import jax.numpy as jnp | ||
|
||
|
||
class MlpBlock(nn.Module): | ||
mlp_dim: int | ||
|
||
@nn.compact | ||
def __call__(self, x): | ||
y = nn.Dense(self.mlp_dim)(x) | ||
y = nn.gelu(y) | ||
return nn.Dense(x.shape[-1])(y) | ||
|
||
|
||
class MixerBlock(nn.Module): | ||
"""Mixer block layer.""" | ||
tokens_mlp_dim: int | ||
channels_mlp_dim: int | ||
|
||
@nn.compact | ||
def __call__(self, x): | ||
y = nn.LayerNorm()(x) | ||
y = jnp.swapaxes(y, 1, 2) | ||
y = MlpBlock(self.tokens_mlp_dim, name='token_mixing')(y) | ||
y = jnp.swapaxes(y, 1, 2) | ||
x = x + y | ||
y = nn.LayerNorm()(x) | ||
return x + MlpBlock(self.channels_mlp_dim, name='channel_mixing')(y) | ||
|
||
|
||
class MlpMixer(nn.Module): | ||
"""Mixer architecture.""" | ||
patches: Any | ||
num_classes: int | ||
num_blocks: int | ||
hidden_dim: int | ||
tokens_mlp_dim: int | ||
channels_mlp_dim: int | ||
|
||
@nn.compact | ||
def __call__(self, inputs, *, train): | ||
del train | ||
x = nn.Conv(self.hidden_dim, self.patches.size, | ||
strides=self.patches.size, name='stem')(inputs) | ||
x = einops.rearrange(x, 'n h w c -> n (h w) c') | ||
for _ in range(self.num_blocks): | ||
x = MixerBlock(self.tokens_mlp_dim, self.channels_mlp_dim)(x) | ||
x = nn.LayerNorm(name='pre_head_layer_norm')(x) | ||
x = jnp.mean(x, axis=1) | ||
return nn.Dense(self.num_classes, kernel_init=nn.initializers.zeros, | ||
name='head')(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters