Beispiel #1
0
    def __init__(self, args):
        super(VAE, self).__init__(args)

        if self.args.dataset_name == 'freyfaces':
            h_size = 210
        elif self.args.dataset_name == 'cifar10':
            h_size = 384
        else:
            h_size = 294

        # encoder: q(z2 | x)
        self.q_z2_layers = nn.Sequential(
            GatedConv2d(self.args.input_size[0], 32, 7, 1, 3),
            GatedConv2d(32, 32, 3, 2, 1), GatedConv2d(32, 64, 5, 1, 2),
            GatedConv2d(64, 64, 3, 2, 1), GatedConv2d(64, 6, 3, 1, 1))
        # linear layers
        self.q_z2_mean = NonLinear(h_size, self.args.z2_size, activation=None)
        self.q_z2_logvar = NonLinear(h_size,
                                     self.args.z2_size,
                                     activation=nn.Hardtanh(min_val=-6.,
                                                            max_val=2.))

        # encoder: q(z1|x,z2)
        # PROCESSING x
        self.q_z1_layers_x = nn.Sequential(
            GatedConv2d(self.args.input_size[0], 32, 3, 1, 1),
            GatedConv2d(32, 32, 3, 2, 1), GatedConv2d(32, 64, 3, 1, 1),
            GatedConv2d(64, 64, 3, 2, 1), GatedConv2d(64, 6, 3, 1, 1))
        # PROCESSING Z2
        self.q_z1_layers_z2 = nn.Sequential(
            GatedDense(self.args.z2_size, h_size))
        self.q_z1_layers_joint = nn.Sequential(GatedDense(2 * h_size, 300))
        self.q_z1_mean = NonLinear(300, self.args.z1_size, activation=None)
        self.q_z1_logvar = NonLinear(300,
                                     self.args.z1_size,
                                     activation=nn.Hardtanh(min_val=-6.,
                                                            max_val=2.))

        # decoder p(z1|z2)
        self.p_z1_layers = nn.Sequential(GatedDense(self.args.z2_size, 300),
                                         GatedDense(300, 300))
        self.p_z1_mean = NonLinear(300, self.args.z1_size, activation=None)
        self.p_z1_logvar = NonLinear(300,
                                     self.args.z1_size,
                                     activation=nn.Hardtanh(min_val=-6.,
                                                            max_val=2.))

        # decoder: p(x | z)
        self.p_x_layers_z1 = nn.Sequential(
            GatedDense(self.args.z1_size, np.prod(self.args.input_size)))
        self.p_x_layers_z2 = nn.Sequential(
            GatedDense(self.args.z2_size, np.prod(self.args.input_size)))

        # PixelCNN
        act = nn.ReLU(True)
        self.pixelcnn = nn.Sequential(
            MaskedConv2d('A',
                         self.args.input_size[0] + 2 * self.args.input_size[0],
                         64,
                         3,
                         1,
                         1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False), nn.BatchNorm2d(64),
            act, MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False), nn.BatchNorm2d(64),
            act, MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act)

        if self.args.input_type == 'binary':
            self.p_x_mean = Conv2d(64, 1, 1, 1, 0, activation=nn.Sigmoid())
        elif self.args.input_type == 'gray' or self.args.input_type == 'continuous':
            self.p_x_mean = Conv2d(64,
                                   self.args.input_size[0],
                                   1,
                                   1,
                                   0,
                                   activation=nn.Sigmoid(),
                                   bias=False)
            self.p_x_logvar = Conv2d(64,
                                     self.args.input_size[0],
                                     1,
                                     1,
                                     0,
                                     activation=nn.Hardtanh(min_val=-4.5,
                                                            max_val=0.),
                                     bias=False)

        # weights initialization
        for m in self.modules():
            if isinstance(m, nn.Linear):
                he_init(m)

        # add pseudo-inputs if VampPrior
        if self.args.prior == 'vampprior':
            self.add_pseudoinputs()
    def __init__(self, args):
        super(VAE, self).__init__(args)

        # encoder: q(z | x)
        if self.args.dataset_name == 'freyfaces':
            h_size = 210
        elif self.args.dataset_name == 'cifar10':
            h_size = 384
        elif self.args.dataset_name == 'svhn':
            h_size = 384
        else:
            h_size = 294

        # encoder: q(z2 | x)
        self.q_z_layers = nn.Sequential(
            Conv2d(self.args.input_size[0], 32, 7, 1, 3, activation=nn.ReLU()),
            nn.BatchNorm2d(32), Conv2d(32, 32, 3, 2, 1, activation=nn.ReLU()),
            nn.BatchNorm2d(32), Conv2d(32, 64, 5, 1, 2, activation=nn.ReLU()),
            nn.BatchNorm2d(64), Conv2d(64, 64, 3, 2, 1, activation=nn.ReLU()),
            nn.BatchNorm2d(64), Conv2d(64, 6, 3, 1, 1, activation=nn.ReLU()))
        '''
        self.q_z_layers = [NonGatedDense(np.prod(self.args.input_size), 300, activation=nn.ReLU())]
        for i in range(args.number_hidden):
            self.q_z_layers.append(NonGatedDense(300, 300, activation=nn.ReLU()))

        self.q_z_layers = nn.ModuleList(self.q_z_layers)
        '''
        self.q_z_mean = Linear(h_size, self.args.z1_size)
        self.q_z_logvar = Linear(
            h_size, self.args.z1_size
        )  #NonLinear(300, self.args.z1_size, activation=nn.Hardtanh(min_val=-6.,max_val=2.))

        # decoder: p(x | z)
        self.p_x_layers = [
            NonGatedDense(self.args.z1_size, 300, activation=nn.ReLU())
        ]
        for i in range(args.number_hidden):
            self.p_x_layers.append(
                NonGatedDense(300, 300, activation=nn.ReLU()))
        self.p_x_layers.append(
            NonGatedDense(300,
                          np.prod(self.args.input_size),
                          activation=nn.ReLU()))
        self.p_x_layers = nn.ModuleList(self.p_x_layers)

        # PixelCNN
        act = nn.ReLU(True)
        self.pixelcnn = nn.Sequential(
            MaskedConv2d('A',
                         self.args.input_size[0] + self.args.input_size[0],
                         64,
                         3,
                         1,
                         1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False), nn.BatchNorm2d(64),
            act, MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False), nn.BatchNorm2d(64),
            act, MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act)

        if self.args.input_type == 'binary':
            self.p_x_mean = Conv2d(64, 1, 1, 1, 0, activation=nn.Sigmoid())
        elif self.args.input_type == 'gray' or self.args.input_type == 'continuous':
            self.p_x_mean = Conv2d(64,
                                   self.args.input_size[0],
                                   1,
                                   1,
                                   0,
                                   activation=nn.Sigmoid(),
                                   bias=False)
            self.p_x_logvar = Conv2d(64,
                                     self.args.input_size[0],
                                     1,
                                     1,
                                     0,
                                     activation=nn.Hardtanh(min_val=-4.5,
                                                            max_val=0.),
                                     bias=False)

        # weights initialization
        for m in self.modules():
            if isinstance(m, nn.Linear):
                he_init(m)
            else:
                if torch.is_tensor(m):
                    torch.nn.init.kaiming_normal(m)

        # add pseudo-inputs if VampPrior
        if self.args.prior == 'vampprior':
            self.add_pseudoinputs()
Beispiel #3
0
    def __init__(self, args):
        super(VAE, self).__init__(args)

        if self.args.dataset_name == 'freyfaces':
            h_size = 210
        elif self.args.dataset_name == 'cifar10':
            h_size = 384
        else:
            h_size = 294

        # encoder: q(z | x)
        self.q_z_layers = nn.ModuleList()

        # conv 0
        self.q_z_layers.append(GatedConv2d(1, 32, 5, 1, 3))
        # conv 1
        self.q_z_layers.append(GatedConv2d(32, 32, 5, 2, 1))
        # conv 2
        self.q_z_layers.append(GatedConv2d(32, 64, 5, 1, 3))
        # conv 3
        self.q_z_layers.append(GatedConv2d(64, 64, 5, 2, 1))
        # conv 7
        self.q_z_layers.append(GatedConv2d(64, 6, 3, 1, 1))

        # linear layers
        self.q_z_mean = nn.Linear(h_size, self.args.z1_size)
        self.q_z_logvar = NonLinear(h_size,
                                    self.args.z1_size,
                                    activation=nn.Hardtanh(min_val=-6.,
                                                           max_val=2.))

        # decoder: p(x | z)
        self.p_x_layers = nn.ModuleList()

        self.p_x_layers.append(
            GatedDense(self.args.z1_size, np.prod(self.args.input_size)))

        # PixelCNN
        act = nn.ReLU()
        self.pixelcnn = nn.Sequential(
            MaskedConv2d('A',
                         self.args.input_size[0] + self.args.input_size[0],
                         64,
                         5,
                         1,
                         2,
                         bias=True), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 5, 1, 2, bias=True), nn.BatchNorm2d(64),
            act, MaskedConv2d('B', 64, 64, 5, 1, 2, bias=True),
            nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 5, 1, 2, bias=True), nn.BatchNorm2d(64),
            act, MaskedConv2d('B', 64, 64, 5, 1, 2, bias=True),
            nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 5, 1, 2,
                         bias=True), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 5, 1, 2,
                         bias=True), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 5, 1, 2,
                         bias=True), nn.BatchNorm2d(64), act)

        if self.args.input_type == 'binary':
            self.p_x_mean = Conv2d(64, 1, 1, 1, 0, activation=nn.Sigmoid())
        elif self.args.input_type == 'gray' or self.args.input_type == 'continuous':
            self.p_x_mean = Conv2d(64,
                                   self.args.input_size[0],
                                   1,
                                   1,
                                   0,
                                   activation=nn.Sigmoid())
            self.p_x_logvar = Conv2d(64,
                                     self.args.input_size[0],
                                     1,
                                     1,
                                     0,
                                     activation=nn.Hardtanh(min_val=-4.5,
                                                            max_val=0.))

        # weights initialization
        for m in self.modules():
            if isinstance(m, nn.Linear):
                he_init(m)

        # add pseudo-inputs if VampPrior
        if self.args.prior == 'vampprior':
            self.add_pseudoinputs()
Beispiel #4
0
    def __init__(self, args):
        super(VAE, self).__init__(args)

        # encoder: q(z | x)
        self.q_z_layers = iRevNet(nBlocks=[18, 18],
                                  nStrides=[1, 2],
                                  nChannels=[16, 64],
                                  nClasses=300,
                                  init_ds=2,
                                  dropout_rate=0.1,
                                  affineBN=True,
                                  in_shape=self.args.input_size,
                                  mult=4)

        #self.q_z_layers = nn.DataParallel(self.q_z_layers)

        self.q_z_mean = Linear(300, self.args.z1_size)
        self.q_z_logvar = NonLinear(300,
                                    self.args.z1_size,
                                    activation=nn.Hardtanh(min_val=-6.,
                                                           max_val=2.))

        # decoder: p(x | z)
        self.p_x_layers = [
            NonGatedDense(self.args.z1_size, 300, activation=nn.ReLU())
        ]
        for i in range(args.number_hidden):
            self.p_x_layers.append(
                NonGatedDense(300, 300, activation=nn.ReLU()))
        self.p_x_layers.append(
            NonGatedDense(300,
                          np.prod(self.args.input_size),
                          activation=nn.ReLU()))
        self.p_x_layers = nn.ModuleList(self.p_x_layers)

        # PixelCNN
        act = nn.ReLU(True)
        self.pixelcnn = nn.Sequential(
            MaskedConv2d('A',
                         self.args.input_size[0] + self.args.input_size[0],
                         64,
                         3,
                         1,
                         1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False), nn.BatchNorm2d(64),
            act, MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False), nn.BatchNorm2d(64),
            act, MaskedConv2d('B', 64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act,
            MaskedConv2d('B', 64, 64, 3, 1, 1,
                         bias=False), nn.BatchNorm2d(64), act)

        if self.args.input_type == 'binary':
            self.p_x_mean = Conv2d(64, 1, 1, 1, 0, activation=nn.Sigmoid())
        elif self.args.input_type == 'gray' or self.args.input_type == 'continuous':
            self.p_x_mean = Conv2d(64,
                                   self.args.input_size[0],
                                   1,
                                   1,
                                   0,
                                   activation=nn.Sigmoid(),
                                   bias=False)
            self.p_x_logvar = Conv2d(64,
                                     self.args.input_size[0],
                                     1,
                                     1,
                                     0,
                                     activation=nn.Hardtanh(min_val=-4.5,
                                                            max_val=0.),
                                     bias=False)

        # weights initialization
        for m in self.modules():
            if isinstance(m, nn.Linear):
                he_init(m)

        # add pseudo-inputs if VampPrior
        if self.args.prior == 'vampprior':
            self.add_pseudoinputs()