MCPcopy Create free account
hub / github.com/OpenGVLab/UniFormerV2 / Bottleneck

Class Bottleneck

extract_clip/model.py:10–55  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9
10class Bottleneck(nn.Module):
11 expansion = 4
12
13 def __init__(self, inplanes, planes, stride=1):
14 super().__init__()
15
16 # all conv layers have stride 1. an avgpool is performed after the second convolution when stride > 1
17 self.conv1 = nn.Conv2d(inplanes, planes, 1, bias=False)
18 self.bn1 = nn.BatchNorm2d(planes)
19 self.relu1 = nn.ReLU(inplace=True)
20
21 self.conv2 = nn.Conv2d(planes, planes, 3, padding=1, bias=False)
22 self.bn2 = nn.BatchNorm2d(planes)
23 self.relu2 = nn.ReLU(inplace=True)
24
25 self.avgpool = nn.AvgPool2d(stride) if stride > 1 else nn.Identity()
26
27 self.conv3 = nn.Conv2d(planes, planes * self.expansion, 1, bias=False)
28 self.bn3 = nn.BatchNorm2d(planes * self.expansion)
29 self.relu3 = nn.ReLU(inplace=True)
30
31 self.downsample = None
32 self.stride = stride
33
34 if stride > 1 or inplanes != planes * Bottleneck.expansion:
35 # downsampling layer is prepended with an avgpool, and the subsequent convolution has stride 1
36 self.downsample = nn.Sequential(OrderedDict([
37 ("-1", nn.AvgPool2d(stride)),
38 ("0", nn.Conv2d(inplanes, planes * self.expansion, 1, stride=1, bias=False)),
39 ("1", nn.BatchNorm2d(planes * self.expansion))
40 ]))
41
42 def forward(self, x: torch.Tensor):
43 identity = x
44
45 out = self.relu1(self.bn1(self.conv1(x)))
46 out = self.relu2(self.bn2(self.conv2(out)))
47 out = self.avgpool(out)
48 out = self.bn3(self.conv3(out))
49
50 if self.downsample is not None:
51 identity = self.downsample(x)
52
53 out += identity
54 out = self.relu3(out)
55 return out
56
57
58class AttentionPool2d(nn.Module):

Callers 1

_make_layerMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected