Skip to content

how to implement the spatial pyramid pooling layer with tensorflow? #8

Description

@jiankang1991

Hi,
Since you implemented the fast-rcnn, do you know how to implement the spatial pyramid pooling layer as introduced in the paper?

I try to implement one according to the paper like this:

def spp_layer(input_, name = 'SPP_layer'):
    '''4 level spp layer 
    spatial bins: [6_6, 3_3, 2_2, 1_1] '''

    shape = input_.get_shape().as_list()

    with tf.variable_scope(name):



        spp_6_6_pool = tf.nn.max_pool(input_, ksize=[1, np.ceil(shape[1]/6).astype(np.int32), np.ceil(shape[1]/6).astype(np.int32), 1], 
                                      strides=[1, shape[1]//6, shape[2]//6, 1], 
                                      padding='SAME')
        print('SPP layer level 6:', spp_6_6_pool.get_shape().as_list())

        spp_3_3_pool = tf.nn.max_pool(input_, ksize=[1, np.ceil(shape[1]/3).astype(np.int32), np.ceil(shape[2]/3).astype(np.int32), 1], 
                                      strides=[1, shape[1]//3, shape[2]//3, 1], 
                                      padding='SAME')
        print('SPP layer level 3:', spp_3_3_pool.get_shape().as_list())

        spp_2_2_pool = tf.nn.max_pool(input_, ksize=[1, np.ceil(shape[1]/2).astype(np.int32), np.ceil(shape[2]/2).astype(np.int32), 1], 
                                      strides=[1, shape[1]//2, shape[2]//2, 1], 
                                      padding='SAME')
        print('SPP layer level 2:', spp_2_2_pool.get_shape().as_list())

        spp_1_1_pool = tf.nn.max_pool(input_, ksize=[1, np.ceil(shape[1]/1).astype(np.int32), np.ceil(shape[2]/1).astype(np.int32), 1], 
                                      strides=[1, shape[1]//1, shape[2]//1, 1], 
                                      padding='SAME')
        print('SPP layer level 1:', spp_1_1_pool.get_shape().as_list())


        spp_6_6_pool_flat = tf.reshape(spp_6_6_pool, [shape[0], -1])
        spp_3_3_pool_flat = tf.reshape(spp_3_3_pool, [shape[0], -1])
        spp_2_2_pool_flat = tf.reshape(spp_2_2_pool, [shape[0], -1])
        spp_1_1_pool_flat = tf.reshape(spp_1_1_pool, [shape[0], -1])

        spp_pool = tf.concat(1, [spp_6_6_pool_flat, spp_3_3_pool_flat, spp_2_2_pool_flat, spp_1_1_pool_flat])

    return spp_pool

But it seems that the output size of max pooling layer in tensorflow is a little big difference. The same setting of kernel size as ceil() and stride size as floor() cannot have the fixed length of fully connected layer.
Do you have some idea how to achieve this?
Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions