예제 #1
0
    def build(self):
        self.logger.info("Model building starts")
        tf.reset_default_graph()
        tf.set_random_seed(self.args.rseed)

        self.input1 = tf.placeholder(tf.float32, shape = [self.args.nbatch, self.height, self.width, self.nchannel])
        self.epsilon_input = tf.placeholder(tf.float32, shape=[self.args.nbatch, self.args.nconti])
        self.objective = tf.placeholder(tf.float32, shape = [self.args.nbatch, self.args.ncat])
        self.istrain = tf.placeholder(tf.bool, shape= [])

        self.generate_sess()

        self.mcf = SolveMaxMatching(nworkers=self.args.nbatch, ntasks=self.args.ncat, k=1, pairwise_lamb=self.args.plamb)
        # Encoding
        self.encoder_net = lie_encoder1_64
        # self.encoder_net = encoder1_64
        self.decoder_net = lie_decoder1_64

        # Continuous rep
        encode_dict = self.encoder_net(self.input1, output_dim=2*self.args.nconti, scope='encoder', group_feats_size=self.args.group_feats_size, reuse=False)
        self.mean_total, self.stddev_total = tf.split(encode_dict['output'], num_or_size_splits=2, axis=1)
        self.enc_gfeats_mat = encode_dict['gfeats_mat']
        self.stddev_total = tf.nn.softplus(self.stddev_total)
        self.z_sample = tf.add(self.mean_total, tf.multiply(self.stddev_total, self.epsilon_input))

        decode_dict = self.decoder_net(z=tf.concat([self.z_sample, self.objective], axis=-1), output_channel=self.nchannel, nconti=self.args.nconti, ncat=self.args.ncat, group_feats_size=self.args.group_feats_size, scope="decoder", lie_norm_type=self.args.lie_norm_type, reuse=False)
        self.dec_output = decode_dict['output']
        self.dec_lie_group_mat = decode_dict['lie_group_mat']
        self.dec_lie_alg = decode_dict['lie_alg']
        self.lie_alg_basis = decode_dict['lie_alg_basis'] # [1, lat_dim, mat_dim, mat_dim]

        # Unary vector
        self.rec_cost_vector = sigmoid_cross_entropy_without_mean(labels=self.input1, logits=self.dec_output)

        # Loss
        self.rec_cost = tf.reduce_mean(self.rec_cost_vector)

        self.kl_cost = vae_kl_cost(mean=self.mean_total, stddev=self.stddev_total)
        self.lie_loss = self.calc_lie_loss(self.enc_gfeats_mat, self.dec_lie_group_mat, self.dec_lie_alg, self.lie_alg_basis, self.args.nbatch)
        self.loss = self.rec_cost + self.kl_cost + self.lie_loss

        # Decode
        self.latent_ph = tf.placeholder(tf.float32, shape = [self.args.nbatch, self.args.nconti+self.args.ncat])
        self.dec_output_ph = tf.nn.sigmoid(self.decoder_net(z=self.latent_ph, output_channel=self.nchannel, nconti=self.args.nconti, ncat=self.args.ncat, group_feats_size=self.args.group_feats_size, scope="decoder", lie_norm_type=self.args.lie_norm_type, reuse=True)['output'])

        self.logger.info("Model building ends")
예제 #2
0
    def build(self):
        self.logger.info("Model building starts")
        tf.reset_default_graph()
        tf.set_random_seed(self.args.rseed)

        self.input1 = tf.placeholder(
            tf.float32,
            shape=[self.args.nbatch, self.height, self.width, self.nchannel])
        self.epsilon_input = tf.placeholder(
            tf.float32, shape=[self.args.nbatch, self.args.nconti])
        self.objective = tf.placeholder(
            tf.float32, shape=[self.args.nbatch, self.args.ncat])
        self.istrain = tf.placeholder(tf.bool, shape=[])
        self.I_weight = tf.placeholder(tf.float32, shape=[])
        self.F_weight = tf.placeholder(tf.float32, shape=[])

        # For VC-Loss
        self.delta_dim = tf.placeholder(tf.int32, shape=[self.args.nbatch])
        if self.args.use_discrete:
            self.objective_2_idx = tf.placeholder(tf.int32,
                                                  shape=[self.args.nbatch])
        else:
            self.objective_2 = tf.placeholder(
                tf.float32, shape=[self.args.nbatch, self.args.ncat])

        self.generate_sess()

        self.mcf = SolveMaxMatching(nworkers=self.args.nbatch,
                                    ntasks=self.args.ncat,
                                    k=1,
                                    pairwise_lamb=self.args.plamb)
        # Encoding
        self.encoder_net = encoder1_64
        self.decoder_net = decoder1_64
        self.disc_net = disc_net_64

        # Continuous rep
        self.mean_total, self.stddev_total = tf.split(self.encoder_net(
            self.input1,
            output_dim=2 * self.args.nconti,
            scope='encoder',
            reuse=False)['output'],
                                                      num_or_size_splits=2,
                                                      axis=1)
        self.stddev_total = tf.nn.softplus(self.stddev_total)
        self.z_sample = tf.add(
            self.mean_total, tf.multiply(self.stddev_total,
                                         self.epsilon_input))

        # For VC-Loss
        if self.args.delta_type == 'onedim':
            # C_delta_latents = tf.random.uniform([minibatch_size], minval=0, maxval=C_global_size, dtype=tf.int32)
            # C_delta_latents = tf.cast(tf.one_hot(C_delta_latents, C_global_size), latents.dtype)
            self.z_delta = tf.cast(
                tf.one_hot(self.delta_dim, self.args.nconti),
                self.z_sample.dtype)
            rand_eps = tf.random.normal([self.args.nbatch, 1],
                                        mean=0.0,
                                        stddev=2.0)
            self.delta_target = self.z_delta * rand_eps
            self.z_added = self.delta_target
            self.z_added = self.z_added + self.z_sample
        elif self.args.delta_type == 'fulldim':
            # C_delta_latents = tf.random.uniform([minibatch_size, C_global_size], minval=0, maxval=1.0, dtype=latents.dtype)
            self.delta_target = tf.random.uniform(
                [self.args.nbatch, self.args.nconti],
                minval=0,
                maxval=1.0,
                dtype=self.z_sample.dtype)
            self.z_added = (self.delta_target - 0.5) * self.args.vc_epsilon
            self.z_added = self.z_added + self.z_sample

        self.dec_output_dict = self.decoder_net(z=tf.concat(
            [self.z_sample, self.objective], axis=-1),
                                                output_channel=self.nchannel,
                                                scope="decoder",
                                                reuse=False)
        self.dec_output = self.dec_output_dict['output']
        self.feat_output = self.dec_output_dict['deconv2d2']
        self.F_loss = tf.reduce_mean(self.feat_output * self.feat_output)
        self.F_loss = self.args.F_beta * self.F_loss

        if self.args.use_discrete:
            self.objective_2 = tf.cast(
                tf.one_hot(self.objective_2_idx, self.args.ncat),
                self.z_added.dtype)
        self.dec_output_2 = self.decoder_net(z=tf.concat(
            [self.z_added, self.objective_2], axis=-1),
                                             output_channel=self.nchannel,
                                             scope="decoder",
                                             reuse=True)['output']
        self.disc_output = self.disc_net(img1=self.dec_output,
                                         img2=self.dec_output_2,
                                         target_dim=self.args.nconti,
                                         scope='discriminator',
                                         reuse=False)['output']

        if self.args.delta_type == 'onedim':
            # Loss VC CEloss
            self.disc_prob = tf.nn.softmax(self.disc_output, axis=1)
            self.I_loss = tf.reduce_mean(
                tf.reduce_sum(self.z_delta * tf.log(self.disc_prob + 1e-12),
                              axis=1))
            self.I_loss = -self.args.C_lambda * self.I_loss
        elif self.args.delta_type == 'fulldim':
            # Loss VC MSEloss
            self.I_loss = tf.reduce_mean(
                tf.reduce_sum(
                    (tf.nn.sigmoid(self.disc_output) - self.delta_target)**2,
                    axis=1))
            self.I_loss = self.args.C_lambda * self.I_loss

        # Unary vector
        self.rec_cost_vector = sigmoid_cross_entropy_without_mean(
            labels=self.input1, logits=self.dec_output)

        # Loss
        self.rec_cost = tf.reduce_mean(self.rec_cost_vector)

        weight = tf.constant(np.array(self.args.nconti * [self.args.beta_max]),
                             dtype=tf.float32)
        kl_cost = vae_kl_cost_weight(mean=self.mean_total,
                                     stddev=self.stddev_total,
                                     weight=weight)
        self.loss = self.rec_cost+kl_cost+tf.losses.get_regularization_loss()+\
                self.I_loss*self.I_weight+self.F_loss*self.F_weight

        tf.summary.scalar('rec_loss', self.rec_cost)
        tf.summary.scalar('I_loss', self.I_loss)
        tf.summary.scalar('F_loss', self.F_loss)
        self.merged = tf.summary.merge_all()

        # Decode
        self.latent_ph = tf.placeholder(
            tf.float32,
            shape=[self.args.nbatch, self.args.nconti + self.args.ncat])
        self.dec_output_ph = tf.nn.sigmoid(
            self.decoder_net(z=self.latent_ph,
                             output_channel=self.nchannel,
                             scope="decoder",
                             reuse=True)['output'])

        # Free Batch Decode
        self.free_latent_ph = tf.placeholder(
            tf.float32, shape=[None, self.args.nconti + self.args.ncat])
        self.free_dec_output_ph = tf.nn.sigmoid(
            self.decoder_net(z=self.free_latent_ph,
                             output_channel=self.nchannel,
                             scope="decoder",
                             reuse=True)['output'])

        self.logger.info("Model building ends")
예제 #3
0
    def build(self):
        self.logger.info("Model building starts")
        tf.reset_default_graph()
        tf.set_random_seed(self.args.rseed)

        self.input1 = tf.placeholder(
            tf.float32,
            shape=[self.args.nbatch, self.height, self.width, self.nchannel])
        self.epsilon_input = tf.placeholder(
            tf.float32, shape=[self.args.nbatch, self.args.nconti])
        self.objective = tf.placeholder(
            tf.float32, shape=[self.args.nbatch, self.args.ncat])
        self.istrain = tf.placeholder(tf.bool, shape=[])

        self.generate_sess()

        self.mcf = SolveMaxMatching(nworkers=self.args.nbatch,
                                    ntasks=self.args.ncat,
                                    k=1,
                                    pairwise_lamb=self.args.plamb)
        # Encoding
        self.encoder_net = encoder1_64
        self.decoder_net = decoder1_64

        # Continuous rep
        self.mean_total, self.stddev_total = tf.split(self.encoder_net(
            self.input1,
            output_dim=2 * self.args.nconti,
            scope='encoder',
            reuse=False)['output'],
                                                      num_or_size_splits=2,
                                                      axis=1)
        self.stddev_total = tf.nn.softplus(self.stddev_total)
        self.z_sample = tf.add(
            self.mean_total, tf.multiply(self.stddev_total,
                                         self.epsilon_input))
        self.dec_output = self.decoder_net(z=tf.concat(
            [self.z_sample, self.objective], axis=-1),
                                           output_channel=self.nchannel,
                                           scope="decoder",
                                           reuse=False)['output']

        # Unary vector
        self.rec_cost_vector = sigmoid_cross_entropy_without_mean(
            labels=self.input1, logits=self.dec_output)

        # Loss
        self.rec_cost = tf.reduce_mean(self.rec_cost_vector)

        self.loss_dict = dict()
        for idx in range(self.args.nconti + 1):
            weight = tf.constant(
                np.array(idx * [self.args.beta_min] +
                         (self.args.nconti - idx) * [self.args.beta_max]),
                dtype=tf.float32)
            kl_cost = vae_kl_cost_weight(mean=self.mean_total,
                                         stddev=self.stddev_total,
                                         weight=weight)
            self.loss_dict[
                idx] = self.rec_cost + kl_cost + tf.losses.get_regularization_loss(
                )

        # Decode
        self.latent_ph = tf.placeholder(
            tf.float32,
            shape=[self.args.nbatch, self.args.nconti + self.args.ncat])
        self.dec_output_ph = tf.nn.sigmoid(
            self.decoder_net(z=self.latent_ph,
                             output_channel=self.nchannel,
                             scope="decoder",
                             reuse=True)['output'])

        self.logger.info("Model building ends")
예제 #4
0
    def build_hash(self):
        self.logger.info("Model building train hash starts")

        self.mcf = SolveMaxMatching(nworkers=self.args.nsclass,
                                    ntasks=self.args.d,
                                    k=1,
                                    pairwise_lamb=self.args.plamb2)

        with slim.arg_scope(
            [slim.fully_connected],
                activation_fn=None,
                weights_regularizer=slim.l2_regularizer(0.0005),
                biases_initializer=tf.zeros_initializer(),
                weights_initializer=tf.truncated_normal_initializer(0.0,
                                                                    0.01)):
            if self.args.ltype == 'triplet':
                # placeholder list
                self.objective_list = [
                    tf.placeholder(dtype=tf.float32,
                                   shape=[self.args.nbatch, self.args.d],
                                   name="objective%d" % i)
                    for i in range(self.args.k)
                ]
                self.embed_k_hash = self.last
                with tf.variable_scope('Hash', reuse=False):
                    self.embed_k_hash = slim.fully_connected(
                        self.embed_k_hash,
                        self.args.d * self.args.k,
                        scope="fc1")  # [batch, d*k]
                self.embed_k_hash_list = tf.split(
                    self.embed_k_hash, num_or_size_splits=self.args.k,
                    axis=1)  # list(k*[batch, d])
                self.embed_k_hash_l2_norm_list = [
                    tf.nn.l2_normalize(v, dim=-1)
                    for v in self.embed_k_hash_list
                ]  # list(k*[batch,d]), each l2 normalize
                self.pairwise_distance = pairwise_distance_w_obj1

                self.loss_hash = 0
                for idx in range(self.args.k):
                    self.loss_hash += triplet_semihard_loss_hash(
                        labels=self.label_list[idx],
                        embeddings=self.embed_k_hash_l2_norm_list[idx],
                        objectives=self.objective_list[idx],
                        pairwise_distance=self.pairwise_distance,
                        margin=self.args.param)
            else:
                self.objective_list = [
                    tf.placeholder(dtype=tf.float32,
                                   shape=[self.args.nbatch // 2, self.args.d],
                                   name="objective%d" % i)
                    for i in range(self.args.k)
                ]
                self.anc_embed_k_hash = self.anc_last
                self.pos_embed_k_hash = self.pos_last
                with tf.variable_scope('Hash', reuse=False):
                    self.anc_embed_k_hash = slim.fully_connected(
                        self.anc_embed_k_hash,
                        self.args.d * self.args.k,
                        scope="fc1")
                with tf.variable_scope('Hash', reuse=True):
                    self.pos_embed_k_hash = slim.fully_connected(
                        self.pos_embed_k_hash,
                        self.args.d * self.args.k,
                        scope="fc1")
                self.anc_embed_k_hash_list = tf.split(
                    self.anc_embed_k_hash,
                    num_or_size_splits=self.args.k,
                    axis=1)  # list(k*[batch, d])
                self.pos_embed_k_hash_list = tf.split(
                    self.pos_embed_k_hash,
                    num_or_size_splits=self.args.k,
                    axis=1)  # list(k*[batch, d])
                self.similarity_func = pairwise_similarity_w_obj1

                self.loss_hash = 0
                for idx in range(self.args.k):
                    self.loss_hash += npairs_loss_hash(labels=self.label_list[idx], embeddings_anchor=self.anc_embed_k_hash_list[idx], embeddings_positive=self.pos_embed_k_hash_list[idx],\
                                            objective=self.objective_list[idx], similarity_func=self.similarity_func, reg_lambda=self.args.param)

        self.EMBED_K_HASH_LIST = self.anc_embed_k_hash_list if self.args.ltype == 'npair' else self.embed_k_hash_l2_norm_list
        self.tree_idx_set = [
            tf.nn.top_k(v, k=self.args.d)[1] for v in self.EMBED_K_HASH_LIST
        ]  # k*[batch_size, d]
        self.tree_idx = tf.transpose(tf.stack(self.tree_idx_set, axis=0),
                                     [1, 0, 2])  # [batch_size, k, d]

        self.logger.info("Model building train hash ends")