예제 #1
0
    def test_actnorm_init_conv(self):
        np.random.seed(52321)
        images_np = np.random.rand(8, 32, 32, 3)
        images = tf.to_float(images_np)

        flow = fl.InputLayer(images)

        layer = fl.ActnormLayer(scale=np.sqrt(np.pi))

        with self.assertRaises(AssertionError):
            layer.get_ddi_init_ops()

        new_flow = layer(flow, forward=True)
        x, logdet, z = new_flow

        init_ops = layer.get_ddi_init_ops()
        self.assertEqual(z, None)

        with self.test_session() as sess:
            # initialize network
            sess.run(tf.global_variables_initializer())
            sess.run(init_ops)
            x, logdet = sess.run([x, logdet])

            self.assertEqual(x.shape, images_np.shape)
            # check var after passing act norm
            self.assertAllClose(np.var(x.reshape([-1, 3]), axis=0),
                                [np.pi] * 3,
                                atol=0.01)
            self.assertAllClose(np.mean(x.reshape([-1, 3]), axis=0), [0.0] * 3,
                                atol=0.01)

        self.forward_inverse(layer, flow)
예제 #2
0
    def test_actnorm_init_conv_iter(self):
        np.random.seed(52321)
        images_ph = tf.placeholder(tf.float32, shape=[8, 32, 32, 3])

        flow = fl.InputLayer(images_ph)

        layer = fl.ActnormLayer(scale=np.sqrt(np.pi))

        new_flow = layer(flow, forward=True)
        x, logdet, z = new_flow
        init_ops = layer.get_ddi_init_ops(num_init_iterations=50)

        with self.test_session() as sess:
            # initialize network
            sess.run(tf.global_variables_initializer())
            for i in range(200):
                sess.run(init_ops,
                         feed_dict={images_ph: np.random.rand(8, 32, 32, 3)})

            for i in range(5):
                x_np, logdet_np = sess.run(
                    [x, logdet],
                    feed_dict={images_ph: np.random.rand(8, 32, 32, 3)})
                self.assertEqual(x.shape, x_np.shape)
                self.assertAllClose(np.var(x_np.reshape([-1, 3]), axis=0),
                                    [np.pi] * 3,
                                    atol=0.1)
                self.assertAllClose(np.mean(x_np.reshape([-1, 3]), axis=0),
                                    [0.0] * 3,
                                    atol=0.1)

        self.forward_inverse(
            layer, flow, feed_dict={images_ph: np.random.rand(8, 32, 32, 3)})

        def feed_dict_fn():
            return {images_ph: np.random.rand(8, 32, 32, 3)}

        def post_init_fn(sess):
            init_ops = layer.get_ddi_init_ops()
            sess.run(init_ops, {images_ph: np.random.rand(8, 32, 32, 3)})

        with tf.variable_scope("TestTrain"):
            layer = fl.ActnormLayer(scale=np.sqrt(np.pi))
            self.try_to_train_identity_layer(layer,
                                             flow,
                                             feed_dict_fn=feed_dict_fn,
                                             post_init_fn=post_init_fn)
예제 #3
0
    def test_actnorm_conv(self):

        images_np = np.random.rand(8, 32, 32, 3)
        images = tf.to_float(images_np)

        flow = fl.InputLayer(images)

        layer = fl.ActnormLayer()
        new_flow = layer(flow, forward=True)
        x, logdet, z = new_flow

        self.assertEqual(z, None)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            x, logdet = sess.run([x, logdet])
            self.assertEqual(x.shape, images_np.shape)

        self.forward_inverse(layer, flow)
예제 #4
0
def step_flow(
    name: str, shift_and_log_scale_fn: Callable[[tf.Tensor], tf.Tensor]
) -> Tuple[fl.ChainLayer, fl.ActnormLayer]:
    """Create single step of the Glow model:

        1. actnorm
        2. invertible conv
        3. affine coupling layer

    Returns:
        step_layer: a flow layer which perform 1-3 operations
        actnorm: a reference of actnorm layer from step 1. This reference can be
            used to initialize this layer using data dependent initialization
    """
    actnorm = fl.ActnormLayer()
    layers = [
        actnorm,
        fl.InvertibleConv1x1Layer(),
        fl.AffineCouplingLayer(shift_and_log_scale_fn=shift_and_log_scale_fn),
    ]
    return fl.ChainLayer(layers, name=name), actnorm