Beispiel #1
0
    def __init__(self, oc, nz, ngf, use_swish, sz):
        super(FastGANGenerator, self).__init__()
        self.sz = sz
        dims = {
            4: 16 * ngf,
            8: 8 * ngf,
            16: 4 * ngf,
            32: 2 * ngf,
            64: 2 * ngf,
            128: ngf,
            256: ngf // 2,
            512: ngf // 4,
            1024: ngf // 8
        }
        self.init_blk = FastGANGenInitBlk(nz, dims[4])
        self.blk_8 = FastGANUpBlk(dims[4], dims[8])
        self.blk_16 = FastGANUpBlk(dims[8], dims[16])
        self.blk_32 = FastGANUpBlk(dims[16], dims[32])
        self.blk_64 = FastGANUpBlk(dims[32], dims[64])
        self.blk_128 = FastGANUpBlk(dims[64], dims[128])
        self.blk_256 = FastGANUpBlk(dims[128], dims[256])
        self.blk_512 = FastGANUpBlk(dims[256], dims[512])
        self.blk_1024 = FastGANUpBlk(dims[512], dims[1024])

        self.se_128_8 = FastGANSEBlk(dims[128], dims[8], use_swish)
        self.se_256_16 = FastGANSEBlk(dims[256], dims[16], use_swish)
        self.se_512_32 = FastGANSEBlk(dims[512], dims[32], use_swish)

        self.last_conv1 = SpectralNorm(
            nn.Conv2d(dims[128], oc, 1, 1, 0, bias=False))
        self.last_conv2 = SpectralNorm(
            nn.Conv2d(dims[sz], oc, 3, 1, 1, bias=False))
Beispiel #2
0
    def __init__(self,
                 num_classes=10,
                 non_negative=[True, True, True, True],
                 norm=[False, False, False, False]):

        super(MNIST_Small_ConvNet, self).__init__()
        self.conv1 = RobustConv2d(1,
                                  16,
                                  4,
                                  2,
                                  padding=1,
                                  non_negative=non_negative[0])
        if norm[0]:
            self.conv1 = SpectralNorm(self.conv1)
        self.conv2 = RobustConv2d(16,
                                  32,
                                  4,
                                  1,
                                  padding=1,
                                  non_negative=non_negative[1])
        if norm[1]:
            self.conv2 = SpectralNorm(self.conv2)
        self.fc1 = RobustLinear(13 * 13 * 32,
                                100,
                                non_negative=non_negative[2])
        if norm[2]:
            self.fc1 = SpectralNorm(self.fc1)
        self.fc2 = RobustLinear(100, 10, non_negative=non_negative[3])
        if norm[3]:
            self.fc2 = SpectralNorm(self.fc2)

        self.activation = F.relu
        self.score_function = self.fc2
Beispiel #3
0
    def __init__(self, ic, oc, norm_type='instancenorm'):
        super(ResBlock, self).__init__()
        self.ic = ic
        self.oc = oc
        self.norm_type = norm_type

        self.relu = nn.ReLU(inplace=True)
        self.reflection_pad1 = nn.ReflectionPad2d(1)
        self.reflection_pad2 = nn.ReflectionPad2d(1)

        self.conv1 = nn.Conv2d(ic, oc, 3, 1, 0, bias=False)
        self.conv2 = nn.Conv2d(oc, oc, 3, 1, 0, bias=False)

        if (self.norm_type == 'spectralnorm'):
            self.conv1 = SpectralNorm(self.conv1)
            self.conv2 = SpectralNorm(self.conv2)
            self.bn1 = Nothing()
            self.bn2 = Nothing()

        else:
            if (self.norm_type == 'batchnorm'):
                self.bn1 = nn.BatchNorm2d(oc)
                self.bn2 = nn.BatchNorm2d(oc)

            elif (self.norm_type == 'instancenorm'):
                self.bn1 = nn.InstanceNorm2d(oc)
                self.bn2 = nn.InstanceNorm2d(oc)
Beispiel #4
0
    def __init__(self, nf, total_residual_blocks):
        super(ResidualBlockSpectralNorm, self).__init__()
        self.lrelu = nn.LeakyReLU(negative_slope=0.1, inplace=True)
        self.conv1 = SpectralNorm(nn.Conv2d(nf, nf, 3, 1, 1, bias=True))
        self.conv2 = SpectralNorm(nn.Conv2d(nf, nf, 3, 1, 1, bias=True))

        initialize_weights([self.conv1, self.conv2], 1)
Beispiel #5
0
    def __init__(self,
                 inc,
                 outc=None,
                 kernel=3,
                 stride=1,
                 activ='lrelu',
                 norm='bn',
                 sn=False):
        super(ResidualBlock, self).__init__()

        if outc is None:
            outc = inc // stride

        self.activ = get_activ(activ)
        pad = kernel // 2
        if sn:
            self.input = SpectralNorm(nn.Conv2d(inc, outc, 1, 1, padding=0))
            self.blocks = nn.Sequential(
                SpectralNorm(nn.Conv2d(inc, outc, kernel, 1, pad)),
                get_norm(norm, outc), nn.LeakyReLU(0.2),
                SpectralNorm(nn.Conv2d(outc, outc, kernel, 1, pad)),
                get_norm(norm, outc))
        else:
            self.input = nn.Conv2d(inc, outc, 1, 1, padding=0)
            self.blocks = nn.Sequential(
                nn.Conv2d(inc, outc, kernel, 1, kernel), get_norm(norm, outc),
                nn.LeakyReLU(0.2), nn.Conv2d(outc, outc, kernel, 1, kernel),
                get_norm(norm, outc))
Beispiel #6
0
    def __init__(self, channels, leak=0.2):
        super(Discriminator, self).__init__()
        self.leak = leak

        self.conv1 = SpectralNorm(
            nn.Conv2d(channels, 64, 3, stride=1, padding=(1, 1)))
        self.conv2 = SpectralNorm(
            nn.Conv2d(64, 64, 4, stride=2, padding=(1, 1)))  # x/2

        self.conv3 = SpectralNorm(
            nn.Conv2d(64, 128, 3, stride=1, padding=(1, 1)))
        self.conv4 = SpectralNorm(
            nn.Conv2d(128, 128, 4, stride=2, padding=(1, 1)))  # x/2

        self.conv5 = SpectralNorm(
            nn.Conv2d(128, 256, 3, stride=1, padding=(1, 1)))
        self.conv6 = SpectralNorm(
            nn.Conv2d(256, 256, 4, stride=2, padding=(1, 1)))  # x/2

        self.conv7 = SpectralNorm(
            nn.Conv2d(256, 512, 3, stride=1, padding=(1, 1)))
        self.conv8 = SpectralNorm(
            nn.Conv2d(512, 512, 4, stride=2, padding=(1, 1)))  # x/2

        self.conv9 = SpectralNorm(
            nn.Conv2d(512, 1024, 3, stride=1, padding=(1, 1)))  # x

        self.fc = SpectralNorm(nn.Linear(4 * 4 * 1024, 1))  # dens
    def __init__(self, ic, oc, upsample=True, use_sn=False):
        super(Generative_ResBlock, self).__init__()
        self.conv1 = nn.Conv2d(ic, oc, 3, 1, 1)
        self.conv2 = nn.Conv2d(oc, oc, 3, 1, 1)
        self.conv3 = nn.Conv2d(ic, oc, 1, 1, 0)

        if (use_sn == False):
            model_list = [
                nn.BatchNorm2d(ic),
                nn.ReLU(inplace=True), self.conv1
            ]
        elif (use_sn == True):
            model_list = [
                nn.BatchNorm2d(ic),
                nn.ReLU(inplace=True),
                SpectralNorm(self.conv1)
            ]
        if (upsample == True):
            model_list.append(UpSample())
        model_list.extend([
            nn.BatchNorm2d(oc),
            nn.ReLU(inplace=True),
        ])
        if (use_sn == False):
            model_list.append(self.conv2)
        elif (use_sn == True):
            model_list.append(SpectralNorm(self.conv2))
        self.model = nn.Sequential(*model_list)

        bypass_list = [self.conv3]
        if (upsample == True):
            bypass_list.append(UpSample())
        self.bypass = nn.Sequential(*bypass_list)
Beispiel #8
0
    def build_conv_block(self, dim, padding_type, activation, use_dropout):
        conv_block = []
        p = 0
        if padding_type == 'reflect':
            conv_block += [nn.ReflectionPad2d(1)]
        elif padding_type == 'replicate':
            conv_block += [nn.ReplicationPad2d(1)]
        elif padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' %
                                      padding_type)

        conv_block += [
            SpectralNorm(nn.Conv2d(dim, dim, kernel_size=3, padding=p)),
            activation
        ]
        if use_dropout:
            conv_block += [nn.Dropout(0.5)]

        p = 0
        if padding_type == 'reflect':
            conv_block += [nn.ReflectionPad2d(1)]
        elif padding_type == 'replicate':
            conv_block += [nn.ReplicationPad2d(1)]
        elif padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' %
                                      padding_type)
        conv_block += [
            SpectralNorm(nn.Conv2d(dim, dim, kernel_size=3, padding=p))
        ]

        return nn.Sequential(*conv_block)
Beispiel #9
0
def convU(in_channels,
          out_channels,
          conv_layer,
          norm_layer,
          kernel_size=3,
          stride=1,
          dilation=1,
          bias=True):
    return nn.Sequential(
        SpectralNorm(
            conv_layer(in_channels,
                       out_channels,
                       kernel_size=kernel_size,
                       stride=stride,
                       dilation=dilation,
                       padding=((kernel_size - 1) // 2) * dilation,
                       bias=bias)),
        #         conv_layer(in_channels, out_channels, kernel_size=kernel_size, stride=stride, dilation=dilation, padding=((kernel_size-1)//2)*dilation, bias=bias),
        #         nn.BatchNorm2d(out_channels),
        nn.LeakyReLU(0.2),
        SpectralNorm(
            conv_layer(out_channels,
                       out_channels,
                       kernel_size=kernel_size,
                       stride=stride,
                       dilation=dilation,
                       padding=((kernel_size - 1) // 2) * dilation,
                       bias=bias)),
    )
    def __init__(self,
                 in_channels,
                 out_channels,
                 hidden_channels=None,
                 upsample=False,
                 n_classes=0,
                 leak=0):
        super(ResBlockGenerator, self).__init__()
        hidden_channels = out_channels if hidden_channels is None else hidden_channels

        self.upsample = upsample
        self.learnable_sc = (in_channels != out_channels) or upsample

        self.conv1 = SpectralNorm(conv3x3(in_channels,
                                          hidden_channels)).apply(init_weight)
        self.conv2 = SpectralNorm(conv3x3(hidden_channels,
                                          out_channels)).apply(init_weight)
        self.conv3 = SpectralNorm(conv1x1(in_channels,
                                          out_channels)).apply(init_weight)
        self.upsampling = nn.Upsample(scale_factor=2)
        self.n_cl = n_classes
        if n_classes == 0:
            self.bn1 = nn.BatchNorm2d(in_channels)
            self.bn2 = nn.BatchNorm2d(hidden_channels)
        else:
            self.bn1 = ConditionalNorm(in_channels, n_classes)
            self.bn2 = ConditionalNorm(hidden_channels, n_classes)

        if leak > 0:
            self.activation = nn.LeakyReLU(leak)
        else:
            self.activation = nn.ReLU()
    def __init__(self, in_channels, out_channels, stride=1):
        super(ResBlockDiscriminator, self).__init__()

        self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1)
        self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1)
        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),
                                       nn.ReLU(), SpectralNorm(self.conv2))
        else:
            self.model = nn.Sequential(
                nn.ReLU(), SpectralNorm(self.conv1), nn.ReLU(),
                SpectralNorm(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)
            nn.init.xavier_uniform_(self.bypass_conv.weight.data, np.sqrt(2))

            self.bypass = nn.Sequential(
                SpectralNorm(self.bypass_conv),
                nn.AvgPool2d(2, stride=stride, padding=0))
    def __init__(self, z_dim=128, channels=3, ch=32, n_classes=0, leak=0):
        super(Generator32, self).__init__()

        self.ch = ch
        self.dense = SpectralNorm(nn.Linear(z_dim,
                                            4 * 4 * 8 * ch)).apply(init_weight)
        self.final = SpectralNorm(conv3x3(ch, channels)).apply(init_weight)

        self.block1 = ResBlockGenerator(8 * ch,
                                        4 * ch,
                                        upsample=True,
                                        n_classes=n_classes,
                                        leak=leak)
        self.block2 = ResBlockGenerator(4 * ch,
                                        2 * ch,
                                        upsample=True,
                                        n_classes=n_classes,
                                        leak=leak)
        self.block3 = ResBlockGenerator(2 * ch,
                                        ch,
                                        upsample=True,
                                        n_classes=n_classes,
                                        leak=leak)

        self.bn = nn.BatchNorm2d(ch)
        if leak > 0:
            self.activation = nn.LeakyReLU(leak)
        else:
            self.activation = nn.ReLU()
Beispiel #13
0
	def __init__(self, ic = 1, oc = 1, norm_type = 'instancenorm', use_sn = False):
		super(ResBlock, self).__init__()
		self.ic = ic
		self.oc = oc
		self.norm_type = norm_type
		self.use_sn = use_sn

		self.relu = nn.ReLU(inplace = True)
		self.reflection_pad1 = nn.ReflectionPad1d(15)
		self.reflection_pad2 = nn.ReflectionPad1d(15)

		self.conv1 = nn.Conv1d(ic, oc, 31, 1, 0, bias = False)
		self.conv2 = nn.Conv1d(oc, oc, 31, 1, 0, bias = False)

		if(self.use_sn == True):
			self.conv1 = SpectralNorm(self.conv1)
			self.conv2 = SpectralNorm(self.conv2)
			
		if(self.norm_type == 'batchnorm'):
			self.bn1 = nn.BatchNorm1d(oc)
			self.bn2 = nn.BatchNorm1d(oc)

		elif(self.norm_type == 'instancenorm'):
			self.bn1 = nn.InstanceNorm1d(oc)
			self.bn2 = nn.InstanceNorm1d(oc)
Beispiel #14
0
def conv_block(in_channels,
               out_channels,
               kernel_size=3,
               stride=1,
               dilation=1,
               bias=True):
    """Conv block used in MSDilationBlock."""

    return nn.Sequential(
        SpectralNorm(
            nn.Conv2d(in_channels,
                      out_channels,
                      kernel_size=kernel_size,
                      stride=stride,
                      dilation=dilation,
                      padding=((kernel_size - 1) // 2) * dilation,
                      bias=bias)),
        nn.LeakyReLU(0.2),
        SpectralNorm(
            nn.Conv2d(out_channels,
                      out_channels,
                      kernel_size=kernel_size,
                      stride=stride,
                      dilation=dilation,
                      padding=((kernel_size - 1) // 2) * dilation,
                      bias=bias)),
    )
Beispiel #15
0
 def __init__(self, big_c, small_c, use_swish):
     super(FastGANSEBlk, self).__init__()
     self.branch = nn.Sequential(
         nn.AdaptiveAvgPool2d(4),
         SpectralNorm(nn.Conv2d(small_c, big_c, 4, 1, 0, bias=False)),
         nn.SiLU() if use_swish else nn.LeakyReLU(0.1),
         SpectralNorm(nn.Conv2d(big_c, big_c, 1, 1, 0, bias=False)),
         nn.Sigmoid())
Beispiel #16
0
 def __init__(self, ic, oc):
     super(FirstDiscBlk, self).__init__()
     self.model = nn.Sequential(SpectralNorm(nn.Conv2d(ic, oc, 3, 1, 1)),
                                nn.ReLU(),
                                SpectralNorm(nn.Conv2d(oc, oc, 3, 1, 1)),
                                nn.AvgPool2d(2))
     self.shortcut = nn.Sequential(nn.AvgPool2d(2),
                                   SpectralNorm(nn.Conv2d(ic, oc, 1, 1, 0)))
Beispiel #17
0
 def __init__(self, sz):
     super(SNDCGANDiscriminator, self).__init__()
     final_sz = sz // 8
     self.conv1 = nn.Sequential(SpectralNorm(nn.Conv2d(3, 64, 3, 1, 1)),
                                nn.LeakyReLU(0.1))
     self.conv2 = nn.Sequential(SpectralNorm(nn.Conv2d(64, 128, 4, 2, 1)),
                                nn.LeakyReLU(0.1))
     self.conv3 = nn.Sequential(SpectralNorm(nn.Conv2d(128, 128, 3, 1, 1)),
                                nn.LeakyReLU(0.1))
     self.conv4 = nn.Sequential(SpectralNorm(nn.Conv2d(128, 256, 4, 2, 1)),
                                nn.LeakyReLU(0.1))
     self.conv5 = nn.Sequential(SpectralNorm(nn.Conv2d(256, 256, 3, 1, 1)),
                                nn.LeakyReLU(0.1))
     self.conv6 = nn.Sequential(SpectralNorm(nn.Conv2d(256, 512, 4, 2, 1)),
                                nn.LeakyReLU(0.1))
     self.conv7 = nn.Sequential(SpectralNorm(nn.Conv2d(512, 512, 3, 1, 1)),
                                nn.LeakyReLU(0.1))
     self.fc = nn.Sequential(
         Reshape(-1, 512 * final_sz * final_sz),
         SpectralNorm(nn.Linear(512 * final_sz * final_sz, 1)))
     for m in self.modules():
         if (isinstance(m, (nn.Linear, nn.Conv2d, nn.ConvTranspose2d))):
             m.weight.data.normal_(0.0, 0.02)
             if (m.bias is not None):
                 m.bias.data.zero_()
	def __init__(self, sz, nc, ndf = 64, use_sigmoid = True, use_bn = True, norm_type = 'batchnorm', activation_type = 'leakyrelu'):
		super(DCGAN_D, self).__init__()
		assert sz > 4, "Image size should be bigger than 4"
		assert sz & (sz-1) == 0, "Image size should be a power of 2"
		self.sz = sz
		self.nc = nc
		self.ndf = ndf
		self.use_bn = use_bn
		self.norm_type = norm_type

		cur_ndf = ndf
		layers = [ConvBlock(self.nc, self.ndf, 4, 2, use_bn = False, activation_type = activation_type)]
		for i in range(int(math.log2(self.sz)) - 3):
			layers.append(ConvBlock(cur_ndf, cur_ndf * 2, 4, 2, use_bn = self.use_bn, norm_type = norm_type, activation_type = activation_type))
			cur_ndf *= 2

		if(self.norm_type == 'spectralnorm'):
			layers.append(SpectralNorm(nn.Conv2d(cur_ndf, 1, 4, 1, 0, bias = False)))
		else:
			layers.append(nn.Conv2d(cur_ndf, 1, 4, 1, 0, bias = False))

		self.main = nn.Sequential(*layers)
		self.sigmoid = nn.Sigmoid()
		self.use_sigmoid = use_sigmoid

		for m in self.modules():
			if(isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d)):
				m.weight.data.normal_(0.0, 0.02)
				if(m.bias is not None):
					m.bias.data.zero_()
	def __init__(self, ni, no, ks, stride, pad = None, use_bn = True, use_pixelshuffle = False, norm_type = 'batchnorm', activation_type = 'leakyrelu'):
		super(ConvBlock, self).__init__()
		self.use_bn = use_bn
		self.use_pixelshuffle = use_pixelshuffle
		self.norm_type = norm_type

		if(pad == None):
			pad = ks // 2 // stride

		if(use_pixelshuffle):
			self.conv = nn.Conv2d(ni // 4, no, ks, stride, pad, bias = False)
			self.pixelshuffle = nn.PixelShuffle(2)
		else:
			self.conv = nn.Conv2d(ni, no, ks, stride, pad, bias = False)

		if(self.use_bn == True):
			if(self.norm_type == 'batchnorm'):
				self.bn = nn.BatchNorm2d(no)
			elif(self.norm_type == 'instancenorm'):
				self.bn = nn.InstanceNorm2d(no)
			elif(self.norm_type == 'spectralnorm'):
				self.conv = SpectralNorm(self.conv)


		if(activation_type == 'relu'):
			self.act = nn.ReLU(inplace = True)
		elif(activation_type == 'leakyrelu'):
			self.act = nn.LeakyReLU(0.2, inplace = True)
		elif(activation_type == 'elu'):
			self.act = nn.ELU(inplace = True)
		elif(activation_type == 'selu'):
			self.act = nn.SELU(inplace = True)
	def __init__(self, sz, nz, nc, ngf = 64, use_bn = True, norm_type = 'batchnorm', activation_type = 'leakyrelu'):
		super(DCGAN_G_PixelShuffle, self).__init__()
		self.sz = sz
		self.nz = nz
		self.nc = nc
		self.ngf = ngf
		self.norm_type = norm_type

		cur_ngf = ngf * self.sz // 8
		layers = [ConvBlock(self.nz, cur_ngf, 4, 1, 3, use_bn = use_bn, use_pixelshuffle = False, norm_type = norm_type, activation_type = activation_type)]
		for i in range(int(math.log2(self.sz)) - 3):
			layers.append(ConvBlock(cur_ngf, cur_ngf // 2, 3, 1, 1, use_bn = use_bn, use_pixelshuffle = True, norm_type = norm_type, activation_type = activation_type))
			cur_ngf = cur_ngf // 2
		
		if(self.norm_type == 'spectralnorm'):
			layers.extend([nn.PixelShuffle(2), SpectralNorm(nn.Conv2d(self.ngf // 4, self.nc, 3, 1, 1, bias = False)), nn.Tanh()])
		else:
			layers.extend([nn.PixelShuffle(2), nn.Conv2d(self.ngf // 4, self.nc, 3, 1, 1, bias = False), nn.Tanh()])

		self.main = nn.Sequential(*layers)
		for m in self.modules():
			if(isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d)):
				m.weight.data.normal_(0.0, 0.02)
				if(m.bias is not None):
					m.bias.data.zero_()
    def __init__(self, channels=3, ch=32, n_classes=0, leak=0, att=False):
        super(Discriminator64, self).__init__()

        if leak > 0:
            self.activation = nn.LeakyReLU(leak)
        else:
            self.activation = nn.ReLU()

        self.ch = ch
        self.att = att
        self.block1 = OptimizedBlock(channels, ch, leak=leak)
        if att:
            self.attention = Attention(ch)
        self.block2 = ResBlockDiscriminator(ch,
                                            ch * 2,
                                            downsample=True,
                                            leak=leak)
        self.block3 = ResBlockDiscriminator(ch * 2,
                                            ch * 4,
                                            downsample=True,
                                            leak=leak)
        self.block4 = ResBlockDiscriminator(ch * 4,
                                            ch * 8,
                                            downsample=True,
                                            leak=leak)
        self.block5 = ResBlockDiscriminator(ch * 8, ch * 16, leak=leak)

        self.fc = SpectralNorm(nn.Linear(self.ch * 16, 1)).apply(init_weight)
        if n_classes > 0:
            self.embed_y = nn.Embedding(n_classes, ch * 16).apply(init_weight)
Beispiel #22
0
 def __init__(self, in_channels,conv_layer=nn.Conv2d, norm_layer=nn.BatchNorm2d, kernel_size=3, dilation=[1,1,1,1], bias=True):
     super(MSDilateBlock, self).__init__()
     self.conv1 =  convU(in_channels, in_channels,conv_layer, norm_layer, kernel_size,dilation=dilation[0], bias=bias)
     self.conv2 =  convU(in_channels, in_channels,conv_layer, norm_layer, kernel_size,dilation=dilation[1], bias=bias)
     self.conv3 =  convU(in_channels, in_channels,conv_layer, norm_layer, kernel_size,dilation=dilation[2], bias=bias)
     self.conv4 =  convU(in_channels, in_channels,conv_layer, norm_layer, kernel_size,dilation=dilation[3], bias=bias)
     self.convi =  SpectralNorm(conv_layer(in_channels*4, in_channels, kernel_size=kernel_size, stride=1, padding=(kernel_size-1)//2, bias=bias))
Beispiel #23
0
	def __init__(self, ni, no, ks, stride, pad = None, pad_type = 'Zero', output_pad = 0, use_bn = True, use_sn = False, norm_type = 'batchnorm', activation_type = 'leakyrelu'):
		super(DeConvBlock, self).__init__()
		self.use_bn = use_bn
		self.use_sn = use_sn
		self.norm_type = norm_type
		self.pad_type = pad_type

		if(pad is None):
			pad = ks // 2 // stride

		if(self.pad_type == 'Zero'):
			self.deconv = nn.ConvTranspose1d(ni, no, ks, stride, pad, output_padding = output_pad, bias = False)
		elif(self.pad_type == 'Reflection'):
			self.deconv = nn.ConvTranspose1d(ni, no, ks, stride, 0, output_padding = output_pad, bias = False)
			self.reflection = nn.ReflectionPad1d(pad)
		
		if(self.use_bn == True):
			if(self.norm_type == 'batchnorm'):
				self.bn = nn.BatchNorm1d(no)
			elif(self.norm_type == 'instancenorm'):
				self.bn = nn.InstanceNorm1d(no)

		if(self.use_sn == True):
			self.deconv = SpectralNorm(self.deconv)

		if(activation_type == 'relu'):
			self.act = nn.ReLU(inplace = True)
		elif(activation_type == 'leakyrelu'):
			self.act = nn.LeakyReLU(0.2, inplace = True)
		elif(activation_type == 'elu'):
			self.act = nn.ELU(inplace = True)
		elif(activation_type == 'selu'):
			self.act = nn.SELU(inplace = True)
		elif(activation_type == None):
			self.act = Nothing()
Beispiel #24
0
    def __init__(self, ic, hc, oc, downsample):
        super(DiscBlk, self).__init__()
        if downsample:
            self.avgpool = nn.AvgPool2d(2)
        else:
            self.avgpool = nn.Identity()

        self.model = nn.Sequential(nn.ReLU(),
                                   SpectralNorm(nn.Conv2d(ic, hc, 3, 1, 1)),
                                   nn.ReLU(),
                                   SpectralNorm(nn.Conv2d(hc, oc, 3, 1, 1)))

        if ic != oc or downsample:
            self.shortcut = nn.Sequential(
                SpectralNorm(nn.Conv2d(ic, oc, 1, 1, 0)), nn.AvgPool2d(2))
        else:
            self.shortcut = nn.Identity()
Beispiel #25
0
 def __init__(self, ic, hc, oc, sz):
     super(FastGANDiscInitBlk, self).__init__()
     if sz == 256:
         self.model = nn.Sequential(
             SpectralNorm(nn.Conv2d(ic, oc, 3, 1, 1, bias=False)),
             nn.LeakyReLU(0.2))
     if sz == 512:
         self.model = nn.Sequential(
             SpectralNorm(nn.Conv2d(ic, oc, 4, 2, 1, bias=False)),
             nn.LeakyReLU(0.2),
         )
     if sz == 1024:
         self.model = nn.Sequential(
             SpectralNorm(nn.Conv2d(ic, hc, 4, 2, 1, bias=False)),
             nn.LeakyReLU(0.2),
             SpectralNorm(nn.Conv2d(hc, oc, 4, 2, 1, bias=False)),
             nn.BatchNorm2d(oc), nn.LeakyReLU(0.2))
Beispiel #26
0
 def __init__(self, num_out):
     super().__init__(
         nn.Sequential(
             SpectralNorm(nn.Conv2d(3, num_out, 4, 2, 1, bias=False)),
             nn.LeakyReLU(0.2, inplace=True),
             SpectralLeakyConv(num_out, num_out * 2, 4, 2, 1),
             SpectralLeakyConv(num_out * 2, num_out * 4, 4, 2, 1),
             SpectralLeakyConv(num_out * 4, num_out * 8, 4, 2, 1)))
Beispiel #27
0
def conv3x3(in_planes, out_planes):
    "3x3 convolution with padding"
    return SpectralNorm(
        nn.Conv2d(in_planes,
                  out_planes,
                  kernel_size=3,
                  stride=1,
                  padding=1,
                  bias=False))
Beispiel #28
0
 def __init__(self, ic, ndf):
     super(SNGANDiscriminator32, self).__init__()
     self.ndf = ndf
     self.model = nn.Sequential(FirstDiscBlk(ic, ndf),
                                DiscBlk(ndf, ndf, ndf, True),
                                DiscBlk(ndf, ndf, ndf, False),
                                DiscBlk(ndf, ndf, ndf, False), nn.ReLU(),
                                nn.AdaptiveAvgPool2d(1))
     self.classifier = SpectralNorm(nn.Linear(ndf, 1, bias=False))
Beispiel #29
0
 def __init__(self):
     super(AdaINDiscriminator64, self).__init__()
     self.blk1 = ConvBlock(3, 32)
     self.blk2 = ConvBlock(32, 64)
     self.blk3 = ConvBlock(64, 128)
     self.blk4 = ConvBlock(128, 256)
     self.blk5 = ConvBlock(256, 512)
     self.fc = SpectralNorm(nn.Linear(512, 1))
     nn.init.xavier_uniform_(self.fc.weight.data, 1.0)
Beispiel #30
0
def conv_block(n_in, n_out, ks, stride, pad=None, bn=True):
    if pad is None:
        pad = ks//2//stride
    if bn == True:
        res = nn.Sequential(
            SpectralNorm(nn.Conv2d(n_in, n_out, kernel_size=ks, bias=False, stride=stride, padding=pad)),
            nn.BatchNorm2d(n_out),
            nn.LeakyReLU(negative_slope=0.2, inplace=True)
        )