-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobilevit_v2.py
307 lines (251 loc) · 9.49 KB
/
mobilevit_v2.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import warnings
from typing import Optional
import numpy as np
from keras import Model, Input
from keras.layers import GlobalAveragePooling2D, Dropout, Dense
from keras.utils import get_file
from configs import get_mobile_vit_v2_configs
from utils.base_layers import ConvLayer, InvertedResidualBlock
from utils.mobilevit_v2_block import MobileViT_v2_Block
WEIGHTS_RELEASE_TAG_VERSION = 0.5
WEIGHTS_URL = "https://github.com/veb-101/keras-vision/releases/download/v{weight_release_tag}/{file_name}"
def MobileViT_v2(
configs,
linear_drop: float = 0.0,
attention_drop: float = 0.0,
dropout: float = 0.0,
num_classes: int | None = 1000,
input_shape: tuple[int, int, int] = (256, 256, 3),
model_name: str = "MobileViT-v2-1.0",
):
"""
Build the MobileViT-v2 model architecture.
Parameters
----------
configs : object
A dataclass instance containing model information such as per-layer output channels, transformer embedding dimensions, transformer repeats, and IR expansion factor.
linear_drop : float, optional
Dropout rate for the Dense layers. Default is 0.0.
attention_drop : float, optional
Dropout rate for the attention matrix. Default is 0.0.
dropout : float, optional
Dropout rate to be applied between different layers. Default is 0.0.
num_classes : int, optional
The number of output classes for the classification task. If None, no classification layer is added. Default is 1000.
input_shape : tuple of int, optional
The shape of the input data in the format (height, width, channels). Default is (256, 256, 3).
model_name : str, optional
The name of the model. Default is "MobileViT-v3-1.0".
Returns
-------
model : keras.Model
The constructed MobileViT-v2 model instance.
Example
-------
>>> configs = get_mobile_vit_v2_configs(width_multiplier=1.0)
>>> model = MobileViT_v2(
>>> configs=configs,
>>> linear_drop=0.1,
>>> attention_drop=0.1,
>>> dropout=0.2,
>>> num_classes=1000,
>>> input_shape=(256, 256, 3),
>>> model_name="MobileViT-v2-1.0"
>>> )
>>> model.summary()
"""
input_layer = Input(shape=input_shape)
# Block 1
out = ConvLayer(
num_filters=configs.block_1_1_dims,
kernel_size=3,
strides=2,
name="block-1-Conv",
)(input_layer)
out = InvertedResidualBlock(
in_channels=configs.block_1_1_dims,
out_channels=configs.block_1_2_dims,
depthwise_stride=1,
expansion_factor=configs.depthwise_expansion_factor,
name="block-1-IR2",
)(out)
# Block 2
out = InvertedResidualBlock(
in_channels=configs.block_1_2_dims,
out_channels=configs.block_2_1_dims,
depthwise_stride=2,
expansion_factor=configs.depthwise_expansion_factor,
name="block-2-IR1",
)(out)
out = InvertedResidualBlock(
in_channels=configs.block_2_1_dims,
out_channels=configs.block_2_2_dims,
depthwise_stride=1,
expansion_factor=configs.depthwise_expansion_factor,
name="block-2-IR2",
)(out)
# # ========================================================
# # According to paper, there should be one more InvertedResidualBlock, but it not present in the final code.
# out_b2_3 = InvertedResidualBlock(
# in_channels=configs.block_2_2_dims,
# out_channels=configs.block_2_3_dims,
# depthwise_stride=1,
# expansion_factor=configs.depthwise_expansion_factor,
# name="block-2-IR3",
# )(out)
# out = out + out_b2_3
# # ========================================================
# Block 3
out = InvertedResidualBlock(
in_channels=configs.block_2_2_dims,
out_channels=configs.block_3_1_dims,
depthwise_stride=2,
expansion_factor=configs.depthwise_expansion_factor,
name="block-3-IR1",
)(out)
out = MobileViT_v2_Block(
out_filters=configs.block_3_2_dims,
embedding_dim=configs.tf_block_3_dims,
transformer_repeats=configs.tf_block_3_repeats,
name="MobileViTBlock-1",
dropout=dropout,
attention_drop=attention_drop,
linear_drop=linear_drop,
)(out)
# Block 4
out = InvertedResidualBlock(
in_channels=configs.block_3_2_dims,
out_channels=configs.block_4_1_dims,
depthwise_stride=2,
expansion_factor=configs.depthwise_expansion_factor,
name="block-4-IR1",
)(out)
out = MobileViT_v2_Block(
out_filters=configs.block_4_2_dims,
embedding_dim=configs.tf_block_4_dims,
transformer_repeats=configs.tf_block_4_repeats,
name="MobileViTBlock-2",
dropout=dropout,
attention_drop=attention_drop,
linear_drop=linear_drop,
)(out)
# Block 5
out = InvertedResidualBlock(
in_channels=configs.block_4_2_dims,
out_channels=configs.block_5_1_dims,
depthwise_stride=2,
expansion_factor=configs.depthwise_expansion_factor,
name="block-5-IR1",
)(out)
out = MobileViT_v2_Block(
out_filters=configs.block_5_2_dims,
embedding_dim=configs.tf_block_5_dims,
transformer_repeats=configs.tf_block_5_repeats,
name="MobileViTBlock-3",
dropout=dropout,
attention_drop=attention_drop,
linear_drop=linear_drop,
)(out)
if num_classes:
# Output layer
out = GlobalAveragePooling2D()(out)
if linear_drop > 0.0:
out = Dropout(rate=dropout)(out)
out = Dense(units=num_classes)(out)
model = Model(inputs=input_layer, outputs=out, name=model_name)
return model
def build_MobileViT_v2(
width_multiplier: float = 1.0,
num_classes: int = 1000,
input_shape: tuple = (256, 256, 3),
include_top: bool = True, # Whether to include the classification layer in the model
pretrained: bool = False, # Whether to load pretrained weights
cache_dir: Optional[str] = None, # Local cache directory for weights
updates: Optional[dict] = None,
pretrained_weight_name: str = "keras_mobilevitv2-im1k-256-0.5.weights.h5",
**kwargs,
):
"""
Build a MobileViT-v2 classification model.
Parameters
----------
width_multiplier : float, optional
A multiplier for the width (number of channels) of the model layers. Default is 1.0,
which corresponds to the base model.
num_classes : int, optional
The number of output classes for the classification task. Default is 1000.
input_shape : tuple, optional
The shape of the input data in the format (height, width, channels). Default is (256, 256, 3).
include_top : bool, optional
Whether to include the fully-connected layer at the top of the network. Default is True.
pretrained : bool, optional
Whether to load pretrained weights for the model. Default is False.
cache_dir : str, optional
Directory to cache the pretrained weights. Default is None.
updates : dict, optional
A dictionary of updates to modify the base model configuration. Default is None.
pretrained_weight_name : str, optional
The name of the file containing the pretrained weights. Default is "keras_mobilevitv2-im1k-256-0.5.weights.h5".
**kwargs : dict, optional
Additional keyword arguments for model customization. These can include:
- linear_drop : float, optional
Dropout rate for Dense layers.
- attention_drop : float, optional
Dropout rate for the attention matrix.
- dropout : float, optional
Dropout rate for in-between different layers
Returns
-------
model : keras.Model
The constructed MobileViT-v2 model instance.
Example
-------
>>> model = build_MobileViT_v2(
>>> width_multiplier=1.0,
>>> num_classes=1000,
>>> input_shape=(256, 256, 3),
>>> include_top=True,
>>> pretrained=True,
>>> cache_dir='/path/to/cache'
>>> )
>>> model.summary()
"""
updated_configs = get_mobile_vit_v2_configs(width_multiplier, updates=updates)
# Build the base model
model = MobileViT_v2(
configs=updated_configs,
num_classes=num_classes if include_top else None,
input_shape=input_shape,
model_name=f"MobileViT-v2-{width_multiplier}",
**kwargs,
)
# Initialize parameters of MobileViT block.
if None in input_shape:
dummy_input_shape = (1, 256, 256, 3)
else:
dummy_input_shape = (1,) + input_shape
model(np.random.randn(*dummy_input_shape), training=False)
if pretrained:
weights_path = get_file(
fname=pretrained_weight_name,
origin=WEIGHTS_URL.format(weight_release_tag=WEIGHTS_RELEASE_TAG_VERSION, file_name=pretrained_weight_name),
cache_subdir="models",
hash_algorithm="auto",
extract=False,
archive_format="auto",
cache_dir=cache_dir,
)
with warnings.catch_warnings():
# Ignore UserWarnings within this block
warnings.simplefilter("ignore", UserWarning)
model.load_weights(weights_path, skip_mismatch=True)
return model
if __name__ == "__main__":
model = build_MobileViT_v2(
width_multiplier=0.75,
input_shape=(None, None, 3),
num_classes=1000,
linear_drop=0.0,
attention_drop=0.0,
)
print(f"{model.name} num. parametes: {model.count_params()}")