{"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-10-05T09:37:59.115506Z","iopub.execute_input":"2023-10-05T09:37:59.115992Z","iopub.status.idle":"2023-10-05T09:37:59.549860Z","shell.execute_reply.started":"2023-10-05T09:37:59.115953Z","shell.execute_reply":"2023-10-05T09:37:59.548595Z"},"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-10-05T09:37:59.555799Z","iopub.execute_input":"2023-10-05T09:37:59.558650Z","iopub.status.idle":"2023-10-05T09:38:09.241883Z","shell.execute_reply.started":"2023-10-05T09:37:59.558613Z","shell.execute_reply":"2023-10-05T09:38:09.240816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32  \nnum_classes = 10  \nepochs = 100","metadata":{"execution":{"iopub.status.busy":"2023-10-05T09:38:09.243287Z","iopub.execute_input":"2023-10-05T09:38:09.244024Z","iopub.status.idle":"2023-10-05T09:38:09.250729Z","shell.execute_reply.started":"2023-10-05T09:38:09.243987Z","shell.execute_reply":"2023-10-05T09:38:09.249028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.datasets import cifar10","metadata":{"execution":{"iopub.status.busy":"2023-10-05T09:38:09.252197Z","iopub.execute_input":"2023-10-05T09:38:09.253188Z","iopub.status.idle":"2023-10-05T09:38:09.263305Z","shell.execute_reply.started":"2023-10-05T09:38:09.253154Z","shell.execute_reply":"2023-10-05T09:38:09.262114Z"},"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-10-05T09:38:09.266825Z","iopub.execute_input":"2023-10-05T09:38:09.267837Z","iopub.status.idle":"2023-10-05T09:38:14.514563Z","shell.execute_reply.started":"2023-10-05T09:38:09.267810Z","shell.execute_reply":"2023-10-05T09:38:14.513402Z"},"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-10-05T09:38:14.515969Z","iopub.execute_input":"2023-10-05T09:38:14.517030Z","iopub.status.idle":"2023-10-05T09:38:14.523998Z","shell.execute_reply.started":"2023-10-05T09:38:14.516997Z","shell.execute_reply":"2023-10-05T09:38:14.522862Z"},"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-10-05T09:38:14.525737Z","iopub.execute_input":"2023-10-05T09:38:14.526590Z","iopub.status.idle":"2023-10-05T09:38:15.082890Z","shell.execute_reply.started":"2023-10-05T09:38:14.526556Z","shell.execute_reply":"2023-10-05T09:38:15.081816Z"},"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-10-05T09:38:15.084358Z","iopub.execute_input":"2023-10-05T09:38:15.085151Z","iopub.status.idle":"2023-10-05T09:38:15.095653Z","shell.execute_reply.started":"2023-10-05T09:38:15.085116Z","shell.execute_reply":"2023-10-05T09:38:15.094556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-10-05T09:38:15.097263Z","iopub.execute_input":"2023-10-05T09:38:15.097680Z","iopub.status.idle":"2023-10-05T09:38:15.174987Z","shell.execute_reply.started":"2023-10-05T09:38:15.097640Z","shell.execute_reply":"2023-10-05T09:38:15.173809Z"},"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-10-05T09:38:15.176718Z","iopub.execute_input":"2023-10-05T09:38:15.177085Z","iopub.status.idle":"2023-10-05T09:38:20.514325Z","shell.execute_reply.started":"2023-10-05T09:38:15.177054Z","shell.execute_reply":"2023-10-05T09:38:20.512994Z"},"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-10-05T09:38:20.520397Z","iopub.execute_input":"2023-10-05T09:38:20.523739Z","iopub.status.idle":"2023-10-05T09:38:20.759135Z","shell.execute_reply.started":"2023-10-05T09:38:20.523690Z","shell.execute_reply":"2023-10-05T09:38:20.757823Z"},"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-10-05T09:38:20.765622Z","iopub.execute_input":"2023-10-05T09:38:20.769103Z","iopub.status.idle":"2023-10-05T09:43:46.000101Z","shell.execute_reply.started":"2023-10-05T09:38:20.769058Z","shell.execute_reply":"2023-10-05T09:43:45.998833Z"},"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-10-05T09:43:46.001962Z","iopub.execute_input":"2023-10-05T09:43:46.002452Z","iopub.status.idle":"2023-10-05T09:43:47.723497Z","shell.execute_reply.started":"2023-10-05T09:43:46.002415Z","shell.execute_reply":"2023-10-05T09:43:47.722289Z"},"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-10-05T09:43:47.728041Z","iopub.execute_input":"2023-10-05T09:43:47.728757Z","iopub.status.idle":"2023-10-05T09:43:48.080606Z","shell.execute_reply.started":"2023-10-05T09:43:47.728711Z","shell.execute_reply":"2023-10-05T09:43:48.079608Z"},"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-10-05T09:43:48.082117Z","iopub.execute_input":"2023-10-05T09:43:48.082758Z","iopub.status.idle":"2023-10-05T09:43:48.221619Z","shell.execute_reply.started":"2023-10-05T09:43:48.082722Z","shell.execute_reply":"2023-10-05T09:43:48.220579Z"},"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-10-05T09:43:48.222987Z","iopub.execute_input":"2023-10-05T09:43:48.223345Z","iopub.status.idle":"2023-10-05T09:43:48.238699Z","shell.execute_reply.started":"2023-10-05T09:43:48.223309Z","shell.execute_reply":"2023-10-05T09:43:48.237593Z"},"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-10-05T09:43:48.240603Z","iopub.execute_input":"2023-10-05T09:43:48.241240Z","iopub.status.idle":"2023-10-05T09:51:13.670885Z","shell.execute_reply.started":"2023-10-05T09:43:48.241202Z","shell.execute_reply":"2023-10-05T09:51:13.669787Z"},"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-10-05T09:51:13.672770Z","iopub.execute_input":"2023-10-05T09:51:13.673131Z","iopub.status.idle":"2023-10-05T09:51:15.244274Z","shell.execute_reply.started":"2023-10-05T09:51:13.673097Z","shell.execute_reply":"2023-10-05T09:51:15.243024Z"},"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-10-05T09:51:15.246049Z","iopub.execute_input":"2023-10-05T09:51:15.246465Z","iopub.status.idle":"2023-10-05T09:51:15.600653Z","shell.execute_reply.started":"2023-10-05T09:51:15.246427Z","shell.execute_reply":"2023-10-05T09:51:15.599675Z"},"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-10-05T09:51:15.602395Z","iopub.execute_input":"2023-10-05T09:51:15.603106Z","iopub.status.idle":"2023-10-05T09:51:15.842506Z","shell.execute_reply.started":"2023-10-05T09:51:15.603069Z","shell.execute_reply":"2023-10-05T09:51:15.841389Z"},"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-10-05T09:51:15.843801Z","iopub.execute_input":"2023-10-05T09:51:15.844179Z","iopub.status.idle":"2023-10-05T09:51:15.858848Z","shell.execute_reply.started":"2023-10-05T09:51:15.844148Z","shell.execute_reply":"2023-10-05T09:51:15.857851Z"},"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-10-05T09:51:15.860898Z","iopub.execute_input":"2023-10-05T09:51:15.861627Z","iopub.status.idle":"2023-10-05T10:08:29.271469Z","shell.execute_reply.started":"2023-10-05T09:51:15.861592Z","shell.execute_reply":"2023-10-05T10:08:29.270379Z"},"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-10-05T10:08:29.273587Z","iopub.execute_input":"2023-10-05T10:08:29.274280Z","iopub.status.idle":"2023-10-05T10:08:32.263109Z","shell.execute_reply.started":"2023-10-05T10:08:29.274240Z","shell.execute_reply":"2023-10-05T10:08:32.261988Z"},"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-10-05T10:08:32.264725Z","iopub.execute_input":"2023-10-05T10:08:32.265354Z","iopub.status.idle":"2023-10-05T10:08:32.564418Z","shell.execute_reply.started":"2023-10-05T10:08:32.265316Z","shell.execute_reply":"2023-10-05T10:08:32.563449Z"},"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":[]}]}