-
-
Save RaphaelMeudec/ee723dbb0ad429bc73f7641b61043765 to your computer and use it in GitHub Desktop.
ResNet Block with Keras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.layers import Input, Conv2D, Activation, BatchNormalization | |
from keras.layers.merge import Add | |
from keras.layers.core import Dropout | |
def res_block(input, filters, kernel_size=(3,3), strides=(1,1), use_dropout=False): | |
""" | |
Instanciate a Keras Resnet Block using sequential API. | |
:param input: Input tensor | |
:param filters: Number of filters to use | |
:param kernel_size: Shape of the kernel for the convolution | |
:param strides: Shape of the strides for the convolution | |
:param use_dropout: Boolean value to determine the use of dropout | |
:return: Keras Model | |
""" | |
x = ReflectionPadding2D((1,1))(input) | |
x = Conv2D(filters=filters, | |
kernel_size=kernel_size, | |
strides=strides,)(x) | |
x = BatchNormalization()(x) | |
x = Activation('relu')(x) | |
if use_dropout: | |
x = Dropout(0.5)(x) | |
x = ReflectionPadding2D((1,1))(x) | |
x = Conv2D(filters=filters, | |
kernel_size=kernel_size, | |
strides=strides,)(x) | |
x = BatchNormalization()(x) | |
# Two convolution layers followed by a direct connection between input and output | |
merged = Add()([input, x]) | |
return merged |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment