{"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":"gpu","dataSources":[{"sourceId":13451,"databundleVersionId":1188070,"sourceType":"competition"},{"sourceId":7662625,"sourceType":"datasetVersion","datasetId":4468210}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import re\nimport os\nimport torch\nimport pydicom\nimport numpy as np\nimport random\nimport pandas as pd\nimport torch.nn as nn\nfrom torch import optim\nfrom torchvision import transforms\nimport torchvision\nfrom torchvision.models import inception_v3\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset\n\nrandom.seed(42)\nnp.random.seed(42)\ntorch.manual_seed(42)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:02.101091Z","iopub.execute_input":"2024-02-20T10:29:02.10138Z","iopub.status.idle":"2024-02-20T10:29:09.688223Z","shell.execute_reply.started":"2024-02-20T10:29:02.101356Z","shell.execute_reply":"2024-02-20T10:29:09.687262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:09.689899Z","iopub.execute_input":"2024-02-20T10:29:09.690331Z","iopub.status.idle":"2024-02-20T10:29:09.695029Z","shell.execute_reply.started":"2024-02-20T10:29:09.690306Z","shell.execute_reply":"2024-02-20T10:29:09.693757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH= r'/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train'\ndevice= 'cuda' if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:09.696133Z","iopub.execute_input":"2024-02-20T10:29:09.696455Z","iopub.status.idle":"2024-02-20T10:29:09.730383Z","shell.execute_reply.started":"2024-02-20T10:29:09.696431Z","shell.execute_reply":"2024-02-20T10:29:09.729579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:09.731636Z","iopub.execute_input":"2024-02-20T10:29:09.73222Z","iopub.status.idle":"2024-02-20T10:29:10.703078Z","shell.execute_reply.started":"2024-02-20T10:29:09.732186Z","shell.execute_reply":"2024-02-20T10:29:10.701999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf= pd.read_csv('/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train.csv')\ntraindf.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:10.705866Z","iopub.execute_input":"2024-02-20T10:29:10.7062Z","iopub.status.idle":"2024-02-20T10:29:15.816529Z","shell.execute_reply.started":"2024-02-20T10:29:10.70617Z","shell.execute_reply":"2024-02-20T10:29:15.815577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf[['ID','Subtype']]= traindf['ID'].str.rsplit(pat='_',n=1,expand=True)\ntraindf.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:15.817936Z","iopub.execute_input":"2024-02-20T10:29:15.818392Z","iopub.status.idle":"2024-02-20T10:29:27.369559Z","shell.execute_reply.started":"2024-02-20T10:29:15.818358Z","shell.execute_reply":"2024-02-20T10:29:27.368593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf= traindf.pivot_table(columns='Subtype',values='Label',index='ID').reset_index()\ntraindf.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:27.371396Z","iopub.execute_input":"2024-02-20T10:29:27.372133Z","iopub.status.idle":"2024-02-20T10:29:33.26385Z","shell.execute_reply.started":"2024-02-20T10:29:27.372094Z","shell.execute_reply":"2024-02-20T10:29:33.262957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf['any'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:33.264928Z","iopub.execute_input":"2024-02-20T10:29:33.265178Z","iopub.status.idle":"2024-02-20T10:29:33.283187Z","shell.execute_reply.started":"2024-02-20T10:29:33.265157Z","shell.execute_reply":"2024-02-20T10:29:33.28213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.replace([np.inf, -np.inf], np.nan, inplace=True)\ntraindf.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:33.285176Z","iopub.execute_input":"2024-02-20T10:29:33.285476Z","iopub.status.idle":"2024-02-20T10:29:33.843141Z","shell.execute_reply.started":"2024-02-20T10:29:33.28544Z","shell.execute_reply":"2024-02-20T10:29:33.842172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a= pydicom.read_file(os.path.join(TRAIN_PATH,'ID_000012eaf.dcm'))\na","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:33.844483Z","iopub.execute_input":"2024-02-20T10:29:33.844795Z","iopub.status.idle":"2024-02-20T10:29:33.872384Z","shell.execute_reply.started":"2024-02-20T10:29:33.84477Z","shell.execute_reply":"2024-02-20T10:29:33.871532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_= traindf.drop(columns=['ID']).sum()\nax= sns.barplot(x=_.index, y=_, capsize=0.01)\nplt.title('Occurances of each class')\nplt.xlabel('Class')\nplt.ylabel('Count')\nax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:33.873322Z","iopub.execute_input":"2024-02-20T10:29:33.8736Z","iopub.status.idle":"2024-02-20T10:29:34.121679Z","shell.execute_reply.started":"2024-02-20T10:29:33.873577Z","shell.execute_reply":"2024-02-20T10:29:34.120597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_.epidural","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.123142Z","iopub.execute_input":"2024-02-20T10:29:34.12394Z","iopub.status.idle":"2024-02-20T10:29:34.130263Z","shell.execute_reply.started":"2024-02-20T10:29:34.123898Z","shell.execute_reply":"2024-02-20T10:29:34.129271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf['any']= traindf['any'].apply(lambda x : 0.0 if x==1.0 else 1.0)\nnot_any= traindf[traindf['any']==1.0]\nepidural = traindf[traindf['epidural']==1.0]\nintraparenchymal = traindf[traindf['intraparenchymal']==1.0]\nintraventricular = traindf[traindf['intraventricular']==1.0]\nsubarachnoid = traindf[traindf['subarachnoid']==1.0]\nsubdural = traindf[traindf['subdural']==1.0]\ndata= [not_any, epidural, intraparenchymal, intraventricular, subarachnoid, subdural]","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.131358Z","iopub.execute_input":"2024-02-20T10:29:34.1317Z","iopub.status.idle":"2024-02-20T10:29:34.528555Z","shell.execute_reply.started":"2024-02-20T10:29:34.131676Z","shell.execute_reply":"2024-02-20T10:29:34.527727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lim= min([i.shape[0] for i in data])\nn= not_any.sample(lim*4)\ndf= pd.concat([i.sample(lim) for i in data], axis=0)\ndf= pd.concat([df,n],axis=0)#.reset_index()","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.533103Z","iopub.execute_input":"2024-02-20T10:29:34.5334Z","iopub.status.idle":"2024-02-20T10:29:34.583651Z","shell.execute_reply.started":"2024-02-20T10:29:34.533376Z","shell.execute_reply":"2024-02-20T10:29:34.582575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.584859Z","iopub.execute_input":"2024-02-20T10:29:34.585151Z","iopub.status.idle":"2024-02-20T10:29:34.591366Z","shell.execute_reply.started":"2024-02-20T10:29:34.585128Z","shell.execute_reply":"2024-02-20T10:29:34.59036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.path.getsize('/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/ID_000012eaf.dcm')*1e-9*df.shape[0],\"GB\")","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.592767Z","iopub.execute_input":"2024-02-20T10:29:34.59309Z","iopub.status.idle":"2024-02-20T10:29:34.601097Z","shell.execute_reply.started":"2024-02-20T10:29:34.593065Z","shell.execute_reply":"2024-02-20T10:29:34.600084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.60431Z","iopub.execute_input":"2024-02-20T10:29:34.605015Z","iopub.status.idle":"2024-02-20T10:29:34.621565Z","shell.execute_reply.started":"2024-02-20T10:29:34.604979Z","shell.execute_reply":"2024-02-20T10:29:34.620666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.reset_index(drop=True,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.622634Z","iopub.execute_input":"2024-02-20T10:29:34.622936Z","iopub.status.idle":"2024-02-20T10:29:34.630323Z","shell.execute_reply.started":"2024-02-20T10:29:34.622911Z","shell.execute_reply":"2024-02-20T10:29:34.629518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pydicom.read_file(os.path.join(TRAIN_PATH,df.iloc[1, 0]+'.dcm'))","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.63155Z","iopub.execute_input":"2024-02-20T10:29:34.631892Z","iopub.status.idle":"2024-02-20T10:29:34.66535Z","shell.execute_reply.started":"2024-02-20T10:29:34.631862Z","shell.execute_reply":"2024-02-20T10:29:34.664421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a= pydicom.read_file(os.path.join(TRAIN_PATH,df.iloc[0, 0]+'.dcm'))\na","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.6665Z","iopub.execute_input":"2024-02-20T10:29:34.666817Z","iopub.status.idle":"2024-02-20T10:29:34.69117Z","shell.execute_reply.started":"2024-02-20T10:29:34.666793Z","shell.execute_reply":"2024-02-20T10:29:34.690226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a.pixel_array","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.692306Z","iopub.execute_input":"2024-02-20T10:29:34.692613Z","iopub.status.idle":"2024-02-20T10:29:34.699443Z","shell.execute_reply.started":"2024-02-20T10:29:34.692589Z","shell.execute_reply":"2024-02-20T10:29:34.698471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def correct_dcm(dcm):\n        x = dcm.pixel_array + 1000\n        px_mode = 4096\n        x[x>=px_mode] = x[x>=px_mode] - px_mode\n        dcm.PixelData = x.tobytes()\n        dcm.RescaleIntercept = -1000\n\ndef window_image(dcm, window_center, window_width):\n\n    if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n        correct_dcm(dcm)\n\n    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    img = np.clip(img, img_min, img_max)\n\n    return img\n\ndef bsb_window(dcm):\n        brain_img = window_image(dcm, 40, 80)\n        subdural_img = window_image(dcm, 80, 200)\n        soft_img = window_image(dcm, 40, 380)\n\n        brain_img = (brain_img - 0) / 80\n        subdural_img = (subdural_img - (-20)) / 200\n        soft_img = (soft_img - (-150)) / 380\n        bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)\n\n        return bsb_img\n\nplt.imshow(bsb_window(a), cmap=plt.cm.bone)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.700693Z","iopub.execute_input":"2024-02-20T10:29:34.700994Z","iopub.status.idle":"2024-02-20T10:29:34.998179Z","shell.execute_reply.started":"2024-02-20T10:29:34.700969Z","shell.execute_reply":"2024-02-20T10:29:34.997262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DicomDataset(Dataset):\n    def __init__(self, img_dir, df, transform=None, labels=True):\n        self.transform = transform\n        self.img_dir = img_dir\n        self.df = df\n        self.labels=labels\n        \n    def correct_dcm(self, dcm):\n        x = dcm.pixel_array + 1000\n        px_mode = 4096\n        x[x>=px_mode] = x[x>=px_mode] - px_mode\n        dcm.PixelData = x.tobytes()\n        dcm.RescaleIntercept = -1000\n\n    def window_image(self, dcm, window_center, window_width):\n\n        if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n            self.correct_dcm(dcm)\n\n        img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept\n        img_min = window_center - window_width // 2\n        img_max = window_center + window_width // 2\n        img = np.clip(img, img_min, img_max)\n\n        return img\n\n    def bsb_window(self, dcm):\n        brain_img = self.window_image(dcm, 40, 80)\n        subdural_img = self.window_image(dcm, 80, 200)\n        soft_img = self.window_image(dcm, 40, 380)\n\n        brain_img = (brain_img - 0) / 80\n        subdural_img = (subdural_img - (-20)) / 200\n        soft_img = (soft_img - (-150)) / 380\n        bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)\n\n        return bsb_img\n    \n#     def zero_center(self, image):\n#         image = image - PIXEL_MEAN\n#         return image\n    \n    \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        if torch.is_tensor(idx):\n            idx = idx.tolist()\n            \n        img_path = os.path.join(self.img_dir, self.df.iloc[idx, 0]+'.dcm')\n        data = pydicom.read_file(img_path)\n        img = self.bsb_window(data)\n        \n        if self.transform:       \n            augmented = self.transform(image=img)\n            img = augmented['image']\n        \n        if self.labels:\n            label = torch.tensor(self.df.iloc[idx, 1:],dtype=torch.float64)#.astype(float).to_numpy()\n            return {'image': img, 'labels': label} \n#         img = GET WINDOWED, NORMALIZED and SCALED PIXEL ARRAY HERE\n        return {'image': img}","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:34.999396Z","iopub.execute_input":"2024-02-20T10:29:34.999694Z","iopub.status.idle":"2024-02-20T10:29:35.01516Z","shell.execute_reply.started":"2024-02-20T10:29:34.99967Z","shell.execute_reply":"2024-02-20T10:29:35.014202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d= DicomDataset(TRAIN_PATH,df,labels=False)\nplt.xticks([]),plt.yticks([])\nplt.imshow(d[0]['image'], cmap=plt.cm.bone)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p= d[0]['image'].copy()\np.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:35.299372Z","iopub.execute_input":"2024-02-20T10:29:35.299745Z","iopub.status.idle":"2024-02-20T10:29:35.317666Z","shell.execute_reply.started":"2024-02-20T10:29:35.299712Z","shell.execute_reply":"2024-02-20T10:29:35.316756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from albumentations import Compose, CenterCrop, HorizontalFlip, Normalize, RandomRotate90\nfrom albumentations.pytorch import ToTensorV2\n\ntransform_train = Compose([CenterCrop(299,299),\n                           Normalize(mean=0.5, std=1.0),\n                           RandomRotate90(p=0.3),\n                           HorizontalFlip(p=0.3),\n                           ToTensorV2()\n])\n\ntransform_test= Compose([CenterCrop(299,299),\n                         Normalize(mean=0.5, std=1.0),\n                         ToTensorV2()\n])","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:35.318806Z","iopub.execute_input":"2024-02-20T10:29:35.319097Z","iopub.status.idle":"2024-02-20T10:29:36.059418Z","shell.execute_reply.started":"2024-02-20T10:29:35.319074Z","shell.execute_reply":"2024-02-20T10:29:36.058406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX,Y= train_test_split(df, test_size=0.2, shuffle=True)\n\ntrain_dataset= DicomDataset(TRAIN_PATH, X, transform=transform_train, labels=True)\ntest_dataset= DicomDataset(TRAIN_PATH, Y, transform=transform_test, labels=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:31:30.945458Z","iopub.execute_input":"2024-02-20T10:31:30.945858Z","iopub.status.idle":"2024-02-20T10:31:31.004975Z","shell.execute_reply.started":"2024-02-20T10:31:30.94583Z","shell.execute_reply":"2024-02-20T10:31:31.003947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_loader_train = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=4)\ndata_loader_test = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:36.075291Z","iopub.execute_input":"2024-02-20T10:29:36.075788Z","iopub.status.idle":"2024-02-20T10:29:36.08312Z","shell.execute_reply.started":"2024-02-20T10:29:36.075752Z","shell.execute_reply":"2024-02-20T10:29:36.082086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch = next(iter(data_loader_train))\nfig, axs = plt.subplots(1, 5, figsize=(15,5))\n\nfor i in np.arange(5):\n    axs[i].imshow(np.transpose(batch['image'][i].numpy(), (1,2,0))[:,:,0], cmap=plt.cm.bone)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:36.084376Z","iopub.execute_input":"2024-02-20T10:29:36.084832Z","iopub.status.idle":"2024-02-20T10:29:39.408507Z","shell.execute_reply.started":"2024-02-20T10:29:36.0848Z","shell.execute_reply":"2024-02-20T10:29:39.407236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Break","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:39.410179Z","iopub.execute_input":"2024-02-20T10:29:39.410543Z","iopub.status.idle":"2024-02-20T10:29:39.415309Z","shell.execute_reply.started":"2024-02-20T10:29:39.410499Z","shell.execute_reply":"2024-02-20T10:29:39.414277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def window_image(img, window_center,window_width, intercept, slope, rescale=True):\n\n#     img = (img*slope +intercept)\n#     img_min = window_center - window_width//2\n#     img_max = window_center + window_width//2\n#     img[img<img_min] = img_min\n#     img[img>img_max] = img_max\n    \n#     if rescale:\n#         # Extra rescaling to 0-1, not in the original notebook\n#         img = (img - img_min) / (img_max - img_min)\n    \n#     return img\n\n# window_image(img, 40, 80, intercept, slope) Brain\n# window_image(img, 80, 200, intercept, slope) Subdural\n# window_image(img, 600, 2800, intercept, slope) Bone","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:39.416597Z","iopub.execute_input":"2024-02-20T10:29:39.416969Z","iopub.status.idle":"2024-02-20T10:29:39.44283Z","shell.execute_reply.started":"2024-02-20T10:29:39.416929Z","shell.execute_reply":"2024-02-20T10:29:39.441721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception= inception_v3(weights='DEFAULT')\nprint(inception)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:39.444108Z","iopub.execute_input":"2024-02-20T10:29:39.444438Z","iopub.status.idle":"2024-02-20T10:29:41.232931Z","shell.execute_reply.started":"2024-02-20T10:29:39.444412Z","shell.execute_reply":"2024-02-20T10:29:41.231936Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes= 6\ninception.fc= nn.Linear(in_features=2048, out_features=6, bias=True)\n\nmodel= inception\nmodel.to(device)\n\ncriterion= nn.BCEWithLogitsLoss()\noptimizer= optim.Adam(model.parameters(),lr=2e-5)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:41.234221Z","iopub.execute_input":"2024-02-20T10:29:41.2346Z","iopub.status.idle":"2024-02-20T10:29:41.429797Z","shell.execute_reply.started":"2024-02-20T10:29:41.234568Z","shell.execute_reply":"2024-02-20T10:29:41.428771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = torch.load('/kaggle/input/ffffff1/chkpt_final.tar')\nmodel.load_state_dict(checkpoint['model_state_dict'])\n# optimizer.load_state_dict(checkpoint['optimizer_state_dict'])","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:41.430974Z","iopub.execute_input":"2024-02-20T10:29:41.431264Z","iopub.status.idle":"2024-02-20T10:29:44.525157Z","shell.execute_reply.started":"2024-02-20T10:29:41.431241Z","shell.execute_reply":"2024-02-20T10:29:44.523916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pp","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:44.526509Z","iopub.execute_input":"2024-02-20T10:29:44.526836Z","iopub.status.idle":"2024-02-20T10:29:44.983317Z","shell.execute_reply.started":"2024-02-20T10:29:44.52681Z","shell.execute_reply":"2024-02-20T10:29:44.98191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checkpoinddddt['epoch']","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:44.984174Z","iopub.status.idle":"2024-02-20T10:29:44.984539Z","shell.execute_reply.started":"2024-02-20T10:29:44.98436Z","shell.execute_reply":"2024-02-20T10:29:44.984375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\nn_epochs = 50\nhistory = {'accuracy': [], 'precision': [], 'loss': [], 'f1': []}\n\nfor epoch in range(n_epochs):\n    print('Epoch {}/{}'.format(epoch, n_epochs - 1))\n    print('-' * 10)\n\n    model.train()\n    tr_loss = 0\n    y_true = []\n    y_pred = []\n\n    total_batches = len(data_loader_train)\n    tk0 = tqdm(data_loader_train, total=total_batches, desc=\"Iteration\", position=0, leave=True)\n\n    for step, batch in enumerate(tk0):\n        inputs = batch[\"image\"]\n        labels = batch[\"labels\"]\n\n        inputs = inputs.to(device, dtype=torch.float)\n        labels = labels.to(device, dtype=torch.float)\n\n        outputs = model(inputs).logits\n        \n        loss = criterion(outputs, labels)\n\n        loss.backward()\n\n        tr_loss += loss.item()\n\n        optimizer.step()\n        optimizer.zero_grad()\n\n        # Collect true and predicted labels for metrics calculation\n        y_true.extend(labels.cpu().numpy())\n        y_pred.extend(outputs.cpu().detach().numpy())\n\n        if epoch == 1 and step > 6000:\n            epoch_loss = tr_loss / 6000\n            print('Training Loss: {:.4f}'.format(epoch_loss))\n            break\n        \n    if epoch and (epoch % 10 == 0):\n        torch.save({\n                'epoch': epoch,\n                'model_state_dict': model.state_dict(),\n                'optimizer_state_dict': optimizer.state_dict(),\n            }, f'chkpt_{epoch}.tar')\n    # Calculate training loss for the epoch\n    epoch_loss = tr_loss / len(data_loader_train)\n    print('Training Loss: {:.4f}'.format(epoch_loss))\n    history['loss'].append(epoch_loss)\n\n    # Calculate and append accuracy, precision, and F1 score to history\n#     y_true = np.round(y_true).astype(int)\n    y_true= np.argmax(y_true, axis=1)\n#     y_pred = np.round(y_pred).astype(int)\n    y_pred= np.argmax(y_pred,axis=1)\n\n    accuracy = accuracy_score(y_true, y_pred)\n    precision = precision_score(y_true, y_pred, average='weighted')\n    f1 = f1_score(y_true, y_pred, average='weighted')\n\n    print('Accuracy: {:.4f}'.format(accuracy))\n    print('Precision: {:.4f}'.format(precision))\n    print('F1 Score: {:.4f}'.format(f1))\n\n    history['accuracy'].append(accuracy)\n    history['precision'].append(precision)\n    history['f1'].append(f1)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:44.986451Z","iopub.status.idle":"2024-02-20T10:29:44.986823Z","shell.execute_reply.started":"2024-02-20T10:29:44.986656Z","shell.execute_reply":"2024-02-20T10:29:44.986671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save({\n                'epoch': epoch,\n                'model_state_dict': model.state_dict(),\n                'optimizer_state_dict': optimizer.state_dict(),\n            }, 'chkpt_final.tar')","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:44.988689Z","iopub.status.idle":"2024-02-20T10:29:44.989382Z","shell.execute_reply.started":"2024-02-20T10:29:44.989126Z","shell.execute_reply":"2024-02-20T10:29:44.989147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Accuracy, f1 score,\ndd= pd.DataFrame(history)\ndd.to_csv('stats.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:29:44.990736Z","iopub.status.idle":"2024-02-20T10:29:44.991177Z","shell.execute_reply.started":"2024-02-20T10:29:44.990952Z","shell.execute_reply":"2024-02-20T10:29:44.990971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\ndata_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)\n\nall_y_true= []\nall_y_pred= []\n\nfor batch in data_loader:\n\n    inputs = batch[\"image\"]\n    labels = batch[\"labels\"]\n\n    inputs = inputs.to(device, dtype=torch.float)\n    labels = labels.to(device, dtype=torch.float)\n\n    outputs = model(inputs).logits\n\n    y_true=labels.cpu().numpy()\n    y_pred=outputs.cpu().detach().numpy()\n\n    y_true= np.argmax(y_true, axis=1)\n    y_pred= np.argmax(y_pred,axis=1)\n\n    all_y_true.extend(y_true)\n    all_y_pred.extend(y_pred)\n\nc= classification_report(all_y_pred,all_y_true, output_dict=True)\nprint(c)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:32:55.076009Z","iopub.execute_input":"2024-02-20T10:32:55.076373Z","iopub.status.idle":"2024-02-20T10:33:49.139457Z","shell.execute_reply.started":"2024-02-20T10:32:55.076342Z","shell.execute_reply":"2024-02-20T10:33:49.138314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c= pd.DataFrame(c)\nc.to_csv('inc_report.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:34:00.621331Z","iopub.execute_input":"2024-02-20T10:34:00.622837Z","iopub.status.idle":"2024-02-20T10:34:00.637986Z","shell.execute_reply.started":"2024-02-20T10:34:00.622788Z","shell.execute_reply":"2024-02-20T10:34:00.636315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(all_y_pred,all_y_true))","metadata":{"execution":{"iopub.status.busy":"2024-02-20T10:34:02.140777Z","iopub.execute_input":"2024-02-20T10:34:02.14117Z","iopub.status.idle":"2024-02-20T10:34:02.169435Z","shell.execute_reply.started":"2024-02-20T10:34:02.141137Z","shell.execute_reply":"2024-02-20T10:34:02.168478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndel variables\ngc.collect()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}