{"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-12-10T19:24:44.552835Z","iopub.execute_input":"2023-12-10T19:24:44.553201Z","iopub.status.idle":"2023-12-10T19:24:46.915197Z","shell.execute_reply.started":"2023-12-10T19:24:44.553172Z","shell.execute_reply":"2023-12-10T19:24:46.913838Z"},"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-10T19:24:46.917270Z","iopub.execute_input":"2023-12-10T19:24:46.917896Z","iopub.status.idle":"2023-12-10T19:24:48.749163Z","shell.execute_reply.started":"2023-12-10T19:24:46.917859Z","shell.execute_reply":"2023-12-10T19:24:48.747175Z"},"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-10T19:24:48.751549Z","iopub.execute_input":"2023-12-10T19:24:48.752061Z","iopub.status.idle":"2023-12-10T19:24:48.764946Z","shell.execute_reply.started":"2023-12-10T19:24:48.752013Z","shell.execute_reply":"2023-12-10T19:24:48.761534Z"},"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-10T19:24:48.768582Z","iopub.execute_input":"2023-12-10T19:24:48.769324Z","iopub.status.idle":"2023-12-10T19:24:49.157068Z","shell.execute_reply.started":"2023-12-10T19:24:48.769259Z","shell.execute_reply":"2023-12-10T19:24:49.155750Z"},"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-10T19:24:49.158725Z","iopub.execute_input":"2023-12-10T19:24:49.159204Z","iopub.status.idle":"2023-12-10T19:24:49.387113Z","shell.execute_reply.started":"2023-12-10T19:24:49.159158Z","shell.execute_reply":"2023-12-10T19:24:49.385894Z"},"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-10T19:24:49.389048Z","iopub.execute_input":"2023-12-10T19:24:49.389821Z","iopub.status.idle":"2023-12-10T19:24:49.445716Z","shell.execute_reply.started":"2023-12-10T19:24:49.389774Z","shell.execute_reply":"2023-12-10T19:24:49.444495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.447537Z","iopub.execute_input":"2023-12-10T19:24:49.448300Z","iopub.status.idle":"2023-12-10T19:24:49.455354Z","shell.execute_reply.started":"2023-12-10T19:24:49.448253Z","shell.execute_reply":"2023-12-10T19:24:49.454272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.457047Z","iopub.execute_input":"2023-12-10T19:24:49.457793Z","iopub.status.idle":"2023-12-10T19:24:49.489919Z","shell.execute_reply.started":"2023-12-10T19:24:49.457751Z","shell.execute_reply":"2023-12-10T19:24:49.488608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 7796]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.492310Z","iopub.execute_input":"2023-12-10T19:24:49.492795Z","iopub.status.idle":"2023-12-10T19:24:49.513305Z","shell.execute_reply.started":"2023-12-10T19:24:49.492751Z","shell.execute_reply":"2023-12-10T19:24:49.511853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.520057Z","iopub.execute_input":"2023-12-10T19:24:49.521080Z","iopub.status.idle":"2023-12-10T19:24:49.535689Z","shell.execute_reply.started":"2023-12-10T19:24:49.521026Z","shell.execute_reply":"2023-12-10T19:24:49.534355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.tail(10)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.537941Z","iopub.execute_input":"2023-12-10T19:24:49.538486Z","iopub.status.idle":"2023-12-10T19:24:49.555129Z","shell.execute_reply.started":"2023-12-10T19:24:49.538440Z","shell.execute_reply":"2023-12-10T19:24:49.553830Z"},"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-10T19:24:49.557053Z","iopub.execute_input":"2023-12-10T19:24:49.557429Z","iopub.status.idle":"2023-12-10T19:24:49.580420Z","shell.execute_reply.started":"2023-12-10T19:24:49.557399Z","shell.execute_reply":"2023-12-10T19:24:49.579166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df['label']","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.582210Z","iopub.execute_input":"2023-12-10T19:24:49.582935Z","iopub.status.idle":"2023-12-10T19:24:49.593719Z","shell.execute_reply.started":"2023-12-10T19:24:49.582789Z","shell.execute_reply":"2023-12-10T19:24:49.592594Z"},"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-10T19:24:49.595637Z","iopub.execute_input":"2023-12-10T19:24:49.596325Z","iopub.status.idle":"2023-12-10T19:24:49.618178Z","shell.execute_reply.started":"2023-12-10T19:24:49.596283Z","shell.execute_reply":"2023-12-10T19:24:49.616956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.620065Z","iopub.execute_input":"2023-12-10T19:24:49.620501Z","iopub.status.idle":"2023-12-10T19:24:49.629681Z","shell.execute_reply.started":"2023-12-10T19:24:49.620456Z","shell.execute_reply":"2023-12-10T19:24:49.628495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels[0:5])\nprint(labels[-5:])","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.631446Z","iopub.execute_input":"2023-12-10T19:24:49.632061Z","iopub.status.idle":"2023-12-10T19:24:49.640254Z","shell.execute_reply.started":"2023-12-10T19:24:49.632027Z","shell.execute_reply":"2023-12-10T19:24:49.638755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.642010Z","iopub.execute_input":"2023-12-10T19:24:49.642943Z","iopub.status.idle":"2023-12-10T19:24:49.655044Z","shell.execute_reply.started":"2023-12-10T19:24:49.642879Z","shell.execute_reply":"2023-12-10T19:24:49.654009Z"},"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-10T19:24:49.656777Z","iopub.execute_input":"2023-12-10T19:24:49.657468Z","iopub.status.idle":"2023-12-10T19:24:49.878198Z","shell.execute_reply.started":"2023-12-10T19:24:49.657435Z","shell.execute_reply":"2023-12-10T19:24:49.876972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['id'] == 9999]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.882665Z","iopub.execute_input":"2023-12-10T19:24:49.883054Z","iopub.status.idle":"2023-12-10T19:24:49.896019Z","shell.execute_reply.started":"2023-12-10T19:24:49.883023Z","shell.execute_reply":"2023-12-10T19:24:49.894762Z"},"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-10T19:24:49.897799Z","iopub.execute_input":"2023-12-10T19:24:49.898260Z","iopub.status.idle":"2023-12-10T19:24:49.912358Z","shell.execute_reply.started":"2023-12-10T19:24:49.898214Z","shell.execute_reply":"2023-12-10T19:24:49.910781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list = list(labels_df['id'])","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.914234Z","iopub.execute_input":"2023-12-10T19:24:49.914805Z","iopub.status.idle":"2023-12-10T19:24:49.933793Z","shell.execute_reply.started":"2023-12-10T19:24:49.914723Z","shell.execute_reply":"2023-12-10T19:24:49.932567Z"},"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-10T19:24:49.935306Z","iopub.execute_input":"2023-12-10T19:24:49.936432Z","iopub.status.idle":"2023-12-10T19:24:49.948560Z","shell.execute_reply.started":"2023-12-10T19:24:49.936381Z","shell.execute_reply":"2023-12-10T19:24:49.947272Z"},"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-10T19:24:49.950107Z","iopub.execute_input":"2023-12-10T19:24:49.951146Z","iopub.status.idle":"2023-12-10T19:24:49.962141Z","shell.execute_reply.started":"2023-12-10T19:24:49.951096Z","shell.execute_reply":"2023-12-10T19:24:49.960736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.963817Z","iopub.execute_input":"2023-12-10T19:24:49.964961Z","iopub.status.idle":"2023-12-10T19:24:49.974295Z","shell.execute_reply.started":"2023-12-10T19:24:49.964913Z","shell.execute_reply":"2023-12-10T19:24:49.972828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.976543Z","iopub.execute_input":"2023-12-10T19:24:49.977488Z","iopub.status.idle":"2023-12-10T19:24:49.986163Z","shell.execute_reply.started":"2023-12-10T19:24:49.977440Z","shell.execute_reply":"2023-12-10T19:24:49.984839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:49.987819Z","iopub.execute_input":"2023-12-10T19:24:49.988244Z","iopub.status.idle":"2023-12-10T19:24:49.997430Z","shell.execute_reply.started":"2023-12-10T19:24:49.988210Z","shell.execute_reply":"2023-12-10T19:24:49.996255Z"},"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-10T19:24:49.999814Z","iopub.execute_input":"2023-12-10T19:24:50.000805Z","iopub.status.idle":"2023-12-10T19:24:50.014409Z","shell.execute_reply.started":"2023-12-10T19:24:50.000758Z","shell.execute_reply":"2023-12-10T19:24:50.013119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.024108Z","iopub.execute_input":"2023-12-10T19:24:50.027690Z","iopub.status.idle":"2023-12-10T19:24:50.034409Z","shell.execute_reply.started":"2023-12-10T19:24:50.027642Z","shell.execute_reply":"2023-12-10T19:24:50.033591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Swap the last two dimensions\nx = np.swapaxes(x, -1, -3)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.035694Z","iopub.execute_input":"2023-12-10T19:24:50.037814Z","iopub.status.idle":"2023-12-10T19:24:50.043751Z","shell.execute_reply.started":"2023-12-10T19:24:50.037768Z","shell.execute_reply":"2023-12-10T19:24:50.042601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(x)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.045433Z","iopub.execute_input":"2023-12-10T19:24:50.046328Z","iopub.status.idle":"2023-12-10T19:24:50.060885Z","shell.execute_reply.started":"2023-12-10T19:24:50.046284Z","shell.execute_reply":"2023-12-10T19:24:50.059255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.062997Z","iopub.execute_input":"2023-12-10T19:24:50.063759Z","iopub.status.idle":"2023-12-10T19:24:50.072362Z","shell.execute_reply.started":"2023-12-10T19:24:50.063718Z","shell.execute_reply":"2023-12-10T19:24:50.071109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# small data for testing while naking the model\n# x = x[:1000]\n# y = y[:1000]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.074799Z","iopub.execute_input":"2023-12-10T19:24:50.075818Z","iopub.status.idle":"2023-12-10T19:24:50.081742Z","shell.execute_reply.started":"2023-12-10T19:24:50.075775Z","shell.execute_reply":"2023-12-10T19:24:50.080598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.083816Z","iopub.execute_input":"2023-12-10T19:24:50.084693Z","iopub.status.idle":"2023-12-10T19:24:50.096123Z","shell.execute_reply.started":"2023-12-10T19:24:50.084649Z","shell.execute_reply":"2023-12-10T19:24:50.094593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.099591Z","iopub.execute_input":"2023-12-10T19:24:50.099992Z","iopub.status.idle":"2023-12-10T19:24:50.106048Z","shell.execute_reply.started":"2023-12-10T19:24:50.099960Z","shell.execute_reply":"2023-12-10T19:24:50.104939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_mid, y_train, y_mid = train_test_split(x, y, test_size=0.2, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.107835Z","iopub.execute_input":"2023-12-10T19:24:50.108727Z","iopub.status.idle":"2023-12-10T19:24:50.122458Z","shell.execute_reply.started":"2023-12-10T19:24:50.108678Z","shell.execute_reply":"2023-12-10T19:24:50.121045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val, x_test, y_val, y_test = train_test_split(x_mid, y_mid, test_size=0.5, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.124739Z","iopub.execute_input":"2023-12-10T19:24:50.125641Z","iopub.status.idle":"2023-12-10T19:24:50.135486Z","shell.execute_reply.started":"2023-12-10T19:24:50.125593Z","shell.execute_reply":"2023-12-10T19:24:50.133989Z"},"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-10T19:24:50.137307Z","iopub.execute_input":"2023-12-10T19:24:50.138223Z","iopub.status.idle":"2023-12-10T19:24:50.145601Z","shell.execute_reply.started":"2023-12-10T19:24:50.138179Z","shell.execute_reply":"2023-12-10T19:24:50.144054Z"},"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\n\nx_val_scaled = x_val/255","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.147094Z","iopub.execute_input":"2023-12-10T19:24:50.147872Z","iopub.status.idle":"2023-12-10T19:24:50.171882Z","shell.execute_reply.started":"2023-12-10T19:24:50.147827Z","shell.execute_reply":"2023-12-10T19:24:50.170567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_scaled[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.173826Z","iopub.execute_input":"2023-12-10T19:24:50.174393Z","iopub.status.idle":"2023-12-10T19:24:50.185842Z","shell.execute_reply.started":"2023-12-10T19:24:50.174347Z","shell.execute_reply":"2023-12-10T19:24:50.184736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Covert the train and test data( Numpy array) into torch tensor","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torchvision import models, transforms\nfrom torch.utils.data import DataLoader, TensorDataset","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:50.187302Z","iopub.execute_input":"2023-12-10T19:24:50.187773Z","iopub.status.idle":"2023-12-10T19:24:54.206054Z","shell.execute_reply.started":"2023-12-10T19:24:50.187732Z","shell.execute_reply":"2023-12-10T19:24:54.204861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_scaled = torch.tensor(x_train_scaled, dtype=torch.float32)\nx_test_scaled = torch.tensor(x_test_scaled, dtype=torch.float32)\nx_val_scaled = torch.tensor(x_val_scaled, dtype=torch.float32)\ny_train = torch.tensor(y_train, dtype=torch.long)\ny_test = torch.tensor(y_test, dtype=torch.long)\ny_val = torch.tensor(y_val, dtype=torch.long)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:24:54.207622Z","iopub.execute_input":"2023-12-10T19:24:54.208136Z","iopub.status.idle":"2023-12-10T19:24:54.260778Z","shell.execute_reply.started":"2023-12-10T19:24:54.208105Z","shell.execute_reply":"2023-12-10T19:24:54.259782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the model class using pretrained VGG16","metadata":{}},{"cell_type":"code","source":"class model_VGG16(nn.Module):\n    def __init__(self, num_classes):\n        super(model_VGG16, self).__init__()\n        # Load pre-trained VGG16 model\n        vgg16_model = models.vgg16(weights='IMAGENET1K_V1')\n        \n        # Modify the first convolutional layer to accept the specified number of input channels\n        self.features = vgg16_model.features\n#         self.features[0] = nn.Conv2d(num_input_channels, 64, kernel_size=3, padding=1)\n        \n        \n        # Additional layers similar to the ResNet50 model\n        self.additional_layers = nn.Sequential(\n            nn.Flatten(),\n            nn.BatchNorm1d(512), # 128 -> 512\n            nn.Linear(512, 512), # 128x128 -> 512x512\n            nn.ReLU(),\n            nn.Dropout(0.5),\n            nn.BatchNorm1d(512), # 128 -> 512\n            nn.Linear(512, num_classes), # 128 -> 512\n            nn.Softmax(dim=1)\n        )\n\n    def forward(self, x):\n        x = self.features(x)\n        x = x.view(x.size(0), -1)\n        x = self.additional_layers(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:21.502875Z","iopub.execute_input":"2023-12-10T19:39:21.503334Z","iopub.status.idle":"2023-12-10T19:39:21.513832Z","shell.execute_reply.started":"2023-12-10T19:39:21.503291Z","shell.execute_reply":"2023-12-10T19:39:21.512350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Instantiating the model, loss function, optimizer","metadata":{}},{"cell_type":"code","source":"# Instantiate the VGG16 model with additional layers\nnum_of_classes = 10 \nmodel = model_VGG16(num_of_classes)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:25.351472Z","iopub.execute_input":"2023-12-10T19:39:25.352391Z","iopub.status.idle":"2023-12-10T19:39:27.471319Z","shell.execute_reply.started":"2023-12-10T19:39:25.352350Z","shell.execute_reply":"2023-12-10T19:39:27.470110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:29.384791Z","iopub.execute_input":"2023-12-10T19:39:29.385232Z","iopub.status.idle":"2023-12-10T19:39:29.393122Z","shell.execute_reply.started":"2023-12-10T19:39:29.385196Z","shell.execute_reply":"2023-12-10T19:39:29.391584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define loss function and optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.RMSprop(model.parameters(), lr=2e-5)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:29.923268Z","iopub.execute_input":"2023-12-10T19:39:29.923770Z","iopub.status.idle":"2023-12-10T19:39:29.931207Z","shell.execute_reply.started":"2023-12-10T19:39:29.923735Z","shell.execute_reply":"2023-12-10T19:39:29.929807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model train","metadata":{}},{"cell_type":"code","source":"# Convert data to PyTorch DataLoader\ntrain_dataset = TensorDataset(x_train_scaled, y_train)\ntrain_loader = DataLoader(train_dataset, batch_size=100, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:30.755866Z","iopub.execute_input":"2023-12-10T19:39:30.756282Z","iopub.status.idle":"2023-12-10T19:39:30.763283Z","shell.execute_reply.started":"2023-12-10T19:39:30.756250Z","shell.execute_reply":"2023-12-10T19:39:30.761808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for inputs, labels in train_loader:\n#         inputs, labels = inputs.to(device), labels.to(device)\n        \n#         print(inputs.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:31.041551Z","iopub.execute_input":"2023-12-10T19:39:31.041972Z","iopub.status.idle":"2023-12-10T19:39:31.047154Z","shell.execute_reply.started":"2023-12-10T19:39:31.041938Z","shell.execute_reply":"2023-12-10T19:39:31.045981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_accuracy = []\ntrain_accuracy = []","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:31.310458Z","iopub.execute_input":"2023-12-10T19:39:31.311079Z","iopub.status.idle":"2023-12-10T19:39:31.315856Z","shell.execute_reply.started":"2023-12-10T19:39:31.311045Z","shell.execute_reply":"2023-12-10T19:39:31.314657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train_scaled.shape)\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:31.754076Z","iopub.execute_input":"2023-12-10T19:39:31.754527Z","iopub.status.idle":"2023-12-10T19:39:31.761302Z","shell.execute_reply.started":"2023-12-10T19:39:31.754476Z","shell.execute_reply":"2023-12-10T19:39:31.760026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 10\nfor epoch in range(num_epochs):\n    model.train()\n    for inputs, labels in train_loader:\n        inputs, labels = inputs.to(device), labels.to(device)\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)  # No need to specify dim when using class indices\n        loss.backward()\n        optimizer.step()\n\n    # Evaluation on validation set\n    model.eval()\n    with torch.no_grad():\n        \n        # Calculate training accuracy\n        _, train_pred = torch.max(outputs, 1)\n        train_acc = torch.sum(train_pred == labels).item() / len(labels)\n        train_accuracy.append(train_acc)\n        \n        # x_val_scaled and y_val are validation data\n        \n        x_val_scaled, y_val = x_val_scaled.to(device), y_val.to(device)\n\n        # Forward pass\n        val_outputs = model(x_val_scaled)\n        val_loss = criterion(val_outputs, y_val)\n\n        # Calculate accuracy\n        _, val_pred = torch.max(val_outputs, 1)\n        val_acc = torch.sum(val_pred == y_val).item() / len(y_val)\n        val_accuracy.append(val_acc)\n\n    # Print and store metrics\n    print(f'Epoch [{epoch+1}/{num_epochs}], Train Accuracy: {train_acc:.4f}, Validation Accuracy: {val_acc:.4f}')\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","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:39:32.279136Z","iopub.execute_input":"2023-12-10T19:39:32.279709Z","iopub.status.idle":"2023-12-10T19:42:35.010958Z","shell.execute_reply.started":"2023-12-10T19:39:32.279664Z","shell.execute_reply":"2023-12-10T19:42:35.009677Z"},"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 confusion_matrix\nimport seaborn as sns\nfrom sklearn.metrics import accuracy_score, f1_score","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:42:35.013112Z","iopub.execute_input":"2023-12-10T19:42:35.013593Z","iopub.status.idle":"2023-12-10T19:42:35.018951Z","shell.execute_reply.started":"2023-12-10T19:42:35.013560Z","shell.execute_reply":"2023-12-10T19:42:35.017599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\nwith torch.no_grad():\n    x_test_scaled, y_test = x_test_scaled.to(device), y_test.to(device)\n\n    # Forward pass on test set\n    test_outputs = model(x_test_scaled)\n\n    # Calculate predictions and confusion matrix\n    _, test_pred = torch.max(test_outputs, 1)\n    conf_mat = confusion_matrix(y_test.cpu().numpy(), test_pred.cpu().numpy())\n    \n    # Calculate test accuracy\n    test_acc = accuracy_score(y_test.cpu().numpy(), test_pred.cpu().numpy())\n\n    # Calculate F1 score\n    test_f1 = f1_score(y_test.cpu().numpy(), test_pred.cpu().numpy(), average='weighted')\n\n# Print test accuracy and F1 score\nprint(f'Test Accuracy: {test_acc:.4f}')\nprint(f'Test F1 Score: {test_f1:.4f}\\n')\n\n# Plot confusion matrix\nplt.figure(figsize=(15, 7))\nsns.heatmap(conf_mat, annot=True, fmt='d', cmap='Blues')\nplt.ylabel('True Labels')\nplt.xlabel('Predicted Labels')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:42:35.021147Z","iopub.execute_input":"2023-12-10T19:42:35.022008Z","iopub.status.idle":"2023-12-10T19:42:36.326539Z","shell.execute_reply.started":"2023-12-10T19:42:35.021964Z","shell.execute_reply":"2023-12-10T19:42:36.324800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving the model","metadata":{}},{"cell_type":"code","source":"# Save the model state dictionary\ntorch.save(model.state_dict(), '/kaggle/working/CIFAR_resnet_model.pth')","metadata":{"execution":{"iopub.status.busy":"2023-12-10T19:42:36.329344Z","iopub.execute_input":"2023-12-10T19:42:36.329780Z","iopub.status.idle":"2023-12-10T19:42:36.413950Z","shell.execute_reply.started":"2023-12-10T19:42:36.329745Z","shell.execute_reply":"2023-12-10T19:42:36.412273Z"},"trusted":true},"execution_count":null,"outputs":[]}]}