MCPcopy Create free account
hub / github.com/HobbitLong/PyContrast / ResNet

Class ResNet

pycontrast/networks/resnet_cmc.py:108–173  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

106
107
108class ResNet(nn.Module):
109
110 def __init__(self, block, layers, width=1):
111 super(ResNet, self).__init__()
112 self.inplanes = 64 * 2
113 self.conv1_v1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
114 self.conv1_v2 = nn.Conv2d(2, 64, kernel_size=7, stride=2, padding=3, bias=False)
115 self.bn1 = nn.BatchNorm2d(self.inplanes)
116 self.relu = nn.ReLU(inplace=True)
117
118 self.base = int(64 * width)
119
120 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
121 self.layer1 = self._make_layer(block, self.base, layers[0])
122 self.layer2 = self._make_layer(block, self.base * 2, layers[1], stride=2)
123 self.layer3 = self._make_layer(block, self.base * 4, layers[2], stride=2)
124 self.layer4 = self._make_layer(block, self.base * 8, layers[3], stride=2)
125 self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
126 # self.fc = nn.Linear(self.base * 8 * block.expansion, low_dim)
127 # self.l2norm = Normalize(2)
128
129 for m in self.modules():
130 if isinstance(m, nn.Conv2d):
131 n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
132 m.weight.data.normal_(0, math.sqrt(2. / n))
133 elif isinstance(m, nn.BatchNorm2d):
134 m.weight.data.fill_(1)
135 m.bias.data.zero_()
136
137 def _make_layer(self, block, planes, blocks, stride=1):
138 downsample = None
139 if stride != 1 or self.inplanes != planes * block.expansion:
140 downsample = nn.Sequential(
141 nn.Conv2d(self.inplanes, planes * block.expansion,
142 kernel_size=1, stride=stride, groups=2, bias=False),
143 nn.BatchNorm2d(planes * block.expansion),
144 )
145
146 layers = list([])
147 layers.append(block(self.inplanes, planes, stride, downsample))
148 self.inplanes = planes * block.expansion
149 for i in range(1, blocks):
150 layers.append(block(self.inplanes, planes))
151
152 return nn.Sequential(*layers)
153
154 def forward(self, x):
155 x1, x2 = torch.split(x, [1, 2], dim=1)
156 x1 = self.conv1_v1(x1)
157 x2 = self.conv1_v2(x2)
158 x = torch.cat([x1, x2], dim=1)
159 x = self.bn1(x)
160 x = self.relu(x)
161 x = self.maxpool(x)
162
163 x = self.layer1(x)
164 x = self.layer2(x)
165 x = self.layer3(x)

Callers 5

resnet18Function · 0.70
resnet34Function · 0.70
resnet50Function · 0.70
resnet101Function · 0.70
resnet152Function · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected