Esempio n. 1
0
def top_feature_net(input, anchors, inds_inside, num_bases):
  stride=8
  with tf.variable_scope("top_base") as sc:
    arg_scope = resnet_v1.resnet_arg_scope(weight_decay=0.0)
    with slim.arg_scope(arg_scope) :
      net, end_points = resnet_v1.resnet_v1_50(input, None, global_pool=False, output_stride=8)
      #pdb.set_trace()
      block=end_points['top_base/resnet_v1_50/block4']
      # block   = conv2d_bn_relu(block, num_kernels=512, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='2')
      tf.summary.histogram('rpn_top_block', block) 
      # tf.summary.histogram('rpn_top_block_weights', tf.get_collection('2/conv_weight')[0])
    with tf.variable_scope('top') as scope:
      #up     = upsample2d(block, factor = 2, has_bias=True, trainable=True, name='1')
      #up     = block
      up      = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
      scores  = conv2d(up, num_kernels=2*num_bases, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='score')
      probs   = tf.nn.softmax( tf.reshape(scores,[-1,2]), name='prob')
      deltas  = conv2d(up, num_kernels=4*num_bases, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='delta')

    #<todo> flip to train and test mode nms (e.g. different nms_pre_topn values): use tf.cond
    with tf.variable_scope('top-nms') as scope:    #non-max
      batch_size, img_height, img_width, img_channel = input.get_shape().as_list()
      img_scale = 1
      # pdb.set_trace()
      rois, roi_scores = tf_rpn_nms( probs, deltas, anchors, inds_inside,
                                       stride, img_width, img_height, img_scale,
                                       nms_thresh=0.7, min_size=stride, nms_pre_topn=300, nms_post_topn=50,
                                       name ='nms')
  
    #<todo> feature = upsample2d(block, factor = 4,  ...)
    feature = block
def top_feature_net(input, anchors, inds_inside, num_bases):
    """temporary net for debugging only. may not follow the paper exactly .... 
    :param input: 
    :param anchors: 
    :param inds_inside: 
    :param num_bases: 
    :return: 
            top_features, top_scores, top_probs, top_deltas, proposals, proposal_scores
    """
    stride=1.
    #with tf.variable_scope('top-preprocess') as scope:
    #    input = input

    with tf.variable_scope('top-block-1') as scope:
        block = conv2d_bn_relu(input, num_kernels=32, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=32, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        block = maxpool(block, kernel_size=(2,2), stride=[1,2,2,1], padding='SAME', name='4' )
        stride *=2

    with tf.variable_scope('top-block-2') as scope:
        block = conv2d_bn_relu(block, num_kernels=64, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=64, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        block = maxpool(block, kernel_size=(2,2), stride=[1,2,2,1], padding='SAME', name='4' )
        stride *=2

    with tf.variable_scope('top-block-3') as scope:
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='3')
        block = maxpool(block, kernel_size=(2,2), stride=[1,2,2,1], padding='SAME', name='4' )
        stride *=2

    with tf.variable_scope('top-block-4') as scope:
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='3')


    with tf.variable_scope('top') as scope:
        #up     = upsample2d(block, factor = 2, has_bias=True, trainable=True, name='1')
        #up     = block
        up      = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        scores  = conv2d(up, num_kernels=2*num_bases, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='score')
        probs   = tf.nn.softmax( tf.reshape(scores,[-1,2]), name='prob')
        deltas  = conv2d(up, num_kernels=4*num_bases, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='delta')

    #<todo> flip to train and test mode nms (e.g. different nms_pre_topn values): use tf.cond
    with tf.variable_scope('top-nms') as scope:    #non-max
        batch_size, img_height, img_width, img_channel = input.get_shape().as_list()
        img_scale = 1
        rois, roi_scores = tf_rpn_nms( probs, deltas, anchors, inds_inside,
                                       stride, img_width, img_height, img_scale,
                                       nms_thresh=0.7, min_size=stride, nms_pre_topn=500, nms_post_topn=100,
                                       name ='nms')

    #<todo> feature = upsample2d(block, factor = 4,  ...)
    feature = block

    print ('top: scale=%f, stride=%d'%(1./stride, stride))
    return feature, scores, probs, deltas, rois, roi_scores, stride
Esempio n. 3
0
def top_feature_net(input, anchors, inds_inside, num_bases):
  stride=8
  with tf.variable_scope("top_base"):
    arg_scope=mobilenet_arg_scope(weight_decay=0.0)
    with slim.arg_scope(arg_scope):
      end_points=mobilenet(input,num_classes=1000,is_training=True,width_multiplier=1,scope='MobileNet')
      block=end_points['top_base/MobileNet/conv_ds_14/depthwise_conv']
    with tf.variable_scope('top') as scope:
      #up     = upsample2d(block, factor = 2, has_bias=True, trainable=True, name='1')
      #up     = block
      up      = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
      scores  = conv2d(up, num_kernels=2*num_bases, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='score')
      probs   = tf.nn.softmax( tf.reshape(scores,[-1,2]), name='prob')
      deltas  = conv2d(up, num_kernels=4*num_bases, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='delta')
  
    #<todo> flip to train and test mode nms (e.g. different nms_pre_topn values): use tf.cond
    with tf.variable_scope('top-nms') as scope:    #non-max
      batch_size, img_height, img_width, img_channel = input.get_shape().as_list()
      img_scale = 1
      # pdb.set_trace()
      rois, roi_scores = tf_rpn_nms( probs, deltas, anchors, inds_inside,
                                       stride, img_width, img_height, img_scale,
                                       nms_thresh=0.7, min_size=stride, nms_pre_topn=500, nms_post_topn=50,
                                       name ='nms')
  
    #<todo> feature = upsample2d(block, factor = 4,  ...)
    feature = block
  
    # print ('top: scale=%f, stride=%d'%(1./stride, stride))
    return feature, scores, probs, deltas, rois, roi_scores
Esempio n. 4
0
def top_feature_net(input, anchors, inds_inside, num_bases):
    """temporary net for debugging only. may not follow the paper exactly .... 
    :param input: 
    :param anchors: 
    :param inds_inside: 
    :param num_bases: 
    :return: 
            top_features, top_scores, top_probs, top_deltas, proposals, proposal_scores
    """
    stride=1.
    #with tf.variable_scope('top-preprocess') as scope:
    #    input = input

    with tf.variable_scope('top-block-1') as scope:
        block = conv2d_bn_relu(input, num_kernels=32, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=32, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        block = maxpool(block, kernel_size=(2,2), stride=[1,2,2,1], padding='SAME', name='4' )
        stride *=2

    with tf.variable_scope('top-block-2') as scope:
        block = conv2d_bn_relu(block, num_kernels=64, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=64, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        block = maxpool(block, kernel_size=(2,2), stride=[1,2,2,1], padding='SAME', name='4' )
        stride *=2

    with tf.variable_scope('top-block-3') as scope:
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='3')
        block = maxpool(block, kernel_size=(2,2), stride=[1,2,2,1], padding='SAME', name='4' )
        stride *=2

    with tf.variable_scope('top-block-4') as scope:
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='3')


    with tf.variable_scope('top') as scope:
        #up     = upsample2d(block, factor = 2, has_bias=True, trainable=True, name='1')
        #up     = block
        up      = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3,3), stride=[1,1,1,1], padding='SAME', name='2')
        scores  = conv2d(up, num_kernels=2*num_bases, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='score')
        probs   = tf.nn.softmax( tf.reshape(scores,[-1,2]), name='prob')
        deltas  = conv2d(up, num_kernels=4*num_bases, kernel_size=(1,1), stride=[1,1,1,1], padding='SAME', name='delta')

    #<todo> flip to train and test mode nms (e.g. different nms_pre_topn values): use tf.cond
    with tf.variable_scope('top-nms') as scope:    #non-max
        batch_size, img_height, img_width, img_channel = input.get_shape().as_list()
        img_scale = 1
        rois, roi_scores = tf_rpn_nms( probs, deltas, anchors, inds_inside,
                                       stride, img_width, img_height, img_scale,
                                       nms_thresh=0.7, min_size=stride, nms_pre_topn=500, nms_post_topn=100,
                                       name ='nms')

    #<todo> feature = upsample2d(block, factor = 4,  ...)
    feature = block

    print ('top: scale=%f, stride=%d'%(1./stride, stride))
    return feature, scores, probs, deltas, rois, roi_scores, stride
Esempio n. 5
0
def top_feature_net_r(input, anchors, inds_inside, num_bases):
    """
    :param input: 
    :param anchors: 
    :param inds_inside: 
    :param num_bases: 
    :return: 
            top_features, top_scores, top_probs, top_deltas, proposals, proposal_scores
    """
    stride=1.
    #with tf.variable_scope('top-preprocess') as scope:
    #    input = input
    batch_size, img_height, img_width, img_channel = input.get_shape().as_list()

    with tf.variable_scope('feature-extract-resnet') as scope:
        print('build_resnet')
        block = ResnetBuilder.resnet_tiny(input)

        # resnet_input = resnet.get_layer('input_1').input
        # resnet_output = resnet.get_layer('add_7').output
        # resnet_f = Model(inputs=resnet_input, outputs=resnet_output)  # add_7
        # # print(resnet_f.summary())
        # block = resnet_f(input)
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(1, 1), stride=[1, 1, 1, 1], padding='SAME', name='2')
        stride = 8
        feature = block


    with tf.variable_scope('predict') as scope:
        # up     = upsample2d(block, factor = 2, has_bias=True, trainable=True, name='1')
        # up     = block
        kernel_size = config.cfg.TOP_CONV_KERNEL_SIZE
        print('\ntop_predict kernal_size: {}\n'.format(kernel_size) )
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(kernel_size, kernel_size),
                            stride=[1, 1, 1, 1], padding='SAME', name='1')
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(kernel_size, kernel_size),
                            stride=[1, 1, 1, 1], padding='SAME', name='2')
        scores = conv2d(block, num_kernels=2 * num_bases, kernel_size=(1, 1), stride=[1, 1, 1, 1], padding='SAME',name='score')
        probs = tf.nn.softmax(tf.reshape(scores, [-1, 2]), name='prob')
        deltas = conv2d(block, num_kernels=4 * num_bases, kernel_size=(1, 1), stride=[1, 1, 1, 1], padding='SAME',name='delta')

    #<todo> flip to train and test mode nms (e.g. different nms_pre_topn values): use tf.cond
    with tf.variable_scope('NMS') as scope:    #non-max

        img_scale = 1
        rois, roi_scores = tf_rpn_nms( probs, deltas, anchors, inds_inside,
                                       stride, img_width, img_height, img_scale,
                                       nms_thresh=0.7, min_size=stride, nms_pre_topn=500, nms_post_topn=100,
                                       name ='nms')



    print ('top: scale=%f, stride=%d'%(1./stride, stride))
    return feature, scores, probs, deltas, rois, roi_scores, stride
def top_feature_net(input, anchors, inds_inside, num_bases):
    stride = 4
    # arg_scope = resnet_v1.resnet_arg_scope(weight_decay=0.0)
    # with slim.arg_scope(arg_scope) :
    with slim.arg_scope(vgg.vgg_arg_scope()):
        # net, end_points = resnet_v1.resnet_v1_50(input, None, global_pool=False, output_stride=8)
        block5, end_points = vgg.vgg_16(input)
        # pdb.set_trace()
        block3 = end_points['vgg_16/conv3/conv3_3']
        block4 = end_points['vgg_16/conv4/conv4_3']
    with tf.variable_scope("top_base") as sc:
        block5_ = conv2d_bn_relu(block5,
                                 num_kernels=256,
                                 kernel_size=(1, 1),
                                 stride=[1, 1, 1, 1],
                                 padding='SAME',
                                 name='5')
        up5 = upsample2d(block5_,
                         factor=2,
                         has_bias=True,
                         trainable=True,
                         name='up1')
        block4_ = conv2d_bn_relu(block4,
                                 num_kernels=256,
                                 kernel_size=(1, 1),
                                 stride=[1, 1, 1, 1],
                                 padding='SAME',
                                 name='4')
        up4 = upsample2d(block4_,
                         factor=2,
                         has_bias=True,
                         trainable=True,
                         name='up2')
        up_ = tf.add(up4, up5, name="up_add")
        block3_ = conv2d_bn_relu(block3,
                                 num_kernels=256,
                                 kernel_size=(1, 1),
                                 stride=[1, 1, 1, 1],
                                 padding='SAME',
                                 name='3')
        up = tf.add(up_, block3_, name='fpn_up_')
        block = conv2d_bn_relu(up,
                               num_kernels=256,
                               kernel_size=(3, 3),
                               stride=[1, 1, 1, 1],
                               padding='SAME',
                               name='fpn_up')
        tf.summary.histogram('rpn_top_block', block)
        # tf.summary.histogram('rpn_top_block_weights', tf.get_collection('2/conv_weight')[0])
        with tf.variable_scope('top') as scope:
            #up     = upsample2d(block, factor = 2, has_bias=True, trainable=True, name='1')
            #up     = block
            up = conv2d_bn_relu(block,
                                num_kernels=128,
                                kernel_size=(3, 3),
                                stride=[1, 1, 1, 1],
                                padding='SAME',
                                name='2')
            scores = conv2d(up,
                            num_kernels=2 * num_bases,
                            kernel_size=(1, 1),
                            stride=[1, 1, 1, 1],
                            padding='SAME',
                            name='score')
            probs = tf.nn.softmax(tf.reshape(scores, [-1, 2]), name='prob')
            deltas = conv2d(up,
                            num_kernels=4 * num_bases,
                            kernel_size=(1, 1),
                            stride=[1, 1, 1, 1],
                            padding='SAME',
                            name='delta')

        #<todo> flip to train and test mode nms (e.g. different nms_pre_topn values): use tf.cond
        with tf.variable_scope('top-nms') as scope:  #non-max
            batch_size, img_height, img_width, img_channel = input.get_shape(
            ).as_list()
            img_scale = 1
            # pdb.set_trace()
            rois, roi_scores = tf_rpn_nms(probs,
                                          deltas,
                                          anchors,
                                          inds_inside,
                                          stride,
                                          img_width,
                                          img_height,
                                          img_scale,
                                          nms_thresh=0.7,
                                          min_size=stride,
                                          nms_pre_topn=nms_pre_topn_,
                                          nms_post_topn=nms_post_topn_,
                                          name='nms')

        #<todo> feature = upsample2d(block, factor = 4,  ...)
        feature = block

        # print ('top: scale=%f, stride=%d'%(1./stride, stride))
    return feature, scores, probs, deltas, rois, roi_scores
Esempio n. 7
0
def top_feature_net_r(input, anchors, inds_inside, num_bases):
    """
    :param input: 
    :param anchors: 
    :param inds_inside: 
    :param num_bases: 
    :return: 
            top_features, top_scores, top_probs, top_deltas, proposals, proposal_scores
    """
    batch_size, img_height, img_width, img_channel = input.get_shape().as_list(
    )

    with tf.variable_scope('feature-extract-resnet') as scope:
        print('build_resnet')
        block = ResnetBuilder.resnet_tiny_smaller_kernel(input)
        feature = block

        top_feature_stride = 4
        # resnet_input = resnet.get_layer('input_1').input
        # resnet_output = resnet.get_layer('add_7').output
        # resnet_f = Model(inputs=resnet_input, outputs=resnet_output)  # add_7
        # # print(resnet_f.summary())
        # block = resnet_f(input)
        block = upsample2d(block,
                           factor=2,
                           has_bias=True,
                           trainable=True,
                           name='upsampling')

    with tf.variable_scope('predict') as scope:
        # block = upsample2d(block, factor=4, has_bias=True, trainable=True, name='1')
        # up     = block
        # kernel_size = config.cfg.TOP_CONV_KERNEL_SIZE
        top_anchors_stride = 2
        block = conv2d_bn_relu(block,
                               num_kernels=128,
                               kernel_size=(1, 1),
                               stride=[1, 1, 1, 1],
                               padding='SAME',
                               name='2')
        scores = conv2d(block,
                        num_kernels=2 * num_bases,
                        kernel_size=(1, 1),
                        stride=[1, 1, 1, 1],
                        padding='SAME',
                        name='score')
        probs = tf.nn.softmax(tf.reshape(scores, [-1, 2]), name='prob')
        deltas = conv2d(block,
                        num_kernels=4 * num_bases,
                        kernel_size=(1, 1),
                        stride=[1, 1, 1, 1],
                        padding='SAME',
                        name='delta')

    with tf.variable_scope('RPN-NMS') as scope:

        train_rois, train_roi_scores = tf_rpn_nms( probs, deltas, anchors, inds_inside, \
                                       top_anchors_stride, img_width, img_height, img_scale=1, \
                                       nms_thresh=0.7, min_size=top_anchors_stride, \
                                       nms_pre_topn=CFG.TRAIN.RPN_NMS_PRE_TOPN, nms_post_topn=CFG.TRAIN.RPN_NMS_POST_TOPN, \
                                       name ='train-rpn-nms')

        infer_rois, infer_roi_scores = tf_rpn_nms( probs, deltas, anchors, inds_inside, \
                                       top_anchors_stride, img_width, img_height, img_scale=1, \
                                       nms_thresh=0.1, min_size=top_anchors_stride, \
                                       nms_pre_topn=CFG.TRAIN.RPN_NMS_PRE_TOPN, nms_post_topn=CFG.TEST.RPN_NMS_POST_TOPN, \
                                       name ='infer-rpn-nms')

    print('top: anchor stride=%d, feature_stride=%d' %
          (top_anchors_stride, top_feature_stride))
    return feature, scores, probs, deltas, train_rois, train_roi_scores, top_anchors_stride,top_feature_stride, \
            infer_rois, infer_roi_scores
Esempio n. 8
0
def top_feature_net_r(input,
                      anchors,
                      inds_inside,
                      num_bases,
                      top_last_states=None):
    """
    :param input: 
    :param anchors: 
    :param inds_inside: 
    :param num_bases: 
    :return: 
            top_features, top_scores, top_probs, top_deltas, proposals, proposal_scores
    """
    stride = 1.
    #with tf.variable_scope('top-preprocess') as scope:
    #    input = input
    batch_size, img_height, img_width, img_channel = input.get_shape().as_list(
    )

    with tf.variable_scope('feature-extract-resnet') as scope:
        print('build_resnet')
        block = ResnetBuilder.resnet_tiny(input)

        # resnet_input = resnet.get_layer('input_1').input
        # resnet_output = resnet.get_layer('add_7').output
        # resnet_f = Model(inputs=resnet_input, outputs=resnet_output)  # add_7
        # # print(resnet_f.summary())
        # block = resnet_f(input)
        block = conv2d_bn_relu(block,
                               num_kernels=128,
                               kernel_size=(1, 1),
                               stride=[1, 1, 1, 1],
                               padding='SAME',
                               name='2')
        stride = 8

    with tf.variable_scope('memory') as scope:
        with tf.variable_scope('get_last_channel') as scope:
            lstm_input = block[:, :, :, 127]
            ori_shape = lstm_input.get_shape().as_list()
            # [batch_size, sequence_length_max, vector_size]
            lstm_input = tf.reshape(lstm_input,
                                    [-1, 1, np.prod(ori_shape[1:])])
        with tf.variable_scope('lstm') as scope:
            lstm_cell = rnn.BasicLSTMCell(np.prod(ori_shape[1:]))
            outputs, top_states = tf.nn.dynamic_rnn(
                lstm_cell,
                lstm_input,
                initial_state=top_last_states,
                dtype=tf.float32)
            outputs = tf.reshape(outputs, [-1, ori_shape[1], ori_shape[2]],
                                 'reshape')
        with tf.variable_scope('merge') as scope:
            block = tf.concat([
                block[:, :, :, 0:127],
                tf.reshape(outputs, [-1, ori_shape[1], ori_shape[2], 1])
            ], 3)

    with tf.variable_scope('predict') as scope:
        # up     = upsample2d(block, factor = 2, has_bias=True, trainable=True, name='1')
        # up     = block
        up = conv2d_bn_relu(block,
                            num_kernels=128,
                            kernel_size=(3, 3),
                            stride=[1, 1, 1, 1],
                            padding='SAME',
                            name='2')
        scores = conv2d(up,
                        num_kernels=2 * num_bases,
                        kernel_size=(1, 1),
                        stride=[1, 1, 1, 1],
                        padding='SAME',
                        name='score')
        probs = tf.nn.softmax(tf.reshape(scores, [-1, 2]), name='prob')
        deltas = conv2d(up,
                        num_kernels=4 * num_bases,
                        kernel_size=(1, 1),
                        stride=[1, 1, 1, 1],
                        padding='SAME',
                        name='delta')

    #<todo> flip to train and test mode nms (e.g. different nms_pre_topn values): use tf.cond
    with tf.variable_scope('NMS') as scope:  #non-max

        img_scale = 1
        rois, roi_scores = tf_rpn_nms(probs,
                                      deltas,
                                      anchors,
                                      inds_inside,
                                      stride,
                                      img_width,
                                      img_height,
                                      img_scale,
                                      nms_thresh=0.7,
                                      min_size=stride,
                                      nms_pre_topn=500,
                                      nms_post_topn=100,
                                      name='nms')

    #<todo> feature = upsample2d(block, factor = 4,  ...)
    feature = block

    print('top: scale=%f, stride=%d' % (1. / stride, stride))
    return feature, scores, probs, deltas, rois, roi_scores, stride, top_states
Esempio n. 9
0
def top_feature_net_r(input, anchors, inds_inside, num_bases, top_last_states=None):
    """
    :param input: 
    :param anchors: 
    :param inds_inside: 
    :param num_bases: 
    :return: 
            top_features, top_scores, top_probs, top_deltas, proposals, proposal_scores
    """
    stride=1.
    #with tf.variable_scope('top-preprocess') as scope:
    #    input = input
    batch_size, img_height, img_width, img_channel = input.get_shape().as_list()

    with tf.variable_scope('feature-extract-resnet') as scope:
        print('build_resnet')
        block = ResnetBuilder.resnet_tiny(input)

        # resnet_input = resnet.get_layer('input_1').input
        # resnet_output = resnet.get_layer('add_7').output
        # resnet_f = Model(inputs=resnet_input, outputs=resnet_output)  # add_7
        # # print(resnet_f.summary())
        # block = resnet_f(input)
        block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(1, 1), stride=[1, 1, 1, 1], padding='SAME', name='2')
        stride = 8

    with tf.variable_scope('memory') as scope:
        with tf.variable_scope('get_last_channel') as scope:
            lstm_input = block[:,:,:,127]
            ori_shape = lstm_input.get_shape().as_list()
            # [batch_size, sequence_length_max, vector_size]
            lstm_input = tf.reshape(lstm_input, [-1, 1, np.prod(ori_shape[1:])])
        with tf.variable_scope('lstm') as scope:
            lstm_cell = rnn.BasicLSTMCell(np.prod(ori_shape[1:]) )
            outputs, top_states = tf.nn.dynamic_rnn(lstm_cell, lstm_input, initial_state=top_last_states, dtype=tf.float32)
            outputs = tf.reshape(outputs, [-1, ori_shape[1],ori_shape[2]], 'reshape')
        with tf.variable_scope('merge') as scope:
            block =  tf.concat([block[:, :, :, 0:127],tf.reshape(outputs,[-1,ori_shape[1],ori_shape[2],1])], 3)



    with tf.variable_scope('predict') as scope:
        # up     = upsample2d(block, factor = 2, has_bias=True, trainable=True, name='1')
        # up     = block
        up = conv2d_bn_relu(block, num_kernels=128, kernel_size=(3, 3), stride=[1, 1, 1, 1], padding='SAME', name='2')
        scores = conv2d(up, num_kernels=2 * num_bases, kernel_size=(1, 1), stride=[1, 1, 1, 1], padding='SAME',name='score')
        probs = tf.nn.softmax(tf.reshape(scores, [-1, 2]), name='prob')
        deltas = conv2d(up, num_kernels=4 * num_bases, kernel_size=(1, 1), stride=[1, 1, 1, 1], padding='SAME',name='delta')

    #<todo> flip to train and test mode nms (e.g. different nms_pre_topn values): use tf.cond
    with tf.variable_scope('NMS') as scope:    #non-max

        img_scale = 1
        rois, roi_scores = tf_rpn_nms( probs, deltas, anchors, inds_inside,
                                       stride, img_width, img_height, img_scale,
                                       nms_thresh=0.7, min_size=stride, nms_pre_topn=500, nms_post_topn=100,
                                       name ='nms')

    #<todo> feature = upsample2d(block, factor = 4,  ...)
    feature = block

    print ('top: scale=%f, stride=%d'%(1./stride, stride))
    return feature, scores, probs, deltas, rois, roi_scores, stride, top_states