# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:ML2]
#     language: python
#     name: conda-env-ML2-py
# ---

# # VGG16

from keras.layers import Input, Dense,Flatten, Conv2D, MaxPooling2D
from keras.models import Model
from keras.utils import plot_model


# ## VGG16 architecture 
#
# <br>
# ![](images/vgg16.png)

def VGG16(input_shape):
    
    """
    Implementation of the popular VGG16 the following architecture:
    CONV2D -> CONV2D -> MAXPOOL -> CONV2D -> CONV2D -> MAXPOOL -> CONV2D -> CONV2D -> CONV2D -> MAXPOOL->
    CONV2D -> CONV2D -> CONV2D -> MAXPOOL -> CONV2D -> CONV2D -> CONV2D -> MAXPOOL -> CONV2D -> CONV2D ->
    CONV2D -> MAXPOOL -> Flatten -> Dense -> Dense -> Dense-> TopLayer
    

    Arguments:
    input_shape -- shape of the images of the dataset
    
    Returns:
    model -- a Model() instance in Keras
    """
    
    X_input = Input(input_shape)
    #Block 1
    X = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(X_input)
    X = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(X)
    X = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(X)
    #Block 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)
    X = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(X)
    #Block 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)
    X = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(X)
    #Block 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)
    X = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(X)
    #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)
    X = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(X)
    
    
    X = Flatten(name='flatten')(X)
    X = Dense(4096, activation='relu', name='fc1')(X)
    X = Dense(4096, activation='relu', name='fc2')(X)
    X = Dense(4096, activation='relu', name='fc3')(X)
    
    model = Model(inputs=X_input, outputs=X)
    
    return model

input_shape = (124, 124, 3)

vgg16_model = VGG16(input_shape)

vgg16_model.summary()

plot_model(vgg16_model, to_file='images/vgg16_model.png', show_shapes=True, show_layer_names=True)

# + [markdown] heading_collapsed=true
# ## Model plot
# ![](images/vgg16_model.png)

StarcoderdataJupyterScriptsDedupFiltered