{"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":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"},{"sourceId":152217357,"sourceType":"kernelVersion"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-12-05T05:40:11.800544Z","iopub.execute_input":"2023-12-05T05:40:11.800828Z","iopub.status.idle":"2023-12-05T05:40:13.250326Z","shell.execute_reply.started":"2023-12-05T05:40:11.800801Z","shell.execute_reply":"2023-12-05T05:40:13.249373Z"},"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-12-05T05:40:13.252576Z","iopub.execute_input":"2023-12-05T05:40:13.253330Z","iopub.status.idle":"2023-12-05T05:40:14.494900Z","shell.execute_reply.started":"2023-12-05T05:40:13.253295Z","shell.execute_reply":"2023-12-05T05:40:14.493917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Analyzing the train set**","metadata":{}},{"cell_type":"code","source":"image_count = data.shape[0]\nprint('Number of images: ', image_count)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:14.496158Z","iopub.execute_input":"2023-12-05T05:40:14.496543Z","iopub.status.idle":"2023-12-05T05:40:14.502052Z","shell.execute_reply.started":"2023-12-05T05:40:14.496506Z","shell.execute_reply":"2023-12-05T05:40:14.500847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:14.505336Z","iopub.execute_input":"2023-12-05T05:40:14.505685Z","iopub.status.idle":"2023-12-05T05:40:14.703824Z","shell.execute_reply.started":"2023-12-05T05:40:14.505653Z","shell.execute_reply":"2023-12-05T05:40:14.702719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display an image\nimg = data[10012]\nplt.figure(figsize=(2,1))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:14.705122Z","iopub.execute_input":"2023-12-05T05:40:14.705468Z","iopub.status.idle":"2023-12-05T05:40:14.923561Z","shell.execute_reply.started":"2023-12-05T05:40:14.705437Z","shell.execute_reply":"2023-12-05T05:40:14.922491Z"},"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-12-05T05:40:14.924962Z","iopub.execute_input":"2023-12-05T05:40:14.925335Z","iopub.status.idle":"2023-12-05T05:40:14.965828Z","shell.execute_reply.started":"2023-12-05T05:40:14.925301Z","shell.execute_reply":"2023-12-05T05:40:14.964976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:14.967046Z","iopub.execute_input":"2023-12-05T05:40:14.967816Z","iopub.status.idle":"2023-12-05T05:40:14.974129Z","shell.execute_reply.started":"2023-12-05T05:40:14.967789Z","shell.execute_reply":"2023-12-05T05:40:14.973014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:14.975560Z","iopub.execute_input":"2023-12-05T05:40:14.975892Z","iopub.status.idle":"2023-12-05T05:40:14.994500Z","shell.execute_reply.started":"2023-12-05T05:40:14.975859Z","shell.execute_reply":"2023-12-05T05:40:14.993632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 7796]","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:14.995628Z","iopub.execute_input":"2023-12-05T05:40:14.995936Z","iopub.status.idle":"2023-12-05T05:40:15.007937Z","shell.execute_reply.started":"2023-12-05T05:40:14.995908Z","shell.execute_reply":"2023-12-05T05:40:15.006916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.012791Z","iopub.execute_input":"2023-12-05T05:40:15.013160Z","iopub.status.idle":"2023-12-05T05:40:15.025263Z","shell.execute_reply.started":"2023-12-05T05:40:15.013136Z","shell.execute_reply":"2023-12-05T05:40:15.024236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.tail(10)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.026569Z","iopub.execute_input":"2023-12-05T05:40:15.026961Z","iopub.status.idle":"2023-12-05T05:40:15.037188Z","shell.execute_reply.started":"2023-12-05T05:40:15.026930Z","shell.execute_reply":"2023-12-05T05:40:15.036192Z"},"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-12-05T05:40:15.038472Z","iopub.execute_input":"2023-12-05T05:40:15.038793Z","iopub.status.idle":"2023-12-05T05:40:15.059669Z","shell.execute_reply.started":"2023-12-05T05:40:15.038765Z","shell.execute_reply":"2023-12-05T05:40:15.058720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df['label']","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.060829Z","iopub.execute_input":"2023-12-05T05:40:15.061251Z","iopub.status.idle":"2023-12-05T05:40:15.069821Z","shell.execute_reply.started":"2023-12-05T05:40:15.061220Z","shell.execute_reply":"2023-12-05T05:40:15.068722Z"},"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-12-05T05:40:15.071241Z","iopub.execute_input":"2023-12-05T05:40:15.071565Z","iopub.status.idle":"2023-12-05T05:40:15.087245Z","shell.execute_reply.started":"2023-12-05T05:40:15.071533Z","shell.execute_reply":"2023-12-05T05:40:15.086367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels[0:5])\nprint(labels[-5:])","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.088270Z","iopub.execute_input":"2023-12-05T05:40:15.088531Z","iopub.status.idle":"2023-12-05T05:40:15.093470Z","shell.execute_reply.started":"2023-12-05T05:40:15.088508Z","shell.execute_reply":"2023-12-05T05:40:15.092553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.094759Z","iopub.execute_input":"2023-12-05T05:40:15.095089Z","iopub.status.idle":"2023-12-05T05:40:15.102144Z","shell.execute_reply.started":"2023-12-05T05:40:15.095065Z","shell.execute_reply":"2023-12-05T05:40:15.101223Z"},"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-12-05T05:40:15.103480Z","iopub.execute_input":"2023-12-05T05:40:15.103806Z","iopub.status.idle":"2023-12-05T05:40:15.315373Z","shell.execute_reply.started":"2023-12-05T05:40:15.103764Z","shell.execute_reply":"2023-12-05T05:40:15.314130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 9999]","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.317301Z","iopub.execute_input":"2023-12-05T05:40:15.318140Z","iopub.status.idle":"2023-12-05T05:40:15.333358Z","shell.execute_reply.started":"2023-12-05T05:40:15.318086Z","shell.execute_reply":"2023-12-05T05:40:15.332233Z"},"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-12-05T05:40:15.335151Z","iopub.execute_input":"2023-12-05T05:40:15.335860Z","iopub.status.idle":"2023-12-05T05:40:15.343909Z","shell.execute_reply.started":"2023-12-05T05:40:15.335815Z","shell.execute_reply":"2023-12-05T05:40:15.342626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list = list(labels_df['id'])","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.345779Z","iopub.execute_input":"2023-12-05T05:40:15.346491Z","iopub.status.idle":"2023-12-05T05:40:15.367689Z","shell.execute_reply.started":"2023-12-05T05:40:15.346451Z","shell.execute_reply":"2023-12-05T05:40:15.366425Z"},"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-12-05T05:40:15.369553Z","iopub.execute_input":"2023-12-05T05:40:15.370354Z","iopub.status.idle":"2023-12-05T05:40:15.379039Z","shell.execute_reply.started":"2023-12-05T05:40:15.370309Z","shell.execute_reply":"2023-12-05T05:40:15.377614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Train Test Split**","metadata":{}},{"cell_type":"code","source":"type(data)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.382330Z","iopub.execute_input":"2023-12-05T05:40:15.383487Z","iopub.status.idle":"2023-12-05T05:40:15.394941Z","shell.execute_reply.started":"2023-12-05T05:40:15.383446Z","shell.execute_reply":"2023-12-05T05:40:15.393441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.397686Z","iopub.execute_input":"2023-12-05T05:40:15.399428Z","iopub.status.idle":"2023-12-05T05:40:15.410610Z","shell.execute_reply.started":"2023-12-05T05:40:15.399384Z","shell.execute_reply":"2023-12-05T05:40:15.409223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.412681Z","iopub.execute_input":"2023-12-05T05:40:15.413578Z","iopub.status.idle":"2023-12-05T05:40:15.421409Z","shell.execute_reply.started":"2023-12-05T05:40:15.413530Z","shell.execute_reply":"2023-12-05T05:40:15.420556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.422746Z","iopub.execute_input":"2023-12-05T05:40:15.423373Z","iopub.status.idle":"2023-12-05T05:40:15.429354Z","shell.execute_reply.started":"2023-12-05T05:40:15.423348Z","shell.execute_reply":"2023-12-05T05:40:15.428595Z"},"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-12-05T05:40:15.430528Z","iopub.execute_input":"2023-12-05T05:40:15.431039Z","iopub.status.idle":"2023-12-05T05:40:15.442143Z","shell.execute_reply.started":"2023-12-05T05:40:15.431004Z","shell.execute_reply":"2023-12-05T05:40:15.440991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(x)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.443548Z","iopub.execute_input":"2023-12-05T05:40:15.443859Z","iopub.status.idle":"2023-12-05T05:40:15.451499Z","shell.execute_reply.started":"2023-12-05T05:40:15.443836Z","shell.execute_reply":"2023-12-05T05:40:15.450680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.457980Z","iopub.execute_input":"2023-12-05T05:40:15.458499Z","iopub.status.idle":"2023-12-05T05:40:15.462596Z","shell.execute_reply.started":"2023-12-05T05:40:15.458467Z","shell.execute_reply":"2023-12-05T05:40:15.461644Z"},"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-12-05T05:40:15.464117Z","iopub.execute_input":"2023-12-05T05:40:15.464743Z","iopub.status.idle":"2023-12-05T05:40:15.470618Z","shell.execute_reply.started":"2023-12-05T05:40:15.464706Z","shell.execute_reply":"2023-12-05T05:40:15.469575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.472073Z","iopub.execute_input":"2023-12-05T05:40:15.472595Z","iopub.status.idle":"2023-12-05T05:40:15.481690Z","shell.execute_reply.started":"2023-12-05T05:40:15.472569Z","shell.execute_reply":"2023-12-05T05:40:15.480859Z"},"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-12-05T05:40:15.482935Z","iopub.execute_input":"2023-12-05T05:40:15.483472Z","iopub.status.idle":"2023-12-05T05:40:15.489761Z","shell.execute_reply.started":"2023-12-05T05:40:15.483445Z","shell.execute_reply":"2023-12-05T05:40:15.488749Z"},"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-12-05T05:40:15.491118Z","iopub.execute_input":"2023-12-05T05:40:15.491506Z","iopub.status.idle":"2023-12-05T05:40:15.497942Z","shell.execute_reply.started":"2023-12-05T05:40:15.491481Z","shell.execute_reply":"2023-12-05T05:40:15.497228Z"},"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-12-05T05:40:15.499005Z","iopub.execute_input":"2023-12-05T05:40:15.499623Z","iopub.status.idle":"2023-12-05T05:40:15.507609Z","shell.execute_reply.started":"2023-12-05T05:40:15.499599Z","shell.execute_reply":"2023-12-05T05:40:15.506762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_scaled[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.508838Z","iopub.execute_input":"2023-12-05T05:40:15.509466Z","iopub.status.idle":"2023-12-05T05:40:15.517085Z","shell.execute_reply.started":"2023-12-05T05:40:15.509431Z","shell.execute_reply":"2023-12-05T05:40:15.516017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the neural network using VGG16","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:15.518408Z","iopub.execute_input":"2023-12-05T05:40:15.518759Z","iopub.status.idle":"2023-12-05T05:40:26.263149Z","shell.execute_reply.started":"2023-12-05T05:40:15.518724Z","shell.execute_reply":"2023-12-05T05:40:26.262357Z"},"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.vgg16 import VGG16\nfrom tensorflow.keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:26.264299Z","iopub.execute_input":"2023-12-05T05:40:26.264844Z","iopub.status.idle":"2023-12-05T05:40:26.375611Z","shell.execute_reply.started":"2023-12-05T05:40:26.264817Z","shell.execute_reply":"2023-12-05T05:40:26.374730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#------------------------------------------------------------------------------\n# Using VGG16 model, with weights pre-trained on ImageNet.\n#------------------------------------------------------------------------------\n\nvgg16_model = VGG16(weights='imagenet',\n                    include_top=False, \n                    classes=10,\n                    input_shape=(32,32,3)# input: 32x32 images with 3 channels -> (32, 32, 3) tensors.\n                   )","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:26.376791Z","iopub.execute_input":"2023-12-05T05:40:26.377723Z","iopub.status.idle":"2023-12-05T05:40:33.394689Z","shell.execute_reply.started":"2023-12-05T05:40:26.377689Z","shell.execute_reply":"2023-12-05T05:40:33.393865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_classes = 10\n\nmodel = models.Sequential() \n\nfor layer in vgg16_model.layers:\n    model.add(layer)\n\nmodel.add(layers.Flatten())  # transforms the input into a one-dimensional array (flattens it) without modifying the actual data. \nmodel.add(Dropout(0.4))\n\nmodel.add(layers.BatchNormalization()) #normalization\nmodel.add(layers.Dense(512, activation='relu'))\n\nmodel.add(layers.Dropout(0.5)) #droping or making somen neuron inactive to avoid overfitting\nmodel.add(layers.Dense(256, activation='relu'))\n\nmodel.add(layers.Dropout(0.5)) #droping or making somen neuron inactive to avoid overfitting\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(num_of_classes, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:33.395858Z","iopub.execute_input":"2023-12-05T05:40:33.396156Z","iopub.status.idle":"2023-12-05T05:40:33.564771Z","shell.execute_reply.started":"2023-12-05T05:40:33.396130Z","shell.execute_reply":"2023-12-05T05:40:33.563869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Metrics and model compilation","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:33.565967Z","iopub.execute_input":"2023-12-05T05:40:33.567427Z","iopub.status.idle":"2023-12-05T05:40:33.583330Z","shell.execute_reply.started":"2023-12-05T05:40:33.567399Z","shell.execute_reply":"2023-12-05T05:40:33.582639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model train","metadata":{}},{"cell_type":"code","source":"history = model.fit(x_train_scaled, y_train, validation_split=0.1, epochs= 10, batch_size=100)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:33.584384Z","iopub.execute_input":"2023-12-05T05:40:33.584729Z","iopub.status.idle":"2023-12-05T05:40:46.125495Z","shell.execute_reply.started":"2023-12-05T05:40:33.584696Z","shell.execute_reply":"2023-12-05T05:40:46.124688Z"},"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-12-05T05:40:46.126641Z","iopub.execute_input":"2023-12-05T05:40:46.126905Z","iopub.status.idle":"2023-12-05T05:40:46.131597Z","shell.execute_reply.started":"2023-12-05T05:40:46.126881Z","shell.execute_reply":"2023-12-05T05:40:46.130399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x_test_scaled)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:46.132816Z","iopub.execute_input":"2023-12-05T05:40:46.133125Z","iopub.status.idle":"2023-12-05T05:40:46.594585Z","shell.execute_reply.started":"2023-12-05T05:40:46.133101Z","shell.execute_reply":"2023-12-05T05:40:46.593705Z"},"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-12-05T05:40:46.595855Z","iopub.execute_input":"2023-12-05T05:40:46.596535Z","iopub.status.idle":"2023-12-05T05:40:46.600839Z","shell.execute_reply.started":"2023-12-05T05:40:46.596498Z","shell.execute_reply":"2023-12-05T05:40:46.599855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1_score(y_test, y_pred_labels, average=\"macro\")","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:46.601959Z","iopub.execute_input":"2023-12-05T05:40:46.602255Z","iopub.status.idle":"2023-12-05T05:40:46.615891Z","shell.execute_reply.started":"2023-12-05T05:40:46.602232Z","shell.execute_reply":"2023-12-05T05:40:46.614872Z"},"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-12-05T05:40:46.617049Z","iopub.execute_input":"2023-12-05T05:40:46.617421Z","iopub.status.idle":"2023-12-05T05:40:46.701561Z","shell.execute_reply.started":"2023-12-05T05:40:46.617385Z","shell.execute_reply":"2023-12-05T05:40:46.700725Z"},"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-12-05T05:40:46.702663Z","iopub.execute_input":"2023-12-05T05:40:46.703291Z","iopub.status.idle":"2023-12-05T05:40:47.162405Z","shell.execute_reply.started":"2023-12-05T05:40:46.703257Z","shell.execute_reply":"2023-12-05T05:40:47.161428Z"},"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-12-05T05:40:47.163657Z","iopub.execute_input":"2023-12-05T05:40:47.164429Z","iopub.status.idle":"2023-12-05T05:40:47.327550Z","shell.execute_reply.started":"2023-12-05T05:40:47.164399Z","shell.execute_reply":"2023-12-05T05:40:47.326733Z"},"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-12-05T05:40:47.328600Z","iopub.execute_input":"2023-12-05T05:40:47.328893Z","iopub.status.idle":"2023-12-05T05:40:47.361323Z","shell.execute_reply.started":"2023-12-05T05:40:47.328871Z","shell.execute_reply":"2023-12-05T05:40:47.360463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(conf_mat)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T05:40:47.362471Z","iopub.execute_input":"2023-12-05T05:40:47.363452Z","iopub.status.idle":"2023-12-05T05:40:47.369192Z","shell.execute_reply.started":"2023-12-05T05:40:47.363419Z","shell.execute_reply":"2023-12-05T05:40:47.368285Z"},"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-12-05T05:40:47.370480Z","iopub.execute_input":"2023-12-05T05:40:47.370994Z","iopub.status.idle":"2023-12-05T05:40:48.035046Z","shell.execute_reply.started":"2023-12-05T05:40:47.370967Z","shell.execute_reply":"2023-12-05T05:40:48.033949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-05T05:40:48.036266Z","iopub.execute_input":"2023-12-05T05:40:48.036556Z","iopub.status.idle":"2023-12-05T05:40:48.271541Z","shell.execute_reply.started":"2023-12-05T05:40:48.036530Z","shell.execute_reply":"2023-12-05T05:40:48.270515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}