File size: 8,413 Bytes
a23ef1a |
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 |
import torch.nn as nn
from .ops import *
class stem(nn.Module):
num_layer = 1
def __init__(self, conv, inplanes, planes, stride=1, norm_layer=nn.BatchNorm2d):
super(stem, self).__init__()
self.conv1 = conv(inplanes, planes, stride)
self.bn1 = norm_layer(planes)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
return out
class basic(nn.Module):
expansion = 1
num_layer = 2
def __init__(self, conv, inplanes, planes, stride=1, midplanes=None, norm_layer=nn.BatchNorm2d):
super(basic, self).__init__()
midplanes = planes if midplanes is None else midplanes
self.conv1 = conv(inplanes, midplanes, stride)
self.bn1 = norm_layer(midplanes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv(midplanes, planes)
self.bn2 = norm_layer(planes)
if stride!=1 or inplanes!=planes*self.expansion:
self.downsample = nn.Sequential(
conv1x1(inplanes, planes, stride),
norm_layer(planes),
)
else:
self.downsample = None
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
class bottleneck(nn.Module):
expansion = 4
num_layer = 3
def __init__(self, conv, inplanes, planes, stride=1, midplanes=None, norm_layer=nn.BatchNorm2d):
super(bottleneck, self).__init__()
midplanes = planes if midplanes is None else midplanes
self.conv1 = conv1x1(inplanes, midplanes)
self.bn1 = norm_layer(midplanes)
self.conv2 = conv(midplanes, midplanes, stride)
self.bn2 = norm_layer(midplanes)
self.conv3 = conv1x1(midplanes, planes * self.expansion)
self.bn3 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
if stride!=1 or inplanes!=planes*self.expansion:
self.downsample = nn.Sequential(
conv1x1(inplanes, planes*self.expansion, stride),
norm_layer(planes*self.expansion),
)
else:
self.downsample = None
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
class invert(nn.Module):
def __init__(self, conv, inp, oup, stride=1, expand_ratio=1, norm_layer=nn.BatchNorm2d):
super(invert, self).__init__()
self.stride = stride
assert stride in [1, 2]
hidden_dim = round(inp * expand_ratio)
self.use_res_connect = self.stride == 1 and inp == oup
if expand_ratio == 1:
self.conv = nn.Sequential(
# dw
conv(hidden_dim, hidden_dim, stride),
norm_layer(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
norm_layer(oup),
)
else:
self.conv = nn.Sequential(
# pw
nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
norm_layer(hidden_dim),
nn.ReLU6(inplace=True),
# dw
conv(hidden_dim, hidden_dim, stride),
norm_layer(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
norm_layer(oup),
)
def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
else:
return self.conv(x)
invert2 = lambda op, inp, outp, stride, **kwargs: invert(op, inp, outp, stride, expand_ratio=2, **kwargs)
invert3 = lambda op, inp, outp, stride, **kwargs: invert(op, inp, outp, stride, expand_ratio=3, **kwargs)
invert4 = lambda op, inp, outp, stride, **kwargs: invert(op, inp, outp, stride, expand_ratio=4, **kwargs)
invert6 = lambda op, inp, outp, stride, **kwargs: invert(op, inp, outp, stride, expand_ratio=6, **kwargs)
def channel_shuffle(x, groups):
batchsize, num_channels, height, width = x.data.size()
channels_per_group = num_channels // groups
# reshape
x = x.view(batchsize, groups, channels_per_group, height, width)
x = torch.transpose(x, 1, 2).contiguous()
# flatten
x = x.view(batchsize, -1, height, width)
return x
class shuffle(nn.Module):
expansion = 1
num_layer = 3
def __init__(self, conv, inplanes, outplanes, stride=1, midplanes=None, norm_layer=nn.BatchNorm2d):
super(shuffle, self).__init__()
inplanes = inplanes // 2 if stride == 1 else inplanes
midplanes = outplanes // 2 if midplanes is None else midplanes
rightoutplanes = outplanes - inplanes
if stride == 2:
self.left_branch = nn.Sequential(
# dw
conv(inplanes, inplanes, stride),
norm_layer(inplanes),
# pw-linear
conv1x1(inplanes, inplanes),
norm_layer(inplanes),
nn.ReLU(inplace=True),
)
self.right_branch = nn.Sequential(
# pw
conv1x1(inplanes, midplanes),
norm_layer(midplanes),
nn.ReLU(inplace=True),
# dw
conv(midplanes, midplanes, stride),
norm_layer(midplanes),
# pw-linear
conv1x1(midplanes, rightoutplanes),
norm_layer(rightoutplanes),
nn.ReLU(inplace=True),
)
self.reduce = stride==2
def forward(self, x):
if self.reduce:
out = torch.cat((self.left_branch(x), self.right_branch(x)), 1)
else:
x1 = x[:, :(x.shape[1]//2), :, :]
x2 = x[:, (x.shape[1]//2):, :, :]
out = torch.cat((x1, self.right_branch(x2)), 1)
return channel_shuffle(out, 2)
class shufflex(nn.Module):
expansion = 1
num_layer = 3
def __init__(self, conv, inplanes, outplanes, stride=1, midplanes=None, norm_layer=nn.BatchNorm2d):
super(shufflex, self).__init__()
inplanes = inplanes // 2 if stride == 1 else inplanes
midplanes = outplanes // 2 if midplanes is None else midplanes
rightoutplanes = outplanes - inplanes
if stride==2:
self.left_branch = nn.Sequential(
# dw
conv(inplanes, inplanes, stride),
norm_layer(inplanes),
# pw-linear
conv1x1(inplanes, inplanes),
norm_layer(inplanes),
nn.ReLU(inplace=True),
)
self.right_branch = nn.Sequential(
# dw
conv(inplanes, inplanes, stride),
norm_layer(inplanes),
# pw-linear
conv1x1(inplanes, midplanes),
norm_layer(midplanes),
nn.ReLU(inplace=True),
# dw
conv(midplanes, midplanes, 1),
norm_layer(midplanes),
# pw-linear
conv1x1(midplanes, midplanes),
norm_layer(midplanes),
nn.ReLU(inplace=True),
# dw
conv(midplanes, midplanes, 1),
norm_layer(midplanes),
# pw-linear
conv1x1(midplanes, rightoutplanes),
norm_layer(rightoutplanes),
nn.ReLU(inplace=True),
)
self.reduce = stride==2
def forward(self, x):
if self.reduce:
out = torch.cat((self.left_branch(x), self.right_branch(x)), 1)
else:
x1 = x[:, :(x.shape[1] // 2), :, :]
x2 = x[:, (x.shape[1] // 2):, :, :]
out = torch.cat((x1, self.right_branch(x2)), 1)
return channel_shuffle(out, 2) |