{"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":"# **CNN with filters visualization per layer in Tensorflow**\n\nMNIST Data of handwritten digits           |  Convolutional filter visualization\n:-------------------------:|:-------------------------:\n![](https://lucidar.me/en/matlab/files/matlab-mnist.png)  |  ![](https://i.imgur.com/t3RiF1J.png)\n\n\n## **Index:**\n\n- [Importing necessary libraries](#import)\n- [Importing the data](#data)\n- [Data Pre-processing](#preprocess)\n    - [Splitting data into x (Values) and y (labels)](#split)\n    - [Null Values](#null)\n    - [Normalization of data](#normalize)\n- [Data Visualization](#visualize)\n- [Neural Network](#nn)\n    - [Defining the model](#define)\n    - [Plotting the model](#plot)\n    - [Compiling & Fitting the model](#fit)\n    - [Visualizing the response of the filters on a prediction](#conv)\n- [Submission](#submit)","metadata":{}},{"cell_type":"markdown","source":"\n## **Importing necessary libraries** <a id=\"import\"></a>","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport keras\nimport pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:07:09.839375Z","iopub.execute_input":"2022-07-12T00:07:09.840154Z","iopub.status.idle":"2022-07-12T00:07:14.798151Z","shell.execute_reply.started":"2022-07-12T00:07:09.840048Z","shell.execute_reply":"2022-07-12T00:07:14.797040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Importing the data** <a id=\"data\"></a>","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:07:20.707537Z","iopub.execute_input":"2022-07-12T00:07:20.708522Z","iopub.status.idle":"2022-07-12T00:07:20.718294Z","shell.execute_reply.started":"2022-07-12T00:07:20.708483Z","shell.execute_reply":"2022-07-12T00:07:20.717140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataTrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndataTrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:07:22.994051Z","iopub.execute_input":"2022-07-12T00:07:22.995300Z","iopub.status.idle":"2022-07-12T00:07:26.322725Z","shell.execute_reply.started":"2022-07-12T00:07:22.995254Z","shell.execute_reply":"2022-07-12T00:07:26.321752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataTest = pd.read_csv('../input/digit-recognizer/test.csv')\ndataTest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:07:29.164832Z","iopub.execute_input":"2022-07-12T00:07:29.165905Z","iopub.status.idle":"2022-07-12T00:07:31.412102Z","shell.execute_reply.started":"2022-07-12T00:07:29.165849Z","shell.execute_reply":"2022-07-12T00:07:31.411130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Data Pre-processing** <a id=\"preprocess\"></a>","metadata":{}},{"cell_type":"markdown","source":"### ***Splitting data into x (Values) and y (labels)*** <a id=\"split\"></a>","metadata":{}},{"cell_type":"code","source":"xTrain = dataTrain.drop(['label'], axis=1).astype('float32').values.reshape(-1,28,28,1)\nyTrain = dataTrain['label'].astype('int32')\nxTest = dataTest.values.reshape(-1,28,28,1)\n\nxTrain.shape, yTrain.shape, xTest.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:07:32.797278Z","iopub.execute_input":"2022-07-12T00:07:32.797685Z","iopub.status.idle":"2022-07-12T00:07:32.998728Z","shell.execute_reply.started":"2022-07-12T00:07:32.797624Z","shell.execute_reply":"2022-07-12T00:07:32.995607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ***Null values*** <a id=\"null\"></a>","metadata":{}},{"cell_type":"code","source":"print(\"Total Null values in Training data: \", sum(dataTrain.isnull().sum()), \"\\nTotal Null values in Testing data: \", sum(dataTest.isnull().sum()))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T20:37:45.493223Z","iopub.execute_input":"2022-07-11T20:37:45.493631Z","iopub.status.idle":"2022-07-11T20:37:45.583350Z","shell.execute_reply.started":"2022-07-11T20:37:45.493599Z","shell.execute_reply":"2022-07-11T20:37:45.581927Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Since we dont have any null vlaue on the dataset, we don't have to deal with them :)","metadata":{}},{"cell_type":"markdown","source":"### ***Normalization of data*** <a id=\"normalize\"></a>","metadata":{}},{"cell_type":"code","source":"print('Max value in Training data:', np.amax(xTrain), 'Min value in Training data:', np.amin(xTrain))\nprint('Max value in Testing data:', np.amax(xTest), 'Min value in Testing data:', np.amin(xTest))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T20:56:44.429339Z","iopub.execute_input":"2022-07-11T20:56:44.429786Z","iopub.status.idle":"2022-07-11T20:56:44.512920Z","shell.execute_reply.started":"2022-07-11T20:56:44.429753Z","shell.execute_reply":"2022-07-11T20:56:44.511656Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### The data ranges from 0 to 255. So we will normalize the data by dividing each pixel value by 255","metadata":{}},{"cell_type":"code","source":"xTrain = xTrain/255\nxTest = xTest/255","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:07:37.465584Z","iopub.execute_input":"2022-07-12T00:07:37.466302Z","iopub.status.idle":"2022-07-12T00:07:37.583544Z","shell.execute_reply.started":"2022-07-12T00:07:37.466262Z","shell.execute_reply":"2022-07-12T00:07:37.582489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ***Data Visualization*** <a id=\"visualize\"></a>","metadata":{}},{"cell_type":"code","source":"dataTrain.info()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataTrain.describe()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Sample data instance**","metadata":{}},{"cell_type":"code","source":"index = np.random.randint(1, xTrain.shape[0]+1)\nplt.imshow(xTrain[index][:,:,0], )\nplt.title(yTrain[index]);\nprint('yTrain Label: ', yTrain[index])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:07:39.896824Z","iopub.execute_input":"2022-07-12T00:07:39.897624Z","iopub.status.idle":"2022-07-12T00:07:40.120092Z","shell.execute_reply.started":"2022-07-12T00:07:39.897572Z","shell.execute_reply":"2022-07-12T00:07:40.118916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ***Neural Network*** <a id=\"nn\"></a>","metadata":{}},{"cell_type":"markdown","source":"#### **Defining the model** <a id=\"define\"></a>","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\ntf.keras.layers.Conv2D(32, (2, 2), activation='relu', input_shape=xTrain.shape[1:]),\ntf.keras.layers.Activation(\"relu\"),\ntf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\ntf.keras.layers.Conv2D(32, (4, 4), activation='relu'),\ntf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\ntf.keras.layers.Flatten(),\ntf.keras.layers.Dense(128, activation='relu'),\ntf.keras.layers.Dense(10, activation='softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:08:00.584616Z","iopub.execute_input":"2022-07-12T00:08:00.585037Z","iopub.status.idle":"2022-07-12T00:08:00.639776Z","shell.execute_reply.started":"2022-07-12T00:08:00.585004Z","shell.execute_reply":"2022-07-12T00:08:00.638683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Plotting the model** <a id=\"plot\"></a>","metadata":{}},{"cell_type":"code","source":"tf.keras.utils.plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:08:04.304166Z","iopub.execute_input":"2022-07-12T00:08:04.305397Z","iopub.status.idle":"2022-07-12T00:08:04.594841Z","shell.execute_reply.started":"2022-07-12T00:08:04.305345Z","shell.execute_reply":"2022-07-12T00:08:04.593610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Compiling & Fitting the model** <a id=\"fit\"></a>","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam', loss= 'sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(xTrain, yTrain, epochs=15)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:08:19.696415Z","iopub.execute_input":"2022-07-12T00:08:19.696957Z","iopub.status.idle":"2022-07-12T00:09:42.281725Z","shell.execute_reply.started":"2022-07-12T00:08:19.696910Z","shell.execute_reply":"2022-07-12T00:09:42.280685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Visualizing the response of the filters on a prediction** <a id=\"conv\"></a>","metadata":{}},{"cell_type":"code","source":"layer_outputs = [layer.output for layer in model.layers[1:7]]\nactivation_model = tf.keras.models.Model(inputs=model.input,outputs=layer_outputs)\nactivations = activation_model.predict(xTrain[20].reshape(1, 28, 28, 1))\nlayer_names = []\nfor layer in model.layers[1:7]:\n    layer_names.append(layer.name)\n    \nimages_per_row = 16\nfor layer_name, layer_activation in zip(layer_names, activations):\n    n_features = layer_activation.shape[-1] \n    size = layer_activation.shape[1] \n    n_cols = n_features // images_per_row\n    display_grid = np.zeros((size * n_cols, images_per_row * size))\n    for col in range(n_cols):\n        for row in range(images_per_row):\n            try:\n                channel_image = layer_activation[0,:, :,col * images_per_row + row]\n                channel_image -= channel_image.mean()\n                channel_image /= channel_image.std()\n                channel_image *= 64\n                channel_image += 128\n                channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n                display_grid[col * size : (col + 1) * size, row * size : (row + 1) * size] = channel_image\n            except:\n                continue\n    scale = 1. / size\n    plt.figure(figsize=(scale * display_grid.shape[1],\n                        scale * display_grid.shape[0]))\n    plt.title(layer_name)\n    plt.grid(False)\n    plt.imshow(display_grid, aspect='auto', cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T00:11:56.733154Z","iopub.execute_input":"2022-07-12T00:11:56.733705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Submission** <a id=\"submit\"></a>","metadata":{}},{"cell_type":"code","source":"yTest = model.predict(xTest)\n\nyTest = np.argmax(yTest, axis=1)\nplt.imshow(xTest[2])\nf'Predicted label is: {yTest[2]}'","metadata":{"execution":{"iopub.status.busy":"2022-07-11T22:52:48.360824Z","iopub.execute_input":"2022-07-11T22:52:48.361644Z","iopub.status.idle":"2022-07-11T22:52:51.963292Z","shell.execute_reply.started":"2022-07-11T22:52:48.361605Z","shell.execute_reply":"2022-07-11T22:52:51.962003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': list(range(1, len(yTest)+1)), 'Label': yTest})\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T22:52:55.172730Z","iopub.execute_input":"2022-07-11T22:52:55.173592Z","iopub.status.idle":"2022-07-11T22:52:55.199384Z","shell.execute_reply.started":"2022-07-11T22:52:55.173543Z","shell.execute_reply":"2022-07-11T22:52:55.198233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\nprint('Submission successful!')","metadata":{},"execution_count":null,"outputs":[]}]}