def classification_fn(logits, training=True): 
    if training:
        training_flag  = True
        trainable_flag = True
    else:
        training_flag  = FLAGS.bnorm_eval_update
        trainable_flag = False
    
    cl_logits = classification(logits, layer_sizes=[64,num_classes],
           bnorm_before=FLAGS.bnorm_before, bnorm_middle=FLAGS.bnorm_middle, # if: apply after first dense layer
           training=training_flag, trainable=trainable_flag, name='classifier')
    return cl_logits
Beispiel #2
0
                          )
    
    eval_lstm_out, eval_state_out = tf.nn.dynamic_rnn(rnn_cell, eval_sketch,
                                                      initial_state=init_state,
                                                      dtype=tf.float32,
                                                      scope='lstm_dynamic'
                                    )



##################
# Classification #
##################

classification_fn =     lambda logits : classification(logits, layer_sizes=[64,num_classes], 
                                   bnorm_before=bnorm_before, bnorm_middle=bnorm_middle, # if: apply after first dense layer
                                   training=True, trainable=True, name='classifier')

# Needs to be separate to not update batch norm params
eval_classification_fn =     lambda logits : classification(logits, layer_sizes=[64,num_classes], 
                                   bnorm_before=bnorm_before, bnorm_middle=bnorm_middle, # if: apply after first dense layer
                                   training=FLAGS.bnorm_eval_update, trainable=False, name='classifier')


with tf.variable_scope("logits", reuse=tf.AUTO_REUSE):
    classifier_logits  = classification_fn(tf.reshape(lstm_out, [-1, hidden_size]))
    classifier_logits  = tf.reshape(classifier_logits, [batch_size, -1, num_classes])
    
    eval_classifier_logits  = eval_classification_fn(tf.reshape(eval_lstm_out, [-1, hidden_size]))
    eval_classifier_logits  = tf.reshape(eval_classifier_logits, [batch_size, -1, num_classes])
# CNN
with tf.variable_scope('cnn/resnet', reuse=tf.AUTO_REUSE):
    logits, _ = resnet_v2.resnet_v2_50(last_imgs,
                                       is_training=True)  # ?x1x1x2048
    logits = conv2d(inputs=logits,
                    num_outputs=FLAGS.cnn_size,
                    kernel_size=[1, 1],
                    activation_fn=None,
                    normalizer_fn=None,
                    scope='logits')
logits = tf.squeeze(logits, [1, 2])

# Classification
cl_logits = classification(logits,
                           layer_sizes=[FLAGS.cnn_size, 345],
                           training=False)

# Training
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=cl_logits,
                                                      labels=labels)
avg_loss = tf.reduce_mean(loss)
global_step = tf.Variable(initial_value=0, trainable=False, name='global_step')
learning_rate = tf.Variable(initial_value=FLAGS.learning_rate,
                            trainable=False,
                            name='learning_rate')
train_op = tf.train.AdamOptimizer(
    learning_rate=learning_rate).minimize(avg_loss)

# Summaries
cl_acc = classification_summary(cl_logits,
Beispiel #4
0
    def model_fn(self, data, mode=None):
        """
        Args:
            data:   dict with attribute sketch,length and labels
            mode:   'train'/'evaluate'/'predict'
        
        Returns:
            dict of states
            dict of losses (+train_op)
            dict of metrics
        
        """
        assert mode in ['train', 'evaluate', 'predict']
        assert mode != 'predict' or not self.FLAGS.small

        # Dicts to return
        dstates = {}
        dlosses = {}
        dmetrics = {}

        # Already have code for imgs --> logits
        logits = data['last_img_logits']
        if mode != 'predict':
            labels = data['labels']

        ##################
        # Classification #
        ##################

        classification_fn = lambda logits: classification(
            logits,
            layer_sizes=[64, self.num_classes],
            bnorm_before=False,
            bnorm_middle=False,  # if: apply after first dense layer
            training=(mode == 'training'),
            trainable=True,
            name='classifier')

        with tf.variable_scope('logits', reuse=tf.AUTO_REUSE):
            dstates['logits_for_cl'] = logits
            cl_logits = classification_fn(logits)
            dstates['cl_logits'] = cl_logits

        ########
        # Loss #
        ########

        if mode in ['train', 'evaluate']:
            with tf.variable_scope('conv_model/classification_loss',
                                   reuse=tf.AUTO_REUSE):
                # Train on last states
                loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
                    labels=labels, logits=cl_logits)
                avg_loss = tf.reduce_mean(loss)
                dlosses.update({'cl_loss': avg_loss, 'train_loss': avg_loss})
                summary_scope = 'train' if mode == 'train' else 'eval'
                dmetrics.update({
                    'train_loss':
                    tf.summary.scalar(summary_scope + '/train_loss', avg_loss),
                    'cl_loss':
                    tf.summary.scalar(summary_scope + '/classification_loss',
                                      avg_loss)
                })

        ############
        # Training #
        ############

        if mode == 'train':
            with tf.variable_scope('conv_model/loss', reuse=tf.AUTO_REUSE):
                optimizer = tf.train.AdamOptimizer(
                    learning_rate=self.train_learning_rate)
                train_vars = None
                gradients = optimizer.compute_gradients(avg_loss,
                                                        var_list=train_vars)
                gradients = [(tf.clip_by_value(grad,
                                               -1 * self.FLAGS.clip_gradient,
                                               self.FLAGS.clip_gradient), var)
                             for grad, var in gradients if grad is not None]
                update_ops = tf.get_collection(
                    tf.GraphKeys.UPDATE_OPS)  # case of batch norm used
                with tf.control_dependencies(update_ops):
                    train_op = optimizer.apply_gradients(
                        grads_and_vars=gradients, global_step=self.global_step)
            dlosses.update({'train_op': train_op})

        #############
        # Summaries #
        #############

        if mode in ['train', 'evaluate']:
            summary_scope = 'train' if mode == 'train' else 'eval'

            with tf.variable_scope('conv_model/summary/' + summary_scope,
                                   reuse=tf.AUTO_REUSE):
                # Classification part
                summary_op, summary_vars = classification_summary(
                    cl_logits, labels, 'accuracy')

                # Validation in top 3
                top3_summary_op, top3_summary_vars = \
                    classification_summary(cl_logits, labels, 'map3_accuracy',
                                           top_k=3, avg_top_k=True)
                cl_summary = tf.summary.merge_all(
                    scope='classification_summary/' + summary_scope)
                dmetrics.update({
                    'classification_summary':
                    cl_summary,
                    'classification_accuracy':
                    summary_vars['accuracy'],
                    'classification_accuracy_map3':
                    top3_summary_vars['map3_accuracy']
                })

        #######################
        # Inference Test Data #
        #######################

        if mode == 'predict':
            predictions = tf.nn.top_k(cl_logits, k=3)[1]
            dstates['prediction'] = predictions

        return dstates, dlosses, dmetrics
Beispiel #5
0
                                             feed_mode=FLAGS.feed_mode)
eval_logits1, eval_logits2, eval_logits3 = cnn_model.logits(
    eval_images, training=False, feed_mode=FLAGS.feed_mode)

# Classifier part
logits_2 = cnn_model.logits(images2,
                            training=False,
                            feed_mode=None,
                            stop_gradient=True)  # Training false for BNorm
eval_logits_2 = cnn_model.logits(eval_images2,
                                 training=False,
                                 feed_mode=None,
                                 stop_gradient=True)

classifier_logits = classification(logits_2,
                                   layer_sizes=[64, num_classes],
                                   name='classifier')
eval_classifier_logits = classification(eval_logits_2,
                                        layer_sizes=[64, num_classes],
                                        name='classifier')  # shared weights

# def l2_normalize(vals):
#     sqr_sum = tf.reduce_sum(vals**2, axis=1, keepdims=True)
#     tf.assert_equal(tf.shape(sqr_sum)[0], 30)
#     return vals/sqr_sum
#
# logits1 = l2_normalize(logits1)
# logits2 = l2_normalize(logits2)
# logits2 = l2_normalize(logits2)
# eval_logits1 = l2_normalize(eval_logits1)
# eval_logits2 = l2_normalize(eval_logits2)