{"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":[{"sourceType":"competition","sourceId":13451,"datasetId":654585,"databundleVersionId":1188070}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","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":{}},{"cell_type":"code","source":"import os\nimport torch\npath = '/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection'\ntrain_path =f\"{path}/stage_2_train\"\ntest_path = f\"{path}/stage_2_test\"\ntrain_csv_path = f\"{path}/stage_2_train.csv\"\nsample_submission_csv_path = f\"{path}/stage_2_sample_submission.csv\"\nprint(os.listdir(path))\nimport pandas as pd\ntrain_dir = os.listdir(train_path)\ntest_dir= os.listdir(test_path) \nlabel_array = ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'any']\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nlen(train_dir), len(test_dir), train_dir[3454].split(\".\")[0],test_dir[1], device , 752803/5\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:05:48.468255Z","iopub.execute_input":"2024-03-10T09:05:48.469411Z","iopub.status.idle":"2024-03-10T09:05:49.6236Z","shell.execute_reply.started":"2024-03-10T09:05:48.469375Z","shell.execute_reply":"2024-03-10T09:05:49.622529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport pydicom\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nimport numpy as np\nfrom tqdm.auto import tqdm\n\nsample =train_dir[0]\n\nimage_path = os.path.join(train_path, sample)\n\ndicom_data = pydicom.dcmread(image_path)\nprint\n # Extract the pixel array from the DICOM data\nimage = dicom_data.pixel_array\nprint(image.shape)\n\n\n# Display the image using matplotlib\nplt.imshow(image, cmap=plt.cm.gray)\nplt.axis('off')  # Turn off axis\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:05:49.626094Z","iopub.execute_input":"2024-03-10T09:05:49.626933Z","iopub.status.idle":"2024-03-10T09:05:49.751586Z","shell.execute_reply.started":"2024-03-10T09:05:49.626895Z","shell.execute_reply":"2024-03-10T09:05:49.750502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv_data = pd.read_csv(train_csv_path)\nsubmission_csv_data = pd.read_csv(sample_submission_csv_path)\nlen(submission_csv_data), len(train_csv_data)\nsubmission_csv_data","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:05:49.75356Z","iopub.execute_input":"2024-03-10T09:05:49.754433Z","iopub.status.idle":"2024-03-10T09:05:53.945219Z","shell.execute_reply.started":"2024-03-10T09:05:49.754393Z","shell.execute_reply":"2024-03-10T09:05:53.944224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef label_tensor(dicomFileId, dataframe):\n    single_image_label = dataframe[dataframe['ID'].str.startswith(dicomFileId)]\n    label_dict= {\"index\" : [], \"value\" : []}\n    labels = []\n    for index, row in single_image_label.iterrows():\n        curLable = row[\"ID\"].split(\"_\")[2]\n        label_dict['index'].append(curLable)\n        label_dict['value'].append(row['Label'])\n#     arrange values of dict comaparing index of dict with label_array index \n    \n    for label in label_array:\n        index = label_dict['index'].index(label)\n        labels.append(label_dict[\"value\"][index]*1)\n    return torch.tensor(labels, dtype=torch.float32)\n        \n        \n\n    \n    \n# single_image_label = train_csv_data[train_csv_data['ID'].str.startswith('ID_27a354d42')]\n\nlabel_tensor('ID_5274707bb', submission_csv_data) ## Test data exapple\n# label_tensor('ID_ffffb670a', train_csv_data) ## Train data exaple\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:05:53.946663Z","iopub.execute_input":"2024-03-10T09:05:53.94707Z","iopub.status.idle":"2024-03-10T09:05:54.182517Z","shell.execute_reply.started":"2024-03-10T09:05:53.947034Z","shell.execute_reply":"2024-03-10T09:05:54.181675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.transform import resize\nclass CustomDataset(Dataset):\n    def __init__(self, dataframe, image_folder_path, label_array=label_array):\n        #pass the pandas data frame isteslf\n        self.dataframe = dataframe\n        # set folder to this value         \n        self.image_folder = os.listdir(image_folder_path)\n        self.image_folder_path = image_folder_path\n        self.label_array = label_array\n\n    def __len__(self):\n        # length folder image length of data as there two sparetd folder for traing and testing data \n        return len(self.image_folder)\n\n    def __getitem__(self, idx):\n        sample =self.image_folder [idx]\n        image_path = os.path.join(self.image_folder_path, sample)\n        dicom_data = pydicom.dcmread(image_path)\n        pixel_array = resize(dicom_data.pixel_array.astype(np.float32), (64, 64), anti_aliasing=True)\n        pixel_array = np.array(pixel_array)\n        image = torch.tensor(pixel_array).unsqueeze(0)\n        label_start = sample.split(\".\")[0]\n        label = label_tensor(label_start, self.dataframe)\n        return image, label\n        \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:05:54.184644Z","iopub.execute_input":"2024-03-10T09:05:54.184954Z","iopub.status.idle":"2024-03-10T09:05:54.193005Z","shell.execute_reply.started":"2024-03-10T09:05:54.184921Z","shell.execute_reply":"2024-03-10T09:05:54.192097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset= CustomDataset(dataframe =train_csv_data , image_folder_path=train_path, label_array=label_array)\nimage, label= train_dataset.__getitem__(1)\nimage.unsqueeze(dim=1).dtype\nimage.shape, label\nlen(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:05:54.194345Z","iopub.execute_input":"2024-03-10T09:05:54.195237Z","iopub.status.idle":"2024-03-10T09:05:56.75246Z","shell.execute_reply.started":"2024-03-10T09:05:54.195194Z","shell.execute_reply":"2024-03-10T09:05:56.751584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\n\ntransform = transforms.Compose([\n    transforms.Resize((64, 64)),  # Resize images to 64x64\n    transforms.ToTensor()          # Convert images to PyTorch tensors\n])\n\n# Dataloader\nBATCH_SIZE=32\ntrain_dataset.tranform= transform\n\n\n\ntrain_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=0 )\nlen(train_dataloader)","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:05:56.753788Z","iopub.execute_input":"2024-03-10T09:05:56.754477Z","iopub.status.idle":"2024-03-10T09:05:56.764127Z","shell.execute_reply.started":"2024-03-10T09:05:56.754448Z","shell.execute_reply":"2024-03-10T09:05:56.762636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.models as models\n\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\nclass CustomResNet18(nn.Module):\n    def __init__(self, num_classes):\n        super(CustomResNet18, self).__init__()\n        self.resnet = models.resnet18(pretrained=False)\n        # Replace the first convolutional layer to accept 1 channel instead of 3\n        self.resnet.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)\n        # Replace the last fully connected layer\n        self.resnet.fc = nn.Linear(self.resnet.fc.in_features, num_classes)\n\n    def forward(self, x):\n        return self.resnet(x)\n\ncurImage = image.unsqueeze(dim=0)\nprint(curImage.dtype)\nmodel_0 = CustomResNet18(6).to(device)\n# output = model_0(curImage)\n# output.shape\n# print(device)","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:05:56.76529Z","iopub.execute_input":"2024-03-10T09:05:56.765626Z","iopub.status.idle":"2024-03-10T09:05:57.009209Z","shell.execute_reply.started":"2024-03-10T09:05:56.765599Z","shell.execute_reply":"2024-03-10T09:05:57.008163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = {\"loss\" : []}\nepochs=1\n\nloss_fn = torch.nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adam(model_0.parameters(), lr=0.001)\n\n\n           \nfor epoch in range(epochs):\n  model_0.train()\n  loss = 0\n  for i, (image, label) in enumerate(train_dataloader):\n    try:\n     image, label = image.to(device), label.to(device)\n     output = model_0(image)\n     curloss = loss_fn(output, label)\n     loss += curloss.item()\n     optimizer.zero_grad()\n     curloss.backward()\n     optimizer.step()\n     if(i % 50 == 0):\n      torch.save(model_0.state_dict(), \"aster_ich_resenet_18_full.pth\")\n      print(f\"Saved {i} batch\")\n    except Exception as e:\n     print(f\"An error occurred: {e}\")\n     continue\n  loss = loss/len(train_dataloader)\n  results[\"loss\"].append(loss)\n  print(f\"Loss {loss}\")\n    \n\n\n\n\n\nresults\n    \n \n    \n    \n    \n    \n","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:10:33.059053Z","iopub.execute_input":"2024-03-10T09:10:33.059473Z","iopub.status.idle":"2024-03-10T09:10:53.162659Z","shell.execute_reply.started":"2024-03-10T09:10:33.059445Z","shell.execute_reply":"2024-03-10T09:10:53.161876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntorch.save(model_0.state_dict(), \"aster_ich_resenet_18_full.pth\")","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:06:16.910445Z","iopub.status.idle":"2024-03-10T09:06:16.910806Z","shell.execute_reply.started":"2024-03-10T09:06:16.910637Z","shell.execute_reply":"2024-03-10T09:06:16.910652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.listdir(\"/kaggle/working\"))","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:13:28.34688Z","iopub.execute_input":"2024-03-10T09:13:28.347658Z","iopub.status.idle":"2024-03-10T09:13:28.354437Z","shell.execute_reply.started":"2024-03-10T09:13:28.347625Z","shell.execute_reply":"2024-03-10T09:13:28.353301Z"},"trusted":true},"execution_count":null,"outputs":[]}]}