{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"},{"sourceId":152217357,"sourceType":"kernelVersion"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Importing dependencies**","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:16.339770Z","iopub.execute_input":"2023-11-26T04:01:16.340266Z","iopub.status.idle":"2023-11-26T04:01:17.903767Z","shell.execute_reply.started":"2023-11-26T04:01:16.340221Z","shell.execute_reply":"2023-11-26T04:01:17.902614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Processing","metadata":{}},{"cell_type":"markdown","source":"**Convert images to numpy arrays**\n\nDone in traing in Extracting train notebook\n\nExporting the process image of numpyarray in data from extracting-train notebook","metadata":{}},{"cell_type":"code","source":"# Load the data\nloaded_data = np.load('/kaggle/input/extracting-train/training_data.npz')\n\n# Access the NumPy array\ndata = loaded_data['data']","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:17.906073Z","iopub.execute_input":"2023-11-26T04:01:17.906764Z","iopub.status.idle":"2023-11-26T04:01:19.819363Z","shell.execute_reply.started":"2023-11-26T04:01:17.906719Z","shell.execute_reply":"2023-11-26T04:01:19.817930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_count = data.shape[0]\nprint('Number of images: ', image_count)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:19.820679Z","iopub.execute_input":"2023-11-26T04:01:19.821079Z","iopub.status.idle":"2023-11-26T04:01:19.827868Z","shell.execute_reply.started":"2023-11-26T04:01:19.821046Z","shell.execute_reply":"2023-11-26T04:01:19.826827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Labels Processing**","metadata":{}},{"cell_type":"code","source":"labels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:19.830292Z","iopub.execute_input":"2023-11-26T04:01:19.830985Z","iopub.status.idle":"2023-11-26T04:01:19.892812Z","shell.execute_reply.started":"2023-11-26T04:01:19.830951Z","shell.execute_reply":"2023-11-26T04:01:19.891707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:19.894809Z","iopub.execute_input":"2023-11-26T04:01:19.895236Z","iopub.status.idle":"2023-11-26T04:01:19.904786Z","shell.execute_reply.started":"2023-11-26T04:01:19.895202Z","shell.execute_reply":"2023-11-26T04:01:19.903448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:19.906431Z","iopub.execute_input":"2023-11-26T04:01:19.906894Z","iopub.status.idle":"2023-11-26T04:01:19.935279Z","shell.execute_reply.started":"2023-11-26T04:01:19.906863Z","shell.execute_reply":"2023-11-26T04:01:19.934428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 7796]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:19.936715Z","iopub.execute_input":"2023-11-26T04:01:19.937110Z","iopub.status.idle":"2023-11-26T04:01:19.953665Z","shell.execute_reply.started":"2023-11-26T04:01:19.937079Z","shell.execute_reply":"2023-11-26T04:01:19.952485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:19.955019Z","iopub.execute_input":"2023-11-26T04:01:19.955359Z","iopub.status.idle":"2023-11-26T04:01:19.967074Z","shell.execute_reply.started":"2023-11-26T04:01:19.955330Z","shell.execute_reply":"2023-11-26T04:01:19.965854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.tail(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:19.968547Z","iopub.execute_input":"2023-11-26T04:01:19.969454Z","iopub.status.idle":"2023-11-26T04:01:19.984928Z","shell.execute_reply.started":"2023-11-26T04:01:19.969421Z","shell.execute_reply":"2023-11-26T04:01:19.983993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Labels are marked serially by id and evenly distributed ( below )","metadata":{}},{"cell_type":"code","source":"labels_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:19.988120Z","iopub.execute_input":"2023-11-26T04:01:19.988480Z","iopub.status.idle":"2023-11-26T04:01:20.016151Z","shell.execute_reply.started":"2023-11-26T04:01:19.988448Z","shell.execute_reply":"2023-11-26T04:01:20.014993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df['label']","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:20.017687Z","iopub.execute_input":"2023-11-26T04:01:20.022188Z","iopub.status.idle":"2023-11-26T04:01:20.033055Z","shell.execute_reply.started":"2023-11-26T04:01:20.022132Z","shell.execute_reply":"2023-11-26T04:01:20.031761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dictionary = {'airplane':0, 'automobile':1, 'bird':2, 'cat':3, 'deer':4, 'dog':5, 'frog':6, 'horse':7, 'ship':8, 'truck':9}\n\nlabels = [labels_dictionary[i] for i in labels_df['label']]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:20.034370Z","iopub.execute_input":"2023-11-26T04:01:20.035225Z","iopub.status.idle":"2023-11-26T04:01:20.062763Z","shell.execute_reply.started":"2023-11-26T04:01:20.035177Z","shell.execute_reply":"2023-11-26T04:01:20.061659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels[0:5])\nprint(labels[-5:])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:20.747532Z","iopub.execute_input":"2023-11-26T04:01:20.747946Z","iopub.status.idle":"2023-11-26T04:01:20.757368Z","shell.execute_reply.started":"2023-11-26T04:01:20.747916Z","shell.execute_reply":"2023-11-26T04:01:20.755841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:21.115243Z","iopub.execute_input":"2023-11-26T04:01:21.115902Z","iopub.status.idle":"2023-11-26T04:01:21.122963Z","shell.execute_reply.started":"2023-11-26T04:01:21.115866Z","shell.execute_reply":"2023-11-26T04:01:21.121694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Checking labels correctness:","metadata":{}},{"cell_type":"code","source":"img = data[9998]\nplt.figure(figsize=(2,1))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:22.155260Z","iopub.execute_input":"2023-11-26T04:01:22.156104Z","iopub.status.idle":"2023-11-26T04:01:22.405290Z","shell.execute_reply.started":"2023-11-26T04:01:22.156066Z","shell.execute_reply":"2023-11-26T04:01:22.404377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 9999]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:22.464110Z","iopub.execute_input":"2023-11-26T04:01:22.464753Z","iopub.status.idle":"2023-11-26T04:01:22.476485Z","shell.execute_reply.started":"2023-11-26T04:01:22.464720Z","shell.execute_reply":"2023-11-26T04:01:22.475255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[9998] # automobile = 1 , and 0 base indexing in List","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:22.771875Z","iopub.execute_input":"2023-11-26T04:01:22.772276Z","iopub.status.idle":"2023-11-26T04:01:22.779294Z","shell.execute_reply.started":"2023-11-26T04:01:22.772242Z","shell.execute_reply":"2023-11-26T04:01:22.778218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list = list(labels_df['id'])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:23.059660Z","iopub.execute_input":"2023-11-26T04:01:23.060800Z","iopub.status.idle":"2023-11-26T04:01:23.076306Z","shell.execute_reply.started":"2023-11-26T04:01:23.060758Z","shell.execute_reply":"2023-11-26T04:01:23.074270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(id_list[0:5])\nprint(id_list[-5:])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:23.377246Z","iopub.execute_input":"2023-11-26T04:01:23.377651Z","iopub.status.idle":"2023-11-26T04:01:23.383824Z","shell.execute_reply.started":"2023-11-26T04:01:23.377616Z","shell.execute_reply":"2023-11-26T04:01:23.382492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Analyzing the train set**","metadata":{}},{"cell_type":"markdown","source":"**Displaying the images**","metadata":{}},{"cell_type":"code","source":"#display an image\nimg = data[9998]\nplt.figure(figsize=(2,1))\nplt.imshow(img)\n\nlabels_df[labels_df['id'] == 9999]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:01:39.884752Z","iopub.execute_input":"2023-11-26T04:01:39.885217Z","iopub.status.idle":"2023-11-26T04:01:40.103066Z","shell.execute_reply.started":"2023-11-26T04:01:39.885181Z","shell.execute_reply":"2023-11-26T04:01:40.101769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display an image\nimg = data[10012]\nplt.figure(figsize=(2,1))\nplt.imshow(img)\n\nlabels_df[labels_df['id'] == 10013]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:02:02.192125Z","iopub.execute_input":"2023-11-26T04:02:02.193646Z","iopub.status.idle":"2023-11-26T04:02:02.431145Z","shell.execute_reply.started":"2023-11-26T04:02:02.193549Z","shell.execute_reply":"2023-11-26T04:02:02.429164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_count = data.shape[0]\nprint('Number of images: ', image_count)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:02:07.408217Z","iopub.execute_input":"2023-11-26T04:02:07.408727Z","iopub.status.idle":"2023-11-26T04:02:07.416704Z","shell.execute_reply.started":"2023-11-26T04:02:07.408688Z","shell.execute_reply":"2023-11-26T04:02:07.415019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Distribution of Classes**","metadata":{}},{"cell_type":"code","source":"class_counts = labels_df['label'].value_counts()\n\n# Plotting a pie chart\nplt.figure(figsize=(8, 8))\nplt.pie(class_counts, labels=class_counts.index, autopct='%1.1f%%', startangle=140)\nplt.title('Distribution of Classes')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:08:35.201332Z","iopub.execute_input":"2023-11-26T04:08:35.201833Z","iopub.status.idle":"2023-11-26T04:08:35.513097Z","shell.execute_reply.started":"2023-11-26T04:08:35.201794Z","shell.execute_reply":"2023-11-26T04:08:35.511494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The classes are equally distributed. Our target is removing the truck class from train data so that the model can be trained in 9 classes and Truck becomes a **novel  class**","metadata":{}},{"cell_type":"markdown","source":"# **Train Test Split**","metadata":{}},{"cell_type":"code","source":"type(data)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:57.704933Z","iopub.execute_input":"2023-11-26T04:16:57.705617Z","iopub.status.idle":"2023-11-26T04:16:57.714665Z","shell.execute_reply.started":"2023-11-26T04:16:57.705564Z","shell.execute_reply":"2023-11-26T04:16:57.712872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:57.855728Z","iopub.execute_input":"2023-11-26T04:16:57.856194Z","iopub.status.idle":"2023-11-26T04:16:57.864812Z","shell.execute_reply.started":"2023-11-26T04:16:57.856162Z","shell.execute_reply":"2023-11-26T04:16:57.863336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data[0])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:58.001313Z","iopub.execute_input":"2023-11-26T04:16:58.001882Z","iopub.status.idle":"2023-11-26T04:16:58.012691Z","shell.execute_reply.started":"2023-11-26T04:16:58.001838Z","shell.execute_reply":"2023-11-26T04:16:58.010688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:58.120373Z","iopub.execute_input":"2023-11-26T04:16:58.121459Z","iopub.status.idle":"2023-11-26T04:16:58.130516Z","shell.execute_reply.started":"2023-11-26T04:16:58.121414Z","shell.execute_reply":"2023-11-26T04:16:58.128902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert image list and label list to numpy arrays\n\nx = data\ny = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:58.291657Z","iopub.execute_input":"2023-11-26T04:16:58.292150Z","iopub.status.idle":"2023-11-26T04:16:58.303396Z","shell.execute_reply.started":"2023-11-26T04:16:58.292113Z","shell.execute_reply":"2023-11-26T04:16:58.302442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:58.426166Z","iopub.execute_input":"2023-11-26T04:16:58.426654Z","iopub.status.idle":"2023-11-26T04:16:58.434569Z","shell.execute_reply.started":"2023-11-26T04:16:58.426617Z","shell.execute_reply":"2023-11-26T04:16:58.433094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:58.547877Z","iopub.execute_input":"2023-11-26T04:16:58.548385Z","iopub.status.idle":"2023-11-26T04:16:58.555221Z","shell.execute_reply.started":"2023-11-26T04:16:58.548344Z","shell.execute_reply":"2023-11-26T04:16:58.554201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#small data for testing while naking the model\n# x = x[:100]\n# y = y[:100]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:58.689332Z","iopub.execute_input":"2023-11-26T04:16:58.689756Z","iopub.status.idle":"2023-11-26T04:16:58.694881Z","shell.execute_reply.started":"2023-11-26T04:16:58.689723Z","shell.execute_reply":"2023-11-26T04:16:58.693818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:58.832836Z","iopub.execute_input":"2023-11-26T04:16:58.833210Z","iopub.status.idle":"2023-11-26T04:16:58.845360Z","shell.execute_reply.started":"2023-11-26T04:16:58.833180Z","shell.execute_reply":"2023-11-26T04:16:58.842746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:58.978855Z","iopub.execute_input":"2023-11-26T04:16:58.979307Z","iopub.status.idle":"2023-11-26T04:16:59.079418Z","shell.execute_reply.started":"2023-11-26T04:16:58.979272Z","shell.execute_reply":"2023-11-26T04:16:59.078007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape, x_train.shape, x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:59.131962Z","iopub.execute_input":"2023-11-26T04:16:59.132381Z","iopub.status.idle":"2023-11-26T04:16:59.138300Z","shell.execute_reply.started":"2023-11-26T04:16:59.132349Z","shell.execute_reply":"2023-11-26T04:16:59.137001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scaling the data\n\nx_train_scaled = x_train/255\n\nx_test_scaled = x_test/255","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:59.321262Z","iopub.execute_input":"2023-11-26T04:16:59.321699Z","iopub.status.idle":"2023-11-26T04:16:59.961236Z","shell.execute_reply.started":"2023-11-26T04:16:59.321663Z","shell.execute_reply":"2023-11-26T04:16:59.959877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_scaled[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:59.963901Z","iopub.execute_input":"2023-11-26T04:16:59.964387Z","iopub.status.idle":"2023-11-26T04:16:59.973953Z","shell.execute_reply.started":"2023-11-26T04:16:59.964342Z","shell.execute_reply":"2023-11-26T04:16:59.972660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Removing the label: Truck( encoding = 9) from training sample","metadata":{}},{"cell_type":"code","source":"x_train_9_classes = []\ny_train_9_classes = []","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:59.975384Z","iopub.execute_input":"2023-11-26T04:16:59.975813Z","iopub.status.idle":"2023-11-26T04:16:59.986413Z","shell.execute_reply.started":"2023-11-26T04:16:59.975781Z","shell.execute_reply":"2023-11-26T04:16:59.985174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, y_label in enumerate(y_train):\n    if y_label != 9:\n        x_train_9_classes.append(x_train_scaled[i])\n        y_train_9_classes.append(y_label)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:16:59.989132Z","iopub.execute_input":"2023-11-26T04:16:59.990165Z","iopub.status.idle":"2023-11-26T04:17:00.049385Z","shell.execute_reply.started":"2023-11-26T04:16:59.990119Z","shell.execute_reply":"2023-11-26T04:17:00.047820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_9_classes = np.array(x_train_9_classes)\ny_train_9_classes = np.array(y_train_9_classes)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:17:00.051455Z","iopub.execute_input":"2023-11-26T04:17:00.052664Z","iopub.status.idle":"2023-11-26T04:17:00.529798Z","shell.execute_reply.started":"2023-11-26T04:17:00.052607Z","shell.execute_reply":"2023-11-26T04:17:00.528612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(x_train), type(x_train_9_classes))","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:17:00.535700Z","iopub.execute_input":"2023-11-26T04:17:00.536604Z","iopub.status.idle":"2023-11-26T04:17:00.543481Z","shell.execute_reply.started":"2023-11-26T04:17:00.536530Z","shell.execute_reply":"2023-11-26T04:17:00.542291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:17:00.767190Z","iopub.execute_input":"2023-11-26T04:17:00.767838Z","iopub.status.idle":"2023-11-26T04:17:00.777829Z","shell.execute_reply.started":"2023-11-26T04:17:00.767804Z","shell.execute_reply":"2023-11-26T04:17:00.773993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_9_classes.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:17:00.972161Z","iopub.execute_input":"2023-11-26T04:17:00.973412Z","iopub.status.idle":"2023-11-26T04:17:00.981446Z","shell.execute_reply.started":"2023-11-26T04:17:00.973348Z","shell.execute_reply":"2023-11-26T04:17:00.980062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(y_train), type(y_train_9_classes))","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:17:01.902768Z","iopub.execute_input":"2023-11-26T04:17:01.904245Z","iopub.status.idle":"2023-11-26T04:17:01.909858Z","shell.execute_reply.started":"2023-11-26T04:17:01.904200Z","shell.execute_reply":"2023-11-26T04:17:01.908680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:17:03.619203Z","iopub.execute_input":"2023-11-26T04:17:03.620527Z","iopub.status.idle":"2023-11-26T04:17:03.627818Z","shell.execute_reply.started":"2023-11-26T04:17:03.620487Z","shell.execute_reply":"2023-11-26T04:17:03.626447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_9_classes.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:17:04.631171Z","iopub.execute_input":"2023-11-26T04:17:04.631839Z","iopub.status.idle":"2023-11-26T04:17:04.640348Z","shell.execute_reply.started":"2023-11-26T04:17:04.631795Z","shell.execute_reply":"2023-11-26T04:17:04.638893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming y_train_9_classes contains the labels after removing one class\nclass_counts = np.bincount(y_train_9_classes)\n\n# Plotting a pie chart\nplt.figure(figsize=(8, 8))\nplt.pie(class_counts, labels=list(labels_dictionary.keys())[:-1], autopct='%1.1f%%', startangle=140)\nplt.title('Distribution of Classes in y_train_9_classes')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:24:02.070962Z","iopub.execute_input":"2023-11-26T04:24:02.071400Z","iopub.status.idle":"2023-11-26T04:24:02.349291Z","shell.execute_reply.started":"2023-11-26T04:24:02.071368Z","shell.execute_reply":"2023-11-26T04:24:02.347874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see there is no truck class in our training examples. We will train our model using this 9 classes which are equally distributed","metadata":{"execution":{"iopub.status.busy":"2023-11-26T04:23:46.577713Z","iopub.execute_input":"2023-11-26T04:23:46.578224Z","iopub.status.idle":"2023-11-26T04:23:46.800665Z","shell.execute_reply.started":"2023-11-26T04:23:46.578186Z","shell.execute_reply":"2023-11-26T04:23:46.798754Z"}}},{"cell_type":"markdown","source":"# Defining the neural network using Resnet50","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:43:26.012054Z","iopub.execute_input":"2023-11-26T03:43:26.012331Z","iopub.status.idle":"2023-11-26T03:43:36.988680Z","shell.execute_reply.started":"2023-11-26T03:43:26.012308Z","shell.execute_reply":"2023-11-26T03:43:36.987592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import Sequential, models, layers\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:43:36.990048Z","iopub.execute_input":"2023-11-26T03:43:36.990729Z","iopub.status.idle":"2023-11-26T03:43:37.104708Z","shell.execute_reply.started":"2023-11-26T03:43:36.990692Z","shell.execute_reply":"2023-11-26T03:43:37.103946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Resnet: https://keras.io/api/applications/resnet/","metadata":{}},{"cell_type":"code","source":"def createModel():\n    \n    num_of_classes = 10\n    \n    convolutional_base = ResNet50(weights='imagenet', include_top=False, input_shape=(256,256,3))\n    \n    model = models.Sequential() \n    \n    model.add(layers.UpSampling2D((2,2))) # 32 * 2 = 64\n    model.add(layers.UpSampling2D((2,2))) # 64 * 2 = 128\n    model.add(layers.UpSampling2D((2,2))) # 128 * 2 = 256\n\n    model.add(convolutional_base)\n\n    model.add(layers.Flatten())  # transforms the input into a one-dimensional array (flattens it) without modifying the actual data. \n\n    model.add(layers.BatchNormalization()) #normalization\n    model.add(layers.Dense(128, activation='relu'))\n\n    model.add(layers.Dropout(0.5)) #droping or making somen neuron inactive to avoid overfitting\n\n    model.add(layers.BatchNormalization())\n    model.add(layers.Dense(num_of_classes, activation='softmax'))\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:43:37.105805Z","iopub.execute_input":"2023-11-26T03:43:37.106174Z","iopub.status.idle":"2023-11-26T03:43:42.140454Z","shell.execute_reply.started":"2023-11-26T03:43:37.106137Z","shell.execute_reply":"2023-11-26T03:43:42.139392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Model creation and compilation","metadata":{}},{"cell_type":"code","source":"model = createModel()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:43:42.199229Z","iopub.execute_input":"2023-11-26T03:43:42.199504Z","iopub.status.idle":"2023-11-26T03:43:42.219522Z","shell.execute_reply.started":"2023-11-26T03:43:42.199481Z","shell.execute_reply":"2023-11-26T03:43:42.218551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model train","metadata":{}},{"cell_type":"code","source":"history = model.fit(x_train_9_classes, y_train_9_classes, validation_split=0.1, epochs= 10, batch_size=100)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:43:42.220548Z","iopub.execute_input":"2023-11-26T03:43:42.220790Z","iopub.status.idle":"2023-11-26T03:47:53.935525Z","shell.execute_reply.started":"2023-11-26T03:43:42.220768Z","shell.execute_reply":"2023-11-26T03:47:53.934636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Evaluation with different metrics and plot","metadata":{}},{"cell_type":"code","source":" from sklearn.metrics import f1_score","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:47:53.937075Z","iopub.execute_input":"2023-11-26T03:47:53.937494Z","iopub.status.idle":"2023-11-26T03:47:53.942782Z","shell.execute_reply.started":"2023-11-26T03:47:53.937454Z","shell.execute_reply":"2023-11-26T03:47:53.941783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x_test_scaled)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:47:53.949467Z","iopub.execute_input":"2023-11-26T03:47:53.949804Z","iopub.status.idle":"2023-11-26T03:48:15.834995Z","shell.execute_reply.started":"2023-11-26T03:47:53.949780Z","shell.execute_reply":"2023-11-26T03:48:15.834123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_labels = y_pred.argmax(1)\n# y_pred_labels = [np.argmax(i) for i in y_pred]","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:15.836206Z","iopub.execute_input":"2023-11-26T03:48:15.836476Z","iopub.status.idle":"2023-11-26T03:48:15.841508Z","shell.execute_reply.started":"2023-11-26T03:48:15.836453Z","shell.execute_reply":"2023-11-26T03:48:15.840451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1 = f1_score(y_test, y_pred_labels, average=\"macro\")\nprint('F1 score =', f1)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:15.842891Z","iopub.execute_input":"2023-11-26T03:48:15.843709Z","iopub.status.idle":"2023-11-26T03:48:15.861323Z","shell.execute_reply.started":"2023-11-26T03:48:15.843675Z","shell.execute_reply":"2023-11-26T03:48:15.860410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy  = model.evaluate(x_test_scaled, y_test)\nprint('Test Accuracy =', accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:15.862383Z","iopub.execute_input":"2023-11-26T03:48:15.863028Z","iopub.status.idle":"2023-11-26T03:48:36.458804Z","shell.execute_reply.started":"2023-11-26T03:48:15.863003Z","shell.execute_reply":"2023-11-26T03:48:36.457958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Extract the metrics from the history object\ntrain_accuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\ntrain_loss = history.history['loss']\nval_loss = history.history['val_loss']\n\n\n# Plot accuracy\nplt.plot(train_accuracy, label='Train Accuracy')\nplt.plot(val_accuracy, label='Validation Accuracy')\nplt.legend()\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.show()\n\n# Plot loss\nplt.plot(train_loss, label='Train Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.legend()\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:36.460087Z","iopub.execute_input":"2023-11-26T03:48:36.460411Z","iopub.status.idle":"2023-11-26T03:48:37.022548Z","shell.execute_reply.started":"2023-11-26T03:48:36.460384Z","shell.execute_reply":"2023-11-26T03:48:37.021628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Confusion Matrix**","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nfrom tensorflow.math import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:37.023803Z","iopub.execute_input":"2023-11-26T03:48:37.024183Z","iopub.status.idle":"2023-11-26T03:48:37.185813Z","shell.execute_reply.started":"2023-11-26T03:48:37.024149Z","shell.execute_reply":"2023-11-26T03:48:37.184940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conf_mat = confusion_matrix(y_test, y_pred_labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:37.187188Z","iopub.execute_input":"2023-11-26T03:48:37.187548Z","iopub.status.idle":"2023-11-26T03:48:37.219193Z","shell.execute_reply.started":"2023-11-26T03:48:37.187515Z","shell.execute_reply":"2023-11-26T03:48:37.218175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(conf_mat)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:37.220283Z","iopub.execute_input":"2023-11-26T03:48:37.220552Z","iopub.status.idle":"2023-11-26T03:48:37.225469Z","shell.execute_reply.started":"2023-11-26T03:48:37.220528Z","shell.execute_reply":"2023-11-26T03:48:37.224601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,7))\nsns.heatmap(conf_mat, annot=True, fmt='d', cmap='Blues')\nplt.ylabel('True Labels')\nplt.xlabel('Predicted Labels')","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:37.226583Z","iopub.execute_input":"2023-11-26T03:48:37.226860Z","iopub.status.idle":"2023-11-26T03:48:37.852561Z","shell.execute_reply.started":"2023-11-26T03:48:37.226836Z","shell.execute_reply":"2023-11-26T03:48:37.851648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see all the true label of **9 ( class truck )** are **misclassified** as this class is not see during the training process. For the network this new class is a ***novel class***.","metadata":{}},{"cell_type":"markdown","source":"# Saving the model","metadata":{}},{"cell_type":"code","source":"\nmodel.save('/kaggle/working/CIFAR_resnet_model.h5')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-26T03:48:37.853969Z","iopub.execute_input":"2023-11-26T03:48:37.854601Z","iopub.status.idle":"2023-11-26T03:48:38.854657Z","shell.execute_reply.started":"2023-11-26T03:48:37.854564Z","shell.execute_reply":"2023-11-26T03:48:38.853661Z"},"trusted":true},"execution_count":null,"outputs":[]}]}