{"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-11-25T06:16:37.214191Z","iopub.execute_input":"2023-11-25T06:16:37.214659Z","iopub.status.idle":"2023-11-25T06:16:38.716417Z","shell.execute_reply.started":"2023-11-25T06:16:37.214623Z","shell.execute_reply":"2023-11-25T06:16:38.715390Z"},"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-25T06:27:15.168625Z","iopub.execute_input":"2023-11-25T06:27:15.169071Z","iopub.status.idle":"2023-11-25T06:27:15.420712Z","shell.execute_reply.started":"2023-11-25T06:27:15.169040Z","shell.execute_reply":"2023-11-25T06:27:15.419568Z"},"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-11-25T06:19:18.527584Z","iopub.execute_input":"2023-11-25T06:19:18.528072Z","iopub.status.idle":"2023-11-25T06:19:18.534269Z","shell.execute_reply.started":"2023-11-25T06:19:18.528038Z","shell.execute_reply":"2023-11-25T06:19:18.533080Z"},"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-11-25T06:20:06.381302Z","iopub.execute_input":"2023-11-25T06:20:06.381771Z","iopub.status.idle":"2023-11-25T06:20:06.610620Z","shell.execute_reply.started":"2023-11-25T06:20:06.381736Z","shell.execute_reply":"2023-11-25T06:20:06.609279Z"},"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-11-25T06:20:18.940924Z","iopub.execute_input":"2023-11-25T06:20:18.941343Z","iopub.status.idle":"2023-11-25T06:20:19.091664Z","shell.execute_reply.started":"2023-11-25T06:20:18.941309Z","shell.execute_reply":"2023-11-25T06:20:19.090532Z"},"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-25T06:20:46.582403Z","iopub.execute_input":"2023-11-25T06:20:46.582862Z","iopub.status.idle":"2023-11-25T06:20:46.643767Z","shell.execute_reply.started":"2023-11-25T06:20:46.582827Z","shell.execute_reply":"2023-11-25T06:20:46.642744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:46.800248Z","iopub.execute_input":"2023-11-25T06:20:46.800807Z","iopub.status.idle":"2023-11-25T06:20:46.810767Z","shell.execute_reply.started":"2023-11-25T06:20:46.800769Z","shell.execute_reply":"2023-11-25T06:20:46.809605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:47.219109Z","iopub.execute_input":"2023-11-25T06:20:47.220295Z","iopub.status.idle":"2023-11-25T06:20:47.239796Z","shell.execute_reply.started":"2023-11-25T06:20:47.220251Z","shell.execute_reply":"2023-11-25T06:20:47.238309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 7796]","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:47.706435Z","iopub.execute_input":"2023-11-25T06:20:47.706910Z","iopub.status.idle":"2023-11-25T06:20:47.721447Z","shell.execute_reply.started":"2023-11-25T06:20:47.706875Z","shell.execute_reply":"2023-11-25T06:20:47.720210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:48.113398Z","iopub.execute_input":"2023-11-25T06:20:48.113864Z","iopub.status.idle":"2023-11-25T06:20:48.126150Z","shell.execute_reply.started":"2023-11-25T06:20:48.113831Z","shell.execute_reply":"2023-11-25T06:20:48.124536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.tail(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:48.607357Z","iopub.execute_input":"2023-11-25T06:20:48.608390Z","iopub.status.idle":"2023-11-25T06:20:48.620335Z","shell.execute_reply.started":"2023-11-25T06:20:48.608339Z","shell.execute_reply":"2023-11-25T06:20:48.619064Z"},"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-25T06:20:49.528352Z","iopub.execute_input":"2023-11-25T06:20:49.529238Z","iopub.status.idle":"2023-11-25T06:20:49.549853Z","shell.execute_reply.started":"2023-11-25T06:20:49.529191Z","shell.execute_reply":"2023-11-25T06:20:49.548534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df['label']","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:49.845085Z","iopub.execute_input":"2023-11-25T06:20:49.846118Z","iopub.status.idle":"2023-11-25T06:20:49.855812Z","shell.execute_reply.started":"2023-11-25T06:20:49.846077Z","shell.execute_reply":"2023-11-25T06:20:49.854548Z"},"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-25T06:20:50.392567Z","iopub.execute_input":"2023-11-25T06:20:50.392974Z","iopub.status.idle":"2023-11-25T06:20:50.411153Z","shell.execute_reply.started":"2023-11-25T06:20:50.392943Z","shell.execute_reply":"2023-11-25T06:20:50.409947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels[0:5])\nprint(labels[-5:])","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:50.937558Z","iopub.execute_input":"2023-11-25T06:20:50.937967Z","iopub.status.idle":"2023-11-25T06:20:50.943613Z","shell.execute_reply.started":"2023-11-25T06:20:50.937935Z","shell.execute_reply":"2023-11-25T06:20:50.942539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:20:51.588304Z","iopub.execute_input":"2023-11-25T06:20:51.588752Z","iopub.status.idle":"2023-11-25T06:20:51.597481Z","shell.execute_reply.started":"2023-11-25T06:20:51.588717Z","shell.execute_reply":"2023-11-25T06:20:51.596028Z"},"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-25T06:21:04.819317Z","iopub.execute_input":"2023-11-25T06:21:04.819719Z","iopub.status.idle":"2023-11-25T06:21:05.030261Z","shell.execute_reply.started":"2023-11-25T06:21:04.819685Z","shell.execute_reply":"2023-11-25T06:21:05.029201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 9999]","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:21:09.040275Z","iopub.execute_input":"2023-11-25T06:21:09.040743Z","iopub.status.idle":"2023-11-25T06:21:09.053884Z","shell.execute_reply.started":"2023-11-25T06:21:09.040706Z","shell.execute_reply":"2023-11-25T06:21:09.052705Z"},"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-25T06:21:10.543917Z","iopub.execute_input":"2023-11-25T06:21:10.544331Z","iopub.status.idle":"2023-11-25T06:21:10.551213Z","shell.execute_reply.started":"2023-11-25T06:21:10.544301Z","shell.execute_reply":"2023-11-25T06:21:10.549866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list = list(labels_df['id'])","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:21:14.731374Z","iopub.execute_input":"2023-11-25T06:21:14.731818Z","iopub.status.idle":"2023-11-25T06:21:14.746794Z","shell.execute_reply.started":"2023-11-25T06:21:14.731782Z","shell.execute_reply":"2023-11-25T06:21:14.745612Z"},"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-25T06:21:15.000349Z","iopub.execute_input":"2023-11-25T06:21:15.001456Z","iopub.status.idle":"2023-11-25T06:21:15.007954Z","shell.execute_reply.started":"2023-11-25T06:21:15.001406Z","shell.execute_reply":"2023-11-25T06:21:15.006588Z"},"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-11-25T06:17:06.398830Z","iopub.execute_input":"2023-11-25T06:17:06.399254Z","iopub.status.idle":"2023-11-25T06:17:06.408686Z","shell.execute_reply.started":"2023-11-25T06:17:06.399218Z","shell.execute_reply":"2023-11-25T06:17:06.407501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:17:08.897823Z","iopub.execute_input":"2023-11-25T06:17:08.898209Z","iopub.status.idle":"2023-11-25T06:17:08.905755Z","shell.execute_reply.started":"2023-11-25T06:17:08.898178Z","shell.execute_reply":"2023-11-25T06:17:08.904416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data[0])","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:17:13.063083Z","iopub.execute_input":"2023-11-25T06:17:13.063467Z","iopub.status.idle":"2023-11-25T06:17:13.070952Z","shell.execute_reply.started":"2023-11-25T06:17:13.063437Z","shell.execute_reply":"2023-11-25T06:17:13.069964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:17:16.053153Z","iopub.execute_input":"2023-11-25T06:17:16.053550Z","iopub.status.idle":"2023-11-25T06:17:16.062214Z","shell.execute_reply.started":"2023-11-25T06:17:16.053503Z","shell.execute_reply":"2023-11-25T06:17:16.060322Z"},"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-25T06:48:52.504038Z","iopub.execute_input":"2023-11-25T06:48:52.505314Z","iopub.status.idle":"2023-11-25T06:48:52.515434Z","shell.execute_reply.started":"2023-11-25T06:48:52.505273Z","shell.execute_reply":"2023-11-25T06:48:52.514221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(x)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:48:52.769947Z","iopub.execute_input":"2023-11-25T06:48:52.770342Z","iopub.status.idle":"2023-11-25T06:48:52.778264Z","shell.execute_reply.started":"2023-11-25T06:48:52.770311Z","shell.execute_reply":"2023-11-25T06:48:52.776791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:48:53.813146Z","iopub.execute_input":"2023-11-25T06:48:53.813563Z","iopub.status.idle":"2023-11-25T06:48:53.820210Z","shell.execute_reply.started":"2023-11-25T06:48:53.813529Z","shell.execute_reply":"2023-11-25T06:48:53.818860Z"},"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-25T06:48:54.016663Z","iopub.execute_input":"2023-11-25T06:48:54.017748Z","iopub.status.idle":"2023-11-25T06:48:54.023110Z","shell.execute_reply.started":"2023-11-25T06:48:54.017707Z","shell.execute_reply":"2023-11-25T06:48:54.021841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:48:54.223474Z","iopub.execute_input":"2023-11-25T06:48:54.223905Z","iopub.status.idle":"2023-11-25T06:48:54.233046Z","shell.execute_reply.started":"2023-11-25T06:48:54.223873Z","shell.execute_reply":"2023-11-25T06:48:54.231827Z"},"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-25T06:48:54.365920Z","iopub.execute_input":"2023-11-25T06:48:54.366332Z","iopub.status.idle":"2023-11-25T06:48:54.372739Z","shell.execute_reply.started":"2023-11-25T06:48:54.366298Z","shell.execute_reply":"2023-11-25T06:48:54.371634Z"},"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-25T06:48:54.521631Z","iopub.execute_input":"2023-11-25T06:48:54.522083Z","iopub.status.idle":"2023-11-25T06:48:54.528837Z","shell.execute_reply.started":"2023-11-25T06:48:54.522047Z","shell.execute_reply":"2023-11-25T06:48:54.527451Z"},"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-25T06:48:54.625559Z","iopub.execute_input":"2023-11-25T06:48:54.625990Z","iopub.status.idle":"2023-11-25T06:48:54.632708Z","shell.execute_reply.started":"2023-11-25T06:48:54.625954Z","shell.execute_reply":"2023-11-25T06:48:54.631322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_scaled[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:48:54.746563Z","iopub.execute_input":"2023-11-25T06:48:54.746955Z","iopub.status.idle":"2023-11-25T06:48:54.757356Z","shell.execute_reply.started":"2023-11-25T06:48:54.746924Z","shell.execute_reply":"2023-11-25T06:48:54.756033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building 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-25T06:48:55.029740Z","iopub.execute_input":"2023-11-25T06:48:55.030399Z","iopub.status.idle":"2023-11-25T06:48:55.035822Z","shell.execute_reply.started":"2023-11-25T06:48:55.030352Z","shell.execute_reply":"2023-11-25T06:48:55.034427Z"},"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-25T06:48:55.150293Z","iopub.execute_input":"2023-11-25T06:48:55.151043Z","iopub.status.idle":"2023-11-25T06:48:55.157991Z","shell.execute_reply.started":"2023-11-25T06:48:55.151004Z","shell.execute_reply":"2023-11-25T06:48:55.156834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Resnet: https://keras.io/api/applications/resnet/","metadata":{}},{"cell_type":"code","source":"convolutional_base = ResNet50(weights='imagenet', include_top=False, input_shape=(256,256,3))","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:48:55.572609Z","iopub.execute_input":"2023-11-25T06:48:55.572989Z","iopub.status.idle":"2023-11-25T06:48:57.804893Z","shell.execute_reply.started":"2023-11-25T06:48:55.572959Z","shell.execute_reply":"2023-11-25T06:48:57.803562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convolutional_base.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:48:57.807093Z","iopub.execute_input":"2023-11-25T06:48:57.807448Z","iopub.status.idle":"2023-11-25T06:48:58.394039Z","shell.execute_reply.started":"2023-11-25T06:48:57.807418Z","shell.execute_reply":"2023-11-25T06:48:58.392059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_classes = 10\n\nmodel = models.Sequential() \nmodel.add(layers.UpSampling2D((2,2))) # 32 * 2 = 64\nmodel.add(layers.UpSampling2D((2,2))) # 64 * 2 = 128\nmodel.add(layers.UpSampling2D((2,2))) # 128 * 2 = 256\n\nmodel.add(convolutional_base)\n\nmodel.add(layers.Flatten())  # transforms the input into a one-dimensional array (flattens it) without modifying the actual data. \n\nmodel.add(layers.BatchNormalization()) \n\nmodel.add(layers.Dense(32, activation='relu')) # less neuron\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.BatchNormalization())\n\nmodel.add(layers.Dense(16, activation='relu')) # less neuron # extra layer\n\nmodel.add(layers.Dense(num_of_classes, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2023-12-05T06:12:07.843201Z","iopub.execute_input":"2023-12-05T06:12:07.843622Z","iopub.status.idle":"2023-12-05T06:12:07.883014Z","shell.execute_reply.started":"2023-12-05T06:12:07.843588Z","shell.execute_reply":"2023-12-05T06:12:07.881904Z"},"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-11-25T06:48:58.460562Z","iopub.execute_input":"2023-11-25T06:48:58.460941Z","iopub.status.idle":"2023-11-25T06:48:58.482620Z","shell.execute_reply.started":"2023-11-25T06:48:58.460909Z","shell.execute_reply":"2023-11-25T06:48:58.481348Z"},"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= 15, batch_size=100)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:52:02.613018Z","iopub.execute_input":"2023-11-25T06:52:02.614004Z","iopub.status.idle":"2023-11-25T06:58:24.619835Z","shell.execute_reply.started":"2023-11-25T06:52:02.613961Z","shell.execute_reply":"2023-11-25T06:58:24.618526Z"},"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-25T06:58:25.220395Z","iopub.execute_input":"2023-11-25T06:58:25.220715Z","iopub.status.idle":"2023-11-25T06:58:25.225950Z","shell.execute_reply.started":"2023-11-25T06:58:25.220687Z","shell.execute_reply":"2023-11-25T06:58:25.224743Z"},"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-25T06:58:25.229796Z","iopub.execute_input":"2023-11-25T06:58:25.230255Z","iopub.status.idle":"2023-11-25T06:58:27.620225Z","shell.execute_reply.started":"2023-11-25T06:58:25.230214Z","shell.execute_reply":"2023-11-25T06:58:27.619033Z"},"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-25T06:58:27.622020Z","iopub.execute_input":"2023-11-25T06:58:27.623326Z","iopub.status.idle":"2023-11-25T06:58:27.629086Z","shell.execute_reply.started":"2023-11-25T06:58:27.623280Z","shell.execute_reply":"2023-11-25T06:58:27.627932Z"},"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-11-25T06:58:27.630598Z","iopub.execute_input":"2023-11-25T06:58:27.630968Z","iopub.status.idle":"2023-11-25T06:58:27.645127Z","shell.execute_reply.started":"2023-11-25T06:58:27.630939Z","shell.execute_reply":"2023-11-25T06:58:27.644285Z"},"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-25T06:58:27.646419Z","iopub.execute_input":"2023-11-25T06:58:27.646929Z","iopub.status.idle":"2023-11-25T06:58:30.065392Z","shell.execute_reply.started":"2023-11-25T06:58:27.646900Z","shell.execute_reply":"2023-11-25T06:58:30.064597Z"},"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-25T06:58:30.066463Z","iopub.execute_input":"2023-11-25T06:58:30.067352Z","iopub.status.idle":"2023-11-25T06:58:30.664821Z","shell.execute_reply.started":"2023-11-25T06:58:30.067317Z","shell.execute_reply":"2023-11-25T06:58:30.663538Z"},"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-25T06:58:30.666327Z","iopub.execute_input":"2023-11-25T06:58:30.666803Z","iopub.status.idle":"2023-11-25T06:58:30.672049Z","shell.execute_reply.started":"2023-11-25T06:58:30.666769Z","shell.execute_reply":"2023-11-25T06:58:30.670873Z"},"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-25T06:58:30.675483Z","iopub.execute_input":"2023-11-25T06:58:30.675901Z","iopub.status.idle":"2023-11-25T06:58:30.684547Z","shell.execute_reply.started":"2023-11-25T06:58:30.675871Z","shell.execute_reply":"2023-11-25T06:58:30.683381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(conf_mat)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T06:58:30.685914Z","iopub.execute_input":"2023-11-25T06:58:30.686250Z","iopub.status.idle":"2023-11-25T06:58:30.697911Z","shell.execute_reply.started":"2023-11-25T06:58:30.686221Z","shell.execute_reply":"2023-11-25T06:58:30.696687Z"},"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-25T06:58:30.699286Z","iopub.execute_input":"2023-11-25T06:58:30.699674Z","iopub.status.idle":"2023-11-25T06:58:31.433104Z","shell.execute_reply.started":"2023-11-25T06:58:30.699645Z","shell.execute_reply":"2023-11-25T06:58:31.432015Z"},"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-11-25T06:49:51.049788Z","iopub.status.idle":"2023-11-25T06:49:51.050783Z","shell.execute_reply.started":"2023-11-25T06:49:51.050562Z","shell.execute_reply":"2023-11-25T06:49:51.050584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}