Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jiankang1991 opened this issue Dec 1, 2016 · 0 comments

Comments

@jiankang1991
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant