Skip to content

Instantly share code, notes, and snippets.

@RaphaelMeudec
Last active March 21, 2018 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RaphaelMeudec/ee723dbb0ad429bc73f7641b61043765 to your computer and use it in GitHub Desktop.
Save RaphaelMeudec/ee723dbb0ad429bc73f7641b61043765 to your computer and use it in GitHub Desktop.
ResNet Block with Keras
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