{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Convolutional Neural Networks (CNN)\n<font color='blue'>\n<br>Content: \n* [Loading the Data Set](#1)\n* [Normalization, Reshape and Label Encoding ](#2)\n* [Train Test Split](#3)\n* [Convolutional Neural Network](#4)\n    * [What is Convolution Operation?](#5)\n    * [Same Padding](#6)\n    * [Max Pooling](#7)\n    * [Flattening](#8)\n    * [Full Connection](#9)\n* [Implementing with Keras](#10)\n    * [Create Model](#11)\n    * [Define Optimizer](#12)\n    * [Compile Model](#13)\n    * [Epochs and Batch Size](#14)\n    * [Data Augmentation](#15)\n    * [Fit the Model](#16)\n    * [Evaluate the Model](#17)\n* [Deep Learning Tutorial for Beginners](https://www.kaggle.com/kanncaa1/deep-learning-tutorial-for-beginners)\n* [Artificial Neural Network with Pytorch](https://www.kaggle.com/kanncaa1/pytorch-tutorial-for-deep-learning-lovers)\n* [Convolutional Neural Network with Pytorch](https://www.kaggle.com/kanncaa1/pytorch-tutorial-for-deep-learning-lovers)\n* [Recurrent Neural Network with Pytorch](https://www.kaggle.com/kanncaa1/recurrent-neural-network-with-pytorch)\n* [Conclusion](#18)\n","metadata":{"_uuid":"8c80a83f7fb1ab87aabbdad97d11627fb0380e8f"}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra     \nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n# import warnings\nimport warnings\n# filter warnings\nwarnings.filterwarnings('ignore')\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-26T22:15:28.255966Z","iopub.execute_input":"2023-03-26T22:15:28.256515Z","iopub.status.idle":"2023-03-26T22:15:29.211087Z","shell.execute_reply.started":"2023-03-26T22:15:28.256328Z","shell.execute_reply":"2023-03-26T22:15:29.209111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n## Loading the Data Set\n* In this part we load and visualize the data.","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"}},{"cell_type":"code","source":"# read train \ntrain = pd.read_csv(\"../input/train.csv\")\nprint(train.shape)\ntrain.head()","metadata":{"_uuid":"6884db4361a209f639cd6f3bf15231d271ecf563","execution":{"iopub.status.busy":"2023-03-26T22:15:29.213337Z","iopub.execute_input":"2023-03-26T22:15:29.213793Z","iopub.status.idle":"2023-03-26T22:15:32.996157Z","shell.execute_reply.started":"2023-03-26T22:15:29.213710Z","shell.execute_reply":"2023-03-26T22:15:32.995093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read test \ntest= pd.read_csv(\"../input/test.csv\")\nprint(test.shape)\ntest.head()","metadata":{"_uuid":"d7ab3a0db0c87aef2bb65fae5f564ff4b1260a96","execution":{"iopub.status.busy":"2023-03-26T22:15:32.997813Z","iopub.execute_input":"2023-03-26T22:15:32.998162Z","iopub.status.idle":"2023-03-26T22:15:35.452862Z","shell.execute_reply.started":"2023-03-26T22:15:32.998085Z","shell.execute_reply":"2023-03-26T22:15:35.451461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# put labels into y_train variable\nY_train = train[\"label\"]\n# Drop 'label' column\nX_train = train.drop(labels = [\"label\"],axis = 1) ","metadata":{"_uuid":"318d94f6e935a6d8210d0ac03707dfa6e3946475","execution":{"iopub.status.busy":"2023-03-26T22:15:35.454325Z","iopub.execute_input":"2023-03-26T22:15:35.454700Z","iopub.status.idle":"2023-03-26T22:15:35.589667Z","shell.execute_reply.started":"2023-03-26T22:15:35.454640Z","shell.execute_reply":"2023-03-26T22:15:35.588322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize number of digits classes\nplt.figure(figsize=(15,7))\ng = sns.countplot(Y_train, palette=\"icefire\")\nplt.title(\"Number of digit classes\")\nY_train.value_counts()","metadata":{"_uuid":"8902b0312e6c047596cf27ebba554a68b82604b2","execution":{"iopub.status.busy":"2023-03-26T22:15:35.591654Z","iopub.execute_input":"2023-03-26T22:15:35.592000Z","iopub.status.idle":"2023-03-26T22:15:35.834572Z","shell.execute_reply.started":"2023-03-26T22:15:35.591934Z","shell.execute_reply":"2023-03-26T22:15:35.833921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot some samples\nimg = X_train.iloc[0].as_matrix()\nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[0,0])\nplt.axis(\"off\")\nplt.show()","metadata":{"_uuid":"4c9d3a1467cf82a3dff04967a9846906f9758ed4","execution":{"iopub.status.busy":"2023-03-26T22:15:35.835660Z","iopub.execute_input":"2023-03-26T22:15:35.835900Z","iopub.status.idle":"2023-03-26T22:15:35.922483Z","shell.execute_reply.started":"2023-03-26T22:15:35.835857Z","shell.execute_reply":"2023-03-26T22:15:35.921047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot some samples\nimg = X_train.iloc[3].as_matrix()\nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[3,0])\nplt.axis(\"off\")\nplt.show()","metadata":{"_uuid":"d6b119ecbe774a45656d1d157f9b33f38adf8e96","execution":{"iopub.status.busy":"2023-03-26T22:15:35.924079Z","iopub.execute_input":"2023-03-26T22:15:35.924409Z","iopub.status.idle":"2023-03-26T22:15:36.004253Z","shell.execute_reply.started":"2023-03-26T22:15:35.924336Z","shell.execute_reply":"2023-03-26T22:15:36.003169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n## Normalization, Reshape and Label Encoding \n* Normalization\n    * We perform a grayscale normalization to reduce the effect of illumination's differences.\n    * If we perform normalization, CNN works faster.\n* Reshape\n    * Train and test images (28 x 28) \n    * We reshape all data to 28x28x1 3D matrices.\n    * Keras needs an extra dimension in the end which correspond to channels. Our images are gray scaled so it use only one channel. \n* Label Encoding  \n    * Encode labels to one hot vectors \n        * 2 => [0,0,1,0,0,0,0,0,0,0]\n        * 4 => [0,0,0,0,1,0,0,0,0,0]","metadata":{"_uuid":"74e341f4845b42101182eda3c990e3dc4dc64dff"}},{"cell_type":"code","source":"# Normalize the data\nX_train = X_train / 255.0\ntest = test / 255.0\nprint(\"x_train shape: \",X_train.shape)\nprint(\"test shape: \",test.shape)","metadata":{"_uuid":"f39e537f15757b5da3363c138a33e50bd78bbf49","execution":{"iopub.status.busy":"2023-03-26T22:15:36.005553Z","iopub.execute_input":"2023-03-26T22:15:36.005790Z","iopub.status.idle":"2023-03-26T22:15:36.469392Z","shell.execute_reply.started":"2023-03-26T22:15:36.005741Z","shell.execute_reply":"2023-03-26T22:15:36.467535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape\nX_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\nprint(\"x_train shape: \",X_train.shape)\nprint(\"test shape: \",test.shape)","metadata":{"_uuid":"d524f6de086928158a76c54d1685a92c1802e230","execution":{"iopub.status.busy":"2023-03-26T22:15:36.471875Z","iopub.execute_input":"2023-03-26T22:15:36.472206Z","iopub.status.idle":"2023-03-26T22:15:36.481526Z","shell.execute_reply.started":"2023-03-26T22:15:36.472155Z","shell.execute_reply":"2023-03-26T22:15:36.479753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.unique()","metadata":{"execution":{"iopub.status.busy":"2023-03-26T22:15:36.482546Z","iopub.execute_input":"2023-03-26T22:15:36.482795Z","iopub.status.idle":"2023-03-26T22:15:36.493830Z","shell.execute_reply.started":"2023-03-26T22:15:36.482740Z","shell.execute_reply":"2023-03-26T22:15:36.492339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Label Encoding \nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nY_train = to_categorical(Y_train, num_classes = 10)","metadata":{"_uuid":"d15d35ca439dce194a96f4442c7a1c085ce24d28","execution":{"iopub.status.busy":"2023-03-26T22:15:36.495955Z","iopub.execute_input":"2023-03-26T22:15:36.496362Z","iopub.status.idle":"2023-03-26T22:15:36.630388Z","shell.execute_reply.started":"2023-03-26T22:15:36.496282Z","shell.execute_reply":"2023-03-26T22:15:36.629474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-26T22:15:36.631673Z","iopub.execute_input":"2023-03-26T22:15:36.631974Z","iopub.status.idle":"2023-03-26T22:15:36.641658Z","shell.execute_reply.started":"2023-03-26T22:15:36.631914Z","shell.execute_reply":"2023-03-26T22:15:36.640004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train[:10]","metadata":{"execution":{"iopub.status.busy":"2023-03-26T22:15:36.643292Z","iopub.execute_input":"2023-03-26T22:15:36.643607Z","iopub.status.idle":"2023-03-26T22:15:36.652482Z","shell.execute_reply.started":"2023-03-26T22:15:36.643538Z","shell.execute_reply":"2023-03-26T22:15:36.651073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n## Train Test Split\n* We split the data into train and test sets.\n* test size is 10%.\n* train size is 90%.","metadata":{"_uuid":"111d4a1e66d823363f890f892bba39d692479663"}},{"cell_type":"code","source":"# Split the train and the validation set for the fitting\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2)\nprint(\"x_train shape\",X_train.shape)\nprint(\"x_test shape\",X_val.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_test shape\",Y_val.shape)","metadata":{"_uuid":"882d9dfb1bee94b2ec1e83911e3e334994d74f4b","execution":{"iopub.status.busy":"2023-03-26T22:15:36.653630Z","iopub.execute_input":"2023-03-26T22:15:36.653857Z","iopub.status.idle":"2023-03-26T22:15:37.425927Z","shell.execute_reply.started":"2023-03-26T22:15:36.653816Z","shell.execute_reply":"2023-03-26T22:15:37.424903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Some examples\nplt.imshow(X_train[2][:,:,0],cmap='gray')\nplt.show()","metadata":{"_uuid":"1cdc57350e1c251ec583093ebba18c51a537f55f","execution":{"iopub.status.busy":"2023-03-26T22:15:37.427220Z","iopub.execute_input":"2023-03-26T22:15:37.427532Z","iopub.status.idle":"2023-03-26T22:15:37.544710Z","shell.execute_reply.started":"2023-03-26T22:15:37.427468Z","shell.execute_reply":"2023-03-26T22:15:37.543679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n## Convolutional Neural Network \n* CNN is used for image classification, object detection \n* <a href=\"https://ibb.co/kV1j9p\"><img src=\"https://preview.ibb.co/nRkBpp/gec2.jpg\" alt=\"gec2\" border=\"0\"></a>","metadata":{"_uuid":"caf4b9f7fcae153ac1d4c0246b1defc9d9d30776"}},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n### What is Convolution Operation?\n* We have some image and feature detector(3*3)\n* Feature detector does not need to be 3 by 3 matrix. It can be 5 by 5 or 7 by 7.\n* Feature detector = kernel = filter\n* Feauture detector detects features like edges or convex shapes. Example, if out input is dog, feature detector can detect features like ear or tail of the dog.\n* feature map = conv(input image, feature detector). Element wise multiplication of matrices.\n* feature map = convolved feature\n* Stride = navigating in input image.\n* We reduce the size of image. This is important bc code runs faster. However, we lost information. \n* We create multiple feature maps bc we use multiple feature detectors(filters).\n* Lets look at gimp. Edge detect: [0,10,0],[10,-4,10],[0,10,0]\n* <a href=\"https://imgbb.com/\"><img src=\"https://image.ibb.co/m4FQC9/gec.jpg\" alt=\"gec\" border=\"0\"></a>\n* After having convolution layer we use ReLU to break up linearity. Increase nonlinearity. Because images are non linear.\n* <a href=\"https://ibb.co/mVZih9\"><img src=\"https://preview.ibb.co/gbcQvU/RELU.jpg\" alt=\"RELU\" border=\"0\"></a>","metadata":{"_uuid":"682cb6f5144794038c0bef1dc0814dde0f7fe6c0"}},{"cell_type":"markdown","source":"<a id=\"6\"></a>\n### Same Padding\n* As we keep applying conv layers, the size of the volume will decrease faster than we would like. In the early layers of our network, we want to preserve as much information about the original input volume so that we can extract those low level features.\n* input size and output size are same.\n* <a href=\"https://ibb.co/jUPkUp\"><img src=\"https://preview.ibb.co/noH5Up/padding.jpg\" alt=\"padding\" border=\"0\"></a>","metadata":{"_uuid":"3af8fb49243719ad70016b64cd4dff6e62c413d9"}},{"cell_type":"markdown","source":"<a id=\"7\"></a>\n### Max Pooling\n* It makes down-sampling or sub-sampling (Reduces the number of parameters)\n* It makes the detection of features invariant to scale or orientation changes.\n* It reduce the amount of parameters and computation in the network, and hence to also control overfitting. \n* <a href=\"https://ibb.co/ckTjN9\"><img src=\"https://preview.ibb.co/gsNYFU/maxpool.jpg\" alt=\"maxpool\" border=\"0\"></a>","metadata":{"_uuid":"6694b8ea366f974d6bd055a2915a5dc06f7a96bb"}},{"cell_type":"markdown","source":"<a id=\"8\"></a>\n### Flattening\n* <a href=\"https://imgbb.com/\"><img src=\"https://image.ibb.co/c7eVvU/flattenigng.jpg\" alt=\"flattenigng\" border=\"0\"></a>","metadata":{"_uuid":"c2fb971fc473c9104a27975242a36f8e9183742c"}},{"cell_type":"markdown","source":"<a id=\"9\"></a>\n### Full Connection\n* Neurons in a fully connected layer have connections to all activations in the previous layer\n* Artificial Neural Network\n* <a href=\"https://ibb.co/hsS14p\"><img src=\"https://preview.ibb.co/evzsAU/fullyc.jpg\" alt=\"fullyc\" border=\"0\"></a>","metadata":{"_uuid":"f437407cf18fdfacf8626baa5e76ed5d64be1a11","trusted":true}},{"cell_type":"markdown","source":"<a id=\"10\"></a>\n## Implementing with Keras","metadata":{"_uuid":"7b290eb3f3b111d6841f8e4093277be1bde05078","trusted":true}},{"cell_type":"markdown","source":"<a id=\"11\"></a>\n### Create Model\n* conv => max pool => dropout => conv => max pool => dropout => fully connected (2 layer)\n* Dropout: Dropout is a technique where randomly selected neurons are ignored during training\n* <a href=\"https://ibb.co/jGcvVU\"><img src=\"https://preview.ibb.co/e7yPPp/dropout.jpg\" alt=\"dropout\" border=\"0\"></a>","metadata":{"_uuid":"fd9feb4c4dadbb8a5cc8a32b5ef582d41b5c0698","trusted":true}},{"cell_type":"code","source":"# \nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\nmodel = Sequential()\n#\nmodel.add(Conv2D(filters = 8, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n#\nmodel.add(Conv2D(filters = 16, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n# fully connected\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","metadata":{"_uuid":"c441d7b3852cee5d3636272d4da2f96b169f81ac","execution":{"iopub.status.busy":"2023-03-26T22:15:37.548615Z","iopub.execute_input":"2023-03-26T22:15:37.548899Z","iopub.status.idle":"2023-03-26T22:15:37.749826Z","shell.execute_reply.started":"2023-03-26T22:15:37.548841Z","shell.execute_reply":"2023-03-26T22:15:37.748779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"12\"></a>\n### Define Optimizer   \n* Adam optimizer: Change the learning rate\n","metadata":{"_uuid":"9a432df491777ca0019db6f6b972581f2c5bd9f9"}},{"cell_type":"code","source":"# Define the optimizer\noptimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)","metadata":{"_uuid":"607a02b42636e3115a1ac7a8edcadf61cf5ea1b0","execution":{"iopub.status.busy":"2023-03-26T22:15:37.751119Z","iopub.execute_input":"2023-03-26T22:15:37.751415Z","iopub.status.idle":"2023-03-26T22:15:37.785648Z","shell.execute_reply.started":"2023-03-26T22:15:37.751360Z","shell.execute_reply":"2023-03-26T22:15:37.783551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"13\"></a>\n### Compile Model\n* categorical crossentropy\n* We make binary cross entropy at previous parts and in machine learning tutorial\n* At this time we use categorical crossentropy. That means that we have multi class.\n* <a href=\"https://ibb.co/jm1bpp\"><img src=\"https://preview.ibb.co/nN3ZaU/cce.jpg\" alt=\"cce\" border=\"0\"></a>\n","metadata":{"_uuid":"91bf542a979c5b191c534876186bf31e70ec7f06"}},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"_uuid":"0d1eefc68470b4cdcec04c2570651da3d97676d0","execution":{"iopub.status.busy":"2023-03-26T22:15:37.787492Z","iopub.execute_input":"2023-03-26T22:15:37.787859Z","iopub.status.idle":"2023-03-26T22:15:37.829805Z","shell.execute_reply.started":"2023-03-26T22:15:37.787770Z","shell.execute_reply":"2023-03-26T22:15:37.828694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"14\"></a>\n### Epochs and Batch Size\n* Say you have a dataset of 10 examples (or samples). You have a **batch size** of 2, and you've specified you want the algorithm to run for 3 **epochs**. Therefore, in each epoch, you have 5 **batches** (10/2 = 5). Each batch gets passed through the algorithm, therefore you have 5 iterations **per epoch**.\n* reference: https://stackoverflow.com/questions/4752626/epoch-vs-iteration-when-training-neural-networks","metadata":{"_uuid":"d5d8fea4cbb9b53cf1bb5089357a33dc179ee981"}},{"cell_type":"code","source":"epochs = 10  # for better result increase the epochs\nbatch_size = 250","metadata":{"_uuid":"a237feb5e53ecbc8799101cb6e699877faafde77","execution":{"iopub.status.busy":"2023-03-26T22:15:37.831025Z","iopub.execute_input":"2023-03-26T22:15:37.831259Z","iopub.status.idle":"2023-03-26T22:15:37.835909Z","shell.execute_reply.started":"2023-03-26T22:15:37.831201Z","shell.execute_reply":"2023-03-26T22:15:37.834544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"15\"></a>\n### Data Augmentation\n* To avoid overfitting problem, we need to expand artificially our handwritten digit dataset\n* Alter the training data with small transformations to reproduce the variations of digit.\n* For example, the number is not centered The scale is not the same (some who write with big/small numbers) The image is rotated.\n* <a href=\"https://ibb.co/k24CUp\"><img src=\"https://preview.ibb.co/nMxXUp/augment.jpg\" alt=\"augment\" border=\"0\"></a>\n    \n","metadata":{"_uuid":"e87c60e45759e0c52c2b72ee562fdb964c8008d5"}},{"cell_type":"code","source":"# data augmentation\ndatagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # dimesion reduction\n        rotation_range=5,  # randomly rotate images in the range 5 degrees\n        zoom_range = 0.1, # Randomly zoom image 10%\n        width_shift_range=0.1,  # randomly shift images horizontally 10%\n        height_shift_range=0.1,  # randomly shift images vertically 10%\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\ndatagen.fit(X_train)","metadata":{"_uuid":"c339fa7d06e9b73a519ca661bed19de482707d1f","execution":{"iopub.status.busy":"2023-03-26T22:15:37.837229Z","iopub.execute_input":"2023-03-26T22:15:37.837500Z","iopub.status.idle":"2023-03-26T22:15:37.991141Z","shell.execute_reply.started":"2023-03-26T22:15:37.837449Z","shell.execute_reply":"2023-03-26T22:15:37.990065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"16\"></a>\n### Fit the model","metadata":{"_uuid":"e42e43f1b3b9e6da0d99dafff8f4a4b514a07f4c"}},{"cell_type":"code","source":"# Fit the model\nhistory = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (X_val,Y_val), steps_per_epoch=X_train.shape[0] // batch_size)","metadata":{"_uuid":"4b2957bb8976a25cdbbbdc3110d68c5035a9773c","execution":{"iopub.status.busy":"2023-03-26T22:15:37.992405Z","iopub.execute_input":"2023-03-26T22:15:37.992806Z","iopub.status.idle":"2023-03-26T22:18:30.931721Z","shell.execute_reply.started":"2023-03-26T22:15:37.992748Z","shell.execute_reply":"2023-03-26T22:18:30.930718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"17\"></a>\n### Evaluate the model\n* Test Loss visualization\n* Confusion matrix\n","metadata":{"_uuid":"debd5424728e11aa30a8513ac7b4f7377193a2da"}},{"cell_type":"code","source":"# Plot the loss and accuracy curves for training and validation \nplt.plot(history.history['val_loss'], color='b', label=\"validation loss\")\nplt.title(\"Test Loss\")\nplt.xlabel(\"Number of Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"_uuid":"180a06f7ae01e69117c6c8258411cfe1b9b7b991","execution":{"iopub.status.busy":"2023-03-26T22:18:30.932967Z","iopub.execute_input":"2023-03-26T22:18:30.933284Z","iopub.status.idle":"2023-03-26T22:18:31.086870Z","shell.execute_reply.started":"2023-03-26T22:18:30.933201Z","shell.execute_reply":"2023-03-26T22:18:31.086165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix\nimport seaborn as sns\n# Predict the values from the validation dataset\nY_pred = model.predict(X_val)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(Y_val,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# plot the confusion matrix\nf,ax = plt.subplots(figsize=(8, 8))\nsns.heatmap(confusion_mtx, annot=True, linewidths=0.01,cmap=\"Greens\",linecolor=\"gray\", fmt= '.1f',ax=ax)\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"_uuid":"6586e37bd470db822086e191a90388e7175d504f","execution":{"iopub.status.busy":"2023-03-26T22:18:31.088109Z","iopub.execute_input":"2023-03-26T22:18:31.088567Z","iopub.status.idle":"2023-03-26T22:18:32.548632Z","shell.execute_reply.started":"2023-03-26T22:18:31.088517Z","shell.execute_reply":"2023-03-26T22:18:32.547924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"18\"></a>\n## Conclusion\n* http://scs.ryerson.ca/~aharley/vis/conv/flat.html\n* HW\n* If you have any question I will be very happy to hear it.","metadata":{"_uuid":"9409bc6a7e905d5887c8eec8d056c9f015bf4878"}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}