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 get the middle features of the effecientnet for FCN/FPN? #117

Closed
lartpang opened this issue Dec 2, 2019 · 4 comments
Closed

How to get the middle features of the effecientnet for FCN/FPN? #117

lartpang opened this issue Dec 2, 2019 · 4 comments

Comments

@lartpang
Copy link

lartpang commented Dec 2, 2019

No description provided.

@lartpang lartpang changed the title How to get the middle feature of the effecientnet for FCN/FPN? How to get the middle features of the effecientnet for FCN/FPN? Dec 2, 2019
@lartpang
Copy link
Author

lartpang commented Dec 3, 2019

I use the method to do it:

class EfficientNet(nn.Module):
    ...
    def extract_features_midconv(self, inputs):
        """ Returns output of the middle convolution layers """
        out_feats = []
        
        # Stem
        x = self._swish(self._bn0(self._conv_stem(inputs)))
        prev_x_size = x.size()[-1]
        out_feats.append(x)
        
        # Blocks
        for idx, block in enumerate(self._blocks):
            drop_connect_rate = self._global_params.drop_connect_rate
            if drop_connect_rate:
                drop_connect_rate *= float(idx) / len(self._blocks)
            x = block(x, drop_connect_rate=drop_connect_rate)
            if x.size()[-1] != prev_x_size:
                prev_x_size = x.size()[-1]
                out_feats.append(x)
            else:
                out_feats[-1] = x
        
        # Head
        x = self._swish(self._bn1(self._conv_head(x)))
        if x.size()[-1] != prev_x_size:
            out_feats.append(x)
        else:
            out_feats[-1] = x
        
        return out_feats

I think this treatment is a little inelegant, do you have a better way?

@fmahoudeau
Copy link

What about this solution ?
It returns features at stride 4, 8, 16 and 32.

def extract_features_midconv(self, inputs):
    """ Returns output of the middle convolution layers """
    out = []

    # Stem
    x = self._swish(self._bn0(self._conv_stem(inputs)))

    # Blocks
    for idx, block in enumerate(self._blocks):
        drop_connect_rate = self._global_params.drop_connect_rate
        if drop_connect_rate:
            drop_connect_rate *= float(idx) / len(self._blocks)
        y = block(x, drop_connect_rate=drop_connect_rate)
        if y.size()[-1] != x.size()[-1]:
            out.append(x)
        x = y
    
    # Head
    x = self._swish(self._bn1(self._conv_head(x)))
    out.append(x)

    return out[1:]

@lartpang
Copy link
Author

lartpang commented Dec 8, 2019

@fmahoudeau Oh..yeah! This is good, and better than my code! 👍

@ahundt
Copy link

ahundt commented Nov 24, 2020

#63 also does this

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

3 participants