{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71608,"databundleVersionId":7895811,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:50:14.918408Z","iopub.execute_input":"2024-07-03T06:50:14.918844Z","iopub.status.idle":"2024-07-03T06:50:15.385714Z","shell.execute_reply.started":"2024-07-03T06:50:14.918813Z","shell.execute_reply":"2024-07-03T06:50:15.384378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport torch\nimport torchvision\n\nfrom torch import nn\nfrom torchvision import transforms","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:50:15.450284Z","iopub.execute_input":"2024-07-03T06:50:15.450822Z","iopub.status.idle":"2024-07-03T06:50:20.931264Z","shell.execute_reply.started":"2024-07-03T06:50:15.45079Z","shell.execute_reply":"2024-07-03T06:50:20.930163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\ndevice","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:50:20.933579Z","iopub.execute_input":"2024-07-03T06:50:20.9346Z","iopub.status.idle":"2024-07-03T06:50:20.943775Z","shell.execute_reply.started":"2024-07-03T06:50:20.934555Z","shell.execute_reply":"2024-07-03T06:50:20.942602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/aiml-general-championship/KCDH2024_Training_GroundTruth.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:50:20.945171Z","iopub.execute_input":"2024-07-03T06:50:20.94558Z","iopub.status.idle":"2024-07-03T06:50:21.0044Z","shell.execute_reply.started":"2024-07-03T06:50:20.945528Z","shell.execute_reply":"2024-07-03T06:50:21.003213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport PIL","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:50:22.769032Z","iopub.execute_input":"2024-07-03T06:50:22.769558Z","iopub.status.idle":"2024-07-03T06:50:22.775628Z","shell.execute_reply.started":"2024-07-03T06:50:22.769513Z","shell.execute_reply":"2024-07-03T06:50:22.774117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport tensorflow as tf\nimport cv2\nfrom keras import backend as K\nfrom keras.layers import Layer,InputSpec\nimport keras.layers as kl\nfrom glob import glob\nfrom sklearn.metrics import roc_curve, auc\nfrom keras.preprocessing import image\nfrom tensorflow.keras.models import Sequential\nfrom sklearn.metrics import roc_auc_score\nfrom tensorflow.keras import callbacks \nfrom tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping\nfrom  matplotlib import pyplot as plt\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.layers import concatenate,Dense, Conv2D, MaxPooling2D, Flatten,Input,Activation,add,AveragePooling2D,BatchNormalization,Dropout\n%matplotlib inline\nimport shutil\nfrom sklearn.metrics import  precision_score, recall_score, accuracy_score,classification_report ,confusion_matrix\nfrom tensorflow.python.platform import build_info as tf_build_info\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\n\nfrom PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:50:31.387727Z","iopub.execute_input":"2024-07-03T06:50:31.388201Z","iopub.status.idle":"2024-07-03T06:50:47.096591Z","shell.execute_reply.started":"2024-07-03T06:50:31.388157Z","shell.execute_reply":"2024-07-03T06:50:47.095095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_2 = pd.read_csv('/kaggle/input/aiml-general-championship/KCDH2024_Training_LesionGroupings.csv')\ndf_2","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:57:51.15725Z","iopub.execute_input":"2024-07-03T06:57:51.157674Z","iopub.status.idle":"2024-07-03T06:57:51.189782Z","shell.execute_reply.started":"2024-07-03T06:57:51.157634Z","shell.execute_reply":"2024-07-03T06:57:51.18871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1 = pd.read_csv('/kaggle/input/aiml-general-championship/KCDH2024_Training_GroundTruth.csv')\ndf_1","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:57:58.09934Z","iopub.execute_input":"2024-07-03T06:57:58.09976Z","iopub.status.idle":"2024-07-03T06:57:58.130688Z","shell.execute_reply.started":"2024-07-03T06:57:58.099729Z","shell.execute_reply":"2024-07-03T06:57:58.129495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l_dx = []\nfor rows in df_1.iterrows():\n    l_dx.append(np.argmax(rows[1].values[1:]))\n","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:58:01.742807Z","iopub.execute_input":"2024-07-03T06:58:01.743233Z","iopub.status.idle":"2024-07-03T06:58:02.383685Z","shell.execute_reply.started":"2024-07-03T06:58:01.743198Z","shell.execute_reply":"2024-07-03T06:58:02.382463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1['dx'] = np.array(l_dx)\ndf_1","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:58:06.899754Z","iopub.execute_input":"2024-07-03T06:58:06.90018Z","iopub.status.idle":"2024-07-03T06:58:06.917637Z","shell.execute_reply.started":"2024-07-03T06:58:06.900146Z","shell.execute_reply":"2024-07-03T06:58:06.916343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mer_df = pd.merge(df_1, df_2, on = 'image')[['lesion_id','image','dx']]\nmer_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:59:09.611355Z","iopub.execute_input":"2024-07-03T06:59:09.611822Z","iopub.status.idle":"2024-07-03T06:59:09.63522Z","shell.execute_reply.started":"2024-07-03T06:59:09.611787Z","shell.execute_reply":"2024-07-03T06:59:09.633918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_in_folder=[img[:-4] for img in os.listdir(\"/kaggle/input/aiml-general-championship/KCDH2024_Training_Input_10K/KCDH2024_Training_Input_10K\")]\nimgs_in_df = mer_df['image'].unique()\nmer_df=mer_df[np.isin(imgs_in_df,imgs_in_folder)]\nmer_df","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:59:16.684905Z","iopub.execute_input":"2024-07-03T06:59:16.685316Z","iopub.status.idle":"2024-07-03T06:59:22.880544Z","shell.execute_reply.started":"2024-07-03T06:59:16.685286Z","shell.execute_reply":"2024-07-03T06:59:22.87937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_count = mer_df.groupby('lesion_id').count()\ndf_count.info()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:59:30.783966Z","iopub.execute_input":"2024-07-03T06:59:30.78443Z","iopub.status.idle":"2024-07-03T06:59:30.812027Z","shell.execute_reply.started":"2024-07-03T06:59:30.784394Z","shell.execute_reply":"2024-07-03T06:59:30.810708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_count = df_count[df_count['dx'] == 1]\ndf_count.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:59:46.587169Z","iopub.execute_input":"2024-07-03T06:59:46.587584Z","iopub.status.idle":"2024-07-03T06:59:46.597423Z","shell.execute_reply.started":"2024-07-03T06:59:46.587555Z","shell.execute_reply":"2024-07-03T06:59:46.596264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def duplicates(x):\n    unique = set(df_count['lesion_id'])\n    if x in unique:\n        return 'no' \n    else:\n        return 'duplicates'","metadata":{"execution":{"iopub.status.busy":"2024-07-03T06:59:54.341476Z","iopub.execute_input":"2024-07-03T06:59:54.341916Z","iopub.status.idle":"2024-07-03T06:59:54.348179Z","shell.execute_reply.started":"2024-07-03T06:59:54.341879Z","shell.execute_reply":"2024-07-03T06:59:54.346865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mer_df['is_duplicate'] = mer_df['lesion_id'].apply(duplicates)\nmer_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:00:01.200097Z","iopub.execute_input":"2024-07-03T07:00:01.200554Z","iopub.status.idle":"2024-07-03T07:00:12.157858Z","shell.execute_reply.started":"2024-07-03T07:00:01.200519Z","shell.execute_reply":"2024-07-03T07:00:12.156502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_count = mer_df[mer_df['is_duplicate'] == 'no']","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:00:18.974103Z","iopub.execute_input":"2024-07-03T07:00:18.974543Z","iopub.status.idle":"2024-07-03T07:00:18.984397Z","shell.execute_reply.started":"2024-07-03T07:00:18.97451Z","shell.execute_reply":"2024-07-03T07:00:18.983103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, test_df = train_test_split(df_count, test_size=0.15, stratify=df_count['dx'])","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:00:29.493531Z","iopub.execute_input":"2024-07-03T07:00:29.49395Z","iopub.status.idle":"2024-07-03T07:00:29.505943Z","shell.execute_reply.started":"2024-07-03T07:00:29.493916Z","shell.execute_reply":"2024-07-03T07:00:29.504787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_count.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:00:39.169887Z","iopub.execute_input":"2024-07-03T07:00:39.170335Z","iopub.status.idle":"2024-07-03T07:00:39.183847Z","shell.execute_reply.started":"2024-07-03T07:00:39.170303Z","shell.execute_reply":"2024-07-03T07:00:39.18265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:00:46.996365Z","iopub.execute_input":"2024-07-03T07:00:46.996783Z","iopub.status.idle":"2024-07-03T07:00:47.009478Z","shell.execute_reply.started":"2024-07-03T07:00:46.996749Z","shell.execute_reply":"2024-07-03T07:00:47.007973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_pd = mer_df","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:00:58.527994Z","iopub.execute_input":"2024-07-03T07:00:58.528436Z","iopub.status.idle":"2024-07-03T07:00:58.536077Z","shell.execute_reply.started":"2024-07-03T07:00:58.528405Z","shell.execute_reply":"2024-07-03T07:00:58.534272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def identify_trainOrtest(x):\n    test_data = set(test_df['image'])\n    if str(x) in test_data:\n        return 'test'\n    else:\n        return 'train'\n\n#creating train_df\ndata_pd['train_test_split'] = data_pd['image'].apply(identify_trainOrtest)\ntrain_df = data_pd[data_pd['train_test_split'] == 'train']\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:01:09.741204Z","iopub.execute_input":"2024-07-03T07:01:09.741622Z","iopub.status.idle":"2024-07-03T07:01:11.108056Z","shell.execute_reply.started":"2024-07-03T07:01:09.741591Z","shell.execute_reply":"2024-07-03T07:01:11.10677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:01:27.685348Z","iopub.execute_input":"2024-07-03T07:01:27.6858Z","iopub.status.idle":"2024-07-03T07:01:27.699598Z","shell.execute_reply.started":"2024-07-03T07:01:27.685762Z","shell.execute_reply":"2024-07-03T07:01:27.698113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['is_duplicate'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:01:42.186752Z","iopub.execute_input":"2024-07-03T07:01:42.187737Z","iopub.status.idle":"2024-07-03T07:01:42.19787Z","shell.execute_reply.started":"2024-07-03T07:01:42.187688Z","shell.execute_reply":"2024-07-03T07:01:42.196447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.sort_values(by = 'lesion_id')","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:01:53.834055Z","iopub.execute_input":"2024-07-03T07:01:53.834524Z","iopub.status.idle":"2024-07-03T07:01:53.866063Z","shell.execute_reply.started":"2024-07-03T07:01:53.834486Z","shell.execute_reply":"2024-07-03T07:01:53.864527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.drop_duplicates(subset = 'lesion_id')\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:02:35.131884Z","iopub.execute_input":"2024-07-03T07:02:35.132392Z","iopub.status.idle":"2024-07-03T07:02:35.153053Z","shell.execute_reply.started":"2024-07-03T07:02:35.132354Z","shell.execute_reply":"2024-07-03T07:02:35.151908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image id of train and test images\ntrain_list = list(train_df['image'])\ntest_list = list(test_df['image'])","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:03:01.823511Z","iopub.execute_input":"2024-07-03T07:03:01.823929Z","iopub.status.idle":"2024-07-03T07:03:01.830929Z","shell.execute_reply.started":"2024-07-03T07:03:01.823899Z","shell.execute_reply":"2024-07-03T07:03:01.829704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_list)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:03:06.699706Z","iopub.execute_input":"2024-07-03T07:03:06.700125Z","iopub.status.idle":"2024-07-03T07:03:06.707844Z","shell.execute_reply.started":"2024-07-03T07:03:06.700092Z","shell.execute_reply":"2024-07-03T07:03:06.706568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_list)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:03:10.891811Z","iopub.execute_input":"2024-07-03T07:03:10.892261Z","iopub.status.idle":"2024-07-03T07:03:10.899824Z","shell.execute_reply.started":"2024-07-03T07:03:10.892227Z","shell.execute_reply":"2024-07-03T07:03:10.898306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mer_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:03:16.843242Z","iopub.execute_input":"2024-07-03T07:03:16.843694Z","iopub.status.idle":"2024-07-03T07:03:16.85907Z","shell.execute_reply.started":"2024-07-03T07:03:16.843659Z","shell.execute_reply":"2024-07-03T07:03:16.857316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/working/train_dir'\ntest_dir = '/kaggle/working/test_dir'","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:08:08.993915Z","iopub.execute_input":"2024-07-03T07:08:08.994788Z","iopub.status.idle":"2024-07-03T07:08:09.000414Z","shell.execute_reply.started":"2024-07-03T07:08:08.994741Z","shell.execute_reply":"2024-07-03T07:08:08.999094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('train_dir',exist_ok = True)\nos.makedirs('test_dir', exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:04:00.837024Z","iopub.execute_input":"2024-07-03T07:04:00.837874Z","iopub.status.idle":"2024-07-03T07:04:00.843571Z","shell.execute_reply.started":"2024-07-03T07:04:00.837838Z","shell.execute_reply":"2024-07-03T07:04:00.842271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mer_df['dx'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:06:48.631027Z","iopub.execute_input":"2024-07-03T07:06:48.631526Z","iopub.status.idle":"2024-07-03T07:06:48.64259Z","shell.execute_reply.started":"2024-07-03T07:06:48.631491Z","shell.execute_reply":"2024-07-03T07:06:48.641249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,7):\n  directory1=train_dir+'/'+str(i)\n  directory2=test_dir+'/'+str(i)\n  os.mkdir(directory1)\n  os.mkdir(directory2)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:08:48.09172Z","iopub.execute_input":"2024-07-03T07:08:48.092225Z","iopub.status.idle":"2024-07-03T07:08:48.099413Z","shell.execute_reply.started":"2024-07-03T07:08:48.09217Z","shell.execute_reply":"2024-07-03T07:08:48.098219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:11:28.407653Z","iopub.execute_input":"2024-07-03T07:11:28.408095Z","iopub.status.idle":"2024-07-03T07:11:28.415296Z","shell.execute_reply.started":"2024-07-03T07:11:28.408063Z","shell.execute_reply":"2024-07-03T07:11:28.414108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_pd = data_pd.set_index('image')","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:12:46.131074Z","iopub.execute_input":"2024-07-03T07:12:46.131535Z","iopub.status.idle":"2024-07-03T07:12:46.140309Z","shell.execute_reply.started":"2024-07-03T07:12:46.131503Z","shell.execute_reply":"2024-07-03T07:12:46.138989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image in train_list:\n    file_name = image+'.jpg'\n    label = str(data_pd.loc[image, 'dx'])\n\n    # path of source image \n    source = os.path.join('/kaggle/input/aiml-general-championship/KCDH2024_Training_Input_10K/KCDH2024_Training_Input_10K', file_name)\n\n    # copying the image from the source to target file\n    target = os.path.join(train_dir, label, file_name)\n\n    shutil.copyfile(source, target)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:14:00.04624Z","iopub.execute_input":"2024-07-03T07:14:00.046681Z","iopub.status.idle":"2024-07-03T07:14:57.647552Z","shell.execute_reply.started":"2024-07-03T07:14:00.04664Z","shell.execute_reply":"2024-07-03T07:14:57.646274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image in test_list:\n\n    file_name = image+'.jpg'\n    label = str(data_pd.loc[image, 'dx'])\n\n    # path of source image \n    source = os.path.join('/kaggle/input/aiml-general-championship/KCDH2024_Training_Input_10K/KCDH2024_Training_Input_10K', file_name)\n\n    # copying the image from the source to target file\n    target = os.path.join(test_dir, label, file_name)\n\n    shutil.copyfile(source, target)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:16:09.279723Z","iopub.execute_input":"2024-07-03T07:16:09.280173Z","iopub.status.idle":"2024-07-03T07:16:16.500624Z","shell.execute_reply.started":"2024-07-03T07:16:09.280132Z","shell.execute_reply":"2024-07-03T07:16:16.499365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nfrom PIL import Image\nfrom PIL import ImageFilter\nimport os\n\npath = '/kaggle/working/train_dir'\npath_tip_100 = 'train_224_dir'\nos.makedirs(path_tip_100, exist_ok = True)\nfor c in os.listdir(path):\n    new_folder_path = os.path.join(path_tip_100, c)\n    os.makedirs(new_folder_path, exist_ok = True)\n    folder_path = os.path.join(path,c)\n    for img_name in os.listdir(folder_path):\n        img_path = os.path.join(folder_path, img_name)\n        load_img=cv2.imread(img_path)\n        # load_img=cv2.cvtColor(load_img, cv2.COLOR_BGR2RGB)\n        # load_img = load_img.astype(np.float32) / 255.0\n        load_img = cv2.resize(load_img, (224,224))\n        fullOutPath = os.path.join(new_folder_path,'resized_'+img_name)\n        cv2.imwrite(fullOutPath,load_img)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:18:34.322428Z","iopub.execute_input":"2024-07-03T07:18:34.32467Z","iopub.status.idle":"2024-07-03T07:19:22.502687Z","shell.execute_reply.started":"2024-07-03T07:18:34.324602Z","shell.execute_reply":"2024-07-03T07:19:22.501274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nfrom PIL import Image\nfrom PIL import ImageFilter\nimport os\n\npath = '/kaggle/working/test_dir'\npath_tip_100 = 'test_224_dir'\nos.makedirs(path_tip_100, exist_ok = True)\nfor c in os.listdir(path):\n    new_folder_path = os.path.join(path_tip_100, c)\n    os.makedirs(new_folder_path, exist_ok = True)\n    folder_path = os.path.join(path,c)\n    for img_name in os.listdir(folder_path):\n        img_path = os.path.join(folder_path, img_name)\n        load_img=cv2.imread(img_path)\n        # load_img=cv2.cvtColor(load_img, cv2.COLOR_BGR2RGB)\n        # load_img = load_img.astype(np.float32) / 255.0\n        load_img = cv2.resize(load_img, (224,224))\n        fullOutPath = os.path.join(new_folder_path,'resized_'+img_name)\n        cv2.imwrite(fullOutPath,load_img)","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:20:12.232838Z","iopub.execute_input":"2024-07-03T07:20:12.233319Z","iopub.status.idle":"2024-07-03T07:20:18.216534Z","shell.execute_reply.started":"2024-07-03T07:20:12.23328Z","shell.execute_reply":"2024-07-03T07:20:18.21528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/working/train_224_dir'\ntest_dir = '/kaggle/working/test_224_dir'","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:22:23.092787Z","iopub.execute_input":"2024-07-03T07:22:23.093306Z","iopub.status.idle":"2024-07-03T07:22:23.099782Z","shell.execute_reply.started":"2024-07-03T07:22:23.093268Z","shell.execute_reply":"2024-07-03T07:22:23.098292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Augmenting images and storing them in temporary directories \nfor img_class in range(0,7):\n\n    #creating temporary directories\n    # creating a base directory\n    img_class = str(img_class)\n    aug_dir = 'aug_train_224_dir'\n    os.makedirs(aug_dir,exist_ok=True)\n    # creating a subdirectory inside the base directory for images of the same class\n    os.makedirs(os.path.join(aug_dir, img_class), exist_ok = True)\n    \n    img_list = os.listdir(os.path.join(train_dir, img_class))\n\n    # Copy images from the class train dir to the img_dir \n    for file_name in img_list:\n\n        # path of source image in training directory\n        source = os.path.join(train_dir, img_class, file_name)\n\n        # creating a target directory to send images \n        target = os.path.join(os.path.join(aug_dir, img_class), file_name)\n\n        # copying the image from the source to target file\n        shutil.copyfile(source, target)\n\n    # Temporary augumented dataset directory.\n    source_path = aug_dir\n\n    # Augmented images will be saved to training directory\n    save_path = aug_dir+ '/' + img_class\n\n    # Creating Image Data Generator to augment images\n    datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n\n        rotation_range=180,\n        width_shift_range=0.1,\n        height_shift_range=0.1,\n        zoom_range=0.1,\n        horizontal_flip=True,\n        vertical_flip=True,\n        fill_mode='nearest'\n\n    )\n\n    batch_size = 50\n\n    aug_datagen = datagen.flow_from_directory(source_path,save_to_dir=save_path,save_format='jpg',target_size=(224, 224),batch_size=batch_size)\n\n    # Generate the augmented images\n    aug_images = 8000 \n\n    num_files = len(os.listdir(os.path.join(aug_dir, img_class)))\n    num_batches = int(np.ceil((aug_images - num_files) / batch_size))\n\n    # creating 8000 augmented images per class\n    for i in range(0, num_batches):\n        images, labels = next(aug_datagen)\n\n    # # delete temporary directory \n    # shutil.rmtree('aug_dir')","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:26:34.415104Z","iopub.execute_input":"2024-07-03T07:26:34.415703Z","iopub.status.idle":"2024-07-03T07:38:06.92074Z","shell.execute_reply.started":"2024-07-03T07:26:34.41566Z","shell.execute_reply":"2024-07-03T07:38:06.91906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/aug_train_224_dir'\ntest_path = '/kaggle/working/test_224_dir'","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:38:06.923284Z","iopub.execute_input":"2024-07-03T07:38:06.92371Z","iopub.status.idle":"2024-07-03T07:38:06.930276Z","shell.execute_reply.started":"2024-07-03T07:38:06.923672Z","shell.execute_reply":"2024-07-03T07:38:06.928572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.cpu_count()","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:38:34.343255Z","iopub.execute_input":"2024-07-03T07:38:34.34372Z","iopub.status.idle":"2024-07-03T07:38:34.351314Z","shell.execute_reply.started":"2024-07-03T07:38:34.343689Z","shell.execute_reply":"2024-07-03T07:38:34.349994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \nfrom torchvision import datasets, transforms\nfrom torch.utils.data import DataLoader\n\nNUM_WORKERS = os.cpu_count()\n\ndef create_dataloaders(\n    train_dir: str,\n    test_dir: str,\n    transform: transforms.Compose,\n    batch_size: int,\n    num_workers: int=NUM_WORKERS\n):\n    # Use ImageFolder to create datasets(s)\n    train_data = datasets.ImageFolder(train_dir, transform = transform)\n    test_data = datasets.ImageFolder(test_dir, transform = transform)\n    \n    # Get class names\n    class_names = str(train_data.classes)\n    \n    # Turn images into data loaders\n    train_dataloader = DataLoader(\n        train_data,\n        batch_size = batch_size,\n        shuffle = True,\n        num_workers =num_workers,\n        pin_memory = True\n    )\n    test_dataloader = DataLoader(\n      test_data,\n      batch_size=batch_size,\n      shuffle=False,\n      num_workers=num_workers,\n      pin_memory=True,\n    )\n\n    return train_dataloader, test_dataloader, class_names","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:40:34.683951Z","iopub.execute_input":"2024-07-03T07:40:34.684483Z","iopub.status.idle":"2024-07-03T07:40:34.696395Z","shell.execute_reply.started":"2024-07-03T07:40:34.684448Z","shell.execute_reply":"2024-07-03T07:40:34.694726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating image size\nIMG_SIZE = 224\n\n# Creating transform pipeline manually\nmanual_transforms = transforms.Compose([\n    transforms.Resize((IMG_SIZE, IMG_SIZE)),\n    transforms.ToTensor()\n])\nprint(f\"Manually created transforms: {manual_transforms}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:45:59.114152Z","iopub.execute_input":"2024-07-03T07:45:59.114649Z","iopub.status.idle":"2024-07-03T07:45:59.12317Z","shell.execute_reply.started":"2024-07-03T07:45:59.11461Z","shell.execute_reply":"2024-07-03T07:45:59.121845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the batch size\nBATCH_SIZE = 32 \n\n# Create data loaders\ntrain_dataloader, test_dataloader, class_names = create_dataloaders(\n    train_dir=train_dir,\n    test_dir=test_dir,\n    transform=manual_transforms, \n    batch_size=BATCH_SIZE\n)\n\ntrain_dataloader, test_dataloader, class_names","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:46:17.451225Z","iopub.execute_input":"2024-07-03T07:46:17.451695Z","iopub.status.idle":"2024-07-03T07:46:17.509862Z","shell.execute_reply.started":"2024-07-03T07:46:17.451658Z","shell.execute_reply":"2024-07-03T07:46:17.508535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 1 \n* Turn images into patches\n* Flatten the patch feature maps into a single dimension\n* Convert the Output into Desired output (flattened 2D patches): (196,768) -> Nx(P2.C) # Current shpae: (1, 768, 196)","metadata":{}},{"cell_type":"code","source":"# 1. Create a class which subclasses nn.Module\nclass PatchEmbedding(nn.Module):\n    \"\"\"Turns a 2D input image into a 1D sequence learnable embedding vector.\n    \n    Args:\n        in_channels (int): Number of color channels for the input images. Defaults to 3.\n        patch_size (int): Size of patches to convert input image into. Defaults to 16.\n        embedding_dim (int): Size of embedding to turn image into. Defaults to 768.\n    \"\"\" \n    # 2. Initialize the class with appropriate variables\n    def __init__(self, \n                 in_channels:int=3,\n                 patch_size:int=16,\n                 embedding_dim:int=768):\n        super().__init__()\n        \n        # 3. Create a layer to turn an image into patches\n        self.patcher = nn.Conv2d(in_channels=in_channels,\n                                 out_channels=embedding_dim,\n                                 kernel_size=patch_size,\n                                 stride=patch_size,\n                                 padding=0)\n\n        # 4. Create a layer to flatten the patch feature maps into a single dimension\n        self.flatten = nn.Flatten(start_dim=2, # only flatten the feature map dimensions into a single vector\n                                  end_dim=3)\n\n    # 5. Define the forward method \n    def forward(self, x):\n        # Create assertion to check that inputs are the correct shape\n        image_resolution = x.shape[-1]\n        assert image_resolution % patch_size == 0, f\"Input image size must be divisble by patch size, image shape: {image_resolution}, patch size: {patch_size}\"\n        \n        # Perform the forward pass\n        x_patched = self.patcher(x)\n        x_flattened = self.flatten(x_patched) \n        \n        # 6. Make sure the output shape has the right order \n        return x_flattened.permute(0, 2, 1) # adjust so the embedding is on the final dimension [batch_size, P^2•C, N] -> [batch_size, N, P^2•C]","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:50:47.432412Z","iopub.execute_input":"2024-07-03T07:50:47.432936Z","iopub.status.idle":"2024-07-03T07:50:47.445859Z","shell.execute_reply.started":"2024-07-03T07:50:47.432898Z","shell.execute_reply":"2024-07-03T07:50:47.444675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport torch\nimport torchvision\n\nfrom torch import nn\nfrom torchvision import transforms\nfrom helpers_functions import set_seeds","metadata":{"execution":{"iopub.status.busy":"2024-07-03T08:00:56.516685Z","iopub.execute_input":"2024-07-03T08:00:56.5172Z","iopub.status.idle":"2024-07-03T08:00:56.571426Z","shell.execute_reply.started":"2024-07-03T08:00:56.517138Z","shell.execute_reply":"2024-07-03T08:00:56.569746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install helper_functions","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:57:15.714182Z","iopub.execute_input":"2024-07-03T07:57:15.715349Z","iopub.status.idle":"2024-07-03T07:57:19.451535Z","shell.execute_reply.started":"2024-07-03T07:57:15.715301Z","shell.execute_reply":"2024-07-03T07:57:19.449966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scikit-learn","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:55:20.402419Z","iopub.execute_input":"2024-07-03T07:55:20.402926Z","iopub.status.idle":"2024-07-03T07:55:37.364991Z","shell.execute_reply.started":"2024-07-03T07:55:20.402883Z","shell.execute_reply":"2024-07-03T07:55:37.363366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 1. Get pretrained weights for ViT-Base\npretrained_vit_weights = torchvision.models.ViT_B_16_Weights.DEFAULT \n\n# 2. Setup a ViT model instance with pretrained weights\npretrained_vit = torchvision.models.vit_b_16(weights=pretrained_vit_weights).to(device)\n\n# 3. Freeze the base parameters\nfor parameter in pretrained_vit.parameters():\n    parameter.requires_grad = False\n    \n# 4. Change the classifier head \nclass_names = [str(i) for i in range(0,7)]\n\nset_seeds()\npretrained_vit.heads = nn.Linear(in_features=768, out_features=len(class_names)).to(device)\n# pretrained_vit # uncomment for model output","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:54:09.53135Z","iopub.execute_input":"2024-07-03T07:54:09.531834Z","iopub.status.idle":"2024-07-03T07:54:15.996407Z","shell.execute_reply.started":"2024-07-03T07:54:09.531802Z","shell.execute_reply":"2024-07-03T07:54:15.994618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = [str(i) for i in range(0,7)]\nclass_names","metadata":{"execution":{"iopub.status.busy":"2024-07-03T07:57:06.696528Z","iopub.execute_input":"2024-07-03T07:57:06.697095Z","iopub.status.idle":"2024-07-03T07:57:06.70807Z","shell.execute_reply.started":"2024-07-03T07:57:06.697048Z","shell.execute_reply":"2024-07-03T07:57:06.706569Z"},"trusted":true},"execution_count":null,"outputs":[]}]}