question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keras Resnet50 implementation pooling options do nothing

See original GitHub issue

Hello,

I was looking at the Resnet50 implementation bundled with Keras: https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py

Supposedly there is an optional pooling toggle that either does either no pooling, global average pooling, or global max pooling at the end of the network. However looking at the code for that section:

... 

x = AveragePooling2D((7, 7), name='avg_pool')(x)

    if include_top:
        x = Flatten()(x)
        x = Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

It seems that no matter what option you pick Global Average Pooling (GAP) is always applied by means of the x = AveragePooling2D((7, 7), name='avg_pool')(x) line which if I understand correctly basically does the same thing as GAP by reducing everything down to a (1, 1, 2048) output.

The (optional) Global Average Pooling or Global Max Pooling operations after this line have nothing to work with anymore since the output is already (1,1) spatially and thus nothing can be averaged or max pooled anymore at this point making the optional toggle for them inoperable. Obviously the “no pooling” option is also non-functional because of this.

I would suggest removing the offending Average Pooling line and letting the GlobalAveragePooling2D operation take care of the this if requested by the user. The other options will then also function.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
levyfancommented, Feb 13, 2018

I agree with @mxvs, AveragePooling2D should be removed if pooling == None.

2reactions
mxvscommented, Sep 21, 2017

Hello,

My point is that the AveragePooling2D(7,7) operation prevents the other options from working.

If you first perform AveragePooling2D(7,7) followed by GlobalMaxPooling2D() you don’t get max pooling at all, since MaxPooling of a (1,1) spatially has nothing to pool (its already (1,1)).

The correct code looks like this:

... 
    if include_top:
        x = AveragePooling2D((7, 7), name='avg_pool')(x)
        x = Flatten()(x)
        x = Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

This way you can either get: A) average pooling + top layer (like in the ResNet Paper) B) GlobalAverage Pooling without the top layer C) GlobalMaxPooling without the top player D) No pooling and simply the output of the last convolutional layer (as its mentioned in the Keras documentation).

The currently implementation prevents options C) and D) from working (since you always get AveragePooling after the last conv layer even if you don’t want that) and option B is currently only flattens (you can’t average pool a (1,1) volume).

Read more comments on GitHub >

github_iconTop Results From Across the Web

ResNet and ResNetV2 - Keras
ResNet50 function · None means that the output of the model will be the 4D tensor output of the last convolutional block. ·...
Read more >
A guide to transfer learning with Keras using ResNet50
In this blog post we will provide a guide through for transfer learning with the main aspects to take into account in the...
Read more >
Transfer Learning — Part — 5.1!! Implementing ResNet in Keras
In this section we will see how we can implement ResNet model in keras to have a foundation to start our real implementation...
Read more >
tf.keras.applications.resnet50.ResNet50 | TensorFlow v2.11.0
Optional pooling mode for feature extraction when include_top is False . None means that the output of the model will be the 4D...
Read more >
Why implementation of Resnet50 in Keras forbids images ...
ResNet50 has 5 stages of downsampling, between MaxPooling of 2x2 and ... doing anything, and this will change the behavior of the network....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found