Пример #1
0
    def recognition(self, input_images, reuse=False):
        with tf.variable_scope("recognition") as scope:
            if reuse:
                scope.reuse_variables()

            im = tf.reshape(input_images, [-1, 3, self.im_size, self.im_size])

            conv1 = Conv2D('Discriminator.1', 3, self.dim_h, 5, im, stride=2)
            out_conv1 = LeakyReLU(conv1)

            conv2 = Conv2D('Discriminator.2',
                           self.dim_h,
                           2 * self.dim_h,
                           5,
                           out_conv1,
                           stride=2)
            out_conv2 = LeakyReLU(conv2)

            conv3 = Conv2D('Discriminator.3',
                           2 * self.dim_h,
                           4 * self.dim_h,
                           5,
                           out_conv2,
                           stride=2)
            out_conv3 = LeakyReLU(conv3)

            # dim_h = 64 |||| w=4, h=4, num_channel=4*64
            h2_flat = tf.reshape(out_conv3, [-1, 4 * 4 * 4 * self.dim_h])
            w_mean = Linear('Discriminator.w_mean', 4 * 4 * 4 * self.dim_h,
                            self.n_z, h2_flat)
            w_stddev = Linear('Discriminator.w_stddev', 4 * 4 * 4 * self.dim_h,
                              self.n_z, h2_flat)

        return w_mean, w_stddev, h2_flat
Пример #2
0
    def recognition(self, input_images, reuse=False):
        with tf.variable_scope("recognition") as scope:
            if reuse:
                scope.reuse_variables()
            # h1 = lrelu(conv2d(input_images, 1, 16, "d_h1")) # 28x28x1 -> 14x14x16
            # h2 = lrelu(conv2d(h1, 16, 32, "d_h2")) # 14x14x16 -> 7x7x32
            # h2_flat = tf.reshape(h2,[self.batchsize, 7*7*32])

            im = tf.reshape(input_images, [-1, 3, self.im_size, self.im_size])

            conv1 = Conv2D('Discriminator.1', 3, self.dim_h, 5, im, stride=2)
            out_conv1 = LeakyReLU(conv1)

            conv2 = Conv2D('Discriminator.2', self.dim_h, 2 * self.dim_h, 5, out_conv1, stride=2)
            out_conv2 = LeakyReLU(conv2)

            conv3 = Conv2D('Discriminator.3', 2 * self.dim_h, 4 * self.dim_h, 5, out_conv2, stride=2)
            out_conv3 = LeakyReLU(conv3)

            # dim_h = 64 |||| w=4, h=4, num_channel=4*64
            h2_flat = tf.reshape(out_conv3, [-1, 4 * 4 * 4 * self.dim_h])
            w_mean = Linear('Discriminator.w_mean', 4 * 4 * 4 * self.dim_h, self.n_z, h2_flat)
            w_stddev = Linear('Discriminator.w_stddev', 4 * 4 * 4 * self.dim_h, self.n_z, h2_flat)

        return w_mean, w_stddev, h2_flat
Пример #3
0
    def generation(self, z, reuse=False):
        with tf.variable_scope("generation") as scope:
            if reuse:
                scope.reuse_variables()
            fc1 = Linear('Generator.Input', self.n_z, 4 * 4 * 4 * self.dim_h, z)
            fc1 = Batchnorm('Generator.BN1', [0], fc1)
            fc1 = tf.nn.relu(fc1)
            out_fc1 = tf.reshape(fc1, [-1, 4 * self.dim_h, 4, 4])

            deconv1 = Deconv2D('Generator.2', 4 * self.dim_h, 2 * self.dim_h, 5, out_fc1)
            deconv1 = Batchnorm('Generator.BN2', [0, 2, 3], deconv1)
            deconv1 = tf.nn.relu(deconv1)
            out_deconv1 = deconv1[:, :, :7, :7]

            deconv2 = Deconv2D('Generator.3', 2 * self.dim_h, self.dim_h, 5, out_deconv1)
            deconv2 = Batchnorm('Generator.BN3', [0, 2, 3], deconv2)
            out_deconv2 = tf.nn.relu(deconv2)

            deconv3 = Deconv2D('Generator.5', self.dim_h, 3, 5, out_deconv2)
            out_deconv3 = tf.sigmoid(deconv3)

            return tf.reshape(out_deconv3, [-1, self.dim_x])
            # z_develop = dense(z, self.n_z, 7*7*32, scope='z_matrix')
            # z_matrix = tf.nn.relu(tf.reshape(z_develop, [self.batchsize, 7, 7, 32]))
            # h1 = tf.nn.relu(conv_transpose(z_matrix, [self.batchsize, 14, 14, 16], "g_h1"))
            # h2 = conv_transpose(h1, [self.batchsize, 28, 28, 1], "g_h2")
            # h2 = tf.nn.sigmoid(h2)

        return h2
Пример #4
0
    def generation(self, z, reuse=False):
        with tf.variable_scope("generation") as scope:
            if reuse:
                scope.reuse_variables()
            fc1 = Linear('Generator.Input', self.n_z, 4 * 4 * 4 * self.dim_h,
                         z)
            fc1 = Batchnorm('Generator.BN1', [0], fc1)
            fc1 = tf.nn.relu(fc1)
            out_fc1 = tf.reshape(fc1, [-1, 4 * self.dim_h, 4, 4])

            deconv1 = Deconv2D('Generator.2', 4 * self.dim_h, 2 * self.dim_h,
                               5, out_fc1)
            deconv1 = Batchnorm('Generator.BN2', [0, 2, 3], deconv1)
            deconv1 = tf.nn.relu(deconv1)
            out_deconv1 = deconv1[:, :, :7, :7]

            deconv2 = Deconv2D('Generator.3', 2 * self.dim_h, self.dim_h, 5,
                               out_deconv1)
            deconv2 = Batchnorm('Generator.BN3', [0, 2, 3], deconv2)
            out_deconv2 = tf.nn.relu(deconv2)

            deconv3 = Deconv2D('Generator.5', self.dim_h, 3, 5, out_deconv2)
            out_deconv3 = tf.sigmoid(deconv3)

            return tf.reshape(out_deconv3, [-1, self.dim_x])
Пример #5
0
    def discriminator(self, x, reuse=False):
        with tf.variable_scope("discriminator") as scope:
            if reuse:
                scope.reuse_variables()
            im = tf.reshape(x, [-1, 3, self.im_size, self.im_size])

            conv1 = Conv2D('Discriminator.1', 3, self.dim_h, 5, im, stride=2)
            out_conv1 = LeakyReLU(conv1)

            conv2 = Conv2D('Discriminator.2',
                           self.dim_h,
                           2 * self.dim_h,
                           5,
                           out_conv1,
                           stride=2)
            out_conv2 = LeakyReLU(conv2)

            conv3 = Conv2D('Discriminator.3',
                           2 * self.dim_h,
                           4 * self.dim_h,
                           5,
                           out_conv2,
                           stride=2)
            out_conv3 = LeakyReLU(conv3)

            fc = tf.reshape(out_conv3, [-1, 4 * 4 * 4 * self.dim_h])
            out_fc = Linear('Discriminator.Output', 4 * 4 * 4 * self.dim_h, 1,
                            fc)
            return tf.reshape(out_fc, [-1]), fc
Пример #6
0
def Discriminator(inputs):
    output = tf.reshape(inputs, [-1, 3, 32, 32])

    output = Conv2D('Discriminator.1', 3, DIM, 5, output, stride=2)
    output = LeakyReLU(output)

    output = Conv2D('Discriminator.2', DIM, 2 * DIM, 5, output, stride=2)
    output = Batchnorm('Discriminator.BN2', [0, 2, 3], output)
    output = LeakyReLU(output)

    output = Conv2D('Discriminator.3', 2 * DIM, 4 * DIM, 5, output, stride=2)
    output = Batchnorm('Discriminator.BN3', [0, 2, 3], output)
    output = LeakyReLU(output)

    hidden_features = tf.reshape(output, [-1, 4 * 4 * 4 * DIM])
    output = Linear('Discriminator.Output', 4 * 4 * 4 * DIM, 1,
                    hidden_features)
    return tf.reshape(output, [-1]), hidden_features
Пример #7
0
def Generator(n_samples, noise=None):
    if noise is None:
        noise = tf.random_normal([n_samples, 128])

    output = Linear('Generator.Input', 128, 4 * 4 * 4 * DIM, noise)
    output = Batchnorm('Generator.BN1', [0], output)
    output = tf.nn.relu(output)
    output = tf.reshape(output, [-1, 4 * DIM, 4, 4])

    output = Deconv2D('Generator.2', 4 * DIM, 2 * DIM, 5, output)
    output = Batchnorm('Generator.BN2', [0, 2, 3], output)
    output = tf.nn.relu(output)

    output = Deconv2D('Generator.3', 2 * DIM, DIM, 5, output)
    output = Batchnorm('Generator.BN3', [0, 2, 3], output)
    output = tf.nn.relu(output)

    output = Deconv2D('Generator.5', DIM, 3, 5, output)
    output = tf.tanh(output)
    return tf.reshape(output, [-1, OUTPUT_DIM])
Пример #8
0
def LeakyReLULayer(name, n_in, n_out, inputs):
    output = Linear(name + '.Linear', n_in, n_out, inputs)
    return LeakyReLU(output)
Пример #9
0
def ReLULayer(name, n_in, n_out, inputs):
    output = Linear(name + '.Linear', n_in, n_out, inputs)
    return tf.nn.relu(output)