-
Notifications
You must be signed in to change notification settings - Fork 518
/
mobileone.py
214 lines (192 loc) · 7.33 KB
/
mobileone.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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
import copy
import torch.nn as nn
from corenet.modeling.layers import GlobalPool, Identity, LinearLayer
from corenet.modeling.models import MODEL_REGISTRY
from corenet.modeling.models.classification.base_image_encoder import BaseImageEncoder
from corenet.modeling.models.classification.config.mobileone import get_configuration
from corenet.modeling.modules.mobileone_block import MobileOneBlock
@MODEL_REGISTRY.register(name="mobileone", type="classification")
class MobileOne(BaseImageEncoder):
"""
This class implements `MobileOne architecture <https://arxiv.org/pdf/2206.04040.pdf>`_
"""
def __init__(self, opts, *args, **kwargs) -> None:
cfg = get_configuration(opts=opts)
super().__init__(opts, *args, **kwargs)
self.opts = opts
image_channels = 3
num_blocks_per_stage = cfg["num_blocks_per_stage"]
width_multipliers = cfg["width_multipliers"]
use_se = cfg["use_se"]
self.num_conv_branches = cfg["num_conv_branches"]
self.inference_mode = getattr(
opts, "model.classification.mobileone.inference_mode"
)
assert len(width_multipliers) == 4
self.in_planes = min(64, int(64 * width_multipliers[0]))
self.model_conf_dict = dict()
# First dense conv
self.conv_1 = MobileOneBlock(
opts,
in_channels=image_channels,
out_channels=self.in_planes,
kernel_size=3,
stride=2,
padding=1,
inference_mode=self.inference_mode,
)
self.model_conf_dict["conv1"] = {"in": image_channels, "out": self.in_planes}
self.layer_1 = Identity()
self.model_conf_dict["layer1"] = {"in": self.in_planes, "out": self.in_planes}
# Build stages
self.cur_layer_idx = 1
self.model_conf_dict["layer2"] = {
"in": self.in_planes,
"out": int(64 * width_multipliers[0]),
}
self.layer_2 = self._make_stage(
int(64 * width_multipliers[0]), num_blocks_per_stage[0], num_se_blocks=0
)
self.model_conf_dict["layer3"] = {
"in": self.in_planes,
"out": int(128 * width_multipliers[1]),
}
self.layer_3 = self._make_stage(
int(128 * width_multipliers[1]), num_blocks_per_stage[1], num_se_blocks=0
)
self.model_conf_dict["layer4"] = {
"in": self.in_planes,
"out": int(256 * width_multipliers[2]),
}
self.layer_4 = self._make_stage(
int(256 * width_multipliers[2]),
num_blocks_per_stage[2],
num_se_blocks=int(num_blocks_per_stage[2] // 2) if use_se else 0,
)
self.model_conf_dict["layer5"] = {
"in": self.in_planes,
"out": int(512 * width_multipliers[3]),
}
self.layer_5 = self._make_stage(
int(512 * width_multipliers[3]),
num_blocks_per_stage[3],
num_se_blocks=num_blocks_per_stage[3] if use_se else 0,
)
# No extra 1x1 conv before classifier
self.conv_1x1_exp = Identity()
self.model_conf_dict["exp_before_cls"] = {
"in": int(512 * width_multipliers[3]),
"out": int(512 * width_multipliers[3]),
}
# Build classifier
pool_type = getattr(opts, "model.layer.global_pool")
self.classifier = nn.Sequential()
self.classifier.add_module(
name="global_pool", module=GlobalPool(pool_type=pool_type, keep_dim=False)
)
self.classifier.add_module(
name="classifier_fc",
module=LinearLayer(
in_features=int(512 * width_multipliers[3]),
out_features=self.n_classes,
bias=True,
),
)
self.model_conf_dict["cls"] = {
"in": int(512 * width_multipliers[3]),
"out": self.n_classes,
}
# check model
self.check_model()
# weight initialization
self.reset_parameters(opts=opts)
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
"""Add model specific arguments"""
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--model.classification.mobileone.variant",
type=str,
default="s1",
help="Variant string for MobileOne. Default: s1",
)
group.add_argument(
"--model.classification.mobileone.inference-mode",
type=bool,
default=False,
help="Flag to instantiate inference mode architecture. Default: False",
)
return parser
def _make_stage(
self, planes: int, num_blocks: int, num_se_blocks: int
) -> nn.Sequential:
"""Build a stage of MobileOne model.
Args:
planes: Number of output channels.
num_blocks: Number of blocks in this stage.
num_se_blocks: Number of SE blocks in this stage.
Returns:
A stage of MobileOne model.
"""
# Get strides for all layers
strides = [2] + [1] * (num_blocks - 1)
blocks = []
for ix, stride in enumerate(strides):
use_se = False
if num_se_blocks > num_blocks:
raise ValueError(
f"Number of SE blocks ({num_se_blocks}) cannot exceed number of layers ({num_blocks})."
)
if ix >= (num_blocks - num_se_blocks):
use_se = True
# MobileOne block with depthwise conv
blocks.append(
MobileOneBlock(
self.opts,
in_channels=self.in_planes,
out_channels=self.in_planes,
kernel_size=3,
stride=stride,
padding=1,
groups=self.in_planes,
inference_mode=self.inference_mode,
use_se=use_se,
num_conv_branches=self.num_conv_branches,
)
)
# MobileOne block with pointwise conv
blocks.append(
MobileOneBlock(
self.opts,
in_channels=self.in_planes,
out_channels=planes,
kernel_size=1,
stride=1,
padding=0,
groups=1,
inference_mode=self.inference_mode,
use_se=use_se,
num_conv_branches=self.num_conv_branches,
)
)
self.in_planes = planes
self.cur_layer_idx += 1
return nn.Sequential(*blocks)
def get_exportable_model(self) -> nn.Module:
"""Method returns a model where a multi-branched structure
used in training is re-parameterized into a single branch
for inference.
Returns:
Reparametrized MobileOne model for faster inference.
"""
# Avoid editing original graph
model = copy.deepcopy(self)
for module in model.modules():
if hasattr(module, "reparameterize"):
module.reparameterize()
return model