<jupyter_start><jupyter_text>This notebook is a tutorial for how to train a ResNet model on Signs dataset.   References:Code: https://github.com/marcopeix/Deep_Learning_AI/tree/master/4.Convolutional%20Neural%20Networks   ResNet blocks images: https://medium.com/analytics-vidhya/understanding-and-implementation-of-residual-networks-resnets-b80f9a507b9c    Let's start with importing relevant libraries<jupyter_code>import numpy as np
from keras import layers
from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from keras.models import Model, load_model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
#from resnets_utils import *
from keras.initializers import glorot_uniform
import scipy.misc
from matplotlib.pyplot import imshow
%matplotlib inline

# import keras.backend as K
# K.set_image_data_format('channels_last')
# K.set_learning_phase(1)<jupyter_output>Using TensorFlow backend.<jupyter_text>Utility Functions<jupyter_code>import os
import numpy as np
import tensorflow as tf
import h5py
import math
import urllib.request

def load_dataset():
    urllib.request.urlretrieve('https://drive.google.com/u/0/uc?id=1uOOg-YDFjyTlJC1-khplcxBInKDEnH5D&export=download', '/content/train_signs.h5')
    train_dataset = h5py.File('/content/train_signs.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
    print("Train data loaded")

    urllib.request.urlretrieve('https://drive.google.com/u/0/uc?id=1C9KqEWm59Hfiaco3lFQ2zZldJFb00zRt&export=download', '/content/test_signs.h5')
    test_dataset = h5py.File('/content/test_signs.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
    print("Test data loaded")
    classes = np.array(test_dataset["list_classes"][:]) # the list of classes
    
    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
    
    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

def convert_to_one_hot(Y, C):
    Y = np.eye(C)[Y.reshape(-1)].T
    return Y<jupyter_output><empty_output><jupyter_text>Loading the data<jupyter_code><jupyter_output><empty_output><jupyter_text>Let's visualize some training examples and their labels<jupyter_code><jupyter_output><empty_output><jupyter_text>Defining the architecture of the ResNet ![alt text](https://drive.google.com/u/0/uc?id=1i9zEzdzjLsI-QvV5NFIDTsNhfs-sHxbS)  Identity Block ![alt text](https://miro.medium.com/max/1400/1*BCbJZXwGDtEdytj9ag_YWw.png)<jupyter_code>def identity_block(X, f, filters, stage, block):
    """
    Implementation of the identity block as defined in in above figure
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    f -- integer, specifying the shape of the middle CONV's window for the main path
    filters -- python list of integers, defining the number of filters in the CONV layers of the main path
    stage -- integer, used to name the layers, depending on their position in the network
    block -- string/character, used to name the layers, depending on their position in the network
    
    Returns:
    X -- output of the identity block, tensor of shape (n_H, n_W, n_C)
    """<jupyter_output><empty_output><jupyter_text>Convolution Block ![alt text](https://miro.medium.com/max/1400/1*sb_4xKI_bRoX6jmZcNTRWw.png)<jupyter_code>def convolutional_block(X, f, filters, stage, block, s = 2):
    """
    Implementation of the convolutional block as defined in above figure
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    f -- integer, specifying the shape of the middle CONV's window for the main path
    filters -- python list of integers, defining the number of filters in the CONV layers of the main path
    stage -- integer, used to name the layers, depending on their position in the network
    block -- string/character, used to name the layers, depending on their position in the network
    s -- Integer, specifying the stride to be used
    
    Returns:
    X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
    """
    
    
def ResNet50(input_shape = (64, 64, 3), classes = 6):
    """
    Implementation of the popular ResNet50 the following architecture:
    CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
    -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER

    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes

    Returns:
    model -- a Model() instance in Keras
    """<jupyter_output><empty_output><jupyter_text>We will **compile** our model by defining loss, optimizer and metrics that we will use to train our network.* loss: [categorical_crossentropy](https://keras.io/api/losses/probabilistic_losses/categoricalcrossentropy-function)* optimizer: [adam](https://keras.io/api/optimizers/adam/)* metrics: [accuracy](https://keras.io/api/metrics/accuracy_metrics/accuracy-class)<jupyter_code># model.summary()<jupyter_output><empty_output><jupyter_text>Now, let's **train our model** by using the parameters that we defined before.<jupyter_code><jupyter_output><empty_output><jupyter_text>**Evaluation** of the trained model on validation data<jupyter_code><jupyter_output><empty_output><jupyter_text>Pre-trained model<jupyter_code># !pip install gdown
import gdown
if 'ResNet50.h5' not in os.listdir('/content/'):
  gdown.download('https://drive.google.com/uc?id=14vW8s9Os7zXGeixXvMO9S1QoHBPM5MPx', '/content/ResNet50.h5', quiet=False)<jupyter_output><empty_output><jupyter_text>Let's visualize the correctly classified images<jupyter_code><jupyter_output><empty_output><jupyter_text>How about we **visualize images that our model predicted incorrectly**.  *   First we have to get the indexes of the images where y_pred and y_test doesn't match*   Then we can visualize them one by one<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10100395497414055, 'fraction_numerical': 0.013690295101916642, 'mean_word_length': 5.071098799630656, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 61, 'https://': 11, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '561348', 'license': 'MIT', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2204, 'n_words': 567, 'path': 'notebooks/2020_07_25_final/ResNet_empty.ipynb', 'repo_name': 'dineshdaultani/CNN_training', 'size': '6574'}