def __init__(self, args, in_channels, out_channels, stride=1): super(ResBlockDiscriminator, self).__init__() #self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1) self.conv1 = my.NormConv(in_channels, out_channels, 3, 1, padding=1, adjustScale=False, NScale=1, T=args.T) #self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1) self.conv2 = my.NormConv(out_channels, out_channels, 3, 1, padding=1, adjustScale=False, NScale=1, T=args.T) nn.init.xavier_uniform_(self.conv1.weight.data, 1.) nn.init.xavier_uniform_(self.conv2.weight.data, 1.) if stride == 1: self.model = nn.Sequential( nn.ReLU(), # SpectralNorm(self.conv1), self.conv1, nn.ReLU(), # SpectralNorm(self.conv2) self.conv2) else: self.model = nn.Sequential( nn.ReLU(), # SpectralNorm(self.conv1), self.conv1, nn.ReLU(), #SpectralNorm(self.conv2), self.conv2, nn.AvgPool2d(2, stride=stride, padding=0)) self.bypass = nn.Sequential() if stride != 1: #self.bypass_conv = nn.Conv2d(in_channels,out_channels, 1, 1, padding=0) self.bypass_conv = my.NormConv(in_channels, out_channels, 1, 1, padding=0, adjustScale=False, NScale=1, T=args.T) nn.init.xavier_uniform_(self.bypass_conv.weight.data, np.sqrt(2)) self.bypass = nn.Sequential( # SpectralNorm(self.bypass_conv), self.bypass_conv, nn.AvgPool2d(2, stride=stride, padding=0))
def __init__(self, inplanes, planes, stride=1, downsample=None): super(BasicBlock, self).__init__() #self.conv1 = conv3x3(inplanes, planes, stride) self.conv1 = my.NormConv(inplanes, planes, 3, stride, padding=1, bias=False) #self.bn1 = nn.BatchNorm2d(planes) self.bn1 = my.Norm(planes) self.relu = nn.ReLU(inplace=True) #self.conv2 = conv3x3(planes, planes) #self.conv2 = Conv2d_ONI(planes, planes, 3, stride=1, padding=1, bias=False, T=7) self.conv2 = my.NormConv(planes, planes, 3, stride=1, padding=1, bias=False) #self.bn2 = nn.BatchNorm2d(planes) self.bn2 = my.Norm(planes) self.downsample = downsample self.stride = stride
def __init__(self, block, layers, num_classes=1000, **kwargs): self.inplanes = 64 super(ResNet_var_ONI, self).__init__() #self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.conv1 = my.NormConv(3, 64, kernel_size=7, stride=2, padding=3, bias=False) 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]) self.layer2 = self._make_layer(block, 128, layers[1], stride=2) self.layer3 = self._make_layer(block, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=2) self.bn1 = nn.BatchNorm2d(512 * block.expansion) self.avgpool = nn.AvgPool2d(7, stride=1) self.fc = nn.Linear(512 * block.expansion, num_classes) for m in self.modules(): if isinstance(m, nn.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.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_()
def __init__(self, args, in_channels, out_channels, stride=1): super(FirstResBlockDiscriminator, self).__init__() #self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1) self.conv1 = my.NormConv(in_channels, out_channels, 3, 1, padding=1, adjustScale=False, NScale=1, T=args.T) #self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1) self.conv2 = my.NormConv(out_channels, out_channels, 3, 1, padding=1, adjustScale=False, NScale=1, T=args.T) #self.bypass_conv = nn.Conv2d(in_channels, out_channels, 1, 1, padding=0) self.bypass_conv = my.NormConv(in_channels, out_channels, 1, 1, padding=0, adjustScale=False, NScale=1, T=args.T) nn.init.xavier_uniform_(self.conv1.weight.data, 1.) nn.init.xavier_uniform_(self.conv2.weight.data, 1.) nn.init.xavier_uniform_(self.bypass_conv.weight.data, np.sqrt(2)) # we don't want to apply ReLU activation to raw image before convolution transformation. self.model = nn.Sequential( # SpectralNorm(self.conv1), self.conv1, nn.ReLU(), # SpectralNorm(self.conv2), self.conv2, nn.AvgPool2d(2)) self.bypass = nn.Sequential( nn.AvgPool2d(2), # SpectralNorm(self.bypass_conv), self.bypass_conv, )
def __init__(self, inplanes, planes, stride=1, downsample=None): super(Bottleneck_var_ONI, self).__init__() self.bn1 = nn.BatchNorm2d(inplanes) #self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.conv1 = my.NormConv(inplanes, planes, kernel_size=1, bias=False) self.bn2 = nn.BatchNorm2d(planes) #self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.conv2 = my.NormConv(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn3 = nn.BatchNorm2d(planes) #self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False) self.conv3 = my.NormConv(planes, planes * 4, kernel_size=1, bias=False) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride
def make_layers(cfg, batch_norm=False): layers = [] in_channels = 3 for v in cfg: if v == 'M': layers += [nn.MaxPool2d(kernel_size=2, stride=2)] else: conv2d = my.NormConv(in_channels, v, kernel_size=3, padding=1) if batch_norm: layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] else: layers += [conv2d, nn.ReLU(inplace=True)] in_channels = v return nn.Sequential(*layers)
def _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( #nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), my.NormConv(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False, ONIRow_Fix=True), #nn.BatchNorm2d(planes * block.expansion), ) my.Norm(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers)
def __init__(self,args): super(Discriminator, self).__init__() #self.conv1 = SpectralNorm(nn.Conv2d(channels, 64, 3, stride=1, padding=(1,1))) self.conv1 = my.NormConv(channels, 64, 3, stride=1, padding=(1,1), bias=True, adjustScale=False, NScale=args.NScale, T=args.T) #self.conv2 = SpectralNorm(nn.Conv2d(64, 64, 4, stride=2, padding=(1,1))) self.conv2 = my.NormConv(64, 64, 4, stride=2, padding=(1,1), bias=True, adjustScale=False, NScale=args.NScale, T=args.T) #self.conv3 = SpectralNorm(nn.Conv2d(64, 128, 3, stride=1, padding=(1,1))) self.conv3 = my.NormConv(64, 128, 3, stride=1, padding=(1,1), bias=True, adjustScale=False, NScale=args.NScale, T=args.T) #self.conv4 = SpectralNorm(nn.Conv2d(128, 128, 4, stride=2, padding=(1,1))) self.conv4 = my.NormConv(128, 128, 4, stride=2, padding=(1,1), bias=True, adjustScale=False, NScale=args.NScale, T=args.T) #self.conv5 = SpectralNorm(nn.Conv2d(128, 256, 3, stride=1, padding=(1,1))) self.conv5 = my.NormConv(128, 256, 3, stride=1, padding=(1,1), bias=True, adjustScale=False, NScale=args.NScale, T=args.T) #self.conv6 = SpectralNorm(nn.Conv2d(256, 256, 4, stride=2, padding=(1,1))) self.conv6 = my.NormConv(256, 256, 4, stride=2, padding=(1,1), bias=True, adjustScale=False, NScale=args.NScale, T=args.T) #self.conv7 = SpectralNorm(nn.Conv2d(256, 512, 3, stride=1, padding=(1,1))) self.conv7 = my.NormConv(256, 512, 3, stride=1, padding=(1,1), bias=True, adjustScale=False, NScale=args.NScale, T=args.T) self.fc = SpectralNorm(nn.Linear(w_g * w_g * 512, 1))