def __init__(self, inplanes, planes, widefactor=1, stride=1, downsample=None, num_domains=2): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = DomainSpecificBatchNorm2d(planes, num_domains) self.conv2 = nn.Conv2d(planes, planes * widefactor, kernel_size=3, stride=stride, padding=1, bias=False) self.bn2 = DomainSpecificBatchNorm2d(planes * widefactor, num_domains) self.conv3 = nn.Conv2d(planes * widefactor, planes * 4, kernel_size=1, bias=False) self.bn3 = DomainSpecificBatchNorm2d(planes * 4, num_domains) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride
def __init__(self, inplanes, planes, stride=1, downsample=None, num_domains=2): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = DomainSpecificBatchNorm2d(planes, num_domains) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = DomainSpecificBatchNorm2d(planes, num_domains) self.downsample = downsample self.stride = stride
def setup_net(self): self.conv1 = nn.Conv2d(self.num_channels, 20, kernel_size=5) self.bn1 = DomainSpecificBatchNorm2d(20, self.num_domains) self.pool1 = nn.MaxPool2d(2) self.conv2 = nn.Conv2d(20, 50, kernel_size=5) self.bn2 = DomainSpecificBatchNorm2d(50, self.num_domains) self.pool2 = nn.MaxPool2d(2) self.fc1 = nn.Linear(50 * 4 * 4, 500) if self.in_features != 0: self.fc2 = nn.Linear(500, self.in_features) self.fc3 = nn.Linear(self.in_features, self.num_classes) else: self.fc2 = nn.Linear(500, self.num_classes)
def _make_layer(self, block, planes, blocks, stride=1, num_domains=2): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = TwoInputSequential( Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), DomainSpecificBatchNorm2d(planes * block.expansion, num_domains), ) layers = [] layers.append( block(self.inplanes, planes, stride, downsample, num_domains=num_domains)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes, num_domains=num_domains)) return TwoInputSequential(*layers)
def __init__(self, block, layers, in_features=256, num_classes=1000, num_domains=2): self.inplanes = 64 self.in_features = in_features self.num_domains = num_domains self.num_classes = num_classes super(DSBNResNet, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = DomainSpecificBatchNorm2d(64, self.num_domains) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0], num_domains=self.num_domains) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, num_domains=self.num_domains) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, num_domains=self.num_domains) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, num_domains=self.num_domains) # self.avgpool = nn.AvgPool2d(7, stride=1) if self.in_features != 0: self.fc1 = nn.Linear(512 * block.expansion, self.in_features) self.fc2 = nn.Linear(self.in_features, num_classes) else: self.fc = nn.Linear(512 * block.expansion, num_classes) for m in self.modules(): if isinstance(m, nn.Conv2d) or isinstance(m, Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.Linear): m.weight.data.normal_(0, 0.01) m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_()