(features, labels, mode, params)
return tf.estimator.EstimatorSpec(
          mode=mode,
          predictions=predictions,
          loss=total_loss,
          train_op=train_op,
          eval_metric_ops=metrics,
          scaffold = tf.train.Scaffold(init_fn=train_helper.get_init_fn_for_scaffold(FLAGS)))
Our model_fn for ResNet to be used with our Estimator.
Our model_fn for ResNet to be used with our Estimator.
Our model_fn for ResNet to be used with our Estimator .
def xdet_model_fn(features, labels, mode, params):
    """Our model_fn for ResNet to be used with our Estimator."""

    shape = labels['shape']
    loc_targets = labels['loc_targets']
    cls_targets = labels['cls_targets']
    match_scores = labels['match_scores']

    global global_anchor_info
    decode_fn = global_anchor_info['decode_fn']
    num_anchors_per_layer = global_anchor_info['num_anchors_per_layer']
    all_num_anchors_depth = global_anchor_info['all_num_anchors_depth']

    with tf.variable_scope(params['model_scope'], default_name=None, values=[features], reuse=tf.AUTO_REUSE):
        backbone = xdet_body_v5.xdet_resnet_v5(params['resnet_size'], params['data_format'])
        backbone_outputs = backbone(inputs=features, is_training=(mode == tf.estimator.ModeKeys.TRAIN))

        cls_pred, location_pred = xdet_body_v5.xdet_head(backbone_outputs, params['num_classes'], all_num_anchors_depth, (mode == tf.estimator.ModeKeys.TRAIN), data_format=params['data_format'])

        if params['data_format'] == 'channels_first':
            cls_pred = [tf.transpose(pred, [0, 2, 3, 1]) for pred in cls_pred]
            location_pred = [tf.transpose(pred, [0, 2, 3, 1]) for pred in location_pred]

        cls_pred = [tf.reshape(pred, [tf.shape(features)[0], -1, params['num_classes']]) for pred in cls_pred]
        location_pred = [tf.reshape(pred, [tf.shape(features)[0], -1, 4]) for pred in location_pred]

        cls_pred = tf.concat(cls_pred, axis=1)
        location_pred = tf.concat(location_pred, axis=1)

        cls_pred = tf.reshape(cls_pred, [-1, params['num_classes']])
        location_pred = tf.reshape(location_pred, [-1, 4])

    with tf.device('/cpu:0'):
        with tf.control_dependencies([cls_pred, location_pred]):
            with tf.name_scope('post_forward'):
                #bboxes_pred = decode_fn(location_pred)
                bboxes_pred = tf.map_fn(lambda _preds : decode_fn(_preds),
                                        tf.reshape(location_pred, [tf.shape(features)[0], -1, 4]),
                                        dtype=[tf.float32] * len(num_anchors_per_layer), back_prop=False)
                #cls_targets = tf.Print(cls_targets, [tf.shape(bboxes_pred[0]),tf.shape(bboxes_pred[1]),tf.shape(bboxes_pred[2]),tf.shape(bboxes_pred[3])])
                bboxes_pred = [tf.reshape(preds, [-1, 4]) for preds in bboxes_pred]
                bboxes_pred = tf.concat(bboxes_pred, axis=0)

                flaten_cls_targets = tf.reshape(cls_targets, [-1])
                flaten_match_scores = tf.reshape(match_scores, [-1])
                flaten_loc_targets = tf.reshape(loc_targets, [-1, 4])

                # each positive examples has one label
                positive_mask = flaten_cls_targets > 0
                n_positives = tf.count_nonzero(positive_mask)

                batch_n_positives = tf.count_nonzero(cls_targets, -1)

                batch_negtive_mask = tf.equal(cls_targets, 0)#tf.logical_and(tf.equal(cls_targets, 0), match_scores > 0.)
                batch_n_negtives = tf.count_nonzero(batch_negtive_mask, -1)

                batch_n_neg_select = tf.cast(params['negative_ratio'] * tf.cast(batch_n_positives, tf.float32), tf.int32)
                batch_n_neg_select = tf.minimum(batch_n_neg_select, tf.cast(batch_n_negtives, tf.int32))

                # hard negative mining for classification
                predictions_for_bg = tf.nn.softmax(tf.reshape(cls_pred, [tf.shape(features)[0], -1, params['num_classes']]))[:, :, 0]
                prob_for_negtives = tf.where(batch_negtive_mask,
                                       0. - predictions_for_bg,
                                       # ignore all the positives
                                       0. - tf.ones_like(predictions_for_bg))
                topk_prob_for_bg, _ = tf.nn.top_k(prob_for_negtives, k=tf.shape(prob_for_negtives)[1])
                score_at_k = tf.gather_nd(topk_prob_for_bg, tf.stack([tf.range(tf.shape(features)[0]), batch_n_neg_select - 1], axis=-1))

                selected_neg_mask = prob_for_negtives >= tf.expand_dims(score_at_k, axis=-1)

                # include both selected negtive and all positive examples
                final_mask = tf.stop_gradient(tf.logical_or(tf.reshape(tf.logical_and(batch_negtive_mask, selected_neg_mask), [-1]), positive_mask))
                total_examples = tf.count_nonzero(final_mask)

                cls_pred = tf.boolean_mask(cls_pred, final_mask)
                location_pred = tf.boolean_mask(location_pred, tf.stop_gradient(positive_mask))
                flaten_cls_targets = tf.boolean_mask(tf.clip_by_value(flaten_cls_targets, 0, params['num_classes']), final_mask)
                flaten_loc_targets = tf.stop_gradient(tf.boolean_mask(flaten_loc_targets, positive_mask))

                predictions = {
                            'classes': tf.argmax(cls_pred, axis=-1),
                            'probabilities': tf.reduce_max(tf.nn.softmax(cls_pred, name='softmax_tensor'), axis=-1),
                            'loc_predict': bboxes_pred }

                cls_accuracy = tf.metrics.accuracy(flaten_cls_targets, predictions['classes'])
                metrics = {'cls_accuracy': cls_accuracy}

                # Create a tensor named train_accuracy for logging purposes.
                tf.identity(cls_accuracy[1], name='cls_accuracy')
                tf.summary.scalar('cls_accuracy', cls_accuracy[1])

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

    # Calculate loss, which includes softmax cross entropy and L2 regularization.
    #cross_entropy = tf.cond(n_positives > 0, lambda: tf.losses.sparse_softmax_cross_entropy(labels=flaten_cls_targets, logits=cls_pred), lambda: 0.)# * (params['negative_ratio'] + 1.)
    #flaten_cls_targets=tf.Print(flaten_cls_targets, [flaten_loc_targets],summarize=50000)
    cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=flaten_cls_targets, logits=cls_pred) * (params['negative_ratio'] + 1.)
    # Create a tensor named cross_entropy for logging purposes.
    tf.identity(cross_entropy, name='cross_entropy_loss')
    tf.summary.scalar('cross_entropy_loss', cross_entropy)

    #loc_loss = tf.cond(n_positives > 0, lambda: modified_smooth_l1(location_pred, tf.stop_gradient(flaten_loc_targets), sigma=1.), lambda: tf.zeros_like(location_pred))
    loc_loss = modified_smooth_l1(location_pred, flaten_loc_targets, sigma=1.)
    #loc_loss = modified_smooth_l1(location_pred, tf.stop_gradient(gtargets))
    loc_loss = tf.reduce_mean(tf.reduce_sum(loc_loss, axis=-1), name='location_loss')
    tf.summary.scalar('location_loss', loc_loss)
    tf.losses.add_loss(loc_loss)

    l2_loss_vars = []
    for trainable_var in tf.trainable_variables():
        if 'batch_normalization' not in trainable_var.name:
            l2_loss_vars.append(tf.nn.l2_loss(trainable_var))

    # Add weight decay to the loss. We exclude the batch norm variables because
    # doing so leads to a small improvement in accuracy.
    total_loss = tf.add(cross_entropy + loc_loss, tf.multiply(params['weight_decay'], tf.add_n(l2_loss_vars), name='l2_loss'), name='total_loss')

    if mode == tf.estimator.ModeKeys.TRAIN:
        global_step = tf.train.get_or_create_global_step()

        lr_values = [params['learning_rate'] * decay for decay in params['lr_decay_factors']]
        learning_rate = tf.train.piecewise_constant(tf.cast(global_step, tf.int32),
                                                    [int(_) for _ in params['decay_boundaries']],
                                                    lr_values)
        truncated_learning_rate = tf.maximum(learning_rate, tf.constant(params['end_learning_rate'], dtype=learning_rate.dtype), name='learning_rate')
        # Create a tensor named learning_rate for logging purposes.
        tf.summary.scalar('learning_rate', truncated_learning_rate)

        optimizer = tf.train.MomentumOptimizer(learning_rate=truncated_learning_rate,
                                                momentum=params['momentum'])

        # Batch norm requires update_ops to be added as a train_op dependency.
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
            train_op = optimizer.minimize(total_loss, global_step)
    else:
        train_op = None

    return tf.estimator.EstimatorSpec(
          mode=mode,
          predictions=predictions,
          loss=total_loss,
          train_op=train_op,
          eval_metric_ops=metrics,
          scaffold = tf.train.Scaffold(init_fn=train_helper.get_init_fn_for_scaffold(FLAGS)))
def xdet_model_fn ( features , labels , mode , params ) : shape = labels [ 'shape' ] loc_targets = labels [ 'loc_targets' ] cls_targets = labels [ 'cls_targets' ] match_scores = labels [ 'match_scores' ] global global_anchor_info decode_fn = global_anchor_info [ 'decode_fn' ] num_anchors_per_layer = global_anchor_info [ 'num_anchors_per_layer' ] all_num_anchors_depth = global_anchor_info [ 'all_num_anchors_depth' ] with tf . variable_scope ( params [ 'model_scope' ] , default_name = None , values = [ features ] , reuse = tf . AUTO_REUSE ) : backbone = xdet_body_v5 . xdet_resnet_v5 ( params [ 'resnet_size' ] , params [ 'data_format' ] ) backbone_outputs = backbone ( inputs = features , is_training = ( mode == tf . estimator . ModeKeys . TRAIN ) ) cls_pred , location_pred = xdet_body_v5 . xdet_head ( backbone_outputs , params [ 'num_classes' ] , all_num_anchors_depth , ( mode == tf . estimator . ModeKeys . TRAIN ) , data_format = params [ 'data_format' ] ) if params [ 'data_format' ] == 'channels_first' : cls_pred = [ tf . transpose ( pred , [ 0 , 2 , 3 , 1 ] ) for pred in cls_pred ] location_pred = [ tf . transpose ( pred , [ 0 , 2 , 3 , 1 ] ) for pred in location_pred ] cls_pred = [ tf . reshape ( pred , [ tf . shape ( features ) [ 0 ] , - 1 , params [ 'num_classes' ] ] ) for pred in cls_pred ] location_pred = [ tf . reshape ( pred , [ tf . shape ( features ) [ 0 ] , - 1 , 4 ] ) for pred in location_pred ] cls_pred = tf . concat ( cls_pred , axis = 1 ) location_pred = tf . concat ( location_pred , axis = 1 ) cls_pred = tf . reshape ( cls_pred , [ - 1 , params [ 'num_classes' ] ] ) location_pred = tf . reshape ( location_pred , [ - 1 , 4 ] ) with tf . device ( '/cpu:0' ) : with tf . control_dependencies ( [ cls_pred , location_pred ] ) : with tf . name_scope ( 'post_forward' ) : #bboxes_pred = decode_fn(location_pred) bboxes_pred = tf . map_fn ( lambda _preds : decode_fn ( _preds ) , tf . reshape ( location_pred , [ tf . shape ( features ) [ 0 ] , - 1 , 4 ] ) , dtype = [ tf . float32 ] * len ( num_anchors_per_layer ) , back_prop = False ) #cls_targets = tf.Print(cls_targets, [tf.shape(bboxes_pred[0]),tf.shape(bboxes_pred[1]),tf.shape(bboxes_pred[2]),tf.shape(bboxes_pred[3])]) bboxes_pred = [ tf . reshape ( preds , [ - 1 , 4 ] ) for preds in bboxes_pred ] bboxes_pred = tf . concat ( bboxes_pred , axis = 0 ) flaten_cls_targets = tf . reshape ( cls_targets , [ - 1 ] ) flaten_match_scores = tf . reshape ( match_scores , [ - 1 ] ) flaten_loc_targets = tf . reshape ( loc_targets , [ - 1 , 4 ] ) # each positive examples has one label positive_mask = flaten_cls_targets > 0 n_positives = tf . count_nonzero ( positive_mask ) batch_n_positives = tf . count_nonzero ( cls_targets , - 1 ) batch_negtive_mask = tf . equal ( cls_targets , 0 ) #tf.logical_and(tf.equal(cls_targets, 0), match_scores > 0.) batch_n_negtives = tf . count_nonzero ( batch_negtive_mask , - 1 ) batch_n_neg_select = tf . cast ( params [ 'negative_ratio' ] * tf . cast ( batch_n_positives , tf . float32 ) , tf . int32 ) batch_n_neg_select = tf . minimum ( batch_n_neg_select , tf . cast ( batch_n_negtives , tf . int32 ) ) # hard negative mining for classification predictions_for_bg = tf . nn . softmax ( tf . reshape ( cls_pred , [ tf . shape ( features ) [ 0 ] , - 1 , params [ 'num_classes' ] ] ) ) [ : , : , 0 ] prob_for_negtives = tf . where ( batch_negtive_mask , 0. - predictions_for_bg , # ignore all the positives 0. - tf . ones_like ( predictions_for_bg ) ) topk_prob_for_bg , _ = tf . nn . top_k ( prob_for_negtives , k = tf . shape ( prob_for_negtives ) [ 1 ] ) score_at_k = tf . gather_nd ( topk_prob_for_bg , tf . stack ( [ tf . range ( tf . shape ( features ) [ 0 ] ) , batch_n_neg_select - 1 ] , axis = - 1 ) ) selected_neg_mask = prob_for_negtives >= tf . expand_dims ( score_at_k , axis = - 1 ) # include both selected negtive and all positive examples final_mask = tf . stop_gradient ( tf . logical_or ( tf . reshape ( tf . logical_and ( batch_negtive_mask , selected_neg_mask ) , [ - 1 ] ) , positive_mask ) ) total_examples = tf . count_nonzero ( final_mask ) cls_pred = tf . boolean_mask ( cls_pred , final_mask ) location_pred = tf . boolean_mask ( location_pred , tf . stop_gradient ( positive_mask ) ) flaten_cls_targets = tf . boolean_mask ( tf . clip_by_value ( flaten_cls_targets , 0 , params [ 'num_classes' ] ) , final_mask ) flaten_loc_targets = tf . stop_gradient ( tf . boolean_mask ( flaten_loc_targets , positive_mask ) ) predictions = { 'classes' : tf . argmax ( cls_pred , axis = - 1 ) , 'probabilities' : tf . reduce_max ( tf . nn . softmax ( cls_pred , name = 'softmax_tensor' ) , axis = - 1 ) , 'loc_predict' : bboxes_pred } cls_accuracy = tf . metrics . accuracy ( flaten_cls_targets , predictions [ 'classes' ] ) metrics = { 'cls_accuracy' : cls_accuracy } # Create a tensor named train_accuracy for logging purposes. tf . identity ( cls_accuracy [ 1 ] , name = 'cls_accuracy' ) tf . summary . scalar ( 'cls_accuracy' , cls_accuracy [ 1 ] ) if mode == tf . estimator . ModeKeys . PREDICT : return tf . estimator . EstimatorSpec ( mode = mode , predictions = predictions ) # Calculate loss, which includes softmax cross entropy and L2 regularization. #cross_entropy = tf.cond(n_positives > 0, lambda: tf.losses.sparse_softmax_cross_entropy(labels=flaten_cls_targets, logits=cls_pred), lambda: 0.)# * (params['negative_ratio'] + 1.) #flaten_cls_targets=tf.Print(flaten_cls_targets, [flaten_loc_targets],summarize=50000) cross_entropy = tf . losses . sparse_softmax_cross_entropy ( labels = flaten_cls_targets , logits = cls_pred ) * ( params [ 'negative_ratio' ] + 1. ) # Create a tensor named cross_entropy for logging purposes. tf . identity ( cross_entropy , name = 'cross_entropy_loss' ) tf . summary . scalar ( 'cross_entropy_loss' , cross_entropy ) #loc_loss = tf.cond(n_positives > 0, lambda: modified_smooth_l1(location_pred, tf.stop_gradient(flaten_loc_targets), sigma=1.), lambda: tf.zeros_like(location_pred)) loc_loss = modified_smooth_l1 ( location_pred , flaten_loc_targets , sigma = 1. ) #loc_loss = modified_smooth_l1(location_pred, tf.stop_gradient(gtargets)) loc_loss = tf . reduce_mean ( tf . reduce_sum ( loc_loss , axis = - 1 ) , name = 'location_loss' ) tf . summary . scalar ( 'location_loss' , loc_loss ) tf . losses . add_loss ( loc_loss ) l2_loss_vars = [ ] for trainable_var in tf . trainable_variables ( ) : if 'batch_normalization' not in trainable_var . name : l2_loss_vars . append ( tf . nn . l2_loss ( trainable_var ) ) # Add weight decay to the loss. We exclude the batch norm variables because # doing so leads to a small improvement in accuracy. total_loss = tf . add ( cross_entropy + loc_loss , tf . multiply ( params [ 'weight_decay' ] , tf . add_n ( l2_loss_vars ) , name = 'l2_loss' ) , name = 'total_loss' ) if mode == tf . estimator . ModeKeys . TRAIN : global_step = tf . train . get_or_create_global_step ( ) lr_values = [ params [ 'learning_rate' ] * decay for decay in params [ 'lr_decay_factors' ] ] learning_rate = tf . train . piecewise_constant ( tf . cast ( global_step , tf . int32 ) , [ int ( _ ) for _ in params [ 'decay_boundaries' ] ] , lr_values ) truncated_learning_rate = tf . maximum ( learning_rate , tf . constant ( params [ 'end_learning_rate' ] , dtype = learning_rate . dtype ) , name = 'learning_rate' ) # Create a tensor named learning_rate for logging purposes. tf . summary . scalar ( 'learning_rate' , truncated_learning_rate ) optimizer = tf . train . MomentumOptimizer ( learning_rate = truncated_learning_rate , momentum = params [ 'momentum' ] ) # Batch norm requires update_ops to be added as a train_op dependency. update_ops = tf . get_collection ( tf . GraphKeys . UPDATE_OPS ) with tf . control_dependencies ( update_ops ) : train_op = optimizer . minimize ( total_loss , global_step ) else : train_op = None return tf . estimator . EstimatorSpec ( mode = mode , predictions = predictions , loss = total_loss , train_op = train_op , eval_metric_ops = metrics , scaffold = tf . train . Scaffold ( init_fn = train_helper . get_init_fn_for_scaffold ( FLAGS ) ) )