{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Install packages","metadata":{}},{"cell_type":"markdown","source":"https://www.kaggle.com/code/derekxue/walkthroughchestx-raymodelfastai/edit/run/137052667","metadata":{}},{"cell_type":"code","source":"!pip install pydicom kornia opencv-python scikit-image pyarrow > /dev/null","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:23:01.410588Z","iopub.execute_input":"2023-07-19T06:23:01.411019Z","iopub.status.idle":"2023-07-19T06:23:18.603113Z","shell.execute_reply.started":"2023-07-19T06:23:01.410974Z","shell.execute_reply":"2023-07-19T06:23:18.601717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.basics import *\nfrom fastai.callback.all import *\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\n\nimport pydicom\n\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:25:13.185067Z","iopub.execute_input":"2023-07-19T06:25:13.185574Z","iopub.status.idle":"2023-07-19T06:25:30.83581Z","shell.execute_reply.started":"2023-07-19T06:25:13.185536Z","shell.execute_reply":"2023-07-19T06:25:30.834775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xray_source = Path('../input/vinbigdata-chest-xray-abnormalities-detection')\ndisplay(xray_source.ls())\ntrain_imgs = xray_source/'train'\ntest_imgs = xray_source/'test'","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:30:02.995846Z","iopub.execute_input":"2023-07-19T06:30:02.997178Z","iopub.status.idle":"2023-07-19T06:30:03.011531Z","shell.execute_reply.started":"2023-07-19T06:30:02.997132Z","shell.execute_reply":"2023-07-19T06:30:03.010335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check on a image file","metadata":{}},{"cell_type":"code","source":"fname = train_imgs.ls()[3]\ndcm = fname.dcmread()\ndcm.show(scale=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:30:09.585017Z","iopub.execute_input":"2023-07-19T06:30:09.585533Z","iopub.status.idle":"2023-07-19T06:30:14.680584Z","shell.execute_reply.started":"2023-07-19T06:30:09.585496Z","shell.execute_reply":"2023-07-19T06:30:14.67719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(fname)","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:30:21.797296Z","iopub.execute_input":"2023-07-19T06:30:21.797745Z","iopub.status.idle":"2023-07-19T06:30:21.811779Z","shell.execute_reply.started":"2023-07-19T06:30:21.797707Z","shell.execute_reply":"2023-07-19T06:30:21.810554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items = get_dicom_files(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:31:42.432984Z","iopub.execute_input":"2023-07-19T06:31:42.433496Z","iopub.status.idle":"2023-07-19T06:32:09.047538Z","shell.execute_reply.started":"2023-07-19T06:31:42.433459Z","shell.execute_reply":"2023-07-19T06:32:09.046504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(items)","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:32:17.208778Z","iopub.execute_input":"2023-07-19T06:32:17.209264Z","iopub.status.idle":"2023-07-19T06:32:17.216833Z","shell.execute_reply.started":"2023-07-19T06:32:17.209224Z","shell.execute_reply":"2023-07-19T06:32:17.215718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### info in train.csv NOT extract yet by following the random seed selection","metadata":{}},{"cell_type":"code","source":"%time\ndicom_dataframe = pd.DataFrame.from_dicoms(items[:5])\ndicom_dataframe[:5]","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:32:44.583452Z","iopub.execute_input":"2023-07-19T06:32:44.583936Z","iopub.status.idle":"2023-07-19T06:32:51.122615Z","shell.execute_reply.started":"2023-07-19T06:32:44.5839Z","shell.execute_reply":"2023-07-19T06:32:51.12158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Find out info in train.csv","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(xray_source/f\"train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:35:08.335385Z","iopub.execute_input":"2023-07-19T06:35:08.335888Z","iopub.status.idle":"2023-07-19T06:35:08.556215Z","shell.execute_reply.started":"2023-07-19T06:35:08.335852Z","shell.execute_reply":"2023-07-19T06:35:08.55507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Find out image size distribution","metadata":{}},{"cell_type":"code","source":"# img_name = train_imgs/f\"{df.image_id[0]}.dicom\"\n# dcm = img_name.dcmread()\n# dcm.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:38:59.997137Z","iopub.execute_input":"2023-07-19T06:38:59.997638Z","iopub.status.idle":"2023-07-19T06:39:01.204308Z","shell.execute_reply.started":"2023-07-19T06:38:59.9976Z","shell.execute_reply":"2023-07-19T06:39:01.201884Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df['size'] = train_imgs/f\"{df.image_id}.dicom\".dcmread().shape\n# df.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-19T06:59:16.323178Z","iopub.execute_input":"2023-07-19T06:59:16.323755Z","iopub.status.idle":"2023-07-19T06:59:16.40368Z","shell.execute_reply.started":"2023-07-19T06:59:16.323712Z","shell.execute_reply":"2023-07-19T06:59:16.401343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prefix = '../input/vinbigdata-chest-xray-abnormalities-detection/train/'\n\ndf['size'] = prefix + df.image_id + \".dicom\"\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-19T07:16:28.938335Z","iopub.execute_input":"2023-07-19T07:16:28.938781Z","iopub.status.idle":"2023-07-19T07:16:29.003491Z","shell.execute_reply.started":"2023-07-19T07:16:28.938747Z","shell.execute_reply":"2023-07-19T07:16:29.002437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.class_name.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-07-19T02:56:54.415639Z","iopub.execute_input":"2023-07-19T02:56:54.416152Z","iopub.status.idle":"2023-07-19T02:56:54.449624Z","shell.execute_reply.started":"2023-07-19T02:56:54.416112Z","shell.execute_reply":"2023-07-19T02:56:54.448606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn,val = RandomSplitter()(items)","metadata":{"execution":{"iopub.status.busy":"2023-07-19T02:56:59.055495Z","iopub.execute_input":"2023-07-19T02:56:59.056048Z","iopub.status.idle":"2023-07-19T02:56:59.074518Z","shell.execute_reply.started":"2023-07-19T02:56:59.056003Z","shell.execute_reply":"2023-07-19T02:56:59.073475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn[:5]","metadata":{"execution":{"iopub.status.busy":"2023-07-19T02:57:02.675415Z","iopub.execute_input":"2023-07-19T02:57:02.675852Z","iopub.status.idle":"2023-07-19T02:57:02.691308Z","shell.execute_reply.started":"2023-07-19T02:57:02.675817Z","shell.execute_reply":"2023-07-19T02:57:02.689969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_tfms = [Resize(360)], bs=32","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xray_block = DataBlock(\n    blocks = (ImageBlock(cls=PILDicom), CategoryBlock),\n    get_x = lambda x:train_imgs/f\"{x[0]}.dicom\",\n    get_y = lambda x:x[1], \n    bs = bs\n    item_tfms = item_tfms,\n    batch_tfms = [*aug_transforms(size=224),\n                  Normalize.from_stats(*imagenet_stats)]\n)\n\ndls = xray_block.dataloaders(df.values)","metadata":{"execution":{"iopub.status.busy":"2023-07-19T02:57:17.46181Z","iopub.execute_input":"2023-07-19T02:57:17.46237Z","iopub.status.idle":"2023-07-19T02:57:37.424628Z","shell.execute_reply.started":"2023-07-19T02:57:17.462327Z","shell.execute_reply":"2023-07-19T02:57:37.422966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Try to find the dicom file shape, resize dicom file","metadata":{}},{"cell_type":"code","source":"dls.show_batch(max_n=6)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}