{"cells":[{"metadata":{},"cell_type":"markdown","source":"I made this kernel for generating masks using deep learning. I was inspired by the discussion in this thread: https://www.kaggle.com/c/osic-pulmonary-fibrosis-progression/discussion/166123\n\nUsing a pre-trained model, we can get a lung mask pretty easily. Let's check this out: ","execution_count":null},{"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/SoufianeDataFan/lungmask","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### **Original Lung CT-Scan**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"d = pydicom.dcmread('../input/osic-pulmonary-fibrosis-progression/train/ID00007637202177411956430/19.dcm')\nimg = d.pixel_array\nfig = plt.figure(figsize=(12, 12))\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### **Mask using U-net(R231) Lung CT-Scan**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from lungmask import mask\nimport SimpleITK as sitk\n\ndef get_mask(filename, plot_mask=False, return_val=False): \n    # Let's an example of a CT scan\n    input_image = sitk.ReadImage(filename)\n    mask_out = mask.apply(input_image)[0]  #default model is U-net(R231)\n    if plot_mask: \n        fig = plt.figure(figsize=(12, 12))\n        plt.imshow(mask_out)\n    if return_val:\n        return mask_out","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_mask = get_mask('../input/osic-pulmonary-fibrosis-progression/train/ID00007637202177411956430/19.dcm',\n                    plot_mask=True,\n                    return_val=True)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As mentioned in the discussion forum, lung mask and segmentation seems like the first step. You can use a morphological approach or deep learning to generate ct-scans masks. \n\nIf you want to use a morphological approach, please refer to [this kernel ](https://www.kaggle.com/miklgr500/unsupervise-lung-detection) by [@miklgr500](https://www.kaggle.com/miklgr500)\n\nThis DL-based approach uses PyTorch and GPU for faster computation. Otherwise, it will force it on the CPU. ","execution_count":null}],"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}