{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":31089,"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},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom PIL import Image\nimport os \nfrom torch.utils.data import Dataset\nimport pandas as pd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T14:52:23.374401Z","iopub.execute_input":"2025-07-19T14:52:23.374791Z","iopub.status.idle":"2025-07-19T14:52:23.966681Z","shell.execute_reply.started":"2025-07-19T14:52:23.374764Z","shell.execute_reply":"2025-07-19T14:52:23.965837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class RNADataset(Dataset):\n    def __init__(self, imagesdirectory, labelsdirectory=None, transforms=None):\n        self.imagesdirectory = imagesdirectory\n        self.transforms = transforms\n        \n        if labelsdirectory is not None:\n            self.df = pd.read_csv(labelsdirectory)\n            self.image_ids = self.df[\"patientId\"].unique()\n        else:\n            self.df = None\n            self.image_ids = [os.path.splitext(f)[0] for f in os.listdir(imagesdirectory) if f.endswith(\".png\")]\n\n    def __len__(self):\n        return len(self.image_ids)\n\n    def __getitem__(self, idx):\n        image_id = self.image_ids[idx]\n        imagepath = os.path.join(self.imagesdirectory, image_id + \".png\")\n        image = Image.open(imagepath).convert(\"RGB\")\n\n        if self.df is not None:\n            records = self.df[self.df[\"patientId\"] == image_id]\n            boxes = records[[\"x\", \"y\", \"width\", \"height\"]].values\n            boxes = torch.as_tensor(boxes, dtype=torch.float32)\n            labels = torch.ones((boxes.shape[0],), dtype=torch.int64)  # Pneumonia = 1\n\n            target = {\n                \"boxes\": boxes,\n                \"labels\": labels,\n                \"image_id\": torch.tensor([idx])\n            }\n        else:\n            # Test verisi için sadece image döndür (etiket yok)\n            target = {}\n\n        if self.transforms:\n            image = self.transforms(image)\n\n        return image, target\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T14:54:13.7491Z","iopub.execute_input":"2025-07-19T14:54:13.749949Z","iopub.status.idle":"2025-07-19T14:54:13.758233Z","shell.execute_reply.started":"2025-07-19T14:54:13.749919Z","shell.execute_reply":"2025-07-19T14:54:13.757309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torchvision\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom torch.utils.data import DataLoader","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T14:54:13.987175Z","iopub.execute_input":"2025-07-19T14:54:13.987619Z","iopub.status.idle":"2025-07-19T14:54:13.992744Z","shell.execute_reply.started":"2025-07-19T14:54:13.98759Z","shell.execute_reply":"2025-07-19T14:54:13.991527Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xtrain_dataset=RNADataset(\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images\",\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\",transforms=None)\nxtest_dataset=RNADataset(\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_test_images\",transforms=None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T14:54:14.226578Z","iopub.execute_input":"2025-07-19T14:54:14.227359Z","iopub.status.idle":"2025-07-19T14:54:14.587195Z","shell.execute_reply.started":"2025-07-19T14:54:14.227255Z","shell.execute_reply":"2025-07-19T14:54:14.586237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train=DataLoader(xtrain_dataset,shuffle=True,batch_size=2,collate_fn=lambda x: tuple(zip(*x)))\nx_test=DataLoader(xtest_dataset,shuffle=False,batch_size=2,collate_fn=lambda x: tuple(zip(*x)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T14:57:37.380223Z","iopub.execute_input":"2025-07-19T14:57:37.380626Z","iopub.status.idle":"2025-07-19T14:57:37.385841Z","shell.execute_reply.started":"2025-07-19T14:57:37.380601Z","shell.execute_reply":"2025-07-19T14:57:37.385009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model=torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T14:58:39.27591Z","iopub.execute_input":"2025-07-19T14:58:39.276215Z","iopub.status.idle":"2025-07-19T14:58:41.404387Z","shell.execute_reply.started":"2025-07-19T14:58:39.276195Z","shell.execute_reply":"2025-07-19T14:58:41.403589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_classes=2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T14:58:51.166875Z","iopub.execute_input":"2025-07-19T14:58:51.167196Z","iopub.status.idle":"2025-07-19T14:58:51.172184Z","shell.execute_reply.started":"2025-07-19T14:58:51.167173Z","shell.execute_reply":"2025-07-19T14:58:51.171054Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\nmodel.to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T15:09:44.624875Z","iopub.execute_input":"2025-07-19T15:09:44.625169Z","iopub.status.idle":"2025-07-19T15:09:44.642021Z","shell.execute_reply.started":"2025-07-19T15:09:44.625149Z","shell.execute_reply":"2025-07-19T15:09:44.640822Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"in_features=model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor=FastRCNNPredictor(in_features,num_classes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T15:09:35.427845Z","iopub.execute_input":"2025-07-19T15:09:35.429002Z","iopub.status.idle":"2025-07-19T15:09:35.434593Z","shell.execute_reply.started":"2025-07-19T15:09:35.428966Z","shell.execute_reply":"2025-07-19T15:09:35.433796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer=torch.optim.SGD(model.parameters(),lr=0.005)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T15:11:45.361668Z","iopub.execute_input":"2025-07-19T15:11:45.362024Z","iopub.status.idle":"2025-07-19T15:11:45.368005Z","shell.execute_reply.started":"2025-07-19T15:11:45.362003Z","shell.execute_reply":"2025-07-19T15:11:45.367013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.train()\nfor epoch in range(5):\n    ","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}