-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
171 lines (136 loc) · 5.62 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'''
- `train-folder`: The folder of training images
- `valid-folder`: The folder of validation images
- `model-folder`: Where the model after training saved
- `num-classes`: The number of your problem classes.
- `batch-size`: The batch size of the dataset
- `c`: Patch Projection Dimension
- `ds`: Token-mixing units. It was mentioned in the paper on [page 3](https://arxiv.org/pdf/2105.01601.pdf)
- `dc`: Channel-mixing units. It was mentioned in the paper on [page 3](https://arxiv.org/pdf/2105.01601.pdf)
- `num-of-mlp-blocks`: The number of MLP Blocks
- `learning-rate`: The learning rate of Adam Optimizer
'''
import numpy as np
import tensorflow as tf
from keras import layers
from tensorflow.keras.layers import Embedding
from tensorflow.keras.layers import Embedding, Input, LayerNormalization, Dense, GlobalAveragePooling1D, Dropout
from tensorflow.keras.layers.experimental.preprocessing import Normalization, Resizing, RandomFlip, RandomRotation, \
RandomZoom
from tensorflow.keras.models import Sequential
class Patches(layers.Layer):
def __init__(self, patch_size):
super(Patches, self).__init__()
self.patch_size = patch_size
def call(self, images):
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, self.patch_size, self.patch_size, 1],
strides=[1, self.patch_size, self.patch_size, 1],
rates=[1, 1, 1, 1],
padding='VALID',
)
dim = patches.shape[-1]
patches = tf.reshape(patches, (batch_size, -1, dim))
return patches
class MLPBlock(tf.keras.layers.Layer):
def __init__(self, S, C, DS, DC):
super(MLPBlock, self).__init__()
self.layerNorm1 = LayerNormalization()
self.layerNorm2 = LayerNormalization()
w_init = tf.random_normal_initializer()
self.DS = DS
self.DC = DC
self.W1 = tf.Variable(
initial_value=w_init(shape=(S, DS), dtype="float32"),
trainable=True,
)
self.W2 = tf.Variable(
initial_value=w_init(shape=(DS, S), dtype="float32"),
trainable=True,
)
self.W3 = tf.Variable(
initial_value=w_init(shape=(C, DC), dtype="float32"),
trainable=True,
)
self.W4 = tf.Variable(
initial_value=w_init(shape=(DC, C), dtype="float32"),
trainable=True,
)
def call(self, X):
# patches (..., S, C)
batch_size, S, C = X.shape
# Token-mixing
# (..., C, S)
X_T = tf.transpose(self.layerNorm1(X), perm=(0, 2, 1))
# assert X_T.shape == (batch_size, C, S), 'X_T.shape: {}'.format(X_T.shape)
W1X = tf.matmul(X_T, self.W1) # (..., C, S) . (S, DS) = (..., C, DS)
# (..., C, DS) . (DS, S) == (..., C, S)
# (..., C, S). T == (..., S, C)
# (..., S, C) + (..., S, C) = (..., S, C)
U = tf.transpose(tf.matmul(tf.nn.gelu(W1X), self.W2), perm=(0, 2, 1)) + X
# Channel-minxing
W3U = tf.matmul(self.layerNorm2(U), self.W3) # (...,S, C) . (C, DC) = (..., S, DC)
Y = tf.matmul(tf.nn.gelu(W3U), self.W4) + U # (..., S, DC) . (..., DC, C) + (..., S, C) = (..., S, C)
return Y
class MLPMixer(tf.keras.models.Model):
def __init__(self, patch_size, S, C, DS, DC, num_of_mlp_blocks, image_size, batch_size, num_classes):
super(MLPMixer, self).__init__()
self.projection = Dense(C)
self.mlpBlocks = [MLPBlock(S, C, DS, DC) for _ in range(num_of_mlp_blocks)]
self.batch_size = batch_size
self.patch_size = patch_size
self.S = S
self.C = C
self.DS = DS
self.DC = DC
self.image_size = image_size
self.num_classes = num_classes
self.data_augmentation = tf.keras.Sequential(
[
Normalization(),
# Resizing(image_size, image_size),
RandomFlip("horizontal"),
RandomRotation(factor=0.02),
RandomZoom(
height_factor=0.2, width_factor=0.2
),
],
name="data_augmentation",
)
self.classificationLayer = Sequential([
GlobalAveragePooling1D(),
Dropout(0.2),
Dense(num_classes, activation='softmax')
])
def extract_patches(self, images, patch_size):
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, patch_size, patch_size, 1],
strides=[1, patch_size, patch_size, 1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patches = tf.reshape(patches, [batch_size, -1, 3 * patch_size ** 2])
return patches
def call(self, images):
# input
# images shape: (batch_size, image_size, image_size, 3) = (32, 64, 64, 3)
batch_size = images.shape[0]
augumented_images = self.data_augmentation(images)
# assert augumented_images.shape == (batch_size, self.image_size, self.image_size, 3)
# patches shape: (batch_size, S, 3 * patch_size ** 2)
X = self.extract_patches(augumented_images, self.patch_size)
# Per-patch Fully-connected
# X shape: (batch_size, S, C)
X = self.projection(X)
# assert X.shape == (batch_size, self.S, self.C)
for block in self.mlpBlocks:
X = block(X)
# assert X.shape == (batch_size, self.S, self.C)
# out shape: (batch_size, C)
out = self.classificationLayer(X)
# assert out.shape == (batch_size, self.num_classes)
return out