File size: 8,044 Bytes
f3b0a90 |
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 |
import torch
import torch.nn as nn
from torchvision.ops.misc import Conv2dNormActivation
from .helpers.utils import make_divisible
from transformers.modeling_utils import PreTrainedModel
from transformers.configuration_utils import PretrainedConfig
def initialize_weights(m):
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode="fan_out")
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm, nn.LayerNorm)):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
if m.bias is not None:
nn.init.zeros_(m.bias)
class Block(nn.Module):
def __init__(self, in_channels, out_channels, expansion_rate, stride):
super().__init__()
exp_channels = make_divisible(in_channels * expansion_rate, 8)
# create the three factorized convs that make up the inverted bottleneck block
exp_conv = Conv2dNormActivation(
in_channels,
exp_channels,
kernel_size=1,
stride=1,
norm_layer=nn.BatchNorm2d,
activation_layer=nn.ReLU,
inplace=False,
)
# depthwise convolution with possible stride
depth_conv = Conv2dNormActivation(
exp_channels,
exp_channels,
kernel_size=3,
stride=stride,
padding=1,
groups=exp_channels,
norm_layer=nn.BatchNorm2d,
activation_layer=nn.ReLU,
inplace=False,
)
proj_conv = Conv2dNormActivation(
exp_channels,
out_channels,
kernel_size=1,
stride=1,
norm_layer=nn.BatchNorm2d,
activation_layer=None,
inplace=False,
)
self.after_block_activation = nn.ReLU()
if in_channels == out_channels:
self.use_shortcut = True
if stride == 1 or stride == (1, 1):
self.shortcut = nn.Sequential()
else:
# average pooling required for shortcut
self.shortcut = nn.Sequential(
nn.AvgPool2d(kernel_size=3, stride=stride, padding=1),
nn.Sequential(),
)
else:
self.use_shortcut = False
self.block = nn.Sequential(exp_conv, depth_conv, proj_conv)
def forward(self, x):
if self.use_shortcut:
x = self.block(x) + self.shortcut(x)
else:
x = self.block(x)
x = self.after_block_activation(x)
return x
class NetworkConfig(PretrainedConfig):
def __init__(
self,
n_classes=10,
in_channels=1,
base_channels=32,
channels_multiplier=2.3,
expansion_rate=3.0,
n_blocks=(3, 2, 1),
strides=dict(b2=(1, 1), b3=(1, 2), b4=(2, 1)),
add_feats=False,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.n_classes = n_classes
self.in_channels = in_channels
self.base_channels = base_channels
self.channels_multiplier = channels_multiplier
self.expansion_rate = expansion_rate
self.n_blocks = n_blocks
self.strides = strides
self.add_feats = add_feats
class Network(PreTrainedModel):
config_class = NetworkConfig
def __init__(self, config):
super().__init__(config)
n_classes = config.n_classes
in_channels = config.in_channels
base_channels = config.base_channels
channels_multiplier = config.channels_multiplier
expansion_rate = config.expansion_rate
n_blocks = config.n_blocks
strides = config.strides
n_stages = len(n_blocks)
self.add_feats = config.add_feats
base_channels = make_divisible(base_channels, 8)
channels_per_stage = [base_channels] + [
make_divisible(base_channels * channels_multiplier**stage_id, 8)
for stage_id in range(n_stages)
]
self.total_block_count = 0
self.in_c = nn.Sequential(
Conv2dNormActivation(
in_channels,
channels_per_stage[0] // 4,
activation_layer=torch.nn.ReLU,
kernel_size=3,
stride=2,
inplace=False,
),
Conv2dNormActivation(
channels_per_stage[0] // 4,
channels_per_stage[0],
activation_layer=torch.nn.ReLU,
kernel_size=3,
stride=2,
inplace=False,
),
)
self.stages = nn.Sequential()
for stage_id in range(n_stages):
stage = self._make_stage(
channels_per_stage[stage_id],
channels_per_stage[stage_id + 1],
n_blocks[stage_id],
strides=strides,
expansion_rate=expansion_rate,
)
self.stages.add_module(f"s{stage_id + 1}", stage)
ff_list = []
ff_list += [
nn.Conv2d(
channels_per_stage[-1],
n_classes,
kernel_size=(1, 1),
stride=(1, 1),
padding=0,
bias=False,
),
nn.BatchNorm2d(n_classes),
]
ff_list.append(nn.AdaptiveAvgPool2d((1, 1)))
self.feed_forward = nn.Sequential(*ff_list)
self.apply(initialize_weights)
def _make_stage(self, in_channels, out_channels, n_blocks, strides, expansion_rate):
stage = nn.Sequential()
for index in range(n_blocks):
block_id = self.total_block_count + 1
bname = f"b{block_id}"
self.total_block_count = self.total_block_count + 1
if bname in strides:
stride = strides[bname]
else:
stride = (1, 1)
block = self._make_block(
in_channels, out_channels, stride=stride, expansion_rate=expansion_rate
)
stage.add_module(bname, block)
in_channels = out_channels
return stage
def _make_block(self, in_channels, out_channels, stride, expansion_rate):
block = Block(in_channels, out_channels, expansion_rate, stride)
return block
def _forward_conv(self, x):
x = self.in_c(x)
x = self.stages(x)
return x
def forward(self, x):
y = self._forward_conv(x)
x = self.feed_forward(y)
logits = x.squeeze(2).squeeze(2)
if self.add_feats:
return logits, y
else:
return logits
def get_model(
n_classes=10,
in_channels=1,
base_channels=32,
channels_multiplier=2.3,
expansion_rate=3.0,
n_blocks=(3, 2, 1),
strides=None,
add_feats=False,
):
"""
@param n_classes: number of the classes to predict
@param in_channels: input channels to the network, for audio it is by default 1
@param base_channels: number of channels after in_conv
@param channels_multiplier: controls the increase in the width of the network after each stage
@param expansion_rate: determines the expansion rate in inverted bottleneck blocks
@param n_blocks: number of blocks that should exist in each stage
@param strides: default value set below
@return: full neural network model based on the specified configs
"""
if strides is None:
strides = dict(b2=(1, 1), b3=(1, 2), b4=(2, 1))
model_config = {
"n_classes": n_classes,
"in_channels": in_channels,
"base_channels": base_channels,
"channels_multiplier": channels_multiplier,
"expansion_rate": expansion_rate,
"n_blocks": n_blocks,
"strides": strides,
"add_feats": add_feats,
}
m = Network(NetworkConfig(**model_config))
return m
|