{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np \nimport pandas as pd \nimport os \nimport pydicom\nimport math \n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:20:36.672859Z","iopub.execute_input":"2026-08-18T06:20:36.673264Z","iopub.status.idle":"2026-08-18T06:20:37.491597Z","shell.execute_reply.started":"2026-08-18T06:20:36.673229Z","shell.execute_reply":"2026-08-18T06:20:37.490992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data= pd.read_csv(\"/kaggle/input/competitions/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:20:37.493315Z","iopub.execute_input":"2026-08-18T06:20:37.493707Z","iopub.status.idle":"2026-08-18T06:20:37.552062Z","shell.execute_reply.started":"2026-08-18T06:20:37.493671Z","shell.execute_reply":"2026-08-18T06:20:37.551405Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:20:37.553042Z","iopub.execute_input":"2026-08-18T06:20:37.553426Z","iopub.status.idle":"2026-08-18T06:20:37.583033Z","shell.execute_reply.started":"2026-08-18T06:20:37.553331Z","shell.execute_reply":"2026-08-18T06:20:37.582210Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image=\"/kaggle/input/competitions/rsna-pneumonia-detection-challenge/stage_2_train_images\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:20:37.584137Z","iopub.execute_input":"2026-08-18T06:20:37.584771Z","iopub.status.idle":"2026-08-18T06:20:37.588420Z","shell.execute_reply.started":"2026-08-18T06:20:37.584742Z","shell.execute_reply":"2026-08-18T06:20:37.587603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"image_path\"]=data[\"patientId\"].apply(lambda x : f\"{train_image}/{x}.dcm\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:20:37.590819Z","iopub.execute_input":"2026-08-18T06:20:37.591102Z","iopub.status.idle":"2026-08-18T06:20:37.615263Z","shell.execute_reply.started":"2026-08-18T06:20:37.591077Z","shell.execute_reply":"2026-08-18T06:20:37.614657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" data[\"image_exists\"]=data[\"image_path\"].apply(os.path.exists)\ndata[\"image_exists\"].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:20:37.616164Z","iopub.execute_input":"2026-08-18T06:20:37.616482Z","iopub.status.idle":"2026-08-18T06:21:31.673986Z","shell.execute_reply.started":"2026-08-18T06:20:37.616447Z","shell.execute_reply":"2026-08-18T06:21:31.673261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nds=pydicom.dcmread(data.loc[0,\"image_path\"])\nimage=ds.pixel_array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:31.674871Z","iopub.execute_input":"2026-08-18T06:21:31.675156Z","iopub.status.idle":"2026-08-18T06:21:31.742893Z","shell.execute_reply.started":"2026-08-18T06:21:31.675132Z","shell.execute_reply":"2026-08-18T06:21:31.742093Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:31.743875Z","iopub.execute_input":"2026-08-18T06:21:31.744410Z","iopub.status.idle":"2026-08-18T06:21:31.750074Z","shell.execute_reply.started":"2026-08-18T06:21:31.744374Z","shell.execute_reply":"2026-08-18T06:21:31.749152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:31.751079Z","iopub.execute_input":"2026-08-18T06:21:31.751430Z","iopub.status.idle":"2026-08-18T06:21:31.762469Z","shell.execute_reply.started":"2026-08-18T06:21:31.751395Z","shell.execute_reply":"2026-08-18T06:21:31.761707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nplt.figure(figsize=(8,8))\nplt.imshow(image,cmap=\"gray\")\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:31.763380Z","iopub.execute_input":"2026-08-18T06:21:31.763613Z","iopub.status.idle":"2026-08-18T06:21:32.212497Z","shell.execute_reply.started":"2026-08-18T06:21:31.763590Z","shell.execute_reply":"2026-08-18T06:21:32.211603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"row=data[data[\"Target\"]==1].iloc[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:32.213455Z","iopub.execute_input":"2026-08-18T06:21:32.213745Z","iopub.status.idle":"2026-08-18T06:21:32.221289Z","shell.execute_reply.started":"2026-08-18T06:21:32.213720Z","shell.execute_reply":"2026-08-18T06:21:32.220551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"row[\"image_path\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:32.222373Z","iopub.execute_input":"2026-08-18T06:21:32.222670Z","iopub.status.idle":"2026-08-18T06:21:32.237531Z","shell.execute_reply.started":"2026-08-18T06:21:32.222637Z","shell.execute_reply":"2026-08-18T06:21:32.236829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ds=pydicom.dcmread(row[\"image_path\"])\nds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:32.238660Z","iopub.execute_input":"2026-08-18T06:21:32.239083Z","iopub.status.idle":"2026-08-18T06:21:32.257322Z","shell.execute_reply.started":"2026-08-18T06:21:32.239044Z","shell.execute_reply":"2026-08-18T06:21:32.256573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image2=ds.pixel_array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:32.260969Z","iopub.execute_input":"2026-08-18T06:21:32.261452Z","iopub.status.idle":"2026-08-18T06:21:32.271626Z","shell.execute_reply.started":"2026-08-18T06:21:32.261424Z","shell.execute_reply":"2026-08-18T06:21:32.270811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(image2, cmap=\"gray\")\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:32.272517Z","iopub.execute_input":"2026-08-18T06:21:32.272751Z","iopub.status.idle":"2026-08-18T06:21:32.554926Z","shell.execute_reply.started":"2026-08-18T06:21:32.272728Z","shell.execute_reply":"2026-08-18T06:21:32.554086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.patches as patches\nfig,ax=plt.subplots(figsize=(8,8))\nrect=patches.Rectangle((row[\"x\"],row[\"y\"]),row[\"width\"],row[\"height\"])\nax.add_patch(rect)\nax.imshow(image2,cmap=\"gray\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:32.555969Z","iopub.execute_input":"2026-08-18T06:21:32.556257Z","iopub.status.idle":"2026-08-18T06:21:33.168229Z","shell.execute_reply.started":"2026-08-18T06:21:32.556231Z","shell.execute_reply":"2026-08-18T06:21:33.167430Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[data[\"patientId\"]==row[\"patientId\"]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.169190Z","iopub.execute_input":"2026-08-18T06:21:33.169491Z","iopub.status.idle":"2026-08-18T06:21:33.198432Z","shell.execute_reply.started":"2026-08-18T06:21:33.169465Z","shell.execute_reply":"2026-08-18T06:21:33.197743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"patientId\"].nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.199308Z","iopub.execute_input":"2026-08-18T06:21:33.199724Z","iopub.status.idle":"2026-08-18T06:21:33.211666Z","shell.execute_reply.started":"2026-08-18T06:21:33.199682Z","shell.execute_reply":"2026-08-18T06:21:33.210897Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.212529Z","iopub.execute_input":"2026-08-18T06:21:33.212849Z","iopub.status.idle":"2026-08-18T06:21:33.218283Z","shell.execute_reply.started":"2026-08-18T06:21:33.212824Z","shell.execute_reply":"2026-08-18T06:21:33.217614Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.219225Z","iopub.execute_input":"2026-08-18T06:21:33.219516Z","iopub.status.idle":"2026-08-18T06:21:33.233669Z","shell.execute_reply.started":"2026-08-18T06:21:33.219491Z","shell.execute_reply":"2026-08-18T06:21:33.232910Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=data[\"image_path\"]\nY=data[[\"patientId\",\"x\",\"y\",\"width\",\"height\",\"Target\"]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.234694Z","iopub.execute_input":"2026-08-18T06:21:33.235040Z","iopub.status.idle":"2026-08-18T06:21:33.246542Z","shell.execute_reply.started":"2026-08-18T06:21:33.235005Z","shell.execute_reply":"2026-08-18T06:21:33.245775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.247583Z","iopub.execute_input":"2026-08-18T06:21:33.248013Z","iopub.status.idle":"2026-08-18T06:21:33.263212Z","shell.execute_reply.started":"2026-08-18T06:21:33.247977Z","shell.execute_reply":"2026-08-18T06:21:33.262556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grouped=data.groupby(\"patientId\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.264138Z","iopub.execute_input":"2026-08-18T06:21:33.264477Z","iopub.status.idle":"2026-08-18T06:21:33.277681Z","shell.execute_reply.started":"2026-08-18T06:21:33.264443Z","shell.execute_reply":"2026-08-18T06:21:33.276888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grouped.get_group(data[\"patientId\"].iloc[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.278784Z","iopub.execute_input":"2026-08-18T06:21:33.279069Z","iopub.status.idle":"2026-08-18T06:21:33.352909Z","shell.execute_reply.started":"2026-08-18T06:21:33.279038Z","shell.execute_reply":"2026-08-18T06:21:33.352088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_boxes(group):\n    boxes=[]\n    for _, row in group.iterrows():\n        if row[\"Target\"]==1:\n            boxes.append([row[\"x\"],row[\"y\"],row[\"width\"],row[\"height\"]])\n    return boxes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.354014Z","iopub.execute_input":"2026-08-18T06:21:33.354295Z","iopub.status.idle":"2026-08-18T06:21:33.359243Z","shell.execute_reply.started":"2026-08-18T06:21:33.354258Z","shell.execute_reply":"2026-08-18T06:21:33.358540Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"get_boxes(grouped.get_group(data[\"patientId\"].iloc[0]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.360188Z","iopub.execute_input":"2026-08-18T06:21:33.360531Z","iopub.status.idle":"2026-08-18T06:21:33.374816Z","shell.execute_reply.started":"2026-08-18T06:21:33.360492Z","shell.execute_reply":"2026-08-18T06:21:33.374170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"detection_data=grouped.first().reset_index()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.375734Z","iopub.execute_input":"2026-08-18T06:21:33.376033Z","iopub.status.idle":"2026-08-18T06:21:33.399633Z","shell.execute_reply.started":"2026-08-18T06:21:33.375999Z","shell.execute_reply":"2026-08-18T06:21:33.399055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"detection_data[\"boxes\"]=grouped.apply(get_boxes).values\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:33.400445Z","iopub.execute_input":"2026-08-18T06:21:33.400675Z","iopub.status.idle":"2026-08-18T06:21:36.998879Z","shell.execute_reply.started":"2026-08-18T06:21:33.400650Z","shell.execute_reply":"2026-08-18T06:21:36.998004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"detection_data[[\"patientId\",\"boxes\"]].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:36.999882Z","iopub.execute_input":"2026-08-18T06:21:37.000207Z","iopub.status.idle":"2026-08-18T06:21:37.012896Z","shell.execute_reply.started":"2026-08-18T06:21:37.000170Z","shell.execute_reply":"2026-08-18T06:21:37.012061Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_image(path):\n    ds=pydicom.dcmread(path)\n    image=ds.pixel_array\n    return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:37.014113Z","iopub.execute_input":"2026-08-18T06:21:37.014433Z","iopub.status.idle":"2026-08-18T06:21:37.025809Z","shell.execute_reply.started":"2026-08-18T06:21:37.014397Z","shell.execute_reply":"2026-08-18T06:21:37.025070Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path=detection_data[\"image_path\"].iloc[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:37.026794Z","iopub.execute_input":"2026-08-18T06:21:37.027069Z","iopub.status.idle":"2026-08-18T06:21:37.038599Z","shell.execute_reply.started":"2026-08-18T06:21:37.027035Z","shell.execute_reply":"2026-08-18T06:21:37.037936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:37.039579Z","iopub.execute_input":"2026-08-18T06:21:37.039877Z","iopub.status.idle":"2026-08-18T06:21:37.053855Z","shell.execute_reply.started":"2026-08-18T06:21:37.039842Z","shell.execute_reply":"2026-08-18T06:21:37.053060Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image.dtype","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:37.054856Z","iopub.execute_input":"2026-08-18T06:21:37.055240Z","iopub.status.idle":"2026-08-18T06:21:37.067259Z","shell.execute_reply.started":"2026-08-18T06:21:37.055204Z","shell.execute_reply":"2026-08-18T06:21:37.066535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image.min(),image.max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:37.068322Z","iopub.execute_input":"2026-08-18T06:21:37.068958Z","iopub.status.idle":"2026-08-18T06:21:37.079825Z","shell.execute_reply.started":"2026-08-18T06:21:37.068927Z","shell.execute_reply":"2026-08-18T06:21:37.079042Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"row=detection_data.iloc[7]\nimage3=load_image(row[\"image_path\"])\nboxes=row[\"boxes\"]\n\nprint(image3.shape)\nprint(boxes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:24:59.146546Z","iopub.execute_input":"2026-08-18T06:24:59.146888Z","iopub.status.idle":"2026-08-18T06:24:59.168994Z","shell.execute_reply.started":"2026-08-18T06:24:59.146857Z","shell.execute_reply":"2026-08-18T06:24:59.168184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig,ax=plt.subplots()\n\nax.imshow(image3, cmap=\"grey\")\nfor box in boxes:\n    x,y,width,height=box\n    rect=patches.Rectangle((x,y),width,height,fill=False)\n    ax.add_patch(rect)\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:25:01.802435Z","iopub.execute_input":"2026-08-18T06:25:01.802762Z","iopub.status.idle":"2026-08-18T06:25:02.165232Z","shell.execute_reply.started":"2026-08-18T06:25:01.802731Z","shell.execute_reply":"2026-08-18T06:25:02.164427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_data,val_data=train_test_split(detection_data,test_size=0.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:37.443989Z","iopub.execute_input":"2026-08-18T06:21:37.444452Z","iopub.status.idle":"2026-08-18T06:21:38.340694Z","shell.execute_reply.started":"2026-08-18T06:21:37.444424Z","shell.execute_reply":"2026-08-18T06:21:38.339987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_data),len(val_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:38.341827Z","iopub.execute_input":"2026-08-18T06:21:38.342458Z","iopub.status.idle":"2026-08-18T06:21:38.348155Z","shell.execute_reply.started":"2026-08-18T06:21:38.342423Z","shell.execute_reply":"2026-08-18T06:21:38.347490Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[\"boxes\"].apply(len).value_counts().head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:38.349198Z","iopub.execute_input":"2026-08-18T06:21:38.350056Z","iopub.status.idle":"2026-08-18T06:21:38.374269Z","shell.execute_reply.started":"2026-08-18T06:21:38.350025Z","shell.execute_reply":"2026-08-18T06:21:38.373591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_data[\"boxes\"].apply(len).value_counts().head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:38.375191Z","iopub.execute_input":"2026-08-18T06:21:38.375611Z","iopub.status.idle":"2026-08-18T06:21:38.390541Z","shell.execute_reply.started":"2026-08-18T06:21:38.375585Z","shell.execute_reply":"2026-08-18T06:21:38.389792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:38.395823Z","iopub.execute_input":"2026-08-18T06:21:38.396099Z","iopub.status.idle":"2026-08-18T06:21:41.509878Z","shell.execute_reply.started":"2026-08-18T06:21:38.396074Z","shell.execute_reply":"2026-08-18T06:21:41.509146Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-18T06:21:41.510901Z","iopub.execute_input":"2026-08-18T06:21:41.511612Z","iopub.status.idle":"2026-08-18T06:21:41.517090Z","shell.execute_reply.started":"2026-08-18T06:21:41.511578Z","shell.execute_reply":"2026-08-18T06:21:41.516113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}