Example #1
0
    def __init__(self, nc, ndf, include_sigmoid):
        super(_netD4, self).__init__()

        self.main = nn.Sequential(
            # input is (nc) x 64 x 64
            SNConv2d(nc, ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            SNConv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            SNConv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            SNConv2d(ndf * 4, ndf * 16, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 16),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*16) x 4 x 4
            SNConv2d(ndf * 16, 1, 4, 1, 0, bias=False),
            #nn.Sigmoid()
        )
        self.sigmoid = nn.Sigmoid()
        self.extra_layers_to_run = []
        if include_sigmoid:
            self.extra_layers_to_run.append(self.sigmoid)
Example #2
0
    def __init__(self, nc, ndf, num_classes):
        super(_netC, self).__init__()

        self.main = nn.Sequential(
            # input is (nc) x 64 x 64
            SNConv2d(nc, ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            SNConv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            SNConv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            SNConv2d(ndf * 4, ndf * 16, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 16),
            nn.LeakyReLU(0.2, inplace=True),
            SNConv2d(ndf * 16, ndf * 64, 4, 1, 0, bias=False),
            # state size. (ndf*16) x 4 x 4
            #SNConv2d(ndf * 16, 1, 4, 1, 0, bias=False),
            #nn.Sigmoid()
        )
        self.linear = nn.Linear(1024*4, num_classes)
        self.softmax = nn.Softmax()
        self.sigmoid = nn.Sigmoid()
Example #3
0
    def __init__(self, nc, ndf, include_sigmoid):
        super(_netD6, self).__init__()

        # Upsampling
        self.down = nn.Sequential(
            SNConv2d(3, 64, 3, 2, 1),
            nn.ReLU(),
        )
        # Fully-connected layers
        self.down_size = (64 // 2)
        down_dim = 64 * (64 // 2)**2
        self.fc = nn.Sequential(
            nn.Linear(down_dim, 32),
            nn.BatchNorm1d(32, 0.8),
            nn.ReLU(inplace=True),
            nn.Linear(32, down_dim),
            nn.BatchNorm1d(down_dim),
            nn.ReLU(inplace=True)
        )
        # Upsampling
        self.up = nn.Sequential(
            nn.Upsample(scale_factor=2),
            SNConv2d(64, 3, 3, 1, 1)
        )
        self.final = nn.Sequential(
            nn.MaxPool3d([3, 64, 64]),
            #nn.Sigmoid()
        )
        self.sigmoid = nn.Sigmoid()
        self.extra_layers_to_run = []
        if include_sigmoid:
            self.extra_layers_to_run.append(self.sigmoid)
Example #4
0
    def __init__(self, nc, ndf, ncontext, ndiscriminators):
        super(_netE, self).__init__()

        self.main = nn.Sequential(
            # input is (nc * ncontext) x 32 x 32 TODO add ncontext
            #nn.Linear(32, ndiscriminators, bias=False),
            SNConv2d(nc, ndf, 7, 4, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf, 3, 7, 4, 1, bias=False),
            nn.Sigmoid())
Example #5
0
 def make_res_block(self, in_channels, out_channels):
     model = []
     model += [
         SNConv2d(in_channels, out_channels, kernel_size=3, padding=1)
     ]
     model += [nn.ReLU()]
     model += [
         SNConv2d(out_channels, out_channels, kernel_size=3, padding=1)
     ]
     model += [nn.AvgPool2d(2)]
     return nn.Sequential(*model)
Example #6
0
    def __init__(self, nc, ndf):
        super(_netD2, self).__init__()

        self.main = nn.Sequential(
            SNConv2d(nc, ndf, 3, 4, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf, ndf, 5, 2, 1, bias=False),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf, 1, 3, 2, 0, bias=False),
            nn.Sigmoid()
        )
Example #7
0
    def __init__(self, nc, ndf):
        super(_netD1, self).__init__()

        self.main = nn.Sequential(SNConv2d(nc, ndf, 5, 2, 2),
                                  nn.LeakyReLU(0.2, inplace=True),
                                  SNConv2d(ndf, ndf * 2, 5, 2, 2),
                                  nn.LeakyReLU(0.2, inplace=True),
                                  SNConv2d(ndf * 2, ndf * 4, 5, 2, 2),
                                  nn.LeakyReLU(0.2, inplace=True),
                                  SNConv2d(ndf * 4, ndf * 8, 5, 2, 2),
                                  nn.LeakyReLU(0.2, inplace=True),
                                  SNConv2d(ndf * 8, 1, 4), nn.Sigmoid())
Example #8
0
    def __init__(self, nc, ndf):
        super(_netD3, self).__init__()

        self.main = nn.Sequential(
            # input is (nc) x 32 x 32
            SNConv2d(nc, ndf, 3, 1, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf, ndf, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            # state size. (ndf) x 1 x 32
            SNConv2d(ndf, ndf * 2, 3, 1, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf*2, ndf * 2, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            # state size. (ndf*2) x 16 x 16
            SNConv2d(ndf * 2, ndf * 4, 3, 1, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf * 4, ndf * 4, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            # state size. (ndf*8) x 4 x 4
            SNConv2d(ndf * 4, ndf * 8, 3, 1, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf * 8, 1, 4, 1, 0, bias=False),
            nn.Sigmoid()
        )
Example #9
0
def cyclegan_discriminator_block(in_filters, out_filters, normalize=True):
    """Returns downsampling layers of each discriminator block"""
    layers = [SNConv2d(in_filters, out_filters, 4, stride=2, padding=1)]
    if normalize:
        layers.append(nn.InstanceNorm2d(out_filters))
    layers.append(nn.LeakyReLU(0.2, inplace=True))
    return layers
Example #10
0
 def make_residual_connect(self, in_channels, out_channels):
     model = []
     model += [
         SNConv2d(in_channels, out_channels, kernel_size=1, padding=0)
     ]
     model += [nn.AvgPool2d(2)]
     return nn.Sequential(*model)
Example #11
0
def dcgan_discriminator_block(in_filters, out_filters, bn=True):
    block = [   SNConv2d(in_filters, out_filters, 3, 2, 1),
                nn.LeakyReLU(0.2, inplace=True),
                nn.Dropout2d(0.25)]
    if bn:
        block.append(nn.BatchNorm2d(out_filters, 0.8))
    return block
Example #12
0
    def make_res_block(self, in_channels, out_channels, hidden_channels,
                       use_BN, downsample):
        model = []
        if use_BN:
            model += [nn.BatchNorm2d(in_channels)]

        model += [nn.ReLU()]
        model += [
            SNConv2d(in_channels, hidden_channels, kernel_size=3, padding=1)
        ]
        model += [nn.ReLU()]
        model += [
            SNConv2d(hidden_channels, out_channels, kernel_size=3, padding=1)
        ]
        if downsample:
            model += [nn.AvgPool2d(2)]
        return nn.Sequential(*model)
    def __init__(self):
        super(_netD, self).__init__()
        ndf = 64
        self.main = nn.Sequential(
            # state size. 1 x 32 x 32
            SNConv2d(1, ndf, 3, 1, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf, ndf, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            # state size. (ndf) x 16 x 16
            SNConv2d(ndf, ndf * 2, 3, 1, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf * 2, ndf * 2, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            # state size. (ndf*2) x 8 x 8
            SNConv2d(ndf * 2, ndf * 4, 3, 1, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf * 4, ndf * 4, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            # state size. (ndf*4) x 4 x 4
            SNConv2d(ndf * 4, ndf * 8, 3, 1, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
        )

        self.ln = nn.Sequential(SNLinear(ndf * 8 * 2 * 5, 1024),
                                nn.LeakyReLU(0.2, inplace=True),
                                SNLinear(1024, 1), nn.Sigmoid())
 def __init__(self):
     super(_netD, self).__init__()
     nc = 1
     ndf = 64
     self.ini_w = 2
     self.ini_h = 5
     self.main = nn.Sequential(
         # input is (nc) x 32 x 32
         SNConv2d(nc, ndf, 3, 1, 1, bias=True),
         nn.LeakyReLU(0.1, inplace=True),
         SNConv2d(ndf, ndf, 4, 2, 1, bias=True),
         nn.LeakyReLU(0.1, inplace=True),
         # state size. (ndf) x 16 x 16
         SNConv2d(ndf, ndf * 2, 3, 1, 1, bias=True),
         nn.LeakyReLU(0.1, inplace=True),
         SNConv2d(ndf * 2, ndf * 2, 4, 2, 1, bias=True),
         nn.LeakyReLU(0.1, inplace=True),
         # state size. (ndf*2) x 8 x 8
         SNConv2d(ndf * 2, ndf * 4, 3, 1, 1, bias=True),
         nn.LeakyReLU(0.1, inplace=True),
         SNConv2d(ndf * 4, ndf * 4, 4, 2, 1, bias=True),
         nn.LeakyReLU(0.1, inplace=True),
         # state size. (ndf*4) x 4 x 4
         SNConv2d(ndf * 4, ndf * 8, 3, 1, 1, bias=True),
         nn.LeakyReLU(0.1, inplace=True),
     )
     self.snlinear = nn.Sequential(
         SNLinear(ndf * 8 * self.ini_w * self.ini_h, 1), nn.Sigmoid())
Example #15
0
    def __init__(self, nc, ndf, include_sigmoid):
        super(_netD1, self).__init__()

        self.main = nn.Sequential(
            # input is (nc) x 32 x 32
            SNConv2d(nc, ndf, 3, 4, 1, bias=False),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf, ndf, 5, 2, 1, bias=False),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf, ndf, 3, 2, 0, bias=False),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(ndf, 1, 3, 1, 0, bias=False),
            #nn.Sigmoid()
        )
        self.sigmoid = nn.Sigmoid()
        self.extra_layers_to_run = []
        if include_sigmoid:
            self.extra_layers_to_run.append(self.sigmoid)
Example #16
0
    def __init__(self, nc, ndf, include_sigmoid):
        super(_netD2, self).__init__()

        self.main = nn.Sequential(
            SNConv2d(nc, ndf, 5, 2, 2), 
            nn.LeakyReLU(0.2, inplace=True),
            SNConv2d(ndf, ndf * 2, 5, 2, 2),
            nn.LeakyReLU(0.2, inplace=True),
            SNConv2d(ndf * 2, ndf * 4, 5, 2, 2),
            nn.LeakyReLU(0.2, inplace=True),
            SNConv2d(ndf * 4, ndf * 8, 5, 2, 2),
            nn.LeakyReLU(0.2, inplace=True),
            SNConv2d(ndf * 8, 1, 4),
            #nn.Sigmoid()
        )
        self.sigmoid = nn.Sigmoid()
        self.extra_layers_to_run = []
        if include_sigmoid:
            self.extra_layers_to_run.append(self.sigmoid)
Example #17
0
    def __init__(self, nc, nef, ndiscriminators, context_vector_size):
        super(_netE, self).__init__()

        self.main = nn.Sequential(
            # input is (nc * ncontext) x 32 x 32 TODO add ncontext
            #nn.Linear(32, ndiscriminators, bias=False),
            SNConv2d(nc, nef, 7, 4, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(nef, ndiscriminators, 7, 4, 1, bias=False),
            nn.Sigmoid()
        )

        self.image = nn.Sequential(
            SNConv2d(nc, nef, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(nef, nef * 2, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(nef * 2, nef * 4, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(nef * 4, ndiscriminators, 7, 4, 1, bias=False),
            nn.LeakyReLU(0.1, inplace=True),
            #nn.Softmax() # remove once context has been added
        )

        self.context = nn.Sequential(
            #nn.Linear(nc * nef * nef + ndiscriminators, nef * 8),
            nn.Linear(context_vector_size + ndiscriminators, nef * 8),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(nef * 8, ndiscriminators),
            nn.Softmax()
        )
Example #18
0
    def __init__(self, nc, nef, ndiscriminators):
        super(_netE, self).__init__()

        self.main = nn.Sequential(
            # input is (nc * ncontext) x 32 x 32 TODO add ncontext
            #nn.Linear(32, ndiscriminators, bias=False),
            SNConv2d(nc, nef, 7, 4, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(nef, ndiscriminators, 7, 4, 1, bias=False),
            nn.Sigmoid())

        self.image = nn.Sequential(
            SNConv2d(nc, nef, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(nef, nef * 2, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(nef * 2, nef * 4, 4, 2, 1, bias=True),
            nn.LeakyReLU(0.1, inplace=True),
            SNConv2d(nef * 4, ndiscriminators, 7, 4, 1, bias=False),
            nn.LeakyReLU(0.1, inplace=True),
        )

        self.context = nn.Sequential(
            nn.Linear(nc * nef * nef + ndiscriminators, nef * 8),
            nn.LeakyReLU(0.2, inplace=True), nn.Linear(nef * 8,
                                                       ndiscriminators),
            nn.Sigmoid())
Example #19
0
    def __init__(self, in_channels=4, lookahead=1, num_actions=18, ngf=64):
        super(_netD, self).__init__()
        self.en_conv1 = SNConv2d(in_channels=in_channels + lookahead,
                                 out_channels=ngf,
                                 kernel_size=8,
                                 stride=4,
                                 padding=1)
        self.en_norm1 = nn.BatchNorm2d(num_features=ngf)

        self.en_relu2 = nn.LeakyReLU(negative_slope=0.2, inplace=True)
        self.en_conv2 = SNConv2d(in_channels=ngf,
                                 out_channels=ngf * 2,
                                 kernel_size=4,
                                 stride=2,
                                 padding=1)
        self.en_norm2 = nn.BatchNorm2d(num_features=ngf * 2)

        self.en_relu3 = nn.LeakyReLU(negative_slope=0.2, inplace=True)
        self.en_conv3 = SNConv2d(in_channels=ngf * 2,
                                 out_channels=ngf * 4,
                                 kernel_size=4,
                                 stride=2,
                                 padding=1)
        self.en_norm3 = nn.BatchNorm2d(num_features=ngf * 4)

        self.en_relu4 = nn.LeakyReLU(negative_slope=0.2, inplace=True)
        self.en_conv4 = SNConv2d(in_channels=ngf * 4,
                                 out_channels=16,
                                 kernel_size=3,
                                 stride=1,
                                 padding=1)
        self.en_norm4 = nn.BatchNorm2d(num_features=16)

        self.dense6 = nn.Linear(in_features=16 * 25 + num_actions * lookahead,
                                out_features=18)
        self.dense7 = nn.Linear(in_features=18 + num_actions * lookahead,
                                out_features=1)
Example #20
0
    def __init__(self, nc, ndf, include_sigmoid):
        super(_netD9, self).__init__()

        self.main = nn.Sequential(
            *cyclegan_discriminator_block(3, 64, normalize=False),
            *cyclegan_discriminator_block(64, 128),
            *cyclegan_discriminator_block(128, 256),
            *cyclegan_discriminator_block(256, 512),
            nn.ZeroPad2d((1, 0, 1, 0)),
            SNConv2d(512, 1, 4, padding=1),
            nn.MaxPool3d([1, 4, 4]),
            #nn.Sigmoid()
        )
        self.sigmoid = nn.Sigmoid()
        self.extra_layers_to_run = []
        if include_sigmoid:
            self.extra_layers_to_run.append(self.sigmoid)
Example #21
0
    def __init__(self):
        super(discriminator_SN_ff, self).__init__()

        self.CNN = nn.Sequential(
            SNConv2d(in_channels=2,
                     out_channels=4,
                     kernel_size=(1, 7),
                     dilation=(1, 1)),
            # nn.BatchNorm2d(64),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=4,
                     out_channels=8,
                     kernel_size=(7, 1),
                     dilation=(1, 1)),
            # nn.BatchNorm2d(64),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=8,
                     out_channels=16,
                     kernel_size=(5, 5),
                     dilation=(1, 1)),
            # nn.BatchNorm2d(64),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=16,
                     out_channels=32,
                     kernel_size=(5, 5),
                     dilation=(3, 3)),
            # nn.BatchNorm2d(64),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=32,
                     out_channels=64,
                     kernel_size=(5, 5),
                     dilation=(5, 5)),
            # nn.BatchNorm2d(64),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=64,
                     out_channels=128,
                     kernel_size=(5, 5),
                     dilation=(13, 13)),
            # nn.BatchNorm2d(64),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=128, out_channels=256, kernel_size=(1, 31)),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=256, out_channels=512, kernel_size=(1, 31)),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=512, out_channels=1024, kernel_size=(1, 31)),
            nn.LeakyReLU(0.3),
            SNConv2d(in_channels=1024, out_channels=1, kernel_size=1),
            nn.LeakyReLU(0.3))

        self.FC3 = nn.Sequential(nn.Dropout(0.5, False), SNLinear(7 * 73, 1),
                                 nn.ReLU())
        for name, param in self.FC3.named_parameters():
            if 'bias' in name:
                nn.init.constant_(param, 0)
            elif 'weight' in name:
                nn.init.xavier_normal_(param)

        self.FC4 = nn.Sequential(nn.Dropout(0.5, False), nn.Linear(600, 257))
        for name, param in self.FC4.named_parameters():
            if 'bias' in name:
                nn.init.constant_(param, 0)
            elif 'weight' in name:
                nn.init.xavier_normal_(param)
        for name, param in self.CNN.named_parameters():
            if 'bias' in name:
                nn.init.constant_(param, 0.0)
            elif 'weight' in name:
                nn.init.xavier_normal_(param)