{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":10196660,"sourceType":"datasetVersion","datasetId":6300381},{"sourceId":10196682,"sourceType":"datasetVersion","datasetId":6300399},{"sourceId":10196689,"sourceType":"datasetVersion","datasetId":6300404}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:00:07.352628Z","iopub.execute_input":"2025-01-08T18:00:07.353308Z","iopub.status.idle":"2025-01-08T18:00:07.699283Z","shell.execute_reply.started":"2025-01-08T18:00:07.353267Z","shell.execute_reply":"2025-01-08T18:00:07.698469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# linear algebra\nimport numpy as np\n# data processing, CSV file I / O (e.g. pd.read_csv)\nimport pandas as pd\nimport os\nimport tensorflow as tf\nimport keras\n# linear algebra\nimport numpy as np\n# data processing, CSV file I / O (e.g. pd.read_csv)\nimport pandas as pd\nimport os\nimport tensorflow as tf\n\nimport cv2\nimport matplotlib.pyplot as plt\n\nimport warnings \nwarnings.filterwarnings('ignore') ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:00:11.636283Z","iopub.execute_input":"2025-01-08T18:00:11.636716Z","iopub.status.idle":"2025-01-08T18:00:23.238068Z","shell.execute_reply.started":"2025-01-08T18:00:11.636684Z","shell.execute_reply":"2025-01-08T18:00:23.237103Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:00:39.134230Z","iopub.execute_input":"2025-01-08T18:00:39.135040Z","iopub.status.idle":"2025-01-08T18:00:39.968328Z","shell.execute_reply.started":"2025-01-08T18:00:39.134992Z","shell.execute_reply":"2025-01-08T18:00:39.967488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n#because of my output is multiclass classification\ny_train_cat = to_categorical(y_train, num_classes=10)\ny_test_cat = to_categorical(y_test, num_classes=10)\n\n#normalization:\nx_train_normalize = x_train / 255.0\nx_test_normalize = x_test / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-14T07:08:15.090064Z","iopub.execute_input":"2024-12-14T07:08:15.090445Z","iopub.status.idle":"2024-12-14T07:08:15.609010Z","shell.execute_reply.started":"2024-12-14T07:08:15.090413Z","shell.execute_reply":"2024-12-14T07:08:15.608302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Importing the necessary libraries\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPool2D,Dense,Flatten,Dropout,Input, AveragePooling2D, Activation,Conv2D, MaxPooling2D, BatchNormalization,Concatenate\nfrom tensorflow.keras.callbacks import EarlyStopping, TensorBoard\nfrom tensorflow.keras import regularizers, optimizers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T14:32:22.220930Z","iopub.execute_input":"2024-12-16T14:32:22.221623Z","iopub.status.idle":"2024-12-16T14:32:22.226139Z","shell.execute_reply.started":"2024-12-16T14:32:22.221588Z","shell.execute_reply":"2024-12-16T14:32:22.225210Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.regularizers import l2\n\nmodelv3 = Sequential()\n\nmodelv3.add(Input(shape = x_train.shape[1:]))\n\nmodelv3.add(Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(MaxPool2D(pool_size=(2, 2)))\nmodelv3.add(Dropout(0.2))\n\nmodelv3.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(MaxPool2D(pool_size=(2, 2)))\nmodelv3.add(Dropout(0.3))\n\nmodelv3.add(Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(MaxPool2D(pool_size=(2, 2))) #adding normalization as a different from the modelv1\nmodelv3.add(Dropout(0.4))\n\n\n#Building Fully Connected Layer (FC Layer)\nmodelv3.add(Flatten())\n\nmodelv3.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization())\nmodelv3.add(Dropout(0.5))\n\nmodelv3.add(Dense(10, activation='softmax'))\n\ncallback = keras.callbacks.EarlyStopping(monitor='val_loss',\n                                         verbose=1,\n                                         patience=5,\n                                         restore_best_weights=True)\n\nopt = keras.optimizers.Adam(learning_rate=0.003)\nmodelv3.compile(loss='categorical_crossentropy', optimizer=opt , metrics=['accuracy'])\nmodelv3.summary()\nhistoryv3 = modelv3.fit(x_train,y_train_cat,epochs=50, batch_size = 32, validation_data=(x_test,y_test_cat), verbose=1,callbacks= [callback]) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T13:27:23.242758Z","iopub.execute_input":"2024-12-13T13:27:23.243111Z","iopub.status.idle":"2024-12-13T13:32:55.896010Z","shell.execute_reply.started":"2024-12-13T13:27:23.243078Z","shell.execute_reply":"2024-12-13T13:32:55.895283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evaluation = modelv3.evaluate(x_test, y_test_cat)   #32 128\nprint('Test Accuracy: {}'.format(evaluation[1]))\n\ntr_acc=historyv3.history['accuracy']\ntr_loss = historyv3.history['loss']\nval_acc= historyv3.history['val_accuracy']\nval_loss = historyv3.history['val_loss']\n\n\n#fig, ax = plt.subplot(figsize = (8,6))\nplt.plot(tr_acc,label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.plot(tr_loss, label='Accuracy Loss')\nplt.plot(val_loss,label='Validation Loss')\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T14:12:17.396215Z","iopub.execute_input":"2024-12-13T14:12:17.397020Z","iopub.status.idle":"2024-12-13T14:12:18.608910Z","shell.execute_reply.started":"2024-12-13T14:12:17.396985Z","shell.execute_reply":"2024-12-13T14:12:18.607965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"a=x_train.copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T13:51:13.704161Z","iopub.execute_input":"2024-12-13T13:51:13.704496Z","iopub.status.idle":"2024-12-13T13:51:14.057731Z","shell.execute_reply.started":"2024-12-13T13:51:13.704468Z","shell.execute_reply":"2024-12-13T13:51:14.056967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\nmodelv3 = Sequential()\n\nmodelv3.add(Input(shape = x_train.shape[1:]))\n\nmodelv3.add(Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(MaxPool2D(pool_size=(2, 2)))\nmodelv3.add(Dropout(0.2))\n\nmodelv3.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(MaxPool2D(pool_size=(2, 2)))\nmodelv3.add(Dropout(0.3))\n\nmodelv3.add(Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu',padding = 'same',kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization()) #adding normalization as a different from the modelv1\nmodelv3.add(MaxPool2D(pool_size=(2, 2))) #adding normalization as a different from the modelv1\nmodelv3.add(Dropout(0.4))\n\n\n#Building Fully Connected Layer (FC Layer)\nmodelv3.add(Flatten())\n\nmodelv3.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\nmodelv3.add(BatchNormalization())\nmodelv3.add(Dropout(0.4)) #it was 0.3\n\nmodelv3.add(Dense(10, activation='softmax'))\n\ncallback = keras.callbacks.EarlyStopping(monitor='val_loss',\n                                         verbose=1,\n                                         patience=10,\n                                         restore_best_weights=True)\n\nopt = keras.optimizers.Adam(learning_rate=0.001) # it was 0.003\nmodelv3.compile(loss='categorical_crossentropy', optimizer=opt , metrics=['accuracy'])\n\n\ndatagen =  ImageDataGenerator(\n                rotation_range=20,\n            \tzoom_range=0.15,\n        \t\twidth_shift_range=0.2,\n        \t\theight_shift_range=0.2,\n        \t\tshear_range=0.15,\n        \t\thorizontal_flip=True,\n        \t\tfill_mode=\"nearest\")\n\n# train\nhistory_aug = modelv3.fit(datagen.flow(x_train, y_train_cat, batch_size=32), \n                            validation_data=(x_test, y_test_cat), \n                            epochs=100, verbose=1,callbacks= [callback])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T16:51:09.693120Z","iopub.execute_input":"2024-12-13T16:51:09.693506Z","iopub.status.idle":"2024-12-13T17:17:02.334001Z","shell.execute_reply.started":"2024-12-13T16:51:09.693473Z","shell.execute_reply":"2024-12-13T17:17:02.333290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evaluation = modelv3.evaluate(x_test, y_test_cat)   #32 128\nprint('Test Accuracy: {}'.format(evaluation[1]))\n\ntr_acc=history_aug.history['accuracy']\ntr_loss = history_aug.history['loss']\nval_acc= history_aug.history['val_accuracy']\nval_loss = history_aug.history['val_loss']\n\n\n#fig, ax = plt.subplot(figsize = (8,6))\nplt.plot(tr_acc,label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.plot(tr_loss, label='Accuracy Loss')\nplt.plot(val_loss,label='Validation Loss')\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T17:33:40.670533Z","iopub.execute_input":"2024-12-13T17:33:40.670869Z","iopub.status.idle":"2024-12-13T17:33:41.977374Z","shell.execute_reply.started":"2024-12-13T17:33:40.670838Z","shell.execute_reply":"2024-12-13T17:33:41.976579Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# First ResNet Study","metadata":{}},{"cell_type":"code","source":"from transformers import AutoImageProcessor, ResNetForImageClassification\nimport torch\nfrom datasets import load_dataset\nfrom PIL import Image\nimport torchvision.datasets as dsets\nimport torchvision.transforms as transforms\nimport torch.nn as nn\nimport pickle\nimport os\nimport torch.nn.functional as F\nimport requests\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport seaborn as sn\nimport pandas as pd\ntorch.manual_seed(1) # Set manual seed\nfrom sklearn import preprocessing\nfrom torch.utils.data import DataLoader, Dataset, Subset\n\n\n\nimage = Image.open('/kaggle/input/kalempic/kalem.jpg')\nimage = image.convert(\"RGB\")\n\nprocessor = AutoImageProcessor.from_pretrained(\"microsoft/resnet-50\")\nmodel = ResNetForImageClassification.from_pretrained(\"microsoft/resnet-50\")\n\ninputs = processor(image, return_tensors=\"pt\")\n\n\nwith torch.no_grad():\n    logits = model(**inputs).logits\n\npredicted_label = logits.argmax(-1).item()\nprint(model.config.id2label[predicted_label])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:00:48.702921Z","iopub.execute_input":"2025-01-08T18:00:48.703237Z","iopub.status.idle":"2025-01-08T18:00:57.193448Z","shell.execute_reply.started":"2025-01-08T18:00:48.703210Z","shell.execute_reply":"2025-01-08T18:00:57.192402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#this is the standart numbers for generic resnet applications\n\nmean=[0.4914, 0.4822, 0.4465]\nstd=[0.2023, 0.1994, 0.2010]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:02.304880Z","iopub.execute_input":"2025-01-08T18:01:02.305511Z","iopub.status.idle":"2025-01-08T18:01:02.309759Z","shell.execute_reply.started":"2025-01-08T18:01:02.305478Z","shell.execute_reply":"2025-01-08T18:01:02.308805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import keras\n#(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()\n\nprint(x_train.shape)\nprint(y_train.shape)\nprint(x_test.shape)\nprint(y_test.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:06.520187Z","iopub.execute_input":"2025-01-08T18:01:06.520886Z","iopub.status.idle":"2025-01-08T18:01:06.525770Z","shell.execute_reply.started":"2025-01-08T18:01:06.520852Z","shell.execute_reply":"2025-01-08T18:01:06.524874Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Packing images with label for plotting","metadata":{}},{"cell_type":"code","source":"'''\nthis classification is not suitable because torch.dataloader is suitable with ((image,label),(image_label))\nnot dictionary phase!\n'''\ndef classification_with_label(data,label):\n    mylabel = label.flatten()\n    myclass=set(mylabel.flatten())\n    mydict = {}\n\n    for each_data, each_label in zip(data,mylabel):\n        if each_label not in mydict:\n            \n            mydict[each_label] = []  # Initialize the list for this label\n        mydict[each_label].append(each_data)\n    \n    return mydict\n\ntrain_pack = classification_with_label(x_train,y_train)\ntest_pack = classification_with_label(x_test,y_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:10.384639Z","iopub.execute_input":"2025-01-08T18:01:10.384989Z","iopub.status.idle":"2025-01-08T18:01:10.417614Z","shell.execute_reply.started":"2025-01-08T18:01:10.384957Z","shell.execute_reply":"2025-01-08T18:01:10.416740Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"index = 0\nlabels = list(set(y_train.flatten()))\nspecific_label=[labels[0],labels[1],labels[2],labels[3],labels[4],labels[6],labels[9]]\n\nfig, ax = plt.subplots(len(specific_label),5,figsize=(16,12))\n\nfor i in range(len(specific_label)):\n    label = specific_label[index]\n    for j in range(5):\n        image = train_pack[label][j]\n        ax[i, j].imshow(image, cmap='gray')\n        ax[i,j].set_title('Class:'+str(label),fontsize = 12)\n        ax[i, j].axis('off')  # Turn off the axis\n            \n    index +=1\n    mytext = 'Images from:'+str(specific_label[i])\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:13.799774Z","iopub.execute_input":"2025-01-08T18:01:13.800122Z","iopub.status.idle":"2025-01-08T18:01:15.578263Z","shell.execute_reply.started":"2025-01-08T18:01:13.800092Z","shell.execute_reply":"2025-01-08T18:01:15.577337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"index = 0\nlabels = list(set(y_train.flatten()))\nspecific_label=[labels[0],labels[1],labels[2],labels[3],labels[4],labels[6],labels[9]]\n\nfig, ax = plt.subplots(len(specific_label),5,figsize=(16,12))\n\nfor i in range(len(specific_label)):\n    label = specific_label[index]\n    for j in range(5):\n        image = test_pack[label][j]\n        ax[i, j].imshow(image, cmap='gray')\n        ax[i,j].set_title('Class:'+str(label),fontsize = 12)\n        ax[i, j].axis('off')  # Turn off the axis\n            \n    index +=1\n    mytext = 'Images from:'+str(specific_label[i])\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:19.857767Z","iopub.execute_input":"2025-01-08T18:01:19.858393Z","iopub.status.idle":"2025-01-08T18:01:21.607616Z","shell.execute_reply.started":"2025-01-08T18:01:19.858362Z","shell.execute_reply":"2025-01-08T18:01:21.606776Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Pipeline for compose of train and test ","metadata":{}},{"cell_type":"code","source":"mean = [0.4914, 0.4822, 0.4465]\nstd = [0.247, 0.243, 0.261]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:25.381584Z","iopub.execute_input":"2025-01-08T18:01:25.382204Z","iopub.status.idle":"2025-01-08T18:01:25.386267Z","shell.execute_reply.started":"2025-01-08T18:01:25.382169Z","shell.execute_reply":"2025-01-08T18:01:25.385298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torchvision import transforms\n\n# -------- INFO --------\n#Training pipeline focuses on augmentation + normalization.\n#Testing pipeline focuses on standard preprocessing + normalization.'\n# -------- INFO --------\n\n#train pipeline for compose\n#COMPOSE OF TRAIN AND TEST\n#This is a pipeline where input data are being processed here\n\ntrain_compose = transforms.Compose([\n    transforms.Resize((224,224)),\n    transforms.RandomRotation(15),\n    transforms.RandomHorizontalFlip(0.1),\n    transforms.ColorJitter(brightness= 0.1,contrast= 0.1,saturation= 0.1),\n    transforms.RandomAdjustSharpness(sharpness_factor= 2,p=0.1),\n    transforms.ToTensor(),\n    transforms.Normalize(mean,std),\n    transforms.RandomErasing(p = 0.75,\n                             scale=(0.02,0.1),\n                             value =1.0,\n                             inplace=False)])\n\n#no need to data augmentation in test so compose parameters are like this:\ntest_compose = transforms.Compose([\n    transforms.Resize((224,224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean,std),\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:29.560007Z","iopub.execute_input":"2025-01-08T18:01:29.560692Z","iopub.status.idle":"2025-01-08T18:01:29.566668Z","shell.execute_reply.started":"2025-01-08T18:01:29.560657Z","shell.execute_reply":"2025-01-08T18:01:29.565737Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## data_label and DataLoader ","metadata":{}},{"cell_type":"code","source":"#https://pytorch.org/tutorials/beginner/basics/data_tutorial.html\nfrom PIL import Image\nfrom torch.utils.data import Dataset, DataLoader\n\nclass ClassificationWithLabelDataset(Dataset):\n    def __init__(self, img_labels, images, transform=None, target_transform=None):\n        \"\"\"\n        Args:\n            img_labels: A list or array of labels for the images.\n            images: A list or array of images (e.g., numpy arrays or file paths).\n            transform: A function/transform to apply to the images.\n            target_transform: A function/transform to apply to the labels.\n        \"\"\"\n        self.img_labels = img_labels\n        self.images = images\n        self.transform = transform\n        self.target_transform = target_transform\n\n    def __len__(self):\n        return len(self.img_labels)  # Number of samples in the dataset\n\n    def __getitem__(self, idx):\n        # Fetch the image and label for the given index\n        image = self.images[idx]\n        label = self.img_labels[idx]\n\n        # Convert the image from NumPy to PIL format\n        image = Image.fromarray(image)\n\n        # Apply transformations if provided\n        if self.transform:\n            image = self.transform(image)\n        if self.target_transform:\n            label = self.target_transform(label)\n\n        return image, label\n\n# Assuming y_train and x_train are the labels and images respectively\ntrain_data = ClassificationWithLabelDataset(y_train, x_train, transform=train_compose)\ntest_data = ClassificationWithLabelDataset(y_test, x_test, transform=test_compose)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:34.172854Z","iopub.execute_input":"2025-01-08T18:01:34.173559Z","iopub.status.idle":"2025-01-08T18:01:34.180215Z","shell.execute_reply.started":"2025-01-08T18:01:34.173514Z","shell.execute_reply":"2025-01-08T18:01:34.179214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\n\nprint('number of CPU core',os.cpu_count())\nprint('optimum value in num of worker:',torch.get_num_threads())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:38.800480Z","iopub.execute_input":"2025-01-08T18:01:38.801338Z","iopub.status.idle":"2025-01-08T18:01:38.805894Z","shell.execute_reply.started":"2025-01-08T18:01:38.801304Z","shell.execute_reply":"2025-01-08T18:01:38.804975Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create DataLoaders for batch processing\n\n#Shuffle:\n#True: Randomly shuffles the data at the beginning of every epoch. This is crucial for training models to avoid learning patterns from the order of the data. ---> Recommended for train\n#False: Loads data in sequential order (useful for evaluation or debugging). ---> Recommended for test\n\n#num_workers: Parallelizes data loading to speed up the process. Each worker loads a portion of the dataset in parallel.\n#Adjust based on your hardware and the dataset size. Too many workers can cause overhead or out-of-memory errors.\n\ntrain_dataloader = DataLoader(train_data, batch_size=32, shuffle=True, num_workers=2)\ntest_dataloader = DataLoader(test_data, batch_size=32, shuffle=False, num_workers=2)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:42.289056Z","iopub.execute_input":"2025-01-08T18:01:42.289679Z","iopub.status.idle":"2025-01-08T18:01:42.294313Z","shell.execute_reply.started":"2025-01-08T18:01:42.289645Z","shell.execute_reply":"2025-01-08T18:01:42.293437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#1 batch packes 32 images with 32 labels!!!!!\nfor images, labels in train_dataloader:\n    print(images.shape)  #expected to see only 32 images so [32,32,32,3] (32 times of 32x32 pixel in 3 channels)\n    print(labels.shape)  #expected to see only 32 labels (32 times labels for each image)\n    break #iterate for only one cycle","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:44.868014Z","iopub.execute_input":"2025-01-08T18:01:44.868718Z","iopub.status.idle":"2025-01-08T18:01:45.287330Z","shell.execute_reply.started":"2025-01-08T18:01:44.868684Z","shell.execute_reply":"2025-01-08T18:01:45.286209Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"In calling the images with labels from the dataloader:\n\n> with the help of : https://pytorch.org/tutorials/beginner/basics/data_tutorial.html#iterate-through-the-dataloader\n  \n\n* train_features, train_labels = next(iter(train_loader))\n* img = train_features[0].squeeze()\n* label = train_labels[0]\n\n","metadata":{}},{"cell_type":"code","source":"#each batch packes 32 images with 32 labels!!!!!\nfor images, labels in train_dataloader:\n    first_batch = images\n    first_batch_labels = labels\n\n    print(images.shape)  #expected to see only 32 images so [32,32,32,3] (32 times of 32x32 pixel in 3 channels)\n    print(labels.shape)  #expected to see only 32 labels (32 times labels for each image)\n    break #iterate for only one cycle\n\n\nfig, axes = plt.subplots(4,8,figsize=(12,6))\n\nfor idx, (img, label) in enumerate(zip(first_batch,first_batch_labels)):\n\n  class_cifar = int(label)\n  #print(img.shape) # torch.Size([3, 224, 224])\n  image = img.permute(1,2,0) #now it is 224,224,3\n  image = image.detach().cpu().numpy()\n  image = (image * 255).astype('uint8')\n  #Convert CHW to HWC for display\n  \n  image = cv2.resize(image,(32,32))\n  \n  ax = axes[idx//8][idx%8]\n  ax.imshow(image)\n  ax.set_title('Class:'+str(class_cifar),fontsize = 8)\n  ax.axis('off')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T18:01:49.603939Z","iopub.execute_input":"2025-01-08T18:01:49.604687Z","iopub.status.idle":"2025-01-08T18:01:51.933265Z","shell.execute_reply.started":"2025-01-08T18:01:49.604651Z","shell.execute_reply":"2025-01-08T18:01:51.932276Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Define the model","metadata":{}},{"cell_type":"code","source":"#go with resnet18 pretrained model\n\nimport torch\nimport torchvision.models as models\n\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\n\n\n#load resnet18\nrmodel = models.resnet18(pretrained = True)\n\nprint(rmodel)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T20:29:39.294832Z","iopub.execute_input":"2025-01-08T20:29:39.295577Z","iopub.status.idle":"2025-01-08T20:29:39.536969Z","shell.execute_reply.started":"2025-01-08T20:29:39.295532Z","shell.execute_reply":"2025-01-08T20:29:39.536207Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#lets customize the FC layer:\n\nrmodel.fc = nn.Linear(rmodel.fc.in_features,out_features = 10) #because the FC must give 10 different classes\nprint(rmodel)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T20:29:43.702047Z","iopub.execute_input":"2025-01-08T20:29:43.702846Z","iopub.status.idle":"2025-01-08T20:29:43.709122Z","shell.execute_reply.started":"2025-01-08T20:29:43.702813Z","shell.execute_reply":"2025-01-08T20:29:43.708284Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#--------------- INFO ---------------\n# The weights, parameters are very valuable that comes from pretrained model. so  first of all, i need to preserve them in training\n# The trainable parameters should come from FC layer, not CONV.\n#--------------- INFO ---------------\n\n\n#here the layer list,\nfor name, param in rmodel.named_parameters():\n    print(f\"Layer: {name}\")\n    print(f\" - Shape: {param.shape}\")\n    print(f\" - Requires Grad: {param.requires_grad}\")\n    print()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T20:29:47.179068Z","iopub.execute_input":"2025-01-08T20:29:47.179704Z","iopub.status.idle":"2025-01-08T20:29:47.186440Z","shell.execute_reply.started":"2025-01-08T20:29:47.179668Z","shell.execute_reply":"2025-01-08T20:29:47.185560Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Enhence the FC LAYER\n\nimport torch.nn as nn\n\n# Replace the FC layer\nrmodel.fc = nn.Sequential(\n    nn.Linear(512, 128),\n    nn.BatchNorm1d(128),\n    nn.ReLU(),\n    nn.Dropout(0.5),  # Optional: Helps prevent overfitting\n    nn.Linear(128, 64),\n    nn.BatchNorm1d(64),\n    nn.ReLU(),\n    nn.Dropout(0.3),  # Optional: Helps prevent overfitting\n    nn.Linear(64, 10)\n)\n\nprint(rmodel)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T20:29:52.444211Z","iopub.execute_input":"2025-01-08T20:29:52.444877Z","iopub.status.idle":"2025-01-08T20:29:52.452914Z","shell.execute_reply.started":"2025-01-08T20:29:52.444843Z","shell.execute_reply":"2025-01-08T20:29:52.452068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for name,param in rmodel.named_parameters():\n  if 'fc' not in name:\n    param.requires_grad = False  #False makes itself frozen\n  else:\n    param.requires_grad = True\n\n\n#lets show up the latest status of rmodel:\nfor name, param in rmodel.named_parameters():\n  print(f'Layer: {name}')\n  print(f' - Shape: {param.shape}')\n  print(f' - Requires Grad: {param.requires_grad}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T20:29:55.904599Z","iopub.execute_input":"2025-01-08T20:29:55.905383Z","iopub.status.idle":"2025-01-08T20:29:55.912292Z","shell.execute_reply.started":"2025-01-08T20:29:55.905352Z","shell.execute_reply":"2025-01-08T20:29:55.911484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Hybrid model\n#WHAT IF UNFREEZE ONLY THE LAST LAYER OF CONV \nfor name,param in rmodel.named_parameters():\n  if 'layer4' in name:\n    param.requires_grad = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T20:30:00.547552Z","iopub.execute_input":"2025-01-08T20:30:00.548180Z","iopub.status.idle":"2025-01-08T20:30:00.552474Z","shell.execute_reply.started":"2025-01-08T20:30:00.548146Z","shell.execute_reply":"2025-01-08T20:30:00.551556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#lets show up the latest status of rmodel:\nfor name, param in rmodel.named_parameters():\n  print(f'Layer: {name}')\n  print(f' - Shape: {param.shape}')\n  print(f' - Requires Grad: {param.requires_grad}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T20:30:02.568473Z","iopub.execute_input":"2025-01-08T20:30:02.569325Z","iopub.status.idle":"2025-01-08T20:30:02.575878Z","shell.execute_reply.started":"2025-01-08T20:30:02.569289Z","shell.execute_reply":"2025-01-08T20:30:02.574938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#set the seeds\n\ntorch.manual_seed(42)\ntorch.cuda.manual_seed(42)\n\n\n#define the device\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nrmodel = rmodel.to(device)\n\nif torch.cuda.is_available():\n  print('GPU is available')\n  print('GPU name:',torch.cuda.get_device_name(0))\nelse:\n  print('GPU is not available')\n\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(rmodel.parameters(),lr = 0.001)\n\n#early stop setup\npatience = 5\nbest_loss = float('inf') #set it as infinite value\nepochs_no_improve = 0    #counts if it is no improvements\nearly_stop = False       #trigger\n\n\n#for overall performance graph\nhistory_train_acc = []\nhistory_train_loss = []\nhistory_val_acc = []\nhistory_val_loss = []\n\n\nnum_epoch = 30\n\nfor epoch in range(num_epoch):\n    #Initiate the parameters\n    correct = 0 #calculation param\n    total = 0   #calculation param\n    train_acc = 0.0         #Train Acc Score for each epoch\n    train_loss = 0.0        #Train Loss Score for each epoch\n    test_acc = 0.0          #Test Acc Score for each epoch\n    test_loss = 0.0         #Test Loss Score for each epoch\n    running_loss = 0.0      #calculation parameter for train\n    running_test_loss = 0.0 #calculation parameter for test\n\n    rmodel.train() #Set the model as training mode\n   \n\n  #------------------ Train the Model ------------------\n    for inputs,labels in train_dataloader:\n    \n        #inputs = inputs.permute(0, 3, 1, 2).to(torch.float).to(device)\n        inputs = inputs.to(torch.float).to(device)\n        #if the label is torch[32,1] then need to squeeze \n        labels = labels.squeeze().to(device)\n\n         # Zero the gradients\n        optimizer.zero_grad()\n        # Forward pass\n   \n        outputs = rmodel(inputs)\n        # Compute loss\n        loss = criterion(outputs,labels)\n\n        # Backward pass and optimize\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n\n        _,predicted = torch.max(outputs,1)\n        correct += (predicted == labels).sum().item()\n        total += labels.size(0)\n\n    train_acc = round((correct/total),3)\n    train_loss = round((running_loss / len(train_dataloader)),3)  \n  \n\n      # ------------------ Evaluate the Model ------------------\n    rmodel.eval()  # Set model to evaluation mode\n    correct = 0\n    total = 0\n\n    with torch.no_grad():\n        for inputs, labels in test_dataloader:\n            inputs = inputs.to(torch.float).to(device)\n            #before squeeze label is torch.size([32,1])\n            #after squeeze label turn to be [32] \n            labels = labels.squeeze().to(device) \n            outputs = rmodel(inputs)\n\n            # Compute loss\n            loss = criterion(outputs, labels)\n            running_test_loss += loss.item()  # Accumulate loss\n        \n            _, predicted = torch.max(outputs, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n      \n    #Calculate test accuracy\n    test_acc = round((correct / total),3)  \n    # Calculate average test loss\n    test_loss = round(running_test_loss / len(test_dataloader),3)\n        \n    history_train_acc.append(train_acc)\n    history_train_loss.append(train_loss)\n    history_val_acc.append(test_acc)\n    history_val_loss.append(test_loss)\n\n    #Printing the results  \n    print(f\"Epoch [{epoch + 1}/{num_epoch}],\"\n          f\"Train Acc: {train_acc},\"\n          f\"Train Loss: {train_loss},\" \n          f\"Test Acc: {test_acc},\"\n          f\"Test Loss: {test_loss}\")\n\n    \n    #if the improvement exists:\n    if test_loss < best_loss:\n        best_loss = test_loss\n        epochs_no_improve = 0 #improvement is still valid so set it as zero\n    else:\n        epochs_no_improve +=1 #counts if no improvement\n\n    print(f\"Early Stop Counter: {epochs_no_improve}/{patience}\")\n\n    if epochs_no_improve >= patience: #stopping the training because overfitting just started-\n        print('early stop triggered!')\n        early_stop = True # set the trigger True\n        break\n\nif early_stop: #if TRUE (early stop)\n    print('Halted training the model due to lack of improvement')\nelse:\n    print('Completed all epoches w/o early stop!')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T20:30:15.924176Z","iopub.execute_input":"2025-01-08T20:30:15.924879Z","iopub.status.idle":"2025-01-08T21:07:27.132488Z","shell.execute_reply.started":"2025-01-08T20:30:15.924831Z","shell.execute_reply":"2025-01-08T21:07:27.131303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = []\nfor e in range(len(history_train_loss)):\n    x.append(e)\n\nplt.plot(x,history_train_loss,label = 'train_loss')\nplt.plot(x,history_val_loss,label = 'val_loss')\nplt.plot(x,history_train_acc,label = 'train_acc')\nplt.plot(x,history_val_acc,label = 'val_acc')\n\n\nplt.xlabel('Epoch Number')\nplt.ylabel('Score')\nplt.title('Acc and Loss Score on Epoch')\nplt.legend()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T21:07:47.008725Z","iopub.execute_input":"2025-01-08T21:07:47.009119Z","iopub.status.idle":"2025-01-08T21:07:47.290738Z","shell.execute_reply.started":"2025-01-08T21:07:47.009083Z","shell.execute_reply":"2025-01-08T21:07:47.290022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install py7zr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T21:09:25.201850Z","iopub.execute_input":"2025-01-08T21:09:25.202665Z","iopub.status.idle":"2025-01-08T21:09:33.439661Z","shell.execute_reply.started":"2025-01-08T21:09:25.202631Z","shell.execute_reply":"2025-01-08T21:09:33.438545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"testdata = '/kaggle/input/cifar-10/test.7z'\nmydir = '/kaggle/working/' \n\nimport py7zr\n\narchive = py7zr.SevenZipFile(testdata, mode='r')\narchive.extractall(path=mydir)\narchive.close()\n\ntest_list = []\ntest_path = '/kaggle/working/test/'\n\nfor each in os.listdir(test_path):\n\n    test_list.append(os.path.join(test_path,each))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T21:09:33.441975Z","iopub.execute_input":"2025-01-08T21:09:33.442738Z","iopub.status.idle":"2025-01-08T21:11:47.530177Z","shell.execute_reply.started":"2025-01-08T21:09:33.442705Z","shell.execute_reply":"2025-01-08T21:11:47.529234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(test_list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T21:12:32.036337Z","iopub.execute_input":"2025-01-08T21:12:32.037222Z","iopub.status.idle":"2025-01-08T21:12:32.042655Z","shell.execute_reply.started":"2025-01-08T21:12:32.037186Z","shell.execute_reply":"2025-01-08T21:12:32.041707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_transform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Adjust size as needed\n    transforms.ToTensor(),\n    transforms.Normalize(mean,std)\n])\n\n# Set model to evaluation mode\nrmodel.eval()\n\n# Initialize lists to store results\npicnames = []\npredictions = []\nstopper = 0 \n\n# Iterate over test images\nwith torch.no_grad():\n    for file_name in test_list:\n        \n        # Load and preprocess the image\n        image = Image.open(file_name).convert('RGB') #here, it is 3x32x32\n        image = test_transform(image).unsqueeze(0).to(device)  # Add batch dimension --- now 1x3x32x32 (having batch size is important in pytorch but no need to have same batch number in test and train!!!!)\n        \n        # Predict\n        outputs = rmodel(image)\n        _, predicted = torch.max(outputs, 1)\n\n        # Store the results\n        file_name = os.path.basename(file_name)  # '1.png'\n        file_name = os.path.splitext(file_name)[0]  # '1'\n        \n        picnames.append(file_name)\n        predictions.append(predicted.item())\n\n\n# Save to CSV (optional)\nimport pandas as pd\ndf = pd.DataFrame({'id': picnames, 'label': predictions})\ndf.to_csv('/kaggle/working/test_prediction.csv', index=False)\nprint(\"Predictions saved to test_prediction.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T21:12:39.330619Z","iopub.execute_input":"2025-01-08T21:12:39.330963Z","iopub.status.idle":"2025-01-08T21:33:28.463574Z","shell.execute_reply.started":"2025-01-08T21:12:39.330933Z","shell.execute_reply":"2025-01-08T21:33:28.462662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T21:37:00.873673Z","iopub.execute_input":"2025-01-08T21:37:00.874209Z","iopub.status.idle":"2025-01-08T21:37:00.882788Z","shell.execute_reply.started":"2025-01-08T21:37:00.874151Z","shell.execute_reply":"2025-01-08T21:37:00.881729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_object= ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']\nnum_object = [0,1,2,3,4,5,6,7,8,9]\nnum_to_class = dict(zip(num_object, class_object))\n# Convert to class names\nclass_predictions = [num_to_class[num] for num in predictions]\nprint(len(class_predictions))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T21:41:30.103265Z","iopub.execute_input":"2025-01-08T21:41:30.103615Z","iopub.status.idle":"2025-01-08T21:41:30.126966Z","shell.execute_reply.started":"2025-01-08T21:41:30.103584Z","shell.execute_reply":"2025-01-08T21:41:30.126202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save to CSV (optional)\nimport pandas as pd\ndf = pd.DataFrame({'id': picnames, 'label': class_predictions})\ndf.to_csv('/kaggle/working/test_prediction_obj.csv', index=False)\nprint(\"Predictions saved to test_prediction_obj.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T21:42:49.052609Z","iopub.execute_input":"2025-01-08T21:42:49.053392Z","iopub.status.idle":"2025-01-08T21:42:49.283793Z","shell.execute_reply.started":"2025-01-08T21:42:49.053360Z","shell.execute_reply":"2025-01-08T21:42:49.282810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''\ntest_transform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Adjust size as needed\n    transforms.ToTensor(),\n    transforms.Normalize(mean,std)\n])\n\n# Set model to evaluation mode\nrmodel.eval()\n\n# Initialize lists to store results\npicnames = []\npredictions = []\nstopper = 0 \n\n# Iterate over test images\nwith torch.no_grad():\n    for file_name in test_list:\n        \n        # Load and preprocess the image\n        image = Image.open(file_name).convert('RGB') #here, it is 3x32x32\n        image_np = np.array(image)  # Shape: (32, 32, 3)\n        image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)\n        image_np = cv2.resize(image_np,(32,32))\n                \n        image = test_transform(image).unsqueeze(0).to(device)  # Add batch dimension --- now 1x3x32x32 (having batch size is important in pytorch but no need to have same batch number in test and train!!!!)\n        \n        # Predict\n        outputs = rmodel(image)\n        _, predicted = torch.max(outputs, 1)\n\n              # Display the image with matplotlib\n        plt.imshow(image_np)  # Show the current image\n        plt.title(f\"Predict: {predicted.item()}\")  # Optional: Add title with file name\n        plt.axis('off')  # Optional: Turn off axis\n        plt.show()  # Display the image before the next iteration\n        \n        # Store the results\n        file_name = os.path.basename(file_name)  # '1.png'\n        file_name = os.path.splitext(file_name)[0]  # '1'\n        \n        picnames.append(file_name)\n        predictions.append(predicted.item())\n        if stopper  == 50:\n            break\n        stopper += 1\n\n\n# Save to CSV (optional)\nimport pandas as pd\ndf = pd.DataFrame({'id': picnames, 'label': predictions})\ndf.to_csv('/kaggle/working/test_predictions.csv', index=False)\nprint(\"Predictions saved to test_predictions.csv\")\n'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T15:35:34.901459Z","iopub.execute_input":"2025-01-08T15:35:34.901853Z","iopub.status.idle":"2025-01-08T15:35:42.299248Z","shell.execute_reply.started":"2025-01-08T15:35:34.901820Z","shell.execute_reply":"2025-01-08T15:35:42.295858Z"}},"outputs":[],"execution_count":null}]}