Skip to content

Instantly share code, notes, and snippets.

@sbugallo
Created January 23, 2018 08:26
Show Gist options
  • Save sbugallo/cb70226bed33c0de49b289a8fbd4b667 to your computer and use it in GitHub Desktop.
Save sbugallo/cb70226bed33c0de49b289a8fbd4b667 to your computer and use it in GitHub Desktop.
VGG16 for MRCNN
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
"""
VGG16 model
"""
def build_vgg_graph(input_image, stage5=False):
"""
Model generator
:param input_image: model input
:param stage5: enables or disables the last stage
:return: returns the network stages
"""
# Stage 1
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(input_image)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
C1 = x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# Stage 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
C2 = x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
# Stage 3
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
C3 = x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
# Stage 4
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
C4 = x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
# Stage 5
if stage5:
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
C5 = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
else:
C5 = None
return [C1, C2, C3, C4, C5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment