{"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":"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-19T20:04:22.585651Z","iopub.execute_input":"2023-09-19T20:04:22.586606Z","iopub.status.idle":"2023-09-19T20:04:22.600160Z","shell.execute_reply.started":"2023-09-19T20:04:22.586572Z","shell.execute_reply":"2023-09-19T20:04:22.599148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nimport pandas as pd\nimport numpy as np\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import confusion_matrix, classification_report\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:23.096581Z","iopub.execute_input":"2023-09-19T20:04:23.096954Z","iopub.status.idle":"2023-09-19T20:04:23.102108Z","shell.execute_reply.started":"2023-09-19T20:04:23.096925Z","shell.execute_reply":"2023-09-19T20:04:23.100845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32  \nnum_classes = 10  \nepochs = 100","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:23.132116Z","iopub.execute_input":"2023-09-19T20:04:23.132383Z","iopub.status.idle":"2023-09-19T20:04:23.136658Z","shell.execute_reply.started":"2023-09-19T20:04:23.132360Z","shell.execute_reply":"2023-09-19T20:04:23.135743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.datasets import cifar10","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:23.160904Z","iopub.execute_input":"2023-09-19T20:04:23.161557Z","iopub.status.idle":"2023-09-19T20:04:23.167472Z","shell.execute_reply.started":"2023-09-19T20:04:23.161527Z","shell.execute_reply":"2023-09-19T20:04:23.166581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:23.194478Z","iopub.execute_input":"2023-09-19T20:04:23.195017Z","iopub.status.idle":"2023-09-19T20:04:24.241108Z","shell.execute_reply.started":"2023-09-19T20:04:23.194984Z","shell.execute_reply":"2023-09-19T20:04:24.240049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('X_train Shape: {}'.format(X_train.shape))\nprint('X_test Shape: {}'.format(X_test.shape))\nprint('Y_train Shape: {}'.format(Y_train.shape))\nprint('Y_test Shape: {}'.format(Y_test.shape))","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:24.243237Z","iopub.execute_input":"2023-09-19T20:04:24.243583Z","iopub.status.idle":"2023-09-19T20:04:24.251893Z","shell.execute_reply.started":"2023-09-19T20:04:24.243551Z","shell.execute_reply":"2023-09-19T20:04:24.250807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalizing","metadata":{}},{"cell_type":"code","source":"X_train = X_train/255\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:24.253295Z","iopub.execute_input":"2023-09-19T20:04:24.253713Z","iopub.status.idle":"2023-09-19T20:04:24.736272Z","shell.execute_reply.started":"2023-09-19T20:04:24.253681Z","shell.execute_reply":"2023-09-19T20:04:24.735235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"One-Hot-Encoding","metadata":{}},{"cell_type":"code","source":"Y_train_en = tf.keras.utils.to_categorical(Y_train,10)\nY_test_en = tf.keras.utils.to_categorical(Y_test,10)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:24.739159Z","iopub.execute_input":"2023-09-19T20:04:24.739555Z","iopub.status.idle":"2023-09-19T20:04:24.745613Z","shell.execute_reply.started":"2023-09-19T20:04:24.739519Z","shell.execute_reply":"2023-09-19T20:04:24.744490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:24.747222Z","iopub.execute_input":"2023-09-19T20:04:24.747659Z","iopub.status.idle":"2023-09-19T20:04:24.756448Z","shell.execute_reply.started":"2023-09-19T20:04:24.747626Z","shell.execute_reply":"2023-09-19T20:04:24.755251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (4, 4), activation=\"relu\",input_shape=(32, 32, 3)),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Conv2D(32, (4, 4), activation=\"relu\",input_shape=(32, 32, 3)),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(units=10, activation='softmax')])\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:24.758050Z","iopub.execute_input":"2023-09-19T20:04:24.758712Z","iopub.status.idle":"2023-09-19T20:04:24.827000Z","shell.execute_reply.started":"2023-09-19T20:04:24.758668Z","shell.execute_reply":"2023-09-19T20:04:24.826141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss ='categorical_crossentropy', optimizer ='adam', metrics =['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:24.828860Z","iopub.execute_input":"2023-09-19T20:04:24.829509Z","iopub.status.idle":"2023-09-19T20:04:24.840997Z","shell.execute_reply.started":"2023-09-19T20:04:24.829476Z","shell.execute_reply":"2023-09-19T20:04:24.839951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\nhistory = model.fit(X_train, Y_train_en, epochs = 30, verbose=1,validation_data=(X_test,Y_test_en))","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:04:24.843206Z","iopub.execute_input":"2023-09-19T20:04:24.844140Z","iopub.status.idle":"2023-09-19T20:08:18.879346Z","shell.execute_reply.started":"2023-09-19T20:04:24.844107Z","shell.execute_reply":"2023-09-19T20:08:18.878128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation = model.evaluate(X_test, Y_test_en)\nprint('Test Accuracy of Base Model: {}'.format(evaluation[1]))","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:08:18.881202Z","iopub.execute_input":"2023-09-19T20:08:18.881909Z","iopub.status.idle":"2023-09-19T20:08:20.569685Z","shell.execute_reply.started":"2023-09-19T20:08:18.881870Z","shell.execute_reply":"2023-09-19T20:08:20.568738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotloss(history):\n    plt.plot(history.history['loss'])\n    plt.plot(history.history['val_loss'])\n    plt.plot(history.history['accuracy'])\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend(['Train', 'Validation', 'Accuracy'])\n    plt.show()\nplotloss(history)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:08:20.573681Z","iopub.execute_input":"2023-09-19T20:08:20.573984Z","iopub.status.idle":"2023-09-19T20:08:20.901636Z","shell.execute_reply.started":"2023-09-19T20:08:20.573958Z","shell.execute_reply":"2023-09-19T20:08:20.900721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As validation loss is quite increasing we can say that the model is overfitting","metadata":{}},{"cell_type":"markdown","source":"Let's try adding Dropout","metadata":{}},{"cell_type":"code","source":"model_2 = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (4, 4), activation=\"relu\",input_shape=(32, 32, 3)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Conv2D(32, (4, 4), activation=\"relu\",input_shape=(32, 32, 3)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dense(units=10, activation='softmax')])\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:08:20.903233Z","iopub.execute_input":"2023-09-19T20:08:20.903634Z","iopub.status.idle":"2023-09-19T20:08:21.040874Z","shell.execute_reply.started":"2023-09-19T20:08:20.903599Z","shell.execute_reply":"2023-09-19T20:08:21.039765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_2.compile(loss ='categorical_crossentropy', optimizer ='adam', metrics =['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:08:21.042358Z","iopub.execute_input":"2023-09-19T20:08:21.042738Z","iopub.status.idle":"2023-09-19T20:08:21.057211Z","shell.execute_reply.started":"2023-09-19T20:08:21.042693Z","shell.execute_reply":"2023-09-19T20:08:21.056011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_2.summary()\nhistory_2 = model_2.fit(X_train, Y_train_en, epochs = 30, verbose=1, validation_data=(X_test,Y_test_en))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:08:21.059039Z","iopub.execute_input":"2023-09-19T20:08:21.059458Z","iopub.status.idle":"2023-09-19T20:14:46.256797Z","shell.execute_reply.started":"2023-09-19T20:08:21.059420Z","shell.execute_reply":"2023-09-19T20:14:46.255725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation = model_2.evaluate(X_test, Y_test_en)\nprint('Test Accuracy of Model_2(with more  filters): {}'.format(evaluation[1]))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:14:46.258496Z","iopub.execute_input":"2023-09-19T20:14:46.258969Z","iopub.status.idle":"2023-09-19T20:14:47.981863Z","shell.execute_reply.started":"2023-09-19T20:14:46.258934Z","shell.execute_reply":"2023-09-19T20:14:47.980816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotloss(history_2):\n    plt.plot(history_2.history['loss'])\n    plt.plot(history_2.history['val_loss'])\n    plt.plot(history_2.history['accuracy'])\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend(['Train', 'Validation', 'Accuracy'])\n    plt.show()    \nplotloss(history_2)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:14:47.983477Z","iopub.execute_input":"2023-09-19T20:14:47.983828Z","iopub.status.idle":"2023-09-19T20:14:48.294456Z","shell.execute_reply.started":"2023-09-19T20:14:47.983795Z","shell.execute_reply":"2023-09-19T20:14:48.293590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_3 = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(64, (4, 4), activation=\"relu\",input_shape=(32, 32, 3),padding='same'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(64, (4, 4), activation=\"relu\",input_shape=(32, 32, 3),padding='same'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(128, (4, 4), activation=\"relu\",input_shape=(32, 32, 3),padding='same'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(128, (4, 4), activation=\"relu\",input_shape=(32, 32, 3),padding='same'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(128, (4, 4), activation=\"relu\",input_shape=(32, 32, 3),padding='same'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(128, (4, 4), activation=\"relu\",input_shape=(32, 32, 3),padding='same'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Dropout(0.35),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(units=10, activation='softmax'),])\n\n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:21:21.911279Z","iopub.execute_input":"2023-09-19T20:21:21.912287Z","iopub.status.idle":"2023-09-19T20:21:22.169036Z","shell.execute_reply.started":"2023-09-19T20:21:21.912249Z","shell.execute_reply":"2023-09-19T20:21:22.167974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_3.compile(loss ='categorical_crossentropy', optimizer ='adam', metrics =['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:21:22.478189Z","iopub.execute_input":"2023-09-19T20:21:22.478531Z","iopub.status.idle":"2023-09-19T20:21:22.492991Z","shell.execute_reply.started":"2023-09-19T20:21:22.478503Z","shell.execute_reply":"2023-09-19T20:21:22.491833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_3.summary()\nhistory_3 = model_3.fit(X_train, Y_train_en, epochs = 30, verbose=1, validation_data=(X_test,Y_test_en))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:21:25.487602Z","iopub.execute_input":"2023-09-19T20:21:25.487976Z","iopub.status.idle":"2023-09-19T20:38:51.719879Z","shell.execute_reply.started":"2023-09-19T20:21:25.487945Z","shell.execute_reply":"2023-09-19T20:38:51.718841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation = model_3.evaluate(X_test, Y_test_en)\nprint('Test Accuracy of Model_2(with more  filters): {}'.format(evaluation[1]))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:38:51.721895Z","iopub.execute_input":"2023-09-19T20:38:51.722329Z","iopub.status.idle":"2023-09-19T20:38:54.675427Z","shell.execute_reply.started":"2023-09-19T20:38:51.722295Z","shell.execute_reply":"2023-09-19T20:38:54.674428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotloss(history_3):\n    plt.plot(history_3.history['loss'])\n    plt.plot(history_3.history['val_loss'])\n    plt.plot(history_3.history['accuracy'])\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend(['Train', 'Validation', 'Accuracy'])\n    plt.show()    \nplotloss(history_3)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T20:38:54.677058Z","iopub.execute_input":"2023-09-19T20:38:54.677760Z","iopub.status.idle":"2023-09-19T20:38:54.948623Z","shell.execute_reply.started":"2023-09-19T20:38:54.677723Z","shell.execute_reply":"2023-09-19T20:38:54.947664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}