Пример #1
0
    def latent_classifier(self, z):
        net = self.arch['classifier']['latent']
        
        x = tf.transpose(z, [0, 2, 1]) # [N, z_dim, n_frames]
        x = tf.expand_dims(x, -1) # [N, z_dim, n_frames, 1]
        
        for i, (o, k, s) in enumerate(zip(net['output'], net['kernel'], net['stride'])):
            with tf.variable_scope('Conv-{}'.format(i)):

                x_tanh = tf.layers.conv2d_transpose(x, o, k, s,
                    padding='same',
                    data_format='channels_first',
                )
                x_tanh = Layernorm(x_tanh, [1, 2, 3], 'Layernorm-{}-tanh'.format(i))
                
                x_sigmoid = tf.layers.conv2d_transpose(x, o, k, s,
                    padding='same',
                    data_format='channels_first',
                )
                x_sigmoid = Layernorm(x_sigmoid, [1, 2, 3], 'Layernorm-{}-sigmoid'.format(i))

                # GLU
                with tf.variable_scope('GLU'):
                    x = tf.sigmoid(x_sigmoid) * tf.tanh(x_tanh)
        
        # carefully design the architecture so that now x has shape [N, C, n_frames, 1]
        batch_size, c, n_frames, w = x.get_shape().as_list()
        x = tf.transpose(x, perm=[0, 2, 1, 3]) # [N, n_frames, C, 1]
        x = tf.squeeze(x, axis=[-1]) # [N, n_frames, C]
        x = tf.layers.dense(x, self.arch['y_dim']) # [N, n_frames, 1]
        return tf.reduce_mean(x, axis=1) #[N, 1]
Пример #2
0
    def _generator(self, z, y, is_training=None):
        net = self.arch['generator']
        h, w, c = net['hwc']

        if y is not None:
            y = tf.nn.embedding_lookup(self.y_emb, y)
            x = self._merge([z, y], h * w * c)
        else:
            x = z
        print "generate x shape:", x.get_shape().as_list()
        x = tf.reshape(x, [-1, c, h, w])  # channel first
        print "generate x reshape:", x.get_shape().as_list()
        for i, (o, k, s) in enumerate(
                zip(net['output'], net['kernel'], net['stride'])):
            x = tf.layers.conv2d_transpose(
                x,
                o,
                k,
                s,
                padding='same',
                data_format='channels_first',
            )
            if i < len(net['output']) - 1:
                x = Layernorm(x, [1, 2, 3], 'ConvT-LN{}'.format(i))
                x = lrelu(x)
            print("ConvT-LN{}, shape:".format(i), x.get_shape().as_list())
        print "generate x shape:", x.get_shape().as_list()
        return x
Пример #3
0
    def _generator(self, z, y, net):
        with tf.device("/gpu:3"):
            x = tf.expand_dims(z, 1)  # [N, 1, n_frames, z_dim]
            x = tf.transpose(x, perm=[0, 3, 2, 1])  # [N, z_dim, n_frames, 1]
            y = tf.nn.embedding_lookup(self.y_emb,
                                       y)  # [N, n_frames, y_emb_dim]
            y = tf.expand_dims(y, 1)  # [N, 1, n_frames, y_emb_dim]

            for i, (o, k, s) in enumerate(
                    zip(net['output'], net['kernel'], net['stride'])):
                with tf.variable_scope('Conv-{}'.format(i)):

                    # concat y along channel axis
                    y_transpose = tf.transpose(
                        y, perm=[0, 3, 2, 1])  # [N, y_emb_dim, n_frames, 1]
                    x = tf.concat(
                        [x, y_transpose],
                        axis=1)  # [N, channels + y_emb_dim, n_frames, 1]

                    if i < len(net['output']) - 1:
                        x_tanh = tf.layers.conv2d_transpose(
                            x,
                            o,
                            k,
                            s,
                            padding='same',
                            data_format='channels_first',
                        )
                        x_tanh = Layernorm(x_tanh, [1, 2, 3],
                                           'Layernorm-{}-tanh'.format(i))

                        x_sigmoid = tf.layers.conv2d_transpose(
                            x,
                            o,
                            k,
                            s,
                            padding='same',
                            data_format='channels_first',
                        )
                        x_sigmoid = Layernorm(x_sigmoid, [1, 2, 3],
                                              'Layernorm-{}-sigmoid'.format(i))

                        # GLU
                        with tf.variable_scope('GLU'):
                            x = tf.sigmoid(x_sigmoid) * tf.tanh(x_tanh)
                    else:
                        x = tf.layers.conv2d_transpose(
                            x,
                            o,
                            k,
                            s,
                            padding='same',
                            data_format='channels_first',
                        )

            x = tf.squeeze(x, axis=[-1])  # [N, C, n_frames]
            x = tf.transpose(x, perm=[0, 2, 1])  # [N, n_frames, C]
            return x