def _build_net(self):
        with tf.variable_scope('actor'):
            with tf.variable_scope('encoder'):
                fc_1 = layers.fc_relu_layer('fc_1', bottom=self.image_in, output_dim=128)
                # continuous_hidden = tf.nn.sigmoid(layers.fc_layer('cont_hidden', bottom=fc_1, output_dim=256))

                _batch_size, _feature_size = self.image_in.get_shape().as_list()

                eps = tf.ones([_batch_size, self.code_length], dtype=bm.D_TYPE) * 0.5
                code_hidden = layers.fc_layer('code_hidden', bottom=fc_1, output_dim=self.code_length)

                codes, code_prob = bm.doubly_sn(code_hidden, eps)

            with tf.variable_scope('decoder'):
                fc_2 = tf.nn.relu(bm.fc_layer_hack('fc_2', bottom=codes, input_dim=self.code_length, output_dim=128),
                                  name='critic_sigmoid_2')
                decode_result = layers.fc_layer('decode_result', fc_2, _feature_size)

        tf.summary.scalar('actor/binary', tf.reduce_mean(codes))
        tf.summary.image('actor/image', tf.reshape(decode_result, [-1, 28, 28, 1]), max_outputs=1)

        return {
            'codes': codes,
            'code_hidden': code_hidden,
            'code_prob': code_prob,
            'decode_result': decode_result}
Beispiel #2
0
def simple_nn(name, tensor_in: tf.Tensor, middle_dim, output_dim=None):
    output_dim = output_dim or tensor_in.shape.as_list()[-1]
    with tf.variable_scope(name):
        fc_1 = layers.fc_layer('fc_1', tensor_in, middle_dim)
        fc_1 = layers.leaky_relu(fc_1)
        fc_2 = layers.fc_layer('fc_2', fc_1, output_dim)
    return fc_2
Beispiel #3
0
    def _build_net(self):
        fc_im = layers.fc_relu_layer('fc_im', self.batch_im_feat, 1024)
        enc_im = tf.sigmoid(layers.fc_layer('enc_im', fc_im, self.code_length))

        fc_sk = layers.fc_relu_layer('fc_sk', self.batch_sk_feat, 1024)
        enc_sk = tf.sigmoid(layers.fc_layer('enc_sk', fc_sk, self.code_length))

        concat_1 = kronecker_layer('kron', self.batch_im_feat,
                                   self.batch_sk_feat)
        batch_adjacency = gcn.build_adjacency(self.batch_semantic)
        gcn_1 = tf.nn.relu(
            gcn.spectrum_conv_layer('gcn_1', concat_1, batch_adjacency, 1024))
        gcn_2 = gcn.spectrum_conv_layer('gcn_2', gcn_1, batch_adjacency,
                                        self.code_length)

        eps = tf.ones([self.batch_size, self.code_length], dtype=D_TYPE) * 0.5

        codes, code_prob = doubly_sn(gcn_2, eps)

        dec_2_mean = layers.fc_layer_2('dec_mean', codes, self.code_length,
                                       300)
        dec_2_var = layers.fc_layer_2('dec_var', codes, self.code_length, 300)

        rslt = dict(enc_im=enc_im,
                    enc_sk=enc_sk,
                    code_logits=gcn_2,
                    codes=codes,
                    code_prob=code_prob,
                    dec_mean=dec_2_mean,
                    dec_var=dec_2_var)
        return rslt
Beispiel #4
0
    def _build_net(self):

        with tf.variable_scope(self.name_scope):
            # fc8 = alex_classifier(self.batch_image, self.label_size)
            fc7, _ = alex_net(self.batch_image)
            fc7 = tf.nn.relu(fc7)
            fc8 = fc_layer('fc_8', fc7, self.label_size)
        return fc8, fc7
Beispiel #5
0
    def _build_net(self):
        super()._build_net()
        connected_emb_2 = tf.concat([self.random_emb, self.z_padding], 1)
        with tf.variable_scope('actor', reuse=True):
            gen_feat, _ = self.inn(connected_emb_2, 0, forward=False)

        with tf.variable_scope('cls'):
            self.v_feat = tf.concat([self.feat, tf.cast(gen_feat, dtype=tf.float32)], 0)
            self.v_label = tf.concat([self.label, self.random_label], 0)
            self.cls_fc = layers.fc_layer('fc_1', self.v_feat, self.cls_num)
Beispiel #6
0
    def _build_net(self):
        self._get_feat()
        self.inn = SimplerINN('mnist',
                              self.dim,
                              depth=1,
                              norm=False,
                              coupling=0,
                              permute=1)
        with tf.variable_scope('actor') as scope:
            self.yz_hat, self.det = self.inn(self.feat, 0)
            self.y_hat = self.yz_hat[:, :self.emb_size]
            self.z_hat = self.yz_hat[:, self.emb_size:]

            scope.reuse_variables()
            self.x_hat, _ = self.inn(tf.stop_gradient(self.yz_hat), 0, False)
            self.gen, _ = self.inn(self.random_condition, 0, False)

        with tf.variable_scope('critic') as scope:
            self.d_y_real = tf.sigmoid(
                layers.fc_layer('fc_y', self.random_condition, 1))

            x_real = tf.concat([self.feat, self.label], 1)
            self.d_x_real = tf.sigmoid(layers.fc_layer('fc_x', x_real, 1))
            scope.reuse_variables()
            self.d_y_fake = tf.sigmoid(layers.fc_layer('fc_y', self.yz_hat, 1))
            x_fake_1 = tf.concat([self.x_hat, self.label], 1)
            x_fake_2 = tf.concat([self.gen, self.random_label], 1)

            x_hat = x_fake_2  # tf.concat([x_fake_1, x_fake_2], 0)
            self.d_x_fake = tf.sigmoid(layers.fc_layer('fc_x', x_hat, 1))

        gen_image = tf.nn.relu(tf.reshape(self.gen, [-1, 28, 28, 1]))
        recon_image = tf.nn.relu(tf.reshape(self.x_hat, [-1, 28, 28, 1]))
        origin_image = tf.reshape(self.feat, [-1, 28, 28, 1])

        tf.summary.image('img', origin_image, max_outputs=1)
        tf.summary.image('recon', recon_image, max_outputs=1)
        tf.summary.image('gen', gen_image, max_outputs=1)
        tf.summary.histogram('y_hat', self.y_hat)
        tf.summary.histogram('y_real', self.label)

        tf.summary.histogram('z_hat', self.z_hat)
        tf.summary.histogram('z_real', self.z_padding)
Beispiel #7
0
def spectrum_conv_layer(name, tensor_in, adjacency, out_dim, size):
    """
    Convolution on a graph with graph Laplacian
    :param name:
    :param tensor_in: [N D]
    :param adjacency: [N N]
    :param out_dim:
    :return:
    """
    fc_sc = layers.fc_layer(name, tensor_in, output_dim=out_dim)
    conv_sc = graph_laplacian(adjacency, size) @ fc_sc
    return conv_sc
    def _build_net(self):
        self._get_feat()
        inn_1 = self.InnModule('inn_1', self.feat_size // 2)
        inn_2 = self.InnModule('inn_2', self.feat_size // 4)

        with tf.variable_scope('actor') as scope:
            # 1. v->s
            self.mid_s, _ = inn_1(self.feat, 0)
            self.mid_s_1 = self.mid_s[:, :self.mid_size]
            self.mid_s_2 = self.mid_s[:, self.mid_size:]

            self.pred_s, _ = inn_2(self.mid_s_1, 0)
            self.pred_s_1 = self.pred_s[:, :self.emb_size]
            self.pred_s_2 = self.pred_s[:, self.emb_size:]

            # 2. s->v'
            scope.reuse_variables()
            connected_2 = tf.concat([self.random_emb, self.z_padding_2], 1)
            self.mid_v, _ = inn_2(connected_2, 0, forward=False)

            connected_1 = tf.concat([self.mid_v, self.z_padding_1], 1)
            self.pred_v, _ = inn_1(connected_1, 0, forward=False)

            # 3. v'->s'
            connected_e_1 = tf.concat([self.cls_emb, self.zero_padding], 1)
            self.mid_e, _ = inn_2(connected_e_1, 0, forward=False)
            self.mid_e = tf.stop_gradient(self.mid_e)

        with tf.variable_scope('critic') as scope:
            # 1. real
            connected_emb = tf.concat([connected_2, self.z_padding_1], axis=1)
            self.d_v_real = tf.sigmoid(layers.fc_layer('fc_v', self.feat, 1))
            self.d_s_real = tf.sigmoid(
                layers.fc_layer('fc_s', connected_emb, 1))
            scope.reuse_variables()
            # 2. fake
            connected_s = tf.concat([self.pred_s, self.mid_s_2], axis=1)
            self.d_v_fake = tf.sigmoid(layers.fc_layer('fc_v', self.pred_v, 1))
            self.d_s_fake = tf.sigmoid(layers.fc_layer('fc_s', connected_s, 1))
Beispiel #9
0
def alex_classifier(tensor_in, out_dim):
    from util.layer.conventional_layers import fc_layer
    fc7, _ = tf.nn.relu(alex_net(tensor_in))
    fc8 = fc_layer('fc_8', fc7, out_dim)
    return fc8
Beispiel #10
0
    def _build_net(self):
        with tf.variable_scope('actor'):
            with tf.variable_scope('encoder'):
                fc_1 = layers.fc_relu_layer('fc_1',
                                            bottom=self.image_in,
                                            output_dim=2048)
                # continuous_hidden = tf.nn.sigmoid(layers.fc_layer('cont_hidden', bottom=fc_1, output_dim=256))

                _batch_size, _feature_size = self.image_in.get_shape().as_list(
                )

                eps = tf.ones([_batch_size, self.code_length],
                              dtype=D_TYPE) * 0.5
                code_hidden = layers.fc_layer('code_hidden',
                                              bottom=fc_1,
                                              output_dim=self.code_length)

                codes, code_prob = doubly_sn(code_hidden, eps)

                batch_adjacency = gcn.build_adjacency_hamming(
                    codes, code_length=self.code_length)

                continuous_hidden = tf.nn.sigmoid(
                    gcn.spectrum_conv_layer('gcn', fc_1, batch_adjacency, 64,
                                            _batch_size))

            with tf.variable_scope('decoder'):
                fc_2 = layers.fc_relu_layer('fc_2', continuous_hidden, 2048)
                decode_result = layers.fc_relu_layer('decode_result', fc_2,
                                                     _feature_size)

        with tf.variable_scope('critic'):
            real_logic = tf.sigmoid(layers.fc_layer('critic',
                                                    bottom=continuous_hidden,
                                                    output_dim=1),
                                    name='critic_sigmoid')

            real_binary_logic = tf.sigmoid(fc_layer_hack(
                'critic_2',
                bottom=codes,
                input_dim=self.code_length,
                output_dim=1),
                                           name='critic_sigmoid_2')

        with tf.variable_scope('critic', reuse=True):
            random_in = tf.random.uniform([_batch_size, 64])
            fake_logic = tf.sigmoid(layers.fc_layer('critic',
                                                    bottom=random_in,
                                                    output_dim=1),
                                    name='critic_sigmoid')

            random_binary = (tf.sign(
                tf.random.uniform([_batch_size, self.code_length]) - 0.5) +
                             1) / 2
            fake_binary_logic = tf.sigmoid(fc_layer_hack(
                'critic_2',
                bottom=random_binary,
                input_dim=self.code_length,
                output_dim=1),
                                           name='critic_sigmoid_2')

        # adj_pic = tf.expand_dims(tf.expand_dims(batch_adjacency, axis=0), axis=-1)

        # tf.summary.image('actor/adj', adj_pic)

        # tf.summary.histogram('critic/fake_cont', random_in)
        # tf.summary.histogram('critic/fake_binary', random_binary)
        tf.summary.scalar('critic/fake_logic', tf.reduce_mean(fake_logic))
        tf.summary.scalar('actor/real_logic', tf.reduce_mean(real_logic))
        # tf.summary.histogram('actor/real_cont', continuous_hidden)
        # tf.summary.histogram('actor/binary', codes)
        # tf.summary.histogram('actor/code_hidden', code_hidden)
        tf.summary.scalar('actor/binary', tf.reduce_mean(codes))

        return {
            'codes': codes,
            'code_hidden': code_hidden,
            'decode_result': decode_result,
            'hidden': continuous_hidden,
            'real_logic': real_logic,
            'fake_logic': fake_logic,
            'real_binary_logic': real_binary_logic,
            'fake_binary_logic': fake_binary_logic
        }