{"cells":[{"metadata":{},"cell_type":"markdown","source":"# **🌈 ResNet50**\n\n---","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## **📦 Importing packages**","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.initializers import he_normal","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## **🦄 Identity block**\n\n![](http://github.com/Kulbear/deep-learning-coursera/raw/997fdb2e2db67acd45d29ae418212463a54be06d/Convolutional%20Neural%20Networks/images/idblock2_kiank.png)\n\nHere in the implementation is of a slightly more powerful version of this identity block, in which the skip connection \"skips over\" 3 hidden layers rather than 2 layers.\n\nI looks like this:\n\n![](http://github.com/Kulbear/deep-learning-coursera/raw/997fdb2e2db67acd45d29ae418212463a54be06d/Convolutional%20Neural%20Networks/images/idblock3_kiank.png)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def identity_block(X, f, filters, stage, block):\n    \"\"\"\n    m -- number of examples\n    \n    Arguments:\n    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)\n    f -- integer, specifying the shape of the middle CONV's window for the main path\n    filters -- python list of integers, defining the number of filters in the CONV layers of the main path\n    stage -- integer, used to name the layers, depending on their position in the network\n    block -- string/character, used to name the layers, depending on their position in the network\n    \n    Returns:\n    X -- output of the identity block, tensor of shape (n_H, n_W, n_C)\n    \"\"\"\n    \n    # Defining name basis\n    conv_name_base = f'res{stage}{block}_branch'\n    bn_name_base = f'bn{stage}{block}_branch'\n    \n    # Retrieve filters\n    F1, F2, F3 = filters\n    \n    # Save the input value. You'll need this later to add back to the main path. \n    X_shortcut = X\n    \n    # strides: (1, 1) (for all layers in Identity block)\n    kernel_initializer = he_normal(seed=0)\n    \n    # ### Main path ###\n    \n    # First component of main path\n    X = Conv2D(filters=F1, kernel_size=(1, 1), padding='valid', name=f'{conv_name_base}2a', kernel_initializer=kernel_initializer)(X)\n    X = BatchNormalization(name=f'{bn_name_base}2a')(X)\n    X = Activation('relu')(X)\n    \n    # Second component of main path\n    X = Conv2D(filters=F2, kernel_size=(f, f), padding='same', name=f'{conv_name_base}2b', kernel_initializer=kernel_initializer)(X)\n    X = BatchNormalization(name=f'{bn_name_base}2b')(X)\n    X = Activation('relu')(X)\n    \n    # Third component of main path\n    X = Conv2D(filters=F3, kernel_size=(1, 1), padding='valid', name=f'{conv_name_base}2c', kernel_initializer=kernel_initializer)(X)\n    X = BatchNormalization(name=f'{bn_name_base}2c')(X)\n    \n    # ### Shortcut path ###\n    \n    # Final step: Add shortcut value to main path, and pass it through a RELU activation\n    X = Add()([X, X_shortcut])\n    X = Activation('relu')(X)\n    \n    return X","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## **🧩 Convolutional block**\n\n![](http://github.com/Kulbear/deep-learning-coursera/raw/997fdb2e2db67acd45d29ae418212463a54be06d/Convolutional%20Neural%20Networks/images/convblock_kiank.png)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def convolutional_block(X, f, filters, stage, block, s=2):\n    \"\"\"\n    m -- number of examples\n    \n    Arguments:\n    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)\n    f -- integer, specifying the shape of the middle CONV's window for the main path\n    filters -- python list of integers, defining the number of filters in the CONV layers of the main path\n    stage -- integer, used to name the layers, depending on their position in the network\n    block -- string/character, used to name the layers, depending on their position in the network\n    s -- Integer, specifying the stride to be used\n\n    Returns:\n    X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)\n    \"\"\"\n    \n    # Defining name basis\n    conv_name_base = f'res{stage}{block}_branch'\n    bn_name_base = f'bn{stage}{block}_branch'\n    \n    # Retrieve filters\n    F1, F2, F3 = filters\n    \n    # Save the input value. You'll need this later to add back to the main path. \n    X_shortcut = X\n    \n    # strides: (1, 1) (for all layers in Identity block)\n    kernel_initializer = he_normal(seed=0)\n    \n    # ### Main path ###\n    \n    # First component of main path\n    X = Conv2D(filters=F1, kernel_size=(1, 1), strides=(s, s), padding='valid', name=f'{conv_name_base}2a', kernel_initializer=kernel_initializer)(X)\n    X = BatchNormalization(name=f'{bn_name_base}2a')(X)\n    X = Activation('relu')(X)\n    \n    # Second component of main path\n    X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=f'{conv_name_base}2b', kernel_initializer=kernel_initializer)(X)\n    X = BatchNormalization(name=f'{bn_name_base}2b')(X)\n    X = Activation('relu')(X)\n    \n    # Third component of main path\n    X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=f'{conv_name_base}2c', kernel_initializer=kernel_initializer)(X)\n    X = BatchNormalization(name=f'{bn_name_base}2c')(X)\n    \n    # ### Shortcut path ###\n    \n    X_shortcut = Conv2D(filters=F3, kernel_size=(1, 1), strides=(s, s), padding='valid', name=f'{conv_name_base}1', kernel_initializer=kernel_initializer)(X_shortcut)\n    X_shortcut = BatchNormalization(name=f'{bn_name_base}1')(X_shortcut)\n    \n    # Final step: Add shortcut value to main path, and pass it through a RELU activation\n    X = Add()([X, X_shortcut])\n    X = Activation('relu')(X)\n    \n    return X","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## **😎 Building ResNet50**\n\n![](http://github.com/Kulbear/deep-learning-coursera/raw/997fdb2e2db67acd45d29ae418212463a54be06d/Convolutional%20Neural%20Networks/images/resnet_kiank.png)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def ResNet50(input_shape=(64, 64, 3), classes=1):\n    \"\"\"\n    Implementation of the popular ResNet50 the following architecture:\n    CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3\n    -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER\n\n    Arguments:\n    input_shape -- shape of the images of the dataset\n    classes -- integer, number of classes\n\n    Returns:\n    model -- a Model() instance in Tensorflow's Keras API\n    \"\"\"\n    \n    # Define the input as a tensor with shape input_shape\n    X_input = Input(input_shape)\n    \n    # Zero-Padding\n    X = ZeroPadding2D((3, 3))(X_input)\n    \n    # Stage 1\n    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer=he_normal(seed=0))(X)\n    X = BatchNormalization(name='bn_conv1')(X)\n    X = Activation('relu')(X)\n    X = MaxPooling2D((3, 3), strides=(2, 2))(X)\n    \n    # Stage 2\n    X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block='a', s=1)\n    X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')\n    X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')\n    \n    # Stage 3\n    X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block='a', s=2)\n    X = identity_block(X, 3, [128, 128, 512], stage=3, block='b')\n    X = identity_block(X, 3, [128, 128, 512], stage=3, block='c')\n    X = identity_block(X, 3, [128, 128, 512], stage=3, block='d')\n    \n    # Stage 4\n    X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block='a', s=2)\n    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='b')\n    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='c')\n    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='d')\n    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='e')\n    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='f')\n\n    # Stage 5 \n    X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block='a', s=2)\n    X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b')\n    X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c')\n\n    # AVGPOOL\n    X = AveragePooling2D(pool_size=(2, 2), padding='same')(X)\n\n    # output layer\n    X = Flatten()(X)\n    X = Dense(classes, activation='sigmoid', name=f'fc{classes}', kernel_initializer=he_normal(seed=0))(X)\n\n    # Create model\n    model = Model(inputs=X_input, outputs=X, name='ResNet50')\n\n    return model","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"---","execution_count":null}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}