{"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":15444,"sourceType":"datasetVersion","datasetId":11102},{"sourceId":151761756,"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-23T19:41:15.447111Z","iopub.execute_input":"2023-11-23T19:41:15.447536Z","iopub.status.idle":"2023-11-23T19:41:15.453949Z","shell.execute_reply.started":"2023-11-23T19:41:15.447498Z","shell.execute_reply":"2023-11-23T19:41:15.452620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Analyzing the train set**","metadata":{}},{"cell_type":"code","source":"# !ls /kaggle/input/extracting-train/train","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:18:54.561255Z","iopub.execute_input":"2023-11-23T19:18:54.561861Z","iopub.status.idle":"2023-11-23T19:18:54.566574Z","shell.execute_reply.started":"2023-11-23T19:18:54.561814Z","shell.execute_reply":"2023-11-23T19:18:54.565362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# counting the number of files in train folder\npath, dirs, files = next(os.walk('/kaggle/input/extracting-train/train'))\nfile_count = len(files)\nprint('Number of images: ', file_count)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:18:54.568088Z","iopub.execute_input":"2023-11-23T19:18:54.568970Z","iopub.status.idle":"2023-11-23T19:19:38.444074Z","shell.execute_reply.started":"2023-11-23T19:18:54.568938Z","shell.execute_reply":"2023-11-23T19:19:38.442691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Displaying the images**","metadata":{}},{"cell_type":"code","source":"#display an image\nimg = cv2.imread('/kaggle/input/extracting-train/train/9999.png')\nplt.figure(figsize=(2,1))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:38.447162Z","iopub.execute_input":"2023-11-23T19:19:38.447561Z","iopub.status.idle":"2023-11-23T19:19:38.792274Z","shell.execute_reply.started":"2023-11-23T19:19:38.447527Z","shell.execute_reply":"2023-11-23T19:19:38.791136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display an image\nimg = cv2.imread('/kaggle/input/extracting-train/train/10013.png')\nplt.figure(figsize=(2,1))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:38.793743Z","iopub.execute_input":"2023-11-23T19:19:38.794055Z","iopub.status.idle":"2023-11-23T19:19:39.008215Z","shell.execute_reply.started":"2023-11-23T19:19:38.794028Z","shell.execute_reply":"2023-11-23T19:19:39.007169Z"},"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-23T19:19:39.009750Z","iopub.execute_input":"2023-11-23T19:19:39.010196Z","iopub.status.idle":"2023-11-23T19:19:39.054477Z","shell.execute_reply.started":"2023-11-23T19:19:39.010150Z","shell.execute_reply":"2023-11-23T19:19:39.053476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.056118Z","iopub.execute_input":"2023-11-23T19:19:39.056593Z","iopub.status.idle":"2023-11-23T19:19:39.064892Z","shell.execute_reply.started":"2023-11-23T19:19:39.056549Z","shell.execute_reply":"2023-11-23T19:19:39.063517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.066752Z","iopub.execute_input":"2023-11-23T19:19:39.067302Z","iopub.status.idle":"2023-11-23T19:19:39.088020Z","shell.execute_reply.started":"2023-11-23T19:19:39.067260Z","shell.execute_reply":"2023-11-23T19:19:39.086458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 7796]","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.089357Z","iopub.execute_input":"2023-11-23T19:19:39.089722Z","iopub.status.idle":"2023-11-23T19:19:39.105098Z","shell.execute_reply.started":"2023-11-23T19:19:39.089684Z","shell.execute_reply":"2023-11-23T19:19:39.104083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.109630Z","iopub.execute_input":"2023-11-23T19:19:39.109991Z","iopub.status.idle":"2023-11-23T19:19:39.122116Z","shell.execute_reply.started":"2023-11-23T19:19:39.109960Z","shell.execute_reply":"2023-11-23T19:19:39.121019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.tail(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.123314Z","iopub.execute_input":"2023-11-23T19:19:39.124107Z","iopub.status.idle":"2023-11-23T19:19:39.139657Z","shell.execute_reply.started":"2023-11-23T19:19:39.124063Z","shell.execute_reply":"2023-11-23T19:19:39.138495Z"},"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-23T19:19:39.141534Z","iopub.execute_input":"2023-11-23T19:19:39.141925Z","iopub.status.idle":"2023-11-23T19:19:39.161845Z","shell.execute_reply.started":"2023-11-23T19:19:39.141896Z","shell.execute_reply":"2023-11-23T19:19:39.160808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df['label']","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.163312Z","iopub.execute_input":"2023-11-23T19:19:39.163641Z","iopub.status.idle":"2023-11-23T19:19:39.173346Z","shell.execute_reply.started":"2023-11-23T19:19:39.163614Z","shell.execute_reply":"2023-11-23T19:19:39.172218Z"},"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-23T19:19:39.175179Z","iopub.execute_input":"2023-11-23T19:19:39.175638Z","iopub.status.idle":"2023-11-23T19:19:39.196150Z","shell.execute_reply.started":"2023-11-23T19:19:39.175594Z","shell.execute_reply":"2023-11-23T19:19:39.195171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels[0:5])\nprint(labels[-5:])","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.197729Z","iopub.execute_input":"2023-11-23T19:19:39.198151Z","iopub.status.idle":"2023-11-23T19:19:39.210168Z","shell.execute_reply.started":"2023-11-23T19:19:39.198110Z","shell.execute_reply":"2023-11-23T19:19:39.208702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.212329Z","iopub.execute_input":"2023-11-23T19:19:39.212851Z","iopub.status.idle":"2023-11-23T19:19:39.224612Z","shell.execute_reply.started":"2023-11-23T19:19:39.212808Z","shell.execute_reply":"2023-11-23T19:19:39.223480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Checking labels correctness:","metadata":{}},{"cell_type":"code","source":"img = cv2.imread('/kaggle/input/extracting-train/train/9999.png')\nplt.figure(figsize=(2,1))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.226329Z","iopub.execute_input":"2023-11-23T19:19:39.226963Z","iopub.status.idle":"2023-11-23T19:19:39.441830Z","shell.execute_reply.started":"2023-11-23T19:19:39.226919Z","shell.execute_reply":"2023-11-23T19:19:39.440663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 9999]","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.443261Z","iopub.execute_input":"2023-11-23T19:19:39.444396Z","iopub.status.idle":"2023-11-23T19:19:39.456617Z","shell.execute_reply.started":"2023-11-23T19:19:39.444352Z","shell.execute_reply":"2023-11-23T19:19:39.455490Z"},"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-23T19:19:39.458317Z","iopub.execute_input":"2023-11-23T19:19:39.458789Z","iopub.status.idle":"2023-11-23T19:19:39.467860Z","shell.execute_reply.started":"2023-11-23T19:19:39.458748Z","shell.execute_reply":"2023-11-23T19:19:39.466688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list = list(labels_df['id'])","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.469541Z","iopub.execute_input":"2023-11-23T19:19:39.470712Z","iopub.status.idle":"2023-11-23T19:19:39.486830Z","shell.execute_reply.started":"2023-11-23T19:19:39.470672Z","shell.execute_reply":"2023-11-23T19:19:39.485646Z"},"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-23T19:19:39.488496Z","iopub.execute_input":"2023-11-23T19:19:39.488869Z","iopub.status.idle":"2023-11-23T19:19:39.500375Z","shell.execute_reply.started":"2023-11-23T19:19:39.488825Z","shell.execute_reply":"2023-11-23T19:19:39.499507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Image Processing**","metadata":{}},{"cell_type":"code","source":"# convert images to numpy arrays\n\ntrain_data_folder = '/kaggle/input/extracting-train/train/'\n\ndata = []\n\nfor id in id_list:\n    image = Image.open(train_data_folder + str(id) + '.png')\n    image = np.array(image)\n    data.append(image)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:19:39.501633Z","iopub.execute_input":"2023-11-23T19:19:39.502575Z","iopub.status.idle":"2023-11-23T19:22:30.147201Z","shell.execute_reply.started":"2023-11-23T19:19:39.502544Z","shell.execute_reply":"2023-11-23T19:22:30.146181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:22:30.149722Z","iopub.execute_input":"2023-11-23T19:22:30.150261Z","iopub.status.idle":"2023-11-23T19:22:30.158941Z","shell.execute_reply.started":"2023-11-23T19:22:30.150137Z","shell.execute_reply":"2023-11-23T19:22:30.157699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:22:30.160636Z","iopub.execute_input":"2023-11-23T19:22:30.161142Z","iopub.status.idle":"2023-11-23T19:22:30.170876Z","shell.execute_reply.started":"2023-11-23T19:22:30.161097Z","shell.execute_reply":"2023-11-23T19:22:30.169743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data[0])","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:22:30.172165Z","iopub.execute_input":"2023-11-23T19:22:30.172539Z","iopub.status.idle":"2023-11-23T19:22:30.188575Z","shell.execute_reply.started":"2023-11-23T19:22:30.172493Z","shell.execute_reply":"2023-11-23T19:22:30.186508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:22:30.190130Z","iopub.execute_input":"2023-11-23T19:22:30.190616Z","iopub.status.idle":"2023-11-23T19:22:30.198999Z","shell.execute_reply.started":"2023-11-23T19:22:30.190581Z","shell.execute_reply":"2023-11-23T19:22:30.197787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert image list and label list to numpy arrays\n\nX = np.array(data)\nY = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:33:40.595380Z","iopub.execute_input":"2023-11-23T19:33:40.595793Z","iopub.status.idle":"2023-11-23T19:33:40.683792Z","shell.execute_reply.started":"2023-11-23T19:33:40.595761Z","shell.execute_reply":"2023-11-23T19:33:40.682625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(X)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:33:41.033210Z","iopub.execute_input":"2023-11-23T19:33:41.034115Z","iopub.status.idle":"2023-11-23T19:33:41.041577Z","shell.execute_reply.started":"2023-11-23T19:33:41.034063Z","shell.execute_reply":"2023-11-23T19:33:41.040503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(Y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:33:41.687964Z","iopub.execute_input":"2023-11-23T19:33:41.688667Z","iopub.status.idle":"2023-11-23T19:33:41.693644Z","shell.execute_reply.started":"2023-11-23T19:33:41.688631Z","shell.execute_reply":"2023-11-23T19:33:41.692641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Train Test Split**","metadata":{}},{"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-23T19:33:49.218294Z","iopub.execute_input":"2023-11-23T19:33:49.218709Z","iopub.status.idle":"2023-11-23T19:33:49.224160Z","shell.execute_reply.started":"2023-11-23T19:33:49.218677Z","shell.execute_reply":"2023-11-23T19:33:49.222768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:40:12.637964Z","iopub.execute_input":"2023-11-23T19:40:12.638401Z","iopub.status.idle":"2023-11-23T19:40:12.647812Z","shell.execute_reply.started":"2023-11-23T19:40:12.638364Z","shell.execute_reply":"2023-11-23T19:40:12.646638Z"},"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-23T19:33:50.784011Z","iopub.execute_input":"2023-11-23T19:33:50.784396Z","iopub.status.idle":"2023-11-23T19:33:50.797938Z","shell.execute_reply.started":"2023-11-23T19:33:50.784365Z","shell.execute_reply":"2023-11-23T19:33:50.796891Z"},"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-23T19:33:51.841586Z","iopub.execute_input":"2023-11-23T19:33:51.842705Z","iopub.status.idle":"2023-11-23T19:33:51.848364Z","shell.execute_reply.started":"2023-11-23T19:33:51.842657Z","shell.execute_reply":"2023-11-23T19:33:51.847201Z"},"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-23T19:33:53.415269Z","iopub.execute_input":"2023-11-23T19:33:53.415880Z","iopub.status.idle":"2023-11-23T19:33:53.433805Z","shell.execute_reply.started":"2023-11-23T19:33:53.415844Z","shell.execute_reply":"2023-11-23T19:33:53.432636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_scaled[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:40:01.741071Z","iopub.execute_input":"2023-11-23T19:40:01.741549Z","iopub.status.idle":"2023-11-23T19:40:01.751305Z","shell.execute_reply.started":"2023-11-23T19:40:01.741514Z","shell.execute_reply":"2023-11-23T19:40:01.749890Z"},"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-23T19:22:30.990266Z","iopub.execute_input":"2023-11-23T19:22:30.990832Z","iopub.status.idle":"2023-11-23T19:22:44.548596Z","shell.execute_reply.started":"2023-11-23T19:22:30.990798Z","shell.execute_reply":"2023-11-23T19:22:44.547597Z"},"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-23T19:22:44.550032Z","iopub.execute_input":"2023-11-23T19:22:44.551129Z","iopub.status.idle":"2023-11-23T19:22:44.716584Z","shell.execute_reply.started":"2023-11-23T19:22:44.551091Z","shell.execute_reply":"2023-11-23T19:22:44.715476Z"},"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-23T19:22:44.718056Z","iopub.execute_input":"2023-11-23T19:22:44.719337Z","iopub.status.idle":"2023-11-23T19:22:51.996562Z","shell.execute_reply.started":"2023-11-23T19:22:44.719302Z","shell.execute_reply":"2023-11-23T19:22:51.995558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convolutional_base.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:22:51.998240Z","iopub.execute_input":"2023-11-23T19:22:51.998572Z","iopub.status.idle":"2023-11-23T19:22:52.575664Z","shell.execute_reply.started":"2023-11-23T19:22:51.998544Z","shell.execute_reply":"2023-11-23T19:22:52.574485Z"},"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()) #normalization\nmodel.add(layers.Dense(128, activation='relu'))\n\nmodel.add(layers.Dropout(0.5)) #droping or making somen neuron inactive to avoid overfitting\n\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(num_of_classes, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:22:52.577281Z","iopub.execute_input":"2023-11-23T19:22:52.577646Z","iopub.status.idle":"2023-11-23T19:22:52.636866Z","shell.execute_reply.started":"2023-11-23T19:22:52.577614Z","shell.execute_reply":"2023-11-23T19:22:52.635477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Metrics and model compilation","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import backend as K","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:27:09.786389Z","iopub.execute_input":"2023-11-23T19:27:09.786842Z","iopub.status.idle":"2023-11-23T19:27:09.792325Z","shell.execute_reply.started":"2023-11-23T19:27:09.786807Z","shell.execute_reply":"2023-11-23T19:27:09.790961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def recall(y_true, y_pred):\n    y_true = K.ones_like(y_true)\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    all_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n\n    recall = true_positives / (all_positives + K.epsilon())\n    return recall","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:27:36.771567Z","iopub.execute_input":"2023-11-23T19:27:36.771963Z","iopub.status.idle":"2023-11-23T19:27:36.779752Z","shell.execute_reply.started":"2023-11-23T19:27:36.771932Z","shell.execute_reply":"2023-11-23T19:27:36.778169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def precision(y_true, y_pred):\n    y_true = K.ones_like(y_true)\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:32:59.044771Z","iopub.execute_input":"2023-11-23T19:32:59.045167Z","iopub.status.idle":"2023-11-23T19:32:59.050932Z","shell.execute_reply.started":"2023-11-23T19:32:59.045135Z","shell.execute_reply":"2023-11-23T19:32:59.050071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef f1_score(y_true, y_pred):\n    precision_m = precision(y_true, y_pred)\n    recall_m = recall(y_true, y_pred)\n    return 2*((precision_m*recall_m)/(precision_m+recall_m+K.epsilon()))","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:32:59.572316Z","iopub.execute_input":"2023-11-23T19:32:59.573281Z","iopub.status.idle":"2023-11-23T19:32:59.579591Z","shell.execute_reply.started":"2023-11-23T19:32:59.573241Z","shell.execute_reply":"2023-11-23T19:32:59.578301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='sparse_categorical_crossentropy', metrics=['acc', f1_score, precision, recall])","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:33:00.366691Z","iopub.execute_input":"2023-11-23T19:33:00.367107Z","iopub.status.idle":"2023-11-23T19:33:00.388881Z","shell.execute_reply.started":"2023-11-23T19:33:00.367072Z","shell.execute_reply":"2023-11-23T19:33:00.387804Z"},"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-11-23T19:34:13.841844Z","iopub.execute_input":"2023-11-23T19:34:13.842247Z","iopub.status.idle":"2023-11-23T19:36:19.315430Z","shell.execute_reply.started":"2023-11-23T19:34:13.842215Z","shell.execute_reply":"2023-11-23T19:36:19.314209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Evaluation with different metrics and plot","metadata":{}},{"cell_type":"code","source":"loss, accuracy, f1_score, precision, recall  = model.evaluate(X_test_scaled, Y_test)\nprint('Test Accuracy =', accuracy)\nprint('Precision = ', precision)\nprint('Recall = ', recall)\nprint('f1_score = ', f1_score)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:36:19.317736Z","iopub.execute_input":"2023-11-23T19:36:19.318203Z","iopub.status.idle":"2023-11-23T19:36:21.845273Z","shell.execute_reply.started":"2023-11-23T19:36:19.318170Z","shell.execute_reply":"2023-11-23T19:36:21.844372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Extract the metrics from the history object\ntrain_accuracy = history.history['acc']\nval_accuracy = history.history['val_acc']\ntrain_loss = history.history['loss']\nval_loss = history.history['val_loss']\ntrain_f1 = history.history['f1_score']\nval_f1 = history.history['val_f1_score']\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()\n\n# Plot F1 score\nplt.plot(train_f1, label='Train F1 Score')\nplt.plot(val_f1, label='Validation F1 Score')\nplt.legend()\nplt.xlabel('Epochs')\nplt.ylabel('F1 Score')\nplt.title('Training and Validation F1 Score')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:37:51.543772Z","iopub.execute_input":"2023-11-23T19:37:51.544310Z","iopub.status.idle":"2023-11-23T19:37:52.551406Z","shell.execute_reply.started":"2023-11-23T19:37:51.544271Z","shell.execute_reply":"2023-11-23T19:37:52.550258Z"},"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-23T19:38:47.808954Z","iopub.execute_input":"2023-11-23T19:38:47.809698Z","iopub.status.idle":"2023-11-23T19:38:47.813979Z","shell.execute_reply.started":"2023-11-23T19:38:47.809663Z","shell.execute_reply":"2023-11-23T19:38:47.812844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_prediction = model.predict(X_test_scaled)\nY_pred_labels = [np.argmax(i) for i in Y_prediction]","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:38:48.270223Z","iopub.execute_input":"2023-11-23T19:38:48.270876Z","iopub.status.idle":"2023-11-23T19:38:51.246263Z","shell.execute_reply.started":"2023-11-23T19:38:48.270838Z","shell.execute_reply":"2023-11-23T19:38:51.245484Z"},"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-23T19:38:51.248454Z","iopub.execute_input":"2023-11-23T19:38:51.249694Z","iopub.status.idle":"2023-11-23T19:38:51.282099Z","shell.execute_reply.started":"2023-11-23T19:38:51.249649Z","shell.execute_reply":"2023-11-23T19:38:51.281236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(conf_mat)","metadata":{"execution":{"iopub.status.busy":"2023-11-23T19:38:51.283636Z","iopub.execute_input":"2023-11-23T19:38:51.284264Z","iopub.status.idle":"2023-11-23T19:38:51.291158Z","shell.execute_reply.started":"2023-11-23T19:38:51.284232Z","shell.execute_reply":"2023-11-23T19:38:51.290050Z"},"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-23T19:38:51.293529Z","iopub.execute_input":"2023-11-23T19:38:51.294224Z","iopub.status.idle":"2023-11-23T19:38:52.048848Z","shell.execute_reply.started":"2023-11-23T19:38:51.294175Z","shell.execute_reply":"2023-11-23T19:38:52.047662Z"},"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-22T08:56:32.596649Z","iopub.execute_input":"2023-11-22T08:56:32.597050Z","iopub.status.idle":"2023-11-22T08:56:32.621771Z","shell.execute_reply.started":"2023-11-22T08:56:32.596996Z","shell.execute_reply":"2023-11-22T08:56:32.620586Z"},"trusted":true},"execution_count":null,"outputs":[]}]}