Ejemplo n.º 1
0
    def build(self):
        self.logger.info("Model building starts")
        tf.reset_default_graph()
        if self.args.ltype == 'npair':
            self.anc_img = tf.placeholder(tf.float32,
                                          shape=[
                                              self.args.nbatch // 2,
                                              self.height, self.width,
                                              self.nchannel
                                          ])
            self.pos_img = tf.placeholder(tf.float32,
                                          shape=[
                                              self.args.nbatch // 2,
                                              self.height, self.width,
                                              self.nchannel
                                          ])
            self.istrain = tf.placeholder(tf.bool, shape=[])
            self.label = tf.placeholder(tf.int32,
                                        shape=[self.args.nbatch // 2])
        else:  # triplet
            self.img = tf.placeholder(tf.float32,
                                      shape=[
                                          self.args.nbatch, self.height,
                                          self.width, self.nchannel
                                      ])
            self.istrain = tf.placeholder(tf.bool, shape=[])
            self.label = tf.placeholder(tf.int32, shape=[self.args.nbatch])

        self.generate_sess()

        self.conv_net = CONV_DICT[self.args.dataset][self.args.conv]

        if self.args.ltype == 'npair':
            self.anc_last, _ = self.conv_net(self.anc_img,
                                             is_training=self.istrain,
                                             reuse=False)
            self.pos_last, _ = self.conv_net(self.pos_img,
                                             is_training=self.istrain,
                                             reuse=True)
            self.anc_last = tf.nn.relu(self.anc_last)
            self.pos_last = tf.nn.relu(self.pos_last)
        else:  #triplet
            self.last, _ = self.conv_net(self.img,
                                         is_training=self.istrain,
                                         reuse=False)
            self.last = tf.nn.relu(self.last)

        with slim.arg_scope([slim.fully_connected],
                            activation_fn=None,
                            weights_regularizer=slim.l2_regularizer(0.0005),
                            biases_initializer=tf.zeros_initializer()):
            if self.args.ltype == 'npair':
                with tf.variable_scope('Embed', reuse=False):
                    self.anc_embed = slim.fully_connected(self.anc_last,
                                                          self.args.m,
                                                          scope="fc1")
                with tf.variable_scope('Embed', reuse=True):
                    self.pos_embed = slim.fully_connected(self.pos_last,
                                                          self.args.m,
                                                          scope="fc1")
                self.loss = npairs_loss(labels=self.label,
                                        embeddings_anchor=self.anc_embed,
                                        embeddings_positive=self.pos_embed,
                                        reg_lambda=self.args.lamb)
            else:  #triplet
                with tf.variable_scope('Embed', reuse=False):
                    self.embed = slim.fully_connected(self.last,
                                                      self.args.m,
                                                      scope="fc1")
                self.embed_l2_norm = tf.nn.l2_normalize(
                    self.embed, dim=-1)  # embedding with l2 normalization

                def pairwise_distance_c(embeddings):
                    return pairwise_distance_euclid(embeddings, squared=True)

                self.loss = triplet_semihard_loss(
                    labels=self.label,
                    embeddings=self.embed_l2_norm,
                    pairwise_distance=pairwise_distance_c,
                    margin=self.args.ma)
        self.loss += tf.losses.get_regularization_loss()

        initialized_variables = get_initialized_vars(self.sess)
        self.logger.info("Variables loaded from pretrained network\n{}".format(
            vars_info_vl(initialized_variables)))
        self.logger.info("Model building ends")
Ejemplo n.º 2
0
    def build(self, pretrain=False):
        self.logger.info("Model building starts")
        tf.reset_default_graph()
        if self.args.hltype == 'npair':
            self.anc_img = tf.placeholder(tf.float32,
                                          shape=[
                                              self.args.nbatch // 2,
                                              self.height, self.width,
                                              self.nchannel
                                          ])
            self.pos_img = tf.placeholder(tf.float32,
                                          shape=[
                                              self.args.nbatch // 2,
                                              self.height, self.width,
                                              self.nchannel
                                          ])
            self.istrain = tf.placeholder(tf.bool, shape=[])
            self.label = tf.placeholder(tf.int32,
                                        shape=[self.args.nbatch // 2])
        else:  # triplet
            self.img = tf.placeholder(tf.float32,
                                      shape=[
                                          self.args.nbatch, self.height,
                                          self.width, self.nchannel
                                      ])
            self.istrain = tf.placeholder(tf.bool, shape=[])
            self.label = tf.placeholder(tf.int32, shape=[self.args.nbatch])

        self.generate_sess()

        self.conv_net = CONV_DICT[self.args.dataset][self.args.conv]

        if self.args.hltype == 'npair':
            self.anc_last, _ = self.conv_net(self.anc_img,
                                             is_training=self.istrain,
                                             reuse=False)
            self.pos_last, _ = self.conv_net(self.pos_img,
                                             is_training=self.istrain,
                                             reuse=True)
            self.anc_last = tf.nn.relu(self.anc_last)
            self.pos_last = tf.nn.relu(self.pos_last)
        else:
            self.last, _ = self.conv_net(self.img,
                                         is_training=self.istrain,
                                         reuse=False)
            self.last = tf.nn.relu(self.last)

        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.hltype == 'npair':
                with tf.variable_scope('Embed', reuse=False):
                    self.anc_embed = slim.fully_connected(self.anc_last,
                                                          self.args.m,
                                                          scope="fc1")
                with tf.variable_scope('Embed', reuse=True):
                    self.pos_embed = slim.fully_connected(self.pos_last,
                                                          self.args.m,
                                                          scope="fc1")
            else:  #triplet
                with tf.variable_scope('Embed', reuse=False):
                    self.embed = slim.fully_connected(self.last,
                                                      self.args.m,
                                                      scope="fc1")

        initialized_variables = get_initialized_vars(self.sess)
        self.logger.info("Variables loaded from pretrained network\n{}".format(
            vars_info_vl(initialized_variables)))
        self.logger.info("Model building ends")