{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"},"colab":{"provenance":[{"file_id":"1FSHVePF9HJAdM2oHmy_Twcvd3hcR7zel","timestamp":1710422730566}],"gpuType":"T4"},"accelerator":"GPU","kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":71608,"databundleVersionId":7895811,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%matplotlib inline\n\n# python libraties\nimport os, cv2,itertools\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nfrom glob import glob\nfrom PIL import Image\n\n# pytorch libraries\nimport torch\nfrom torch import optim,nn\nfrom torch.autograd import Variable\nfrom torch.utils.data import DataLoader,Dataset\nfrom torchvision import models,transforms\n\n# sklearn libraries\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\n\n# ensure results are reproducible\nnp.random.seed(10)\ntorch.manual_seed(10)\ntorch.cuda.manual_seed(10)","metadata":{"id":"c40b9da2-c586-4fd9-a3ca-5bfd4b55c8e6","execution":{"iopub.status.busy":"2024-04-10T17:36:28.451851Z","iopub.execute_input":"2024-04-10T17:36:28.452535Z","iopub.status.idle":"2024-04-10T17:36:28.462761Z","shell.execute_reply.started":"2024-04-10T17:36:28.452504Z","shell.execute_reply":"2024-04-10T17:36:28.461763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"/kaggle/input/aiml-general-championship\"\nprint(os.listdir(data_dir))","metadata":{"id":"f5a8f919-a3b2-4855-92eb-eae26a2d7c67","executionInfo":{"status":"ok","timestamp":1710422021319,"user_tz":-330,"elapsed":9,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"c600f9b6-4d76-490e-fbaa-74b1c920b45f","execution":{"iopub.status.busy":"2024-04-10T17:36:28.947851Z","iopub.execute_input":"2024-04-10T17:36:28.948227Z","iopub.status.idle":"2024-04-10T17:36:28.954911Z","shell.execute_reply.started":"2024-04-10T17:36:28.948196Z","shell.execute_reply":"2024-04-10T17:36:28.954060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1>Loading Data... </h1>","metadata":{"id":"3a59dac0-0469-4277-97cf-91d3184db2f8"}},{"cell_type":"code","source":"img_data_dir = data_dir + r\"/KCDH2024_Training_Input_10K/KCDH2024_Training_Input_10K\"\nall_image_path = glob(os.path.join(img_data_dir, '*.jpg'))\nimageid_path_dict = {os.path.splitext(os.path.basename(x))[0]: x for x in all_image_path}  # key - image filename,   value - path to image\nlesion_type_dict = {\n    'nv': 'Melanocytic nevi',\n    'mel': 'dermatofibroma',\n    'bkl': 'Benign keratosis-like lesions ',\n    'bcc': 'Basal cell carcinoma',\n    'akiec': 'Actinic keratoses',\n    'vasc': 'Vascular lesions',\n    'df': 'Dermatofibroma'\n}","metadata":{"id":"0f8a1675-e8fe-4073-a708-81d5ff299e25","execution":{"iopub.status.busy":"2024-04-10T17:36:29.021123Z","iopub.execute_input":"2024-04-10T17:36:29.021439Z","iopub.status.idle":"2024-04-10T17:36:29.090083Z","shell.execute_reply.started":"2024-04-10T17:36:29.021411Z","shell.execute_reply":"2024-04-10T17:36:29.089393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(imageid_path_dict)","metadata":{"id":"fca57305-c2bb-46a1-8334-26860f85c74b","executionInfo":{"status":"ok","timestamp":1710422075694,"user_tz":-330,"elapsed":12,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"ee9a2953-1766-4e7e-cc93-dd13b9721bfa","execution":{"iopub.status.busy":"2024-04-10T17:36:29.091557Z","iopub.execute_input":"2024-04-10T17:36:29.091819Z","iopub.status.idle":"2024-04-10T17:36:29.097422Z","shell.execute_reply.started":"2024-04-10T17:36:29.091795Z","shell.execute_reply":"2024-04-10T17:36:29.096520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(imageid_path_dict[\"ISIC_0024308\"])\nimg.shape","metadata":{"id":"24c97e68-8a4d-47ef-8568-6301572606e9","executionInfo":{"status":"ok","timestamp":1710422077428,"user_tz":-330,"elapsed":1739,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"de3af7e2-cb65-4ccb-e1af-68d3a6f15499","execution":{"iopub.status.busy":"2024-04-10T17:36:29.142712Z","iopub.execute_input":"2024-04-10T17:36:29.143311Z","iopub.status.idle":"2024-04-10T17:36:29.159100Z","shell.execute_reply.started":"2024-04-10T17:36:29.143284Z","shell.execute_reply":"2024-04-10T17:36:29.158324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def compute_img_mean_std(image_paths):\n    \"\"\"\n        computing the mean and std of three channel on the whole dataset,\n        first we should normalize the image from 0-255 to 0-1\n    \"\"\"\n\n    img_h, img_w = 224, 224              # Size to resize..\n    imgs = []\n    means, stdevs = [], []\n\n    for i in tqdm(range(len(image_paths))):\n        img = cv2.imread(image_paths[i])\n        img = cv2.resize(img, (img_h, img_w))\n        imgs.append(img)\n\n    imgs = np.stack(imgs, axis=3)\n    print(imgs.shape)\n\n    imgs = imgs.astype(np.float32) / 255.\n\n    for i in range(3):\n        pixels = imgs[:, :, i, :].ravel()  # resize to one row\n        means.append(np.mean(pixels))\n        stdevs.append(np.std(pixels))\n\n    means.reverse()  # BGR --> RGB\n    stdevs.reverse()\n\n    print(\"normMean = {}\".format(means))\n    print(\"normStd = {}\".format(stdevs))\n    return means,stdevs","metadata":{"id":"f7fc46fa-902e-4f03-8121-bf9713b2ce46","execution":{"iopub.status.busy":"2024-04-10T17:36:29.317736Z","iopub.execute_input":"2024-04-10T17:36:29.318469Z","iopub.status.idle":"2024-04-10T17:36:29.326369Z","shell.execute_reply.started":"2024-04-10T17:36:29.318438Z","shell.execute_reply":"2024-04-10T17:36:29.325422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# means , stdevs = compute_img_mean_std(all_image_path)","metadata":{"id":"b214f0bb-9b9a-4056-87d0-2fcb3ceae02e","execution":{"iopub.status.busy":"2024-04-10T17:36:29.449257Z","iopub.execute_input":"2024-04-10T17:36:29.449578Z","iopub.status.idle":"2024-04-10T17:36:29.455682Z","shell.execute_reply.started":"2024-04-10T17:36:29.449552Z","shell.execute_reply":"2024-04-10T17:36:29.454817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Store Values to save time in future..\nnorm_mean = [0.76696384, 0.54525656, 0.56884694]\nnorm_std = [0.13945772, 0.15192385, 0.16916788]","metadata":{"id":"951cf86b-ce11-4ffe-a8d5-05bce76a8b2f","execution":{"iopub.status.busy":"2024-04-10T17:36:29.597639Z","iopub.execute_input":"2024-04-10T17:36:29.597930Z","iopub.status.idle":"2024-04-10T17:36:29.602654Z","shell.execute_reply.started":"2024-04-10T17:36:29.597904Z","shell.execute_reply":"2024-04-10T17:36:29.601692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"b440bd71-d7b5-478c-ac87-6f805e6f1dc3"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lesion_db = pd.read_csv(os.path.join(data_dir, 'KCDH2024_Training_LesionGroupings.csv'))\ntruth_db = pd.read_csv(os.path.join(data_dir, 'KCDH2024_Training_GroundTruth.csv'))\n\ndf = pd.merge(lesion_db, truth_db, on = 'image', how = 'inner')\ndf.drop(\"diagnosis_confirm_type\", axis = 1, inplace = True)\ndf['path'] = df['image'].map(imageid_path_dict.get)\ndf","metadata":{"id":"c6b294f0-8314-4385-b301-4d2950aae9e7","executionInfo":{"status":"ok","timestamp":1710422078771,"user_tz":-330,"elapsed":1347,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"e531eb9a-efde-4420-d4c1-ba257ae6a8a8","execution":{"iopub.status.busy":"2024-04-10T17:36:29.891847Z","iopub.execute_input":"2024-04-10T17:36:29.892555Z","iopub.status.idle":"2024-04-10T17:36:29.950854Z","shell.execute_reply.started":"2024-04-10T17:36:29.892523Z","shell.execute_reply":"2024-04-10T17:36:29.949959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove the rows not containg path to images..\ndf = df[df['path'].notna()]\ndf","metadata":{"id":"11bd67f6-cb38-4565-8ee3-8da234bc54e6","executionInfo":{"status":"ok","timestamp":1710422078771,"user_tz":-330,"elapsed":21,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"1a990ea3-879b-4c62-9410-144666ef7f27","execution":{"iopub.status.busy":"2024-04-10T17:36:30.039739Z","iopub.execute_input":"2024-04-10T17:36:30.040560Z","iopub.status.idle":"2024-04-10T17:36:30.058737Z","shell.execute_reply.started":"2024-04-10T17:36:30.040528Z","shell.execute_reply":"2024-04-10T17:36:30.057819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert 7 different columns of different lesisons to single ...\n\ncell_type_idx = []\n\nfor index, row in df.iterrows():\n    cell_type_idx_row = row[\"MEL\"], row[\"NV\"], row[\"BCC\"], row[\"AKIEC\"], row[\"BKL\"], row[\"DF\"], row[\"VASC\"]\n    cell_type_idx.append(cell_type_idx_row.index(1))\n\n# Assign a new column..\ndf[\"cell_type_idx\"] = cell_type_idx\n\n# Drop older columns..\ndf.drop( columns = [\"MEL\", \"NV\", \"BCC\", \"AKIEC\", \"BKL\", \"DF\", \"VASC\"], inplace = True)\n\ndf.head()","metadata":{"id":"8a1af7d5-f0dc-4757-abb1-0b41eb80c4a8","executionInfo":{"status":"ok","timestamp":1710422078772,"user_tz":-330,"elapsed":21,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"da33ba9d-3063-4645-bc42-75752768cff0","execution":{"iopub.status.busy":"2024-04-10T17:36:30.191922Z","iopub.execute_input":"2024-04-10T17:36:30.192183Z","iopub.status.idle":"2024-04-10T17:36:31.023926Z","shell.execute_reply.started":"2024-04-10T17:36:30.192159Z","shell.execute_reply":"2024-04-10T17:36:31.022827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Determine how many images are associated with each lesion_id ?\ndf_undup = df.groupby('lesion_id').count()\n\n# Filter out lesion_id's that have only one image associated with it\ndf_undup = df_undup[df_undup['image'] == 1]\ndf_undup.reset_index(inplace=True)\ndf_undup.head()","metadata":{"id":"5d85effd-a57f-4333-917b-9bd71efd85f2","executionInfo":{"status":"ok","timestamp":1710422080001,"user_tz":-330,"elapsed":1246,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"03ec15de-e75a-417c-e839-194328304f97","execution":{"iopub.status.busy":"2024-04-10T17:36:31.025971Z","iopub.execute_input":"2024-04-10T17:36:31.026334Z","iopub.status.idle":"2024-04-10T17:36:31.056521Z","shell.execute_reply.started":"2024-04-10T17:36:31.026301Z","shell.execute_reply":"2024-04-10T17:36:31.055603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_undup = df.groupby('lesion_id').count()\ndf_undup.head()\n\n# Filter out lesion_id's that have only one image associated with it\ndf_undup = df_undup[df_undup['image'] == 1]\ndf_undup.reset_index(inplace=True)\ndf_undup[:]","metadata":{"id":"a8ac9dfc-9f52-4b5c-b8be-9d730ac9e35c","executionInfo":{"status":"ok","timestamp":1710422080001,"user_tz":-330,"elapsed":21,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"ab8786b3-2803-4298-fc3f-4f752d987751","execution":{"iopub.status.busy":"2024-04-10T17:36:31.058162Z","iopub.execute_input":"2024-04-10T17:36:31.059162Z","iopub.status.idle":"2024-04-10T17:36:31.089869Z","shell.execute_reply.started":"2024-04-10T17:36:31.059129Z","shell.execute_reply":"2024-04-10T17:36:31.088966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Identify lesion_id's that have duplicate images and those that have only one image.\ndef get_duplicates(x):\n    unique_list = list(df_undup['lesion_id'])\n    if x in unique_list:\n        return 'unduplicated'\n    else:\n        return 'duplicated'\n\n# Create a new colum that is a copy of the lesion_id column\ndf['duplicates'] = df['lesion_id']\n# Apply the function to this new column\ndf['duplicates'] = df['duplicates'].apply(get_duplicates)\ndf.head()","metadata":{"id":"b35c408f-cc71-4dd0-bf2b-836f9e4b1ebc","executionInfo":{"status":"ok","timestamp":1710422084926,"user_tz":-330,"elapsed":4944,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"47c9aeba-b623-4b4c-b409-b3667370b179","execution":{"iopub.status.busy":"2024-04-10T17:36:31.091571Z","iopub.execute_input":"2024-04-10T17:36:31.091889Z","iopub.status.idle":"2024-04-10T17:36:38.428129Z","shell.execute_reply.started":"2024-04-10T17:36:31.091866Z","shell.execute_reply":"2024-04-10T17:36:38.427216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['duplicates'].value_counts()","metadata":{"id":"39273193-7dde-456a-a827-759c52031820","executionInfo":{"status":"ok","timestamp":1710422084928,"user_tz":-330,"elapsed":30,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"e5897e64-1dc5-4700-ae34-3c08a244d423","execution":{"iopub.status.busy":"2024-04-10T17:36:38.429583Z","iopub.execute_input":"2024-04-10T17:36:38.430182Z","iopub.status.idle":"2024-04-10T17:36:38.438643Z","shell.execute_reply.started":"2024-04-10T17:36:38.430145Z","shell.execute_reply":"2024-04-10T17:36:38.437743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filter out images that don't have duplicates (count = 1)  (Removed augmented images)\n# We will use this data to get validation set..\ndf_undup = df[df['duplicates'] == 'unduplicated']\ndf_undup.shape","metadata":{"id":"e6ae5025-6eec-4616-a72a-9eb6bb6ad4b3","executionInfo":{"status":"ok","timestamp":1710422084928,"user_tz":-330,"elapsed":27,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"3b5dc897-452d-47ba-9cfb-d8fe8e0885de","execution":{"iopub.status.busy":"2024-04-10T17:36:38.439953Z","iopub.execute_input":"2024-04-10T17:36:38.440290Z","iopub.status.idle":"2024-04-10T17:36:38.450652Z","shell.execute_reply.started":"2024-04-10T17:36:38.440257Z","shell.execute_reply":"2024-04-10T17:36:38.449629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a val set using df as none of these images have augmented duplicates in the training set now..\ny = df_undup['cell_type_idx']\n_, df_val = train_test_split(df_undup, test_size=0.2, random_state=101, stratify=y)\ndf_val.shape","metadata":{"id":"a38f0226-af35-4f6e-8ea2-053197731a73","executionInfo":{"status":"ok","timestamp":1710422084928,"user_tz":-330,"elapsed":24,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"b634fb83-cec1-4aea-efa5-712e0e762156","execution":{"iopub.status.busy":"2024-04-10T17:36:38.453218Z","iopub.execute_input":"2024-04-10T17:36:38.453561Z","iopub.status.idle":"2024-04-10T17:36:38.466065Z","shell.execute_reply.started":"2024-04-10T17:36:38.453532Z","shell.execute_reply":"2024-04-10T17:36:38.465193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val['cell_type_idx'].value_counts()","metadata":{"id":"c0255feb-0fc2-4bc8-a1cd-8c5e4f317007","executionInfo":{"status":"ok","timestamp":1710422084928,"user_tz":-330,"elapsed":23,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"74f7fd4a-d1b4-49ec-dac1-b2771e40ce28","execution":{"iopub.status.busy":"2024-04-10T17:36:38.466994Z","iopub.execute_input":"2024-04-10T17:36:38.467224Z","iopub.status.idle":"2024-04-10T17:36:38.474492Z","shell.execute_reply.started":"2024-04-10T17:36:38.467203Z","shell.execute_reply":"2024-04-10T17:36:38.473671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove the validation rows from the original data to get training rows..\n\n# This function identifies if an image is part of the train or val set.\ndef get_val_rows(x):\n    # create a list of all the lesion_id's in the val set\n    val_list = list(df_val['image'])\n    if str(x) in val_list:\n        return 'val'\n    else:\n        return 'train'\n\n# Identify train and val rows..\n# Create a new colum that is a copy of the image column\ndf['train_or_val'] = df['image']\n\n# Apply the function to this new column\ndf['train_or_val'] = df['train_or_val'].apply(get_val_rows)\n\n# Filter out training rows\ndf_train = df[df['train_or_val'] == 'train']\ndf_train = df_train.drop('train_or_val', axis = 1, inplace = False)\nprint(len(df_train))\nprint(len(df_val))","metadata":{"id":"44d369f8-5367-4960-b1d2-a1bd1338c15d","executionInfo":{"status":"ok","timestamp":1710422085935,"user_tz":-330,"elapsed":1027,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"9097dd94-b7ca-45f0-c1b2-72b780e04a18","execution":{"iopub.status.busy":"2024-04-10T17:36:38.475686Z","iopub.execute_input":"2024-04-10T17:36:38.476005Z","iopub.status.idle":"2024-04-10T17:36:40.160330Z","shell.execute_reply.started":"2024-04-10T17:36:38.475971Z","shell.execute_reply":"2024-04-10T17:36:40.159440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['cell_type_idx'].value_counts()\n\n# Duplicate rows to balance the number of rows in 7 classes..\ndata_aug_rate = [6.3,1.025,13,19.8,6.1,56.4,47.7]\n\n# Iterate over unique values of 'cell_type_idx'\nfor i in df_train['cell_type_idx'].unique():\n\n    if data_aug_rate[i] > 1:\n\n        # Filter the DataFrame for the current value of 'cell_type_idx'\n        filtered_df = df_train[df_train['cell_type_idx'] == i]\n\n        # Duplicate rows based on the data augmentation rate for this value of 'cell_type_idx'\n        duplicated_rows = filtered_df.sample(frac=data_aug_rate[i] - 1, replace=True)\n\n        # Concatenate the original DataFrame with the duplicated rows\n        df_train = pd.concat([df_train, duplicated_rows], ignore_index=True)\n\ndf_train['cell_type_idx'].value_counts()","metadata":{"scrolled":true,"id":"0d2ccb27-b509-4770-a076-399dd8eb92e0","executionInfo":{"status":"ok","timestamp":1710422085935,"user_tz":-330,"elapsed":16,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"0221034e-2a47-4a23-8d38-ae423a93e1d4","execution":{"iopub.status.busy":"2024-04-10T17:36:40.161516Z","iopub.execute_input":"2024-04-10T17:36:40.161797Z","iopub.status.idle":"2024-04-10T17:36:40.191385Z","shell.execute_reply.started":"2024-04-10T17:36:40.161772Z","shell.execute_reply":"2024-04-10T17:36:40.190558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1>DataSet Spliiting</h1>","metadata":{"id":"91da8f11-4bad-4b95-8186-f2f02ee1912e"}},{"cell_type":"code","source":"len(df_train)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T17:36:40.192571Z","iopub.execute_input":"2024-04-10T17:36:40.193153Z","iopub.status.idle":"2024-04-10T17:36:40.198662Z","shell.execute_reply.started":"2024-04-10T17:36:40.193120Z","shell.execute_reply":"2024-04-10T17:36:40.197712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the test set again in a validation set and a true test set:\n\ndf_val, df_test = train_test_split(df_val, test_size=0.5)\ndf_train = df_train.reset_index()\ndf_val = df_val.reset_index()\ndf_test = df_test.reset_index()","metadata":{"id":"a15096f3-dd3c-4619-9c72-60caca33a5ad","execution":{"iopub.status.busy":"2024-04-10T17:36:40.199774Z","iopub.execute_input":"2024-04-10T17:36:40.200052Z","iopub.status.idle":"2024-04-10T17:36:40.215073Z","shell.execute_reply.started":"2024-04-10T17:36:40.200028Z","shell.execute_reply":"2024-04-10T17:36:40.214269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(df_test))\ndf_test['cell_type_idx'].value_counts()","metadata":{"id":"6a26a7c9-1a3d-49f6-a7dd-ce909f363a55","executionInfo":{"status":"ok","timestamp":1710422085936,"user_tz":-330,"elapsed":14,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"5c9dbb71-93a4-4de2-a680-58bc7a62df9c","execution":{"iopub.status.busy":"2024-04-10T17:36:40.216178Z","iopub.execute_input":"2024-04-10T17:36:40.216933Z","iopub.status.idle":"2024-04-10T17:36:40.224656Z","shell.execute_reply.started":"2024-04-10T17:36:40.216899Z","shell.execute_reply":"2024-04-10T17:36:40.223708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1>Define Model</h1>","metadata":{"id":"322b282d-4c86-4376-addd-51cd043cfb49"}},{"cell_type":"code","source":"# feature_extract is a boolean that defines finetuning or feature extracting.\n# If feature_extract = False, the model is finetuned and all model parameters are updated.\n# If feature_extract = True, only the last layer parameters are updated, the others remain fixed.\n\ndef set_parameter_requires_grad(model, feature_extracting):\n    if feature_extracting:\n        for param in model.parameters():\n            param.requires_grad = False","metadata":{"id":"7b36d853-5649-4dbd-b99e-d013008f7c90","execution":{"iopub.status.busy":"2024-04-10T17:36:40.225856Z","iopub.execute_input":"2024-04-10T17:36:40.226181Z","iopub.status.idle":"2024-04-10T17:36:40.233121Z","shell.execute_reply.started":"2024-04-10T17:36:40.226151Z","shell.execute_reply":"2024-04-10T17:36:40.232064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):\n    # Initialize these variables which will be set\n    model_ft = None\n    input_size = 0\n    \n    if model_name == \"efficientnet\":\n        model_ft = models.efficientnet_b3(pretrained=True, progress=True)\n        set_parameter_requires_grad(model_ft, feature_extract)\n        input_size = 224\n\n    else:\n        print(\"Invalid model name, exiting...\")\n        exit()\n\n    return model_ft, input_size","metadata":{"id":"b2430227-8d6c-45a3-b73b-a7280f5091e6","execution":{"iopub.status.busy":"2024-04-10T18:31:45.930563Z","iopub.execute_input":"2024-04-10T18:31:45.931238Z","iopub.status.idle":"2024-04-10T18:31:45.937774Z","shell.execute_reply.started":"2024-04-10T18:31:45.931147Z","shell.execute_reply":"2024-04-10T18:31:45.936816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.is_available()","metadata":{"id":"pDKXPsvtLlCW","executionInfo":{"status":"ok","timestamp":1710422085936,"user_tz":-330,"elapsed":12,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"e10014fd-9ce7-4338-bafd-144e3ff8a384","execution":{"iopub.status.busy":"2024-04-10T18:31:46.100281Z","iopub.execute_input":"2024-04-10T18:31:46.100694Z","iopub.status.idle":"2024-04-10T18:31:46.107116Z","shell.execute_reply.started":"2024-04-10T18:31:46.100663Z","shell.execute_reply":"2024-04-10T18:31:46.106232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a new model variable..\n\nmodel_name = \"efficientnet\"\nnum_classes = 7\nfeature_extract = False\n# Initialize the model\nmodel_ft, input_size = initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)\n\n# Define the device:\ndevice = torch.device('cuda:0')\n# device = torch.device('cpu') # If using cpu\n# Put the model on the device:\nmodel = model_ft.to(device)","metadata":{"id":"269c3eef-51cc-4816-b76f-7b799f1ae2ef","executionInfo":{"status":"ok","timestamp":1710422088710,"user_tz":-330,"elapsed":2785,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"b6c2760e-3c3d-4c72-b295-453897e5d2bc","execution":{"iopub.status.busy":"2024-04-10T18:31:46.274429Z","iopub.execute_input":"2024-04-10T18:31:46.275153Z","iopub.status.idle":"2024-04-10T18:31:46.662563Z","shell.execute_reply.started":"2024-04-10T18:31:46.275115Z","shell.execute_reply":"2024-04-10T18:31:46.661774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<H1>Augmenting Data...</H1>","metadata":{"id":"bc6ed264-20e3-4738-a852-e56fa710caba"}},{"cell_type":"code","source":"# define the transformation of the train images.\ntrain_transform = transforms.Compose([transforms.Resize((input_size,input_size)),transforms.RandomHorizontalFlip(),\n                                      transforms.RandomVerticalFlip(),\n                                      transforms.RandomRotation(20),\n                                      transforms.ColorJitter(brightness=0.1, contrast=0.1, hue=0.1),\n                                      transforms.ToTensor(), \n                                      transforms.Normalize(norm_mean, norm_std)])\n\n\n# define the transformation of the val images.\nval_transform = transforms.Compose([transforms.Resize((input_size,input_size)),\n                                    transforms.ToTensor(),\n                                    transforms.Normalize(norm_mean, norm_std)])\n\n# define the transformation of the test images.\ntest_transform = transforms.Compose([transforms.Resize((input_size,input_size)),\n                                     transforms.ToTensor(),\n                                    transforms.Normalize(norm_mean, norm_std)])","metadata":{"id":"7523d804-777c-45d9-9c5e-fea359dca2d6","execution":{"iopub.status.busy":"2024-04-10T18:31:46.664217Z","iopub.execute_input":"2024-04-10T18:31:46.664617Z","iopub.status.idle":"2024-04-10T18:31:46.672553Z","shell.execute_reply.started":"2024-04-10T18:31:46.664583Z","shell.execute_reply":"2024-04-10T18:31:46.671405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a pytorch dataloader for dataset..\nclass HAM10000(Dataset):\n    def __init__(self, df, transform=None):\n        self.df = df\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, index):\n        # Load data and get label\n        X = Image.open(self.df['path'][index])\n        y = torch.tensor(int(self.df['cell_type_idx'][index]))\n\n        if self.transform:\n            X = self.transform(X)\n\n        return X, y","metadata":{"id":"c770294a-3192-437e-a6ea-491246ccda2a","execution":{"iopub.status.busy":"2024-04-10T18:31:46.785004Z","iopub.execute_input":"2024-04-10T18:31:46.785506Z","iopub.status.idle":"2024-04-10T18:31:46.791830Z","shell.execute_reply.started":"2024-04-10T18:31:46.785480Z","shell.execute_reply":"2024-04-10T18:31:46.790861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the training set using the table train_df and using the defined transitions (train_transform)\ntraining_set = HAM10000(df_train, transform=train_transform)\ntrain_loader = DataLoader(training_set, batch_size=32, shuffle=True, num_workers=4)\n\n# Same for the validation set:\nvalidation_set = HAM10000(df_val, transform=train_transform)\nval_loader = DataLoader(validation_set, batch_size=32, shuffle=False, num_workers=4)\n\n# Same for the test set:\ntest_set = HAM10000(df_test, transform=train_transform)\ntest_loader = DataLoader(test_set, batch_size=32, shuffle=False, num_workers=4)","metadata":{"id":"03bf71e2-b9f3-4d3e-b41f-5d60ee984b66","executionInfo":{"status":"ok","timestamp":1710422088711,"user_tz":-330,"elapsed":26,"user":{"displayName":"Madhav Agrawal","userId":"14470504424331194097"}},"outputId":"cfa2b5f0-b30a-4d81-bf40-6eed24282f16","execution":{"iopub.status.busy":"2024-04-10T18:31:46.947505Z","iopub.execute_input":"2024-04-10T18:31:46.948295Z","iopub.status.idle":"2024-04-10T18:31:46.954463Z","shell.execute_reply.started":"2024-04-10T18:31:46.948262Z","shell.execute_reply":"2024-04-10T18:31:46.953403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this function is used during training process, to calculate the loss and accuracy\nclass AverageMeter(object):\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.val = 0\n        self.avg = 0\n        self.sum = 0\n        self.count = 0\n\n    def update(self, val, n=1):\n        self.val = val\n        self.sum += val * n\n        self.count += n\n        self.avg = self.sum / self.count","metadata":{"id":"3364d0cb-439f-4364-895d-f589cf8caff6","execution":{"iopub.status.busy":"2024-04-10T18:31:47.117938Z","iopub.execute_input":"2024-04-10T18:31:47.118570Z","iopub.status.idle":"2024-04-10T18:31:47.125272Z","shell.execute_reply.started":"2024-04-10T18:31:47.118542Z","shell.execute_reply":"2024-04-10T18:31:47.124228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_loss_train, total_acc_train = [],[]\n\ndef train(train_loader, model, criterion, optimizer, epoch, threshold_batch):\n    model.train()\n    train_loss = AverageMeter()\n    train_acc = AverageMeter()\n    curr_iter = (epoch - 1) * len(train_loader)\n    for i, data in enumerate(train_loader):\n        \n        if threshold_batch and i>= threshold_batch:\n            break       \n\n        images, labels = data\n        N = images.size(0)\n        images = Variable(images).to(device)\n        labels = Variable(labels).to(device)\n\n        optimizer.zero_grad()\n        outputs = model(images)\n\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        prediction = outputs.max(1, keepdim=True)[1]\n        train_acc.update(prediction.eq(labels.view_as(prediction)).sum().item()/N)\n        train_loss.update(loss.item())\n        curr_iter += 1\n        if (i + 1) % 100 == 0:\n            print('[epoch %d], [iter %d / %d], [train loss %.5f], [train acc %.5f]' % (\n                epoch, i + 1, len(train_loader), train_loss.avg, train_acc.avg))\n            total_loss_train.append(train_loss.avg)\n            total_acc_train.append(train_acc.avg)\n    return train_loss.avg, train_acc.avg","metadata":{"id":"04368d6e-67ea-4322-970f-f23baec8d359","execution":{"iopub.status.busy":"2024-04-10T18:31:47.260935Z","iopub.execute_input":"2024-04-10T18:31:47.261730Z","iopub.status.idle":"2024-04-10T18:31:47.271099Z","shell.execute_reply.started":"2024-04-10T18:31:47.261699Z","shell.execute_reply":"2024-04-10T18:31:47.270137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validate(val_loader, model, criterion, optimizer, epoch):\n    model.eval()\n    val_loss = AverageMeter()\n    val_acc = AverageMeter()\n    \n    total_val_loss = []\n    total_val_acc = []\n\n    with torch.no_grad():\n        for i, data in tqdm(enumerate(val_loader)):\n\n            images, labels = data\n            N = images.size(0)\n            images = Variable(images).to(device)\n            labels = Variable(labels).to(device)\n\n            outputs = model(images)\n            prediction = outputs.max(1, keepdim=True)[1]\n\n            val_acc.update(prediction.eq(labels.view_as(prediction)).sum().item()/N)\n            val_loss.update(criterion(outputs, labels).item())\n            \n            if (i + 1) % 3 == 0:\n                total_val_acc.append(val_acc.avg)\n                total_val_loss.append(val_loss.avg)\n\n    print('------------------------------------------------------------')\n    print('[epoch %d], [val loss %.5f], [val acc %.5f]' % (epoch, val_loss.avg, val_acc.avg))\n    print('------------------------------------------------------------')\n    return  total_val_loss , total_val_acc","metadata":{"id":"0cf9d7a6-6a20-4285-96ef-6115387ea8b8","execution":{"iopub.status.busy":"2024-04-10T18:31:47.397953Z","iopub.execute_input":"2024-04-10T18:31:47.398561Z","iopub.status.idle":"2024-04-10T18:31:47.407259Z","shell.execute_reply.started":"2024-04-10T18:31:47.398531Z","shell.execute_reply":"2024-04-10T18:31:47.406405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Start Training..\ndef train_model(epoch_num, lr, optimizer_func = optim.Adam, threshold_batch = None, criterion = nn.CrossEntropyLoss()):\n\n    best_val_acc = 0\n    total_loss_val, total_acc_val = [],[]\n    for epoch in tqdm(range(1, epoch_num+1)):\n\n        # set optimizer and loss function\n        optimizer = optimizer_func(model.parameters(), lr= lr)\n        criterion = criterion.to(device)\n\n        loss_train, acc_train = train(train_loader, model, criterion, optimizer, epoch, threshold_batch)\n        loss_val, acc_val = validate(val_loader, model, criterion , optimizer, epoch)\n        total_loss_val += loss_val\n        total_acc_val += acc_val\n    return total_loss_val, total_acc_val","metadata":{"scrolled":true,"id":"5a283e24-b1ee-40e9-9d81-baa3054d9afa","outputId":"d231e35d-08f8-42c7-add8-69778ce48856","execution":{"iopub.status.busy":"2024-04-10T18:31:47.554483Z","iopub.execute_input":"2024-04-10T18:31:47.554751Z","iopub.status.idle":"2024-04-10T18:31:47.561552Z","shell.execute_reply.started":"2024-04-10T18:31:47.554727Z","shell.execute_reply":"2024-04-10T18:31:47.560519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Store Training data..\ntotal_loss_val, total_acc_val = [],[]\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:31:47.997545Z","iopub.execute_input":"2024-04-10T18:31:47.998278Z","iopub.status.idle":"2024-04-10T18:31:48.002778Z","shell.execute_reply.started":"2024-04-10T18:31:47.998246Z","shell.execute_reply":"2024-04-10T18:31:48.001774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training - 1st\ndata = train_model(1,1e-3)\ntotal_loss_val += data[0]\ntotal_acc_val += data[1]","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:31:48.329619Z","iopub.execute_input":"2024-04-10T18:31:48.330439Z","iopub.status.idle":"2024-04-10T18:36:56.327767Z","shell.execute_reply.started":"2024-04-10T18:31:48.330407Z","shell.execute_reply":"2024-04-10T18:36:56.326590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check results on test dataset..\nvalidate(test_loader, model, criterion, optimizer, 1)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:36:56.330277Z","iopub.execute_input":"2024-04-10T18:36:56.331048Z","iopub.status.idle":"2024-04-10T18:37:00.306007Z","shell.execute_reply.started":"2024-04-10T18:36:56.331005Z","shell.execute_reply":"2024-04-10T18:37:00.304954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training - 2nd..\ntrain_model(1,1e-5,threshold_batch = 300)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:37:00.307387Z","iopub.execute_input":"2024-04-10T18:37:00.307707Z","iopub.status.idle":"2024-04-10T18:38:16.293424Z","shell.execute_reply.started":"2024-04-10T18:37:00.307676Z","shell.execute_reply":"2024-04-10T18:38:16.292298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check results on test dataset..\nvalidate(test_loader, model, criterion, optimizer, 1)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:38:16.296688Z","iopub.execute_input":"2024-04-10T18:38:16.297403Z","iopub.status.idle":"2024-04-10T18:38:20.140585Z","shell.execute_reply.started":"2024-04-10T18:38:16.297343Z","shell.execute_reply":"2024-04-10T18:38:20.139511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training - 3rd..\ntrain_model(1,1e-5,threshold_batch = 300)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:38:20.142559Z","iopub.execute_input":"2024-04-10T18:38:20.142863Z","iopub.status.idle":"2024-04-10T18:39:36.460623Z","shell.execute_reply.started":"2024-04-10T18:38:20.142835Z","shell.execute_reply":"2024-04-10T18:39:36.459414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check results on test dataset..\nvalidate(test_loader, model, criterion, optimizer, 1)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:36.462204Z","iopub.execute_input":"2024-04-10T18:39:36.462566Z","iopub.status.idle":"2024-04-10T18:39:40.382079Z","shell.execute_reply.started":"2024-04-10T18:39:36.462534Z","shell.execute_reply":"2024-04-10T18:39:40.381039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> Analaysis Of Accuracies.. </h1>","metadata":{}},{"cell_type":"code","source":"total_loss_val","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:40.383464Z","iopub.execute_input":"2024-04-10T18:39:40.383756Z","iopub.status.idle":"2024-04-10T18:39:40.390144Z","shell.execute_reply.started":"2024-04-10T18:39:40.383728Z","shell.execute_reply":"2024-04-10T18:39:40.389091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Analysing accuracy and loss of validation data over the model..\n\nfig = plt.figure(num = 1)\nfig2 = fig.add_subplot(1,1,1)\nfig2.plot(total_acc_val, label = 'validation accuracy')\nfig2.plot(total_loss_val, label = 'validation loss')\n\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:40.391337Z","iopub.execute_input":"2024-04-10T18:39:40.391643Z","iopub.status.idle":"2024-04-10T18:39:40.664408Z","shell.execute_reply.started":"2024-04-10T18:39:40.391619Z","shell.execute_reply":"2024-04-10T18:39:40.663434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Analysing accuracy and loss of training data over the model..\n\nfig = plt.figure(num=1)\nfig1 = fig.add_subplot(1,1,1)\nfig1.plot(total_acc_train, label = 'training accuracy')\nfig1.plot(total_loss_train, label = 'training loss')\n\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:40.665856Z","iopub.execute_input":"2024-04-10T18:39:40.666244Z","iopub.status.idle":"2024-04-10T18:39:40.941244Z","shell.execute_reply.started":"2024-04-10T18:39:40.666207Z","shell.execute_reply":"2024-04-10T18:39:40.940346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    \n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:40.944530Z","iopub.execute_input":"2024-04-10T18:39:40.944902Z","iopub.status.idle":"2024-04-10T18:39:40.954264Z","shell.execute_reply.started":"2024-04-10T18:39:40.944874Z","shell.execute_reply":"2024-04-10T18:39:40.953312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This is Validation data evaluation\n\nmodel.eval()\ny_label = []\ny_predict = []\nwith torch.no_grad():\n    for i, data in enumerate(val_loader):\n        images, labels = data\n        N = images.size(0)\n        images = Variable(images).to(device)\n        outputs = model(images)\n        prediction = outputs.max(1, keepdim=True)[1]\n        y_label.extend(labels.cpu().numpy())\n        y_predict.extend(np.squeeze(prediction.cpu().numpy().T))\n\n# compute the confusion matrix for the validation matrix..\nconfusion_mtx = confusion_matrix(y_label, y_predict)\n# plot the confusion matrix..\nplot_labels = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc','mel']\nplot_confusion_matrix(confusion_mtx, plot_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:40.955508Z","iopub.execute_input":"2024-04-10T18:39:40.955781Z","iopub.status.idle":"2024-04-10T18:39:45.324192Z","shell.execute_reply.started":"2024-04-10T18:39:40.955757Z","shell.execute_reply":"2024-04-10T18:39:45.323116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This is test data evaluation\n\nmodel.eval()\ntest_y_label = []\ntest_y_predict = []\nwith torch.no_grad():\n    for i, data in enumerate(test_loader):\n        images, labels = data\n        N = images.size(0)\n        images = Variable(images).to(device)\n        outputs = model(images)\n        prediction = outputs.max(1, keepdim=True)[1]\n        test_y_label.extend(labels.cpu().numpy())\n        test_y_predict.extend(np.squeeze(prediction.cpu().numpy().T))\n\n# compute the confusion matrix for the test data..\nconfusion_mtx_test = confusion_matrix(test_y_label, test_y_predict)\n\n# plot the confusion matrix\nplot_labels = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc','mel']\nplot_confusion_matrix(confusion_mtx, plot_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:45.325945Z","iopub.execute_input":"2024-04-10T18:39:45.326684Z","iopub.status.idle":"2024-04-10T18:39:49.868952Z","shell.execute_reply.started":"2024-04-10T18:39:45.326640Z","shell.execute_reply":"2024-04-10T18:39:49.867794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate a validation classification report\nreport = classification_report(y_label, y_predict, target_names=plot_labels)\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:49.870201Z","iopub.execute_input":"2024-04-10T18:39:49.870523Z","iopub.status.idle":"2024-04-10T18:39:49.887646Z","shell.execute_reply.started":"2024-04-10T18:39:49.870485Z","shell.execute_reply":"2024-04-10T18:39:49.886729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate a test classification report\nreport = classification_report(test_y_label, test_y_predict, target_names=plot_labels)\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:49.888576Z","iopub.execute_input":"2024-04-10T18:39:49.888819Z","iopub.status.idle":"2024-04-10T18:39:49.904506Z","shell.execute_reply.started":"2024-04-10T18:39:49.888797Z","shell.execute_reply":"2024-04-10T18:39:49.903595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# True vs Incoorect classified analysis for validation data\n\nlabel_frac_error = 1 - np.diag(confusion_mtx) / np.sum(confusion_mtx, axis=1)\nplt.bar(np.arange(7),label_frac_error)\nplt.xlabel('True Label')\nplt.ylabel('Fraction classified incorrectly')","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:49.905709Z","iopub.execute_input":"2024-04-10T18:39:49.906503Z","iopub.status.idle":"2024-04-10T18:39:50.207156Z","shell.execute_reply.started":"2024-04-10T18:39:49.906468Z","shell.execute_reply":"2024-04-10T18:39:50.206172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# True vs Incoorect classified analysis for test data\n\nlabel_frac_error = 1 - np.diag(confusion_mtx_test) / np.sum(confusion_mtx_test, axis=1)\nplt.bar(np.arange(7),label_frac_error)\nplt.xlabel('True Label')\nplt.ylabel('Fraction classified incorrectly')","metadata":{"execution":{"iopub.status.busy":"2024-04-10T18:39:50.208573Z","iopub.execute_input":"2024-04-10T18:39:50.208874Z","iopub.status.idle":"2024-04-10T18:39:50.473987Z","shell.execute_reply.started":"2024-04-10T18:39:50.208847Z","shell.execute_reply":"2024-04-10T18:39:50.472995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> Predictions... </h1>","metadata":{}},{"cell_type":"code","source":"# Get data..\nimg_data_dir = data_dir + r\"/KCDH2024_Test_Input/KCDH2024_Test_Input\"\nall_image_path = glob(os.path.join(img_data_dir, '*.jpg'))\nimageid_path_dict = {os.path.splitext(os.path.basename(x))[0]: x for x in all_image_path}  # key - image filename,   value - path to image\nlen(imageid_path_dict)","metadata":{"id":"c6790879-389d-4ff7-93d4-ddfe30320f3f","execution":{"iopub.status.busy":"2024-03-16T08:05:40.007377Z","iopub.execute_input":"2024-03-16T08:05:40.008301Z","iopub.status.idle":"2024-03-16T08:05:40.028668Z","shell.execute_reply.started":"2024-03-16T08:05:40.008265Z","shell.execute_reply":"2024-03-16T08:05:40.027812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Header of txt file..\ntext = \"ID,Class\\n\"\n\n# Rows ..\nmodel.eval()\nwith torch.no_grad():\n    for imagename, path in tqdm(imageid_path_dict.items()):\n         X = Image.open(path)\n         X = test_transform(X)\n         X = Variable(X).to(device)\n         output = model(X.unsqueeze(0))\n         prediction = output.max(1, keepdim=True)[1].item()\n         text += f\"{imagename},{prediction}\\n\"","metadata":{"execution":{"iopub.status.busy":"2024-03-16T08:09:00.393768Z","iopub.execute_input":"2024-03-16T08:09:00.394788Z","iopub.status.idle":"2024-03-16T08:09:39.802024Z","shell.execute_reply.started":"2024-03-16T08:09:00.394732Z","shell.execute_reply":"2024-03-16T08:09:39.801142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(f'predictions.csv', 'w') as txtfile:\n    txtfile.write(text)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T08:10:05.499061Z","iopub.execute_input":"2024-03-16T08:10:05.499444Z","iopub.status.idle":"2024-03-16T08:10:05.504731Z","shell.execute_reply.started":"2024-03-16T08:10:05.499392Z","shell.execute_reply":"2024-03-16T08:10:05.503790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}