# -*- coding: utf-8 -*-
"""Copy of ResNet

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1SVTcgc1E5KpIs-UPv9ogh9kYscBUhkLN
"""

#originally coded on google colab
#code borrowed from https://github.com/minhthangdang/SignLanguageRecognitionResNet

from google.colab import drive
drive.mount('/content/drive')

import numpy as np
import pandas as pd
import tensorflow as tf
import math
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.initializers import glorot_uniform

def load_dataset():
	# read train dataset
	train_dataset = pd.read_csv('/content/drive/My Drive/504 Project/Data/sign_mnist_train/sign_mnist_train.csv')
	train_set_y_orig = labels = train_dataset['label'].values # train set labels
	train_dataset.drop('label', axis = 1, inplace = True) # drop the label coloumn from the training set
	train_set_x_orig = train_dataset.values # train set features
	# convert X to (m, n_H, n_W, n_C) where 
	# m is number of examples, n_H is height, n_W is width and n_C is number of channels
	train_set_x_orig = train_set_x_orig.reshape((train_set_x_orig.shape[0], 28, 28, 1))

	# read test dataset
	test_dataset = pd.read_csv('/content/drive/My Drive/504 Project/Data/sign_mnist_test/sign_mnist_test.csv')
	test_set_y_orig = test_dataset['label'].values # test set labels
	test_dataset.drop('label', axis = 1, inplace = True) # drop the label coloumn from the test set
	test_set_x_orig = test_dataset.values # test set features
	# convert X to (m, n_H, n_W, n_C) where 
	# m is number of examples, n_H is height, n_W is width and n_C is number of channels
	test_set_x_orig = test_set_x_orig.reshape((test_set_x_orig.shape[0], 28, 28, 1))

	classes = np.array(labels)
	classes = np.unique(classes)

	return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

def identity_block(X, filters, stage, block):
    """
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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)
    """
    
    # defining name basis
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    
    # Retrieve Filters
    F1, F2 = filters
    
    # Save the input value. You'll need this later to add back to the main path. 
    X_shortcut = X
    
    # First component of main path
    X = Conv2D(filters = F1, kernel_size = (3, 3), strides = (1,1), padding = 'same', name = conv_name_base + '2a', kernel_initializer = glorot_uniform())(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
    X = Activation('relu')(X)
       
    # Second component of main path
    X = Conv2D(filters = F2, kernel_size = (3, 3), strides = (1,1), padding = 'same', name = conv_name_base + '2b', kernel_initializer = glorot_uniform())(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X)

    # Final step: Add shortcut value to main path, and pass it through a RELU activation
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    
    return X

def convolutional_block(X, filters, stage, block):
    """
    Implementation of the convolutional block as defined in Figure 4
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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 convolutional block, tensor of shape (n_H, n_W, n_C)
    """
    
    # defining name basis
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    
    # Retrieve Filters
    F1, F2 = filters
    
    # Save the input value
    X_shortcut = X

    ##### MAIN PATH #####
    # First component of main path 
    X = Conv2D(F1, (3, 3), strides = (2, 2), padding = 'same', name = conv_name_base + '2a', kernel_initializer = glorot_uniform())(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
    X = Activation('relu')(X)

    # Second component of main path
    X = Conv2D(F2, (3, 3), strides = (1, 1), padding = 'same', name = conv_name_base + '2b', kernel_initializer = glorot_uniform())(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X)

    ##### SHORTCUT PATH ####
    X_shortcut = Conv2D(F2, (3, 3), strides = (2, 2), padding = 'same', name = conv_name_base + '1', kernel_initializer = glorot_uniform())(X_shortcut)
    X_shortcut = BatchNormalization(axis = 3, name = bn_name_base + '1')(X_shortcut)

    # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    
    return X

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.initializers import glorot_uniform
#from utils_resnet import identity_block, convolutional_block

def ResNet18(input_shape = (28, 28, 1), classes = 24):
    """
    Implementation of the popular ResNet18
    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes
    Returns:
    model -- a Model() instance in Keras
    """
    
    # Define the input as a tensor with shape input_shape
    X = X_input = Input(input_shape)

    
    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)
    
    # Stage 1
    X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3, name = 'bn_conv1')(X)
    X = Activation('relu')(X)
    #X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    # Stage 2
    X = convolutional_block(X, [64, 64], stage=2, block='a')
    X = identity_block(X, [64, 64], stage=2, block='b')

    # Stage 3
    X = convolutional_block(X, [128, 128], stage=3, block='a')
    X = identity_block(X, [128, 128], stage=3, block='b')

    # Stage 4
    X = convolutional_block(X, [256, 256], stage=4, block='a')
    X = identity_block(X, [256, 256], stage=4, block='b')

    # Stage 5
    X = convolutional_block(X, [512, 512], stage=5, block='a')
    X = identity_block(X, [512, 512], stage=5, block='b')

    # AVGPOOL
    # X = AveragePooling2D(pool_size=(2,2), name='avg_pool')(X)

    # output layer
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X)
    
    # Create model
    model = Model(inputs = X_input, outputs = X, name='ResNet18')

    return model

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelBinarizer
#from utils_resnet import load_dataset
#from model import ResNet18

import keras.backend as K
K.set_image_data_format('channels_last')
K.set_learning_phase(1)

X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Example of pictures
ROWS = 10
fig, axes = plt.subplots(ROWS, ROWS, figsize=(10, 10))
for i in range(ROWS):
	for j in range(ROWS):
		k = np.random.choice(range(X_train_orig.shape[0]))
		axes[i][j].set_axis_off()
		axes[i][j].imshow(X_train_orig[k].reshape((28, 28)))
#plt.show()

# Normalize image vectors
X_train = X_train_orig/255.
X_test = X_test_orig/255.

# Convert training and test labels to one hot matrices
label_binrizer = LabelBinarizer()
Y_train = label_binrizer.fit_transform(Y_train_orig)
Y_test = label_binrizer.fit_transform(Y_test_orig)

print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

"""# train the neural network
model = ResNet18(input_shape = (28, 28, 1), classes = 24)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, Y_train, epochs = 2, batch_size = 32)

# test the neural network
preds = model.evaluate(X_test, Y_test)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))"""

#model = load_model('my_model.h5')

#np.argmax(model.predict(m))

377fb5387d7d0127c7d81f0c9de1c5c02bd9d267
<mask token>


def load_dataset():
    train_dataset = pd.read_csv(
        '/content/drive/My Drive/504 Project/Data/sign_mnist_train/sign_mnist_train.csv'
        )
    train_set_y_orig = labels = train_dataset['label'].values
    train_dataset.drop('label', axis=1, inplace=True)
    train_set_x_orig = train_dataset.values
    train_set_x_orig = train_set_x_orig.reshape((train_set_x_orig.shape[0],
        28, 28, 1))
    test_dataset = pd.read_csv(
        '/content/drive/My Drive/504 Project/Data/sign_mnist_test/sign_mnist_test.csv'
        )
    test_set_y_orig = test_dataset['label'].values
    test_dataset.drop('label', axis=1, inplace=True)
    test_set_x_orig = test_dataset.values
    test_set_x_orig = test_set_x_orig.reshape((test_set_x_orig.shape[0], 28,
        28, 1))
    classes = np.array(labels)
    classes = np.unique(classes)
    return (train_set_x_orig, train_set_y_orig, test_set_x_orig,
        test_set_y_orig, classes)


def identity_block(X, filters, stage, block):
    """
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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)
    """
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    F1, F2 = filters
    X_shortcut = X
    X = Conv2D(filters=F1, kernel_size=(3, 3), strides=(1, 1), padding=
        'same', name=conv_name_base + '2a', kernel_initializer=glorot_uniform()
        )(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(filters=F2, kernel_size=(3, 3), strides=(1, 1), padding=
        'same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform()
        )(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    return X


def convolutional_block(X, filters, stage, block):
    """
    Implementation of the convolutional block as defined in Figure 4
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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 convolutional block, tensor of shape (n_H, n_W, n_C)
    """
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    F1, F2 = filters
    X_shortcut = X
    X = Conv2D(F1, (3, 3), strides=(2, 2), padding='same', name=
        conv_name_base + '2a', kernel_initializer=glorot_uniform())(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(F2, (3, 3), strides=(1, 1), padding='same', name=
        conv_name_base + '2b', kernel_initializer=glorot_uniform())(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X_shortcut = Conv2D(F2, (3, 3), strides=(2, 2), padding='same', name=
        conv_name_base + '1', kernel_initializer=glorot_uniform())(X_shortcut)
    X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut
        )
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    return X


<mask token>


def ResNet18(input_shape=(28, 28, 1), classes=24):
    """
    Implementation of the popular ResNet18
    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes
    Returns:
    model -- a Model() instance in Keras
    """
    X = X_input = Input(input_shape)
    X = ZeroPadding2D((3, 3))(X_input)
    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer
        =glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    X = convolutional_block(X, [64, 64], stage=2, block='a')
    X = identity_block(X, [64, 64], stage=2, block='b')
    X = convolutional_block(X, [128, 128], stage=3, block='a')
    X = identity_block(X, [128, 128], stage=3, block='b')
    X = convolutional_block(X, [256, 256], stage=4, block='a')
    X = identity_block(X, [256, 256], stage=4, block='b')
    X = convolutional_block(X, [512, 512], stage=5, block='a')
    X = identity_block(X, [512, 512], stage=5, block='b')
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes),
        kernel_initializer=glorot_uniform(seed=0))(X)
    model = Model(inputs=X_input, outputs=X, name='ResNet18')
    return model


<mask token>

<mask token>
drive.mount('/content/drive')
<mask token>


def load_dataset():
    train_dataset = pd.read_csv(
        '/content/drive/My Drive/504 Project/Data/sign_mnist_train/sign_mnist_train.csv'
        )
    train_set_y_orig = labels = train_dataset['label'].values
    train_dataset.drop('label', axis=1, inplace=True)
    train_set_x_orig = train_dataset.values
    train_set_x_orig = train_set_x_orig.reshape((train_set_x_orig.shape[0],
        28, 28, 1))
    test_dataset = pd.read_csv(
        '/content/drive/My Drive/504 Project/Data/sign_mnist_test/sign_mnist_test.csv'
        )
    test_set_y_orig = test_dataset['label'].values
    test_dataset.drop('label', axis=1, inplace=True)
    test_set_x_orig = test_dataset.values
    test_set_x_orig = test_set_x_orig.reshape((test_set_x_orig.shape[0], 28,
        28, 1))
    classes = np.array(labels)
    classes = np.unique(classes)
    return (train_set_x_orig, train_set_y_orig, test_set_x_orig,
        test_set_y_orig, classes)


def identity_block(X, filters, stage, block):
    """
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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)
    """
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    F1, F2 = filters
    X_shortcut = X
    X = Conv2D(filters=F1, kernel_size=(3, 3), strides=(1, 1), padding=
        'same', name=conv_name_base + '2a', kernel_initializer=glorot_uniform()
        )(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(filters=F2, kernel_size=(3, 3), strides=(1, 1), padding=
        'same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform()
        )(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    return X


def convolutional_block(X, filters, stage, block):
    """
    Implementation of the convolutional block as defined in Figure 4
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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 convolutional block, tensor of shape (n_H, n_W, n_C)
    """
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    F1, F2 = filters
    X_shortcut = X
    X = Conv2D(F1, (3, 3), strides=(2, 2), padding='same', name=
        conv_name_base + '2a', kernel_initializer=glorot_uniform())(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(F2, (3, 3), strides=(1, 1), padding='same', name=
        conv_name_base + '2b', kernel_initializer=glorot_uniform())(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X_shortcut = Conv2D(F2, (3, 3), strides=(2, 2), padding='same', name=
        conv_name_base + '1', kernel_initializer=glorot_uniform())(X_shortcut)
    X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut
        )
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    return X


<mask token>


def ResNet18(input_shape=(28, 28, 1), classes=24):
    """
    Implementation of the popular ResNet18
    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes
    Returns:
    model -- a Model() instance in Keras
    """
    X = X_input = Input(input_shape)
    X = ZeroPadding2D((3, 3))(X_input)
    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer
        =glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    X = convolutional_block(X, [64, 64], stage=2, block='a')
    X = identity_block(X, [64, 64], stage=2, block='b')
    X = convolutional_block(X, [128, 128], stage=3, block='a')
    X = identity_block(X, [128, 128], stage=3, block='b')
    X = convolutional_block(X, [256, 256], stage=4, block='a')
    X = identity_block(X, [256, 256], stage=4, block='b')
    X = convolutional_block(X, [512, 512], stage=5, block='a')
    X = identity_block(X, [512, 512], stage=5, block='b')
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes),
        kernel_initializer=glorot_uniform(seed=0))(X)
    model = Model(inputs=X_input, outputs=X, name='ResNet18')
    return model


<mask token>
K.set_image_data_format('channels_last')
K.set_learning_phase(1)
<mask token>
for i in range(ROWS):
    for j in range(ROWS):
        k = np.random.choice(range(X_train_orig.shape[0]))
        axes[i][j].set_axis_off()
        axes[i][j].imshow(X_train_orig[k].reshape((28, 28)))
<mask token>
print('number of training examples = ' + str(X_train.shape[0]))
print('number of test examples = ' + str(X_test.shape[0]))
print('X_train shape: ' + str(X_train.shape))
print('Y_train shape: ' + str(Y_train.shape))
print('X_test shape: ' + str(X_test.shape))
print('Y_test shape: ' + str(Y_test.shape))
<mask token>

<mask token>
drive.mount('/content/drive')
<mask token>


def load_dataset():
    train_dataset = pd.read_csv(
        '/content/drive/My Drive/504 Project/Data/sign_mnist_train/sign_mnist_train.csv'
        )
    train_set_y_orig = labels = train_dataset['label'].values
    train_dataset.drop('label', axis=1, inplace=True)
    train_set_x_orig = train_dataset.values
    train_set_x_orig = train_set_x_orig.reshape((train_set_x_orig.shape[0],
        28, 28, 1))
    test_dataset = pd.read_csv(
        '/content/drive/My Drive/504 Project/Data/sign_mnist_test/sign_mnist_test.csv'
        )
    test_set_y_orig = test_dataset['label'].values
    test_dataset.drop('label', axis=1, inplace=True)
    test_set_x_orig = test_dataset.values
    test_set_x_orig = test_set_x_orig.reshape((test_set_x_orig.shape[0], 28,
        28, 1))
    classes = np.array(labels)
    classes = np.unique(classes)
    return (train_set_x_orig, train_set_y_orig, test_set_x_orig,
        test_set_y_orig, classes)


def identity_block(X, filters, stage, block):
    """
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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)
    """
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    F1, F2 = filters
    X_shortcut = X
    X = Conv2D(filters=F1, kernel_size=(3, 3), strides=(1, 1), padding=
        'same', name=conv_name_base + '2a', kernel_initializer=glorot_uniform()
        )(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(filters=F2, kernel_size=(3, 3), strides=(1, 1), padding=
        'same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform()
        )(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    return X


def convolutional_block(X, filters, stage, block):
    """
    Implementation of the convolutional block as defined in Figure 4
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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 convolutional block, tensor of shape (n_H, n_W, n_C)
    """
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    F1, F2 = filters
    X_shortcut = X
    X = Conv2D(F1, (3, 3), strides=(2, 2), padding='same', name=
        conv_name_base + '2a', kernel_initializer=glorot_uniform())(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(F2, (3, 3), strides=(1, 1), padding='same', name=
        conv_name_base + '2b', kernel_initializer=glorot_uniform())(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X_shortcut = Conv2D(F2, (3, 3), strides=(2, 2), padding='same', name=
        conv_name_base + '1', kernel_initializer=glorot_uniform())(X_shortcut)
    X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut
        )
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    return X


<mask token>


def ResNet18(input_shape=(28, 28, 1), classes=24):
    """
    Implementation of the popular ResNet18
    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes
    Returns:
    model -- a Model() instance in Keras
    """
    X = X_input = Input(input_shape)
    X = ZeroPadding2D((3, 3))(X_input)
    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer
        =glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    X = convolutional_block(X, [64, 64], stage=2, block='a')
    X = identity_block(X, [64, 64], stage=2, block='b')
    X = convolutional_block(X, [128, 128], stage=3, block='a')
    X = identity_block(X, [128, 128], stage=3, block='b')
    X = convolutional_block(X, [256, 256], stage=4, block='a')
    X = identity_block(X, [256, 256], stage=4, block='b')
    X = convolutional_block(X, [512, 512], stage=5, block='a')
    X = identity_block(X, [512, 512], stage=5, block='b')
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes),
        kernel_initializer=glorot_uniform(seed=0))(X)
    model = Model(inputs=X_input, outputs=X, name='ResNet18')
    return model


<mask token>
K.set_image_data_format('channels_last')
K.set_learning_phase(1)
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()
ROWS = 10
fig, axes = plt.subplots(ROWS, ROWS, figsize=(10, 10))
for i in range(ROWS):
    for j in range(ROWS):
        k = np.random.choice(range(X_train_orig.shape[0]))
        axes[i][j].set_axis_off()
        axes[i][j].imshow(X_train_orig[k].reshape((28, 28)))
X_train = X_train_orig / 255.0
X_test = X_test_orig / 255.0
label_binrizer = LabelBinarizer()
Y_train = label_binrizer.fit_transform(Y_train_orig)
Y_test = label_binrizer.fit_transform(Y_test_orig)
print('number of training examples = ' + str(X_train.shape[0]))
print('number of test examples = ' + str(X_test.shape[0]))
print('X_train shape: ' + str(X_train.shape))
print('Y_train shape: ' + str(Y_train.shape))
print('X_test shape: ' + str(X_test.shape))
print('Y_test shape: ' + str(Y_test.shape))
<mask token>

<mask token>
from google.colab import drive
drive.mount('/content/drive')
import numpy as np
import pandas as pd
import tensorflow as tf
import math
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.initializers import glorot_uniform


def load_dataset():
    train_dataset = pd.read_csv(
        '/content/drive/My Drive/504 Project/Data/sign_mnist_train/sign_mnist_train.csv'
        )
    train_set_y_orig = labels = train_dataset['label'].values
    train_dataset.drop('label', axis=1, inplace=True)
    train_set_x_orig = train_dataset.values
    train_set_x_orig = train_set_x_orig.reshape((train_set_x_orig.shape[0],
        28, 28, 1))
    test_dataset = pd.read_csv(
        '/content/drive/My Drive/504 Project/Data/sign_mnist_test/sign_mnist_test.csv'
        )
    test_set_y_orig = test_dataset['label'].values
    test_dataset.drop('label', axis=1, inplace=True)
    test_set_x_orig = test_dataset.values
    test_set_x_orig = test_set_x_orig.reshape((test_set_x_orig.shape[0], 28,
        28, 1))
    classes = np.array(labels)
    classes = np.unique(classes)
    return (train_set_x_orig, train_set_y_orig, test_set_x_orig,
        test_set_y_orig, classes)


def identity_block(X, filters, stage, block):
    """
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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)
    """
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    F1, F2 = filters
    X_shortcut = X
    X = Conv2D(filters=F1, kernel_size=(3, 3), strides=(1, 1), padding=
        'same', name=conv_name_base + '2a', kernel_initializer=glorot_uniform()
        )(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(filters=F2, kernel_size=(3, 3), strides=(1, 1), padding=
        'same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform()
        )(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    return X


def convolutional_block(X, filters, stage, block):
    """
    Implementation of the convolutional block as defined in Figure 4
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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 convolutional block, tensor of shape (n_H, n_W, n_C)
    """
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    F1, F2 = filters
    X_shortcut = X
    X = Conv2D(F1, (3, 3), strides=(2, 2), padding='same', name=
        conv_name_base + '2a', kernel_initializer=glorot_uniform())(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(F2, (3, 3), strides=(1, 1), padding='same', name=
        conv_name_base + '2b', kernel_initializer=glorot_uniform())(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X_shortcut = Conv2D(F2, (3, 3), strides=(2, 2), padding='same', name=
        conv_name_base + '1', kernel_initializer=glorot_uniform())(X_shortcut)
    X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut
        )
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    return X


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.initializers import glorot_uniform


def ResNet18(input_shape=(28, 28, 1), classes=24):
    """
    Implementation of the popular ResNet18
    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes
    Returns:
    model -- a Model() instance in Keras
    """
    X = X_input = Input(input_shape)
    X = ZeroPadding2D((3, 3))(X_input)
    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer
        =glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    X = convolutional_block(X, [64, 64], stage=2, block='a')
    X = identity_block(X, [64, 64], stage=2, block='b')
    X = convolutional_block(X, [128, 128], stage=3, block='a')
    X = identity_block(X, [128, 128], stage=3, block='b')
    X = convolutional_block(X, [256, 256], stage=4, block='a')
    X = identity_block(X, [256, 256], stage=4, block='b')
    X = convolutional_block(X, [512, 512], stage=5, block='a')
    X = identity_block(X, [512, 512], stage=5, block='b')
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes),
        kernel_initializer=glorot_uniform(seed=0))(X)
    model = Model(inputs=X_input, outputs=X, name='ResNet18')
    return model


import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelBinarizer
import keras.backend as K
K.set_image_data_format('channels_last')
K.set_learning_phase(1)
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()
ROWS = 10
fig, axes = plt.subplots(ROWS, ROWS, figsize=(10, 10))
for i in range(ROWS):
    for j in range(ROWS):
        k = np.random.choice(range(X_train_orig.shape[0]))
        axes[i][j].set_axis_off()
        axes[i][j].imshow(X_train_orig[k].reshape((28, 28)))
X_train = X_train_orig / 255.0
X_test = X_test_orig / 255.0
label_binrizer = LabelBinarizer()
Y_train = label_binrizer.fit_transform(Y_train_orig)
Y_test = label_binrizer.fit_transform(Y_test_orig)
print('number of training examples = ' + str(X_train.shape[0]))
print('number of test examples = ' + str(X_test.shape[0]))
print('X_train shape: ' + str(X_train.shape))
print('Y_train shape: ' + str(Y_train.shape))
print('X_test shape: ' + str(X_test.shape))
print('Y_test shape: ' + str(Y_test.shape))
<mask token>

# -*- coding: utf-8 -*-
"""Copy of ResNet

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1SVTcgc1E5KpIs-UPv9ogh9kYscBUhkLN
"""

#originally coded on google colab
#code borrowed from https://github.com/minhthangdang/SignLanguageRecognitionResNet

from google.colab import drive
drive.mount('/content/drive')

import numpy as np
import pandas as pd
import tensorflow as tf
import math
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.initializers import glorot_uniform

def load_dataset():
	# read train dataset
	train_dataset = pd.read_csv('/content/drive/My Drive/504 Project/Data/sign_mnist_train/sign_mnist_train.csv')
	train_set_y_orig = labels = train_dataset['label'].values # train set labels
	train_dataset.drop('label', axis = 1, inplace = True) # drop the label coloumn from the training set
	train_set_x_orig = train_dataset.values # train set features
	# convert X to (m, n_H, n_W, n_C) where 
	# m is number of examples, n_H is height, n_W is width and n_C is number of channels
	train_set_x_orig = train_set_x_orig.reshape((train_set_x_orig.shape[0], 28, 28, 1))

	# read test dataset
	test_dataset = pd.read_csv('/content/drive/My Drive/504 Project/Data/sign_mnist_test/sign_mnist_test.csv')
	test_set_y_orig = test_dataset['label'].values # test set labels
	test_dataset.drop('label', axis = 1, inplace = True) # drop the label coloumn from the test set
	test_set_x_orig = test_dataset.values # test set features
	# convert X to (m, n_H, n_W, n_C) where 
	# m is number of examples, n_H is height, n_W is width and n_C is number of channels
	test_set_x_orig = test_set_x_orig.reshape((test_set_x_orig.shape[0], 28, 28, 1))

	classes = np.array(labels)
	classes = np.unique(classes)

	return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

def identity_block(X, filters, stage, block):
    """
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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)
    """
    
    # defining name basis
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    
    # Retrieve Filters
    F1, F2 = filters
    
    # Save the input value. You'll need this later to add back to the main path. 
    X_shortcut = X
    
    # First component of main path
    X = Conv2D(filters = F1, kernel_size = (3, 3), strides = (1,1), padding = 'same', name = conv_name_base + '2a', kernel_initializer = glorot_uniform())(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
    X = Activation('relu')(X)
       
    # Second component of main path
    X = Conv2D(filters = F2, kernel_size = (3, 3), strides = (1,1), padding = 'same', name = conv_name_base + '2b', kernel_initializer = glorot_uniform())(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X)

    # Final step: Add shortcut value to main path, and pass it through a RELU activation
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    
    return X

def convolutional_block(X, filters, stage, block):
    """
    Implementation of the convolutional block as defined in Figure 4
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    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 convolutional block, tensor of shape (n_H, n_W, n_C)
    """
    
    # defining name basis
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    
    # Retrieve Filters
    F1, F2 = filters
    
    # Save the input value
    X_shortcut = X

    ##### MAIN PATH #####
    # First component of main path 
    X = Conv2D(F1, (3, 3), strides = (2, 2), padding = 'same', name = conv_name_base + '2a', kernel_initializer = glorot_uniform())(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
    X = Activation('relu')(X)

    # Second component of main path
    X = Conv2D(F2, (3, 3), strides = (1, 1), padding = 'same', name = conv_name_base + '2b', kernel_initializer = glorot_uniform())(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2b')(X)

    ##### SHORTCUT PATH ####
    X_shortcut = Conv2D(F2, (3, 3), strides = (2, 2), padding = 'same', name = conv_name_base + '1', kernel_initializer = glorot_uniform())(X_shortcut)
    X_shortcut = BatchNormalization(axis = 3, name = bn_name_base + '1')(X_shortcut)

    # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    
    return X

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.initializers import glorot_uniform
#from utils_resnet import identity_block, convolutional_block

def ResNet18(input_shape = (28, 28, 1), classes = 24):
    """
    Implementation of the popular ResNet18
    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes
    Returns:
    model -- a Model() instance in Keras
    """
    
    # Define the input as a tensor with shape input_shape
    X = X_input = Input(input_shape)

    
    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)
    
    # Stage 1
    X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3, name = 'bn_conv1')(X)
    X = Activation('relu')(X)
    #X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    # Stage 2
    X = convolutional_block(X, [64, 64], stage=2, block='a')
    X = identity_block(X, [64, 64], stage=2, block='b')

    # Stage 3
    X = convolutional_block(X, [128, 128], stage=3, block='a')
    X = identity_block(X, [128, 128], stage=3, block='b')

    # Stage 4
    X = convolutional_block(X, [256, 256], stage=4, block='a')
    X = identity_block(X, [256, 256], stage=4, block='b')

    # Stage 5
    X = convolutional_block(X, [512, 512], stage=5, block='a')
    X = identity_block(X, [512, 512], stage=5, block='b')

    # AVGPOOL
    # X = AveragePooling2D(pool_size=(2,2), name='avg_pool')(X)

    # output layer
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X)
    
    # Create model
    model = Model(inputs = X_input, outputs = X, name='ResNet18')

    return model

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelBinarizer
#from utils_resnet import load_dataset
#from model import ResNet18

import keras.backend as K
K.set_image_data_format('channels_last')
K.set_learning_phase(1)

X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Example of pictures
ROWS = 10
fig, axes = plt.subplots(ROWS, ROWS, figsize=(10, 10))
for i in range(ROWS):
	for j in range(ROWS):
		k = np.random.choice(range(X_train_orig.shape[0]))
		axes[i][j].set_axis_off()
		axes[i][j].imshow(X_train_orig[k].reshape((28, 28)))
#plt.show()

# Normalize image vectors
X_train = X_train_orig/255.
X_test = X_test_orig/255.

# Convert training and test labels to one hot matrices
label_binrizer = LabelBinarizer()
Y_train = label_binrizer.fit_transform(Y_train_orig)
Y_test = label_binrizer.fit_transform(Y_test_orig)

print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

"""# train the neural network
model = ResNet18(input_shape = (28, 28, 1), classes = 24)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, Y_train, epochs = 2, batch_size = 32)

# test the neural network
preds = model.evaluate(X_test, Y_test)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))"""

#model = load_model('my_model.h5')

#np.argmax(model.predict(m))
