def conv3x3mtl(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return Conv2dMtl(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
def __init__(self, block, layers, num_classes=10): self.inplanes = 16 super(ResNetMtl, self).__init__() self.conv1 = Conv2dMtl(3, 16, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(16) self.relu = nn.ReLU(inplace=True) self.layer1 = self._make_layer(block, 16, layers[0]) self.layer2 = self._make_layer(block, 32, layers[1], stride=2) self.layer3 = self._make_layer(block, 64, layers[2], stride=2, last_phase=True) self.avgpool = nn.AvgPool2d(8, stride=1) self.fc = modified_linear.CosineLinear(64 * block.expansion, num_classes) for m in self.modules(): if isinstance(m, Conv2dMtl): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0)
def _make_layer(self, block, planes, blocks, stride=1, last_phase=False): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( Conv2dMtl(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion if last_phase: for i in range(1, blocks-1): layers.append(block(self.inplanes, planes)) layers.append(block(self.inplanes, planes, last=True)) else: for i in range(1, blocks): layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers)
def conv3x3mtl(in_planes, out_planes, stride=1): return Conv2dMtl(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)