{"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":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport pydicom as dicom\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport seaborn as sns\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nimport torchvision.transforms as transforms","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-09T23:03:14.222331Z","iopub.execute_input":"2024-02-09T23:03:14.222993Z","iopub.status.idle":"2024-02-09T23:03:22.462372Z","shell.execute_reply.started":"2024-02-09T23:03:14.222960Z","shell.execute_reply":"2024-02-09T23:03:22.461415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv')\ndetailed_labels = pd.read_csv('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_detailed_class_info.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:03:33.119527Z","iopub.execute_input":"2024-02-09T23:03:33.120718Z","iopub.status.idle":"2024-02-09T23:03:33.232560Z","shell.execute_reply.started":"2024-02-09T23:03:33.120679Z","shell.execute_reply":"2024-02-09T23:03:33.231597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_labels = labels.drop_duplicates(['patientId'])\nunique_detailed_labels = detailed_labels.drop_duplicates(['patientId'])","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:03:33.781389Z","iopub.execute_input":"2024-02-09T23:03:33.781778Z","iopub.status.idle":"2024-02-09T23:03:33.806589Z","shell.execute_reply.started":"2024-02-09T23:03:33.781747Z","shell.execute_reply":"2024-02-09T23:03:33.805673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_labels.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:03:35.596875Z","iopub.execute_input":"2024-02-09T23:03:35.597558Z","iopub.status.idle":"2024-02-09T23:03:35.615072Z","shell.execute_reply.started":"2024-02-09T23:03:35.597525Z","shell.execute_reply":"2024-02-09T23:03:35.614082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom.dcmread('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/0025d2de-bd78-4d36-9f72-e15a5e22ca82.dcm').pixel_array","metadata":{"execution":{"iopub.status.busy":"2024-02-09T11:20:17.792694Z","iopub.execute_input":"2024-02-09T11:20:17.793083Z","iopub.status.idle":"2024-02-09T11:20:17.808411Z","shell.execute_reply.started":"2024-02-09T11:20:17.793054Z","shell.execute_reply":"2024-02-09T11:20:17.807475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# t\n\nclass TrainDatasetClassif(Dataset):\n    def __init__(self, root, unique_labels, transforms=None):\n        self.transforms = transforms\n        self.root = root\n        self.unique_labels = unique_labels\n        \n    def __len__(self):\n        return len(self.unique_labels)\n    \n    def __getitem__(self, idx):\n        image_path = self.root + self.unique_labels.iloc[idx, 0] + '.dcm'\n        image = dicom.dcmread(image_path).pixel_array\n        label = self.unique_labels.loc[idx, 'Target']\n        \n        image = np.expand_dims(image, axis=-1)\n        if self.transforms:\n            image = self.transforms(image)\n        \n        return image, label","metadata":{"execution":{"iopub.status.busy":"2024-02-09T11:20:13.393483Z","iopub.execute_input":"2024-02-09T11:20:13.394311Z","iopub.status.idle":"2024-02-09T11:20:13.403920Z","shell.execute_reply.started":"2024-02-09T11:20:13.394263Z","shell.execute_reply":"2024-02-09T11:20:13.402588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformer = transforms.Compose([\n        transforms.ToTensor(),\n        transforms.ConvertImageDtype(torch.uint8),\n        transforms.Resize((320, 320), antialias=True),\n#         transforms.Lambda(lambda x: transforms.functional.invert(x)),\n#         transforms.RandomAffine(degrees=(-10,10), translate=(0.1, 0.1)),\n#         transforms.RandomPerspective(distortion_scale=0.1, p=0.5),\n        transforms.ConvertImageDtype(torch.float32)\n    ])","metadata":{"execution":{"iopub.status.busy":"2024-02-08T10:09:26.998970Z","iopub.execute_input":"2024-02-08T10:09:26.999433Z","iopub.status.idle":"2024-02-08T10:09:27.006776Z","shell.execute_reply.started":"2024-02-08T10:09:26.999370Z","shell.execute_reply":"2024-02-08T10:09:27.005397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds_classif = TrainDatasetClassif('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/', unique_labels, transformer)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T10:09:30.082002Z","iopub.execute_input":"2024-02-08T10:09:30.082486Z","iopub.status.idle":"2024-02-08T10:09:30.087931Z","shell.execute_reply.started":"2024-02-08T10:09:30.082446Z","shell.execute_reply":"2024-02-08T10:09:30.086740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_ds_classif, batch_size=64, shuffle=True, num_workers=4)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T10:09:42.841000Z","iopub.execute_input":"2024-02-08T10:09:42.841496Z","iopub.status.idle":"2024-02-08T10:09:42.847450Z","shell.execute_reply.started":"2024-02-08T10:09:42.841447Z","shell.execute_reply":"2024-02-08T10:09:42.846240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = unique_detailed_labels\nclass_names = df['class'].value_counts().index\nclass_counts = df['class'].value_counts().values\nbar_colors = ['tab:red', 'tab:blue', 'tab:green']\n\nfig, ax = plt.subplots()\nax.bar(class_names, class_counts, color=bar_colors)\nax.set(ylabel='count', title='Class', ylim=(0, 15000))\nfor p in ax.patches:\n    height = p.get_height()\n    ax.text(p.get_x()+p.get_width()/2.,\n            height + 300,\n            '{:1.2f}%'.format(100*height/len(df)),\n            ha=\"center\") \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:03:42.237648Z","iopub.execute_input":"2024-02-09T23:03:42.238063Z","iopub.status.idle":"2024-02-09T23:03:42.460928Z","shell.execute_reply.started":"2024-02-09T23:03:42.238029Z","shell.execute_reply":"2024-02-09T23:03:42.459901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = unique_labels\nclass_names = ['No Lung Opacity','Lung Opacity']\nclass_counts = df['Target'].value_counts().values\nbar_colors = ['tab:red', 'tab:green']\n\nfig, ax = plt.subplots()\nax.bar(class_names, class_counts, color=bar_colors)\nax.set(ylabel='count', title='Target', ylim=(0, 25000))\nfor p in ax.patches:\n    height = p.get_height()\n    ax.text(p.get_x()+p.get_width()/2.,\n            height + 300,\n            '{:1.2f}%'.format(100*height/len(df)),\n            ha=\"center\") \n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:05:56.631546Z","iopub.execute_input":"2024-02-09T23:05:56.631952Z","iopub.status.idle":"2024-02-09T23:05:56.793172Z","shell.execute_reply.started":"2024-02-09T23:05:56.631923Z","shell.execute_reply":"2024-02-09T23:05:56.792121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_labels = pd.merge(unique_labels, unique_detailed_labels, how='inner')","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:03:51.528119Z","iopub.execute_input":"2024-02-09T23:03:51.528483Z","iopub.status.idle":"2024-02-09T23:03:51.555726Z","shell.execute_reply.started":"2024-02-09T23:03:51.528456Z","shell.execute_reply":"2024-02-09T23:03:51.554783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_labels.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:03:53.371261Z","iopub.execute_input":"2024-02-09T23:03:53.371616Z","iopub.status.idle":"2024-02-09T23:03:53.384988Z","shell.execute_reply.started":"2024-02-09T23:03:53.371591Z","shell.execute_reply":"2024-02-09T23:03:53.383835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10,5))\ntmp = unique_labels.groupby('Target')['class'].value_counts()\ndf = pd.DataFrame(data={'Count': tmp.values}, index=tmp.index).reset_index()\nsns.barplot(ax=ax,x = 'Target', y='Count',hue='class',data=df, palette='Set1')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:10:42.894666Z","iopub.execute_input":"2024-02-09T23:10:42.895063Z","iopub.status.idle":"2024-02-09T23:10:43.117249Z","shell.execute_reply.started":"2024-02-09T23:10:42.895031Z","shell.execute_reply":"2024-02-09T23:10:43.116119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(2, 2, sharey=True, tight_layout=True)\n\nn_bins = 100\n\naxs[0,0].hist(labels.dropna()['x'], bins=n_bins, color = \"lightblue\")\naxs[0,0].set_title('X')\naxs[0,1].hist(labels.dropna()['y'], bins=n_bins, color = \"red\")\naxs[0,1].set_title('Y')\naxs[1,0].hist(labels.dropna()['width'], bins=n_bins, color = \"orange\")\naxs[1,0].set_title('Width')\naxs[1,1].hist(labels.dropna()['height'], bins=n_bins, color = \"green\")\naxs[1,1].set_title('Height')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:09:21.955522Z","iopub.execute_input":"2024-02-09T23:09:21.955942Z","iopub.status.idle":"2024-02-09T23:09:23.444293Z","shell.execute_reply.started":"2024-02-09T23:09:21.955913Z","shell.execute_reply":"2024-02-09T23:09:23.443196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lung_opacity_position = pd.DataFrame()\n\nlung_opacity_position['xc'] = labels['x'] + labels['width'] / 2\nlung_opacity_position['yc'] = labels['y'] + labels['height'] / 2\n\n\nfig, ax = plt.subplots()\nax.set_facecolor('black')\nlung_opacity_position.plot.scatter(x='xc', y='yc', xlim=(0,1024), ylim=(0,1024), \n                           ax=ax, alpha=0.2, marker=\".\", color=\"red\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T23:15:51.454096Z","iopub.execute_input":"2024-02-09T23:15:51.454513Z","iopub.status.idle":"2024-02-09T23:15:51.657575Z","shell.execute_reply.started":"2024-02-09T23:15:51.454483Z","shell.execute_reply":"2024-02-09T23:15:51.656240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}