Conversion from keras to pytorch (

I’m trying to convert CNN model code from Keras to Pytorch.

here is the original keras model: it comes from this article: " Squeeze-and-Excitation Networks"

def se_block(in_block, ch, ratio=16):
    x = GlobalAveragePooling2D()(in_block)
    x = Dense(ch//ratio, activation='relu')(x)
    x = Dense(ch, activation='sigmoid')(x)
    return multiply()([in_block, x])

here is what i have done but i am unsure since i have mixed results when adding this module to my network:

def GlobalAveragePooling(x):
    x=torch.mean(x.view(x.size(0),x.size(1),-1),dim=2)
    return x   

class se_block(nn.Module):
    def __init__(self,in_block,ch,ratio=16):
        super(se_block,self).__init__()
        self.GlobalAveragePooling=GlobalAveragePooling()
        self.Dense1=nn.Linear(in_features=in_block,out_features=ch//ratio,)
        self.Dense2=nn.Linear(in_features=ch//ratio,out_features=ch)

    def forward(x):
        x=GlobalAveragePooling(x)
        x=Dense1(x)
        x=F.relu(x)
        x=Dense2(x)
        x=F.sigmoid(x)
        return x 



Thanks :slight_smile:

Hi,

That looks good.
Aren’t you missing the last multiply though? If you you might want to modify your forward to keep a reference to the input and return input * x.

1 Like

Hello,
Thanks for the help, yes indeed I had forgotten to multiply the input by the weight !