{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Intro & Package Install"},{"metadata":{},"cell_type":"markdown","source":"Following a quick implementation of a Deep learning approach for lung segmentation based on U-net pre-trained Models : \n* **U-net(R231)**\n* **U-net(LTRCLobes)**\n* **U-net(LTRCLobes_R231)**"},{"metadata":{},"cell_type":"markdown","source":"![lung-mask](https://raw.githubusercontent.com/JoHof/lungmask/master/figures/figure.png)","attachments":{}},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport pydicom\n\nfrom skimage.measure import label,regionprops\nfrom skimage.segmentation import clear_border\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"! pip install git+https://github.com/JoHof/lungmask","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **Lung Mask with U-Net**"},{"metadata":{},"cell_type":"markdown","source":"I wrote a function to apply lung mask on `dicom` images. It worked for `OSIC Competition` on [this kernel](https://www.kaggle.com/smerllo/lung-mask-using-u-net-r231) but it returned an error for me. [i-pan](https://www.kaggle.com/vaillant) helped me with a more suitable function as below to apply the mask on each patient"},{"metadata":{"trusted":true},"cell_type":"code","source":"import SimpleITK as sitk\nfrom lungmask import mask as lungmask_mask\n\n# credit to: https://www.kaggle.com/c/rsna-str-pulmonary-embolism-detection/discussion/183161#1012049\ndef get_lung_mask(f):\n    series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(f)\n    sorted_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(f, series_IDs[0])\n    series_reader = sitk.ImageSeriesReader()\n    series_reader.SetFileNames(sorted_file_names)\n    series_reader.MetaDataDictionaryArrayUpdateOn()\n    series_reader.LoadPrivateTagsOn()\n    image = series_reader.Execute()\n    segmentation = (lungmask_mask.apply(image) > 0).astype('uint8')\n    return segmentation, image, sorted_file_names\n\n\nexample = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/b4548bee81e8/ac1aea5d7662/'\nout_put = get_lung_mask(example)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's see the shape of the segmentation array: "},{"metadata":{"trusted":true},"cell_type":"code","source":"out_put[0].shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Mask using U-net(R231) Lung CT-Scan"},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\nplt.imshow(out_put[0][120])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Some of the masks are empty "},{"metadata":{},"cell_type":"markdown","source":"I hope this helps."}],"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":4,"nbformat_minor":4}