google-research
211 строк · 4.4 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Sparse/dense MobileNetV1 model configurations."""
17
18
19class Config(object):
20"""Config base class."""
21
22def __init__(self, fuse_bnbr=False):
23self.num_blocks = 13
24self.width = None
25self.block_nonzeros = None
26self.block_config = ["dense"] * self.num_blocks
27self.fuse_bnbr = fuse_bnbr
28self.num_classes = 1000
29
30
31class W14(Config):
32"""Width 1.4."""
33
34def __init__(self, fuse_bnbr=False):
35super(W14, self).__init__(fuse_bnbr)
36self.width = 1.4
37
38
39class W12(Config):
40"""Width 1.2."""
41
42def __init__(self, fuse_bnbr=False):
43super(W12, self).__init__(fuse_bnbr)
44self.width = 1.2
45
46
47class W10(Config):
48"""Width 1.0."""
49
50def __init__(self, fuse_bnbr=False):
51super(W10, self).__init__(fuse_bnbr)
52self.width = 1.0
53
54
55class W18S90D0(Config):
56"""Width 1.8, sparsity 90%, dense first block."""
57
58def __init__(self, fuse_bnbr=False):
59super(W18S90D0, self).__init__(fuse_bnbr)
60self.block_nonzeros = [
616440,
622668,
635382,
6410765,
6521530,
6642688,
6784640,
6884640,
6984640,
7084640,
7184640,
72169280,
73338560,
74]
75self.block_config = ["dense"] + ["sparse"] * (self.num_blocks - 1)
76self.width = 1.8
77
78
79class W17S90D0(W18S90D0):
80"""Width 1.7, sparsity 90%, dense first block."""
81
82def __init__(self, fuse_bnbr=False):
83super(W17S90D0, self).__init__(fuse_bnbr)
84self.block_nonzeros = [
856048,
862333,
874666,
889331,
8918662,
9037670,
9176038,
9276038,
9376038,
9476038,
9576038,
96152077,
97304154,
98]
99self.width = 1.7
100
101
102class W16S90D0(W18S90D0):
103"""Width 1.6, sparsity 90%, dense first block."""
104
105def __init__(self, fuse_bnbr=False):
106super(W16S90D0, self).__init__(fuse_bnbr)
107self.block_nonzeros = [
1084896,
1092122,
1104326,
1118486,
11216646,
11333293,
11466586,
11566586,
11666586,
11766586,
11866586,
119133824,
120268960,
121]
122self.width = 1.6
123
124
125class W15S90D0(W18S90D0):
126"""Width 1.5, sparsity 90%, dense first block."""
127
128def __init__(self, fuse_bnbr=False):
129super(W15S90D0, self).__init__(fuse_bnbr)
130self.block_nonzeros = [
1314608,
1321843,
1333686,
1347373,
13514746,
13629491,
13758982,
13858982,
13958982,
14058982,
14158982,
142117965,
143235930,
144]
145self.width = 1.5
146
147
148class W14S90D0(W18S90D0):
149"""Width 1.4, sparsity 90%, dense first block."""
150
151def __init__(self, fuse_bnbr=False):
152super(W14S90D0, self).__init__(fuse_bnbr)
153self.block_nonzeros = [
1544272,
1551566,
1563098,
1576336,
15812960,
15925920,
16051840,
16151840,
16251840,
16351840,
16451840,
165103104,
166205062,
167]
168self.width = 1.4
169
170
171class W13S90D0(W18S90D0):
172"""Width 1.3, sparsity 90%, dense first block."""
173
174def __init__(self, fuse_bnbr=False):
175super(W13S90D0, self).__init__(fuse_bnbr)
176self.block_nonzeros = [
1773320,
1781394,
1792822,
1805645,
18111290,
18222310,
18344090,
18444090,
18544090,
18644090,
18744090,
18888179,
189176358,
190]
191self.width = 1.3
192
193
194def get_config(width, sparsity):
195"""Getter for model configuration."""
196tag = "_".join(["mb"] + [str(int(x)) for x in [width * 10, sparsity * 100]])
197configs = {
198"mb_10_0": W10,
199"mb_12_0": W12,
200"mb_14_0": W14,
201"mb_13_90": W13S90D0,
202"mb_14_90": W14S90D0,
203"mb_15_90": W15S90D0,
204"mb_16_90": W16S90D0,
205"mb_17_90": W17S90D0,
206"mb_18_90": W18S90D0,
207}
208if tag not in configs:
209raise ValueError("Could not find config for width {}, sparsity {}.".format(
210width, sparsity))
211return configs[tag]
212