Пример #1
0
    def test_conv_deconv_transpose(self):
        def feval(X, y):
            f = K.function(X, y)
            shape = (np.random.randint(8, 18), ) + tuple(X.shape.as_list()[1:])
            x = np.random.rand(*shape)
            return f(x)

        prog = Progbar(target=2 * 3 * 3 * 2 * 2, print_report=True)
        for X in (K.placeholder(shape=(None, 13, 12, 25)),
                  K.placeholder(shape=(None, 13, 12, 8, 25))):
            for strides in (1, 2, 3):
                for filter_size in (3, 4, 5):
                    for num_filters in (8, 25):
                        for pad in ("same", "valid"):
                            for dilation in (1, ):
                                # ====== progress ====== #
                                prog['test'] = "#Dim:%d;Stride:%d;Filter:%d;Channel:%d;Pad:%s" % \
                                    (X.shape.ndims, strides, filter_size, num_filters, pad)
                                prog.add(1)
                                # ====== test Conv ====== #
                                f = N.Conv(num_filters=num_filters,
                                           filter_size=filter_size,
                                           pad=pad,
                                           strides=strides,
                                           activation=tf.nn.relu,
                                           dilation=dilation)
                                fT = f.T
                                y = f(X)
                                self.assertEqual(
                                    feval(X, y).shape[1:],
                                    tuple(y.shape.as_list()[1:]))
                                yT = fT(y)
                                self.assertEqual(
                                    feval(X, yT).shape[1:],
                                    tuple(yT.shape.as_list()[1:]))
                                self.assertEqual(X.shape.as_list(),
                                                 yT.shape.as_list())
                                # ====== test Transpose ====== #
                                f = N.TransposeConv(num_filters=num_filters,
                                                    filter_size=filter_size,
                                                    pad=pad,
                                                    strides=strides,
                                                    activation=K.relu,
                                                    dilation=dilation)
                                fT = f.T
                                y = f(X)
                                self.assertEqual(
                                    feval(X, y).shape[1:],
                                    tuple(y.shape.as_list()[1:]))
                                yT = fT(y)
                                self.assertEqual(
                                    feval(X, yT).shape[1:],
                                    tuple(yT.shape.as_list()[1:]))
                                self.assertEqual(X.shape.as_list(),
                                                 yT.shape.as_list())
Пример #2
0
        N.BatchNorm(),

        N.Conv(num_filters=64, filter_size=3, pad='valid'),
        N.BatchNorm(activation=tf.nn.elu),
        N.Pool(),

        N.Flatten(outdim=2),
        N.Dense(num_units=args.dim)
    ], debug=True, name='EncoderNetwork')

    f_decoder = N.Sequence([
        N.Dropout(level=LATENT_DROPOUT, noise_type='uniform'),
        N.Noise(level=1.0, noise_type='gaussian'),
        N.Dimshuffle((0, 'x', 'x', 1)),

        N.TransposeConv(num_filters=64, filter_size=3, pad='valid'),
        N.Upsample(size=2, axes=(1, 2)),
        N.BatchNorm(),

        N.TransposeConv(num_filters=64, filter_size=3, pad='same'),
        N.BatchNorm(),

        N.TransposeConv(num_filters=32, filter_size=3, pad='valid'),
        N.Upsample(size=2, axes=(1, 2),
                   desire_shape=None if is_cifar10 else (None, 14, 14, None)),
        N.BatchNorm(),

        N.TransposeConv(num_filters=3 if is_cifar10 else 1,
                        filter_size=3, strides=2, pad='same'),
        N.Dimshuffle((0, 3, 1, 2)) if is_cifar10 else N.Squeeze(axis=-1)
    ], debug=True, name='DecoderNetwork')
Пример #3
0
def convolutional_vae(X, saved_states, **kwargs):
    """ convolutional_vae

    Return
    ------
    [y_encoder, y_decoder]

    States
    ------
    [f_inference (encoder), f_generative (decoder)]

    """
    n = kwargs.get('n', 10)
    batch_size = K.get_shape(X)[0]
    if batch_size is None:
        raise ValueError(
            "You must specify batch_size dimension for the input placeholder.")
    # ====== init ====== #
    if saved_states is None:
        # Encoder
        f_inference = N.Sequence([
            N.Reshape(shape=(-1, 28, 28, 1)),
            N.Conv(num_filters=32,
                   filter_size=3,
                   strides=1,
                   pad='valid',
                   b_init=init_ops.constant_initializer(0.),
                   activation=K.elu),
            N.Conv(num_filters=64,
                   filter_size=5,
                   strides=2,
                   pad='same',
                   b_init=init_ops.constant_initializer(0.),
                   activation=K.elu),
            N.Dropout(level=0.1),
            N.Flatten(outdim=2),
            N.Dense(num_units=n * 2, b_init=None),
            N.BatchNorm(axes=0)
        ],
                                 debug=True,
                                 name='Encoder')
        # Decoder
        f_generative = N.Sequence([
            N.Dimshuffle(pattern=(0, 'x', 'x', 1)),
            N.TransposeConv(num_filters=64,
                            filter_size=3,
                            strides=1,
                            pad='valid',
                            b_init=init_ops.constant_initializer(0.),
                            activation=K.elu),
            N.TransposeConv(num_filters=32,
                            filter_size=5,
                            strides=2,
                            pad='same',
                            b_init=init_ops.constant_initializer(0.),
                            activation=K.elu),
            N.TransposeConv(num_filters=1,
                            filter_size=13,
                            strides=3,
                            pad='valid',
                            b_init=None),
            N.BatchNorm(activation=K.linear),
            N.Flatten(outdim=3)
        ],
                                  debug=True,
                                  name="Decoder")
    else:
        f_inference, f_generative = saved_states
    # ====== Perfrom ====== #
    # Encoder
    y_encoder = f_inference(K.cast(X, 'float32'))
    mu = y_encoder[:, :n]
    sigma = K.softplus(y_encoder[:, n:])
    qz = Normal(mu=mu, sigma=sigma, name='Normal_qz')
    # Decoder
    z = Normal(mu=K.zeros(shape=(batch_size, n)),
               sigma=K.ones(shape=(batch_size, n)),
               name="Normal_pz")
    logits = f_generative(z)
    X_reconstruct = Bernoulli(logits=logits)
    # inference
    params = f_inference.parameters + f_generative.parameters
    inference = ed.KLqp(latent_vars={z: qz}, data={X_reconstruct: X})
    # ====== get cost for training ====== #
    # Bind p(x, z) and q(z | x) to the same placeholder for x.
    if K.is_training():
        import tensorflow as tf
        inference.initialize()
        if True:
            optimizer = tf.train.AdamOptimizer(0.01, epsilon=1.0)
            updates = optimizer.apply_gradients(
                optimizer.compute_gradients(inference.loss, var_list=params))
            init = tf.global_variables_initializer()
            init.run()
            f_train = K.function(X, inference.loss, updates)
        else:
            optimizer = tf.train.AdamOptimizer(0.01, epsilon=1.0)
            inference.initialize(optimizer=optimizer, var_list=params)
            init = tf.global_variables_initializer()
            init.run()
            f_train = lambda x: inference.update(feed_dict={X: x})['loss']
    samples = K.sigmoid(logits)
    return (samples, z, qz), (f_inference, f_generative)