Can I combine Conv2D and LeakyReLU into a single layer?

Issue

The keras Conv2D layer does not come with an activation function itself. I am currently rebuilding the YOLOv1 model for practicing. In the YOLOv1 model, there are several Conv2D layers followed by activations using the leaky relu function. Is there a way to combine

from keras.layers import Conv2D, LeakyReLU

...

def model(input):
    ...

    X = Conv2D(filters, kernel_size)(X)
    X = LeakyReLU(X)

    ...

into a single line of code, like X = conv_with_leaky_relu(X)? I think it should be similar to

def conv_with_leaky_relu(*args, **kwargs):
    X = Conv2D(*args, **kwargs)(X)
    X = LeakyReLU(X)
    return X

but this of course doesn’t work because it is undefined what X ist. Any ideas?

Solution

You can just pass it as an activation:

X = Conv2D(filters, kernel_size, activation=LeakyReLU())(X)

Answered By – Yannick Funk

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published