{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\n\nimport numpy as np\nimport pandas as pd\n\nimport pydicom\nimport cv2\nimport matplotlib.pyplot as plt\n\nfrom keras.models import *\nfrom keras.layers import *\nfrom keras.optimizers import *\nfrom keras.losses import binary_crossentropy\nfrom keras.utils import Sequence\nfrom keras import backend as keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, LearningRateScheduler\n\nfrom glob import glob\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"INPUT_DIR = os.path.join(\"..\", \"input\")\n\nSEGMENTATION_DIR = os.path.join(INPUT_DIR, \"u-net-lung-segmentation-montgomery-shenzhen\")\nSEGMENTATION_MODEL = os.path.join(SEGMENTATION_DIR, \"unet_lung_seg.hdf5\")\nSEGMENTATION_RESULT = \"segmentation\"\nSEGMENTATION_RESULT_TRAIN = os.path.join(SEGMENTATION_RESULT, \"train\")\nSEGMENTATION_RESULT_TEST = os.path.join(SEGMENTATION_RESULT, \"test\")\n\nRSNA_DIR = os.path.join(INPUT_DIR, \"rsna-pneumonia-detection-challenge\")\nRSNA_TRAIN_DIR = os.path.join(RSNA_DIR, \"stage_1_train_images\")\nRSNA_TEST_DIR = os.path.join(RSNA_DIR, \"stage_1_test_images\")\nRSNA_LABELS_FILE = os.path.join(RSNA_DIR, \"stage_1_train_labels.csv\")\nRSNA_CLASS_INFO_FILE = os.path.join(RSNA_DIR, \"stage_1_detailed_class_info.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1ffe790831d41db0c351f71cedd6ec6829d7eb8"},"cell_type":"code","source":"!mkdir segmentation\n!mkdir segmentation/train\n!mkdir segmentation/test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7103dfa1e328dab7f6fc8cbe757649be0b8b2eb1"},"cell_type":"code","source":"def dice_coef(y_true, y_pred):\n    y_true_f = keras.flatten(y_true)\n    y_pred_f = keras.flatten(y_pred)\n    intersection = keras.sum(y_true_f * y_pred_f)\n    return (2. * intersection + 1) / (keras.sum(y_true_f) + keras.sum(y_pred_f) + 1)\n\ndef dice_coef_loss(y_true, y_pred):\n    return -dice_coef(y_true, y_pred)\n\nsegmentation_model = load_model(SEGMENTATION_MODEL, \\\n                                custom_objects={'dice_coef_loss': dice_coef_loss, \\\n                                                'dice_coef': dice_coef})\n\nsegmentation_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9012d4956bc38d4457f6afc30b67e7e9ff5e8d48"},"cell_type":"code","source":"def image_to_train(img):\n    npy = img / 255\n    npy = np.reshape(npy, npy.shape + (1,))\n    npy = np.reshape(npy,(1,) + npy.shape)\n    return npy\n\ndef train_to_image(npy):\n    img = (npy[0,:, :, 0] * 255.).astype(np.uint8)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f529e004c517fe0f27cf5f51132d6954ebe60182"},"cell_type":"code","source":"def segment_image(pid, img, save_to):\n    img = cv2.resize(img, (512, 512))\n    segm_ret = segmentation_model.predict(image_to_train(img), \\\n                                          verbose=0)\n\n    img = cv2.bitwise_and(img, img, mask=train_to_image(segm_ret))\n    \n    cv2.imwrite(os.path.join(save_to, \"%s.png\" % pid), img)\n\nfor filename in tqdm(glob(os.path.join(RSNA_TRAIN_DIR, \"*.dcm\"))):\n    pid, fileext = os.path.splitext(os.path.basename(filename))\n    img = pydicom.dcmread(filename).pixel_array\n    segment_image(pid, img, SEGMENTATION_RESULT_TRAIN)\n\nfor filename in tqdm(glob(os.path.join(RSNA_TEST_DIR, \"*.dcm\"))):\n    pid, fileext = os.path.splitext(os.path.basename(filename))\n    img = pydicom.dcmread(filename).pixel_array\n    segment_image(pid, img, SEGMENTATION_RESULT_TEST)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"538c6c455286ea643c4075b46413ab51e004ca29"},"cell_type":"code","source":"!tar zcf segmentation.tgz --directory=segmentation .\n!rm -rf segmentation","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}