{"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":"!mkdir ../input_c\n!mkdir ../input_c/segmentation\n!mkdir ../input_c/segmentation/test\n!mkdir ../input_c/segmentation/train\n!mkdir ../input_c/segmentation/train/augmentation\n!mkdir ../input_c/segmentation/train/image\n!mkdir ../input_c/segmentation/train/mask\n!mkdir ../input_c/segmentation/train/dilate","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:08:25.888651Z","iopub.execute_input":"2021-07-13T06:08:25.889099Z","iopub.status.idle":"2021-07-13T06:08:31.744958Z","shell.execute_reply.started":"2021-07-13T06:08:25.889061Z","shell.execute_reply":"2021-07-13T06:08:31.74403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input_c/segmentation","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:08:31.746542Z","iopub.execute_input":"2021-07-13T06:08:31.746899Z","iopub.status.idle":"2021-07-13T06:08:32.475716Z","shell.execute_reply.started":"2021-07-13T06:08:31.746847Z","shell.execute_reply":"2021-07-13T06:08:32.47476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom skimage.color import rgb2gray\nfrom tensorflow.keras import Input\nfrom tensorflow.keras.models import Model, load_model, save_model\nfrom tensorflow.keras.layers import Input, Activation, BatchNormalization, Dropout, Lambda, Conv2D, Conv2DTranspose, MaxPooling2D, concatenate, add\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom glob import glob\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:08:32.477549Z","iopub.execute_input":"2021-07-13T06:08:32.477894Z","iopub.status.idle":"2021-07-13T06:08:32.485922Z","shell.execute_reply.started":"2021-07-13T06:08:32.477846Z","shell.execute_reply":"2021-07-13T06:08:32.484373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_C_DIR = os.path.join(\"..\", \"input_c\")\nINPUT_DIR = os.path.join(\"..\", \"input\")\n\nSEGMENTATION_DIR = os.path.join(INPUT_C_DIR, \"segmentation\")\nSEGMENTATION_TEST_DIR = os.path.join(SEGMENTATION_DIR, \"test\")\nSEGMENTATION_TRAIN_DIR = os.path.join(SEGMENTATION_DIR, \"train\")\nSEGMENTATION_AUG_DIR = os.path.join(SEGMENTATION_TRAIN_DIR, \"augmentation\")\nSEGMENTATION_IMAGE_DIR = os.path.join(SEGMENTATION_TRAIN_DIR, \"image\")\nSEGMENTATION_MASK_DIR = os.path.join(SEGMENTATION_TRAIN_DIR, \"mask\")\nSEGMENTATION_DILATE_DIR = os.path.join(SEGMENTATION_TRAIN_DIR, \"dilate\")\nSEGMENTATION_SOURCE_DIR = os.path.join(INPUT_DIR, \\\n                                       \"pulmonary-chest-xray-abnormalities\")\n\nSHENZHEN_TRAIN_DIR = os.path.join(SEGMENTATION_SOURCE_DIR, \"ChinaSet_AllFiles\", \\\n                                  \"ChinaSet_AllFiles\")\nSHENZHEN_IMAGE_DIR = os.path.join(SHENZHEN_TRAIN_DIR, \"CXR_png\")\nSHENZHEN_MASK_DIR = os.path.join(INPUT_DIR, \"shcxr-lung-mask\", \"mask\", \"mask\")\n\nMONTGOMERY_TRAIN_DIR = os.path.join(SEGMENTATION_SOURCE_DIR, \\\n                                    \"Montgomery\", \"MontgomerySet\")\nMONTGOMERY_IMAGE_DIR = os.path.join(MONTGOMERY_TRAIN_DIR, \"CXR_png\")\nMONTGOMERY_LEFT_MASK_DIR = os.path.join(MONTGOMERY_TRAIN_DIR, \\\n                                        \"ManualMask\", \"leftMask\")\nMONTGOMERY_RIGHT_MASK_DIR = os.path.join(MONTGOMERY_TRAIN_DIR, \\\n                                         \"ManualMask\", \"rightMask\")\n\nDILATE_KERNEL = np.ones((15, 15), np.uint8)\n\nBATCH_SIZE=2\n\n#Prod\nEPOCHS=35\n\n#Desv\n#EPOCHS=16","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:08:32.488009Z","iopub.execute_input":"2021-07-13T06:08:32.488568Z","iopub.status.idle":"2021-07-13T06:08:32.504481Z","shell.execute_reply.started":"2021-07-13T06:08:32.48852Z","shell.execute_reply":"2021-07-13T06:08:32.503533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(MONTGOMERY_LEFT_MASK_DIR)\n!ls MONTGOMERY_LEFT_MASK_DIR","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:08:32.505983Z","iopub.execute_input":"2021-07-13T06:08:32.506753Z","iopub.status.idle":"2021-07-13T06:08:33.257265Z","shell.execute_reply.started":"2021-07-13T06:08:32.506704Z","shell.execute_reply":"2021-07-13T06:08:33.256019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"montgomery_left_mask_dir = glob(os.path.join(MONTGOMERY_LEFT_MASK_DIR, '*.png'))\n#montgomery_test = montgomery_left_mask_dir[0:50]\nmontgomery_train= montgomery_left_mask_dir[:]\n\nfor left_image_file in tqdm(montgomery_left_mask_dir):\n    base_file = os.path.basename(left_image_file)\n    image_file = os.path.join(MONTGOMERY_IMAGE_DIR, base_file)\n    right_image_file = os.path.join(MONTGOMERY_RIGHT_MASK_DIR, base_file)\n\n    image = cv2.imread(image_file)\n    left_mask = cv2.imread(left_image_file, cv2.IMREAD_GRAYSCALE)\n    right_mask = cv2.imread(right_image_file, cv2.IMREAD_GRAYSCALE)\n    \n    image = cv2.resize(image, (512, 512))\n    left_mask = cv2.resize(left_mask, (512, 512))\n    right_mask = cv2.resize(right_mask, (512, 512))\n    \n    mask = np.maximum(left_mask, right_mask)\n    mask_dilate = cv2.dilate(mask, DILATE_KERNEL, iterations=1)\n    \n    if (left_image_file in montgomery_train):\n        cv2.imwrite(os.path.join(SEGMENTATION_IMAGE_DIR, base_file), \\\n                    image)\n        cv2.imwrite(os.path.join(SEGMENTATION_MASK_DIR, base_file), \\\n                    mask)\n        cv2.imwrite(os.path.join(SEGMENTATION_DILATE_DIR, base_file), \\\n                    mask_dilate)","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:08:33.258911Z","iopub.execute_input":"2021-07-13T06:08:33.259224Z","iopub.status.idle":"2021-07-13T06:09:30.670508Z","shell.execute_reply.started":"2021-07-13T06:08:33.25919Z","shell.execute_reply":"2021-07-13T06:09:30.669636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_colored_dilate(image, mask_image, dilate_image):\n    mask_image_gray = cv2.cvtColor(mask_image, cv2.COLOR_BGR2GRAY)\n    dilate_image_gray = cv2.cvtColor(dilate_image, cv2.COLOR_BGR2GRAY)\n    \n    mask = cv2.bitwise_and(mask_image, mask_image, mask=mask_image_gray)\n    dilate = cv2.bitwise_and(dilate_image, dilate_image, mask=dilate_image_gray)\n    \n    mask_coord = np.where(mask!=[0,0,0])\n    dilate_coord = np.where(dilate!=[0,0,0])\n\n    mask[mask_coord[0],mask_coord[1],:]=[255,0,0]\n    dilate[dilate_coord[0],dilate_coord[1],:] = [0,0,255]\n\n    ret = cv2.addWeighted(image, 0.7, dilate, 0.3, 0)\n    ret = cv2.addWeighted(ret, 0.7, mask, 0.3, 0)\n\n    return ret\n\ndef add_colored_mask(image, mask_image):\n    mask_image_gray = cv2.cvtColor(mask_image, cv2.COLOR_BGR2GRAY)\n                                        \n    mask = cv2.bitwise_and(mask_image, mask_image, mask=mask_image_gray)\n    \n    mask_coord = np.where(mask!=[0,0,0])\n\n    mask[mask_coord[0],mask_coord[1],:]=[255,0,0]\n\n    ret = cv2.addWeighted(image, 0.7, mask, 0.3, 0)\n\n    return ret\n\ndef diff_mask(ref_image, mask_image):\n    mask_image_gray = cv2.cvtColor(mask_image, cv2.COLOR_BGR2GRAY)\n    \n    mask = cv2.bitwise_and(mask_image, mask_image, mask=mask_image_gray)\n    \n    mask_coord = np.where(mask!=[0,0,0])\n\n    mask[mask_coord[0],mask_coord[1],:]=[255,0,0]\n\n    ret = cv2.addWeighted(ref_image, 0.7, mask, 0.3, 0)\n    return ret","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:09:30.673533Z","iopub.execute_input":"2021-07-13T06:09:30.673826Z","iopub.status.idle":"2021-07-13T06:09:30.68625Z","shell.execute_reply.started":"2021-07-13T06:09:30.673797Z","shell.execute_reply":"2021-07-13T06:09:30.68535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shenzhen_mask_dir = glob(os.path.join(SHENZHEN_MASK_DIR, '*.png'))\n#shenzhen_test = shenzhen_mask_dir[0:50]\nshenzhen_train= shenzhen_mask_dir[:]\n\nfor mask_file in tqdm(shenzhen_mask_dir):\n    base_file = os.path.basename(mask_file).replace(\"_mask\", \"\")\n    image_file = os.path.join(SHENZHEN_IMAGE_DIR, base_file)\n\n    image = cv2.imread(image_file)\n    mask = cv2.imread(mask_file, cv2.IMREAD_GRAYSCALE)\n        \n    image = cv2.resize(image, (512, 512))\n    mask = cv2.resize(mask, (512, 512))\n    mask_dilate = cv2.dilate(mask, DILATE_KERNEL, iterations=1)\n    \n    if (mask_file in shenzhen_train):\n        cv2.imwrite(os.path.join(SEGMENTATION_IMAGE_DIR, base_file), \\\n                    image)\n        cv2.imwrite(os.path.join(SEGMENTATION_MASK_DIR, base_file), \\\n                    mask)\n        cv2.imwrite(os.path.join(SEGMENTATION_DILATE_DIR, base_file), \\\n                    mask_dilate)","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:09:30.688165Z","iopub.execute_input":"2021-07-13T06:09:30.688641Z","iopub.status.idle":"2021-07-13T06:11:04.05246Z","shell.execute_reply.started":"2021-07-13T06:09:30.688608Z","shell.execute_reply":"2021-07-13T06:11:04.0515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = glob(os.path.join(SEGMENTATION_IMAGE_DIR, \"*.png\"))\n#test_files = glob(os.path.join(SEGMENTATION_TEST_DIR, \"*.png\"))\nmask_files = glob(os.path.join(SEGMENTATION_MASK_DIR, \"*.png\"))\ndilate_files = glob(os.path.join(SEGMENTATION_DILATE_DIR, \"*.png\"))\n\n(len(train_files), \\\n len(mask_files), \\\n len(dilate_files))","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:11:04.053809Z","iopub.execute_input":"2021-07-13T06:11:04.054123Z","iopub.status.idle":"2021-07-13T06:11:04.073826Z","shell.execute_reply.started":"2021-07-13T06:11:04.054092Z","shell.execute_reply":"2021-07-13T06:11:04.072472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dice_coef(y_true, y_pred):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + 1) / (K.sum(y_true_f) + K.sum(y_pred_f) + 1)\n\ndef dice_coef_loss(y_true, y_pred):\n    return -dice_coef(y_true, y_pred)\n\ndef iou(y_true, y_pred):\n    intersection = K.sum(K.abs(y_true * y_pred))\n    sum_ = K.sum(K.square(y_true)) + K.sum(K.square(y_pred))\n    jac = (intersection) / (sum_ - intersection)\n    return jac","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:12:56.151721Z","iopub.execute_input":"2021-07-13T06:12:56.152428Z","iopub.status.idle":"2021-07-13T06:12:56.16048Z","shell.execute_reply.started":"2021-07-13T06:12:56.152355Z","shell.execute_reply":"2021-07-13T06:12:56.159613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('../input/lungs-mask/5_unet_lung_seg.hdf5',custom_objects={'dice_coef_loss': dice_coef_loss, 'iou': iou, 'dice_coef': dice_coef})","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:20:25.829851Z","iopub.execute_input":"2021-07-13T06:20:25.830224Z","iopub.status.idle":"2021-07-13T06:20:28.882837Z","shell.execute_reply.started":"2021-07-13T06:20:25.830188Z","shell.execute_reply":"2021-07-13T06:20:28.881994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\n\nfor i in range(10):\n    index=np.random.randint(1,len(train_files))\n    img = cv2.imread(train_files[index])\n    img = cv2.resize(img, (512,512))\n    img = preprocess_input(img)\n    img = img[np.newaxis, :, :, :]\n    pred = model.predict(img)\n\n    plt.figure(figsize=(12,12))\n    plt.subplot(1,3,1)\n    plt.imshow(cv2.imread(train_files[index]))\n    plt.title('Original Image')\n    plt.subplot(1,3,2)\n    plt.imshow(np.squeeze(cv2.imread(dilate_files[index])))\n    plt.title('Original Mask')\n    plt.subplot(1,3,3)\n    plt.imshow(np.squeeze(pred) > .5)\n    plt.title('Prediction')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-13T07:05:32.198526Z","iopub.execute_input":"2021-07-13T07:05:32.198931Z","iopub.status.idle":"2021-07-13T07:06:13.271525Z","shell.execute_reply.started":"2021-07-13T07:05:32.198894Z","shell.execute_reply":"2021-07-13T07:06:13.270291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport csv\nimport random\nimport pydicom\nimport numpy as np\nimport pandas as pd\nfrom skimage import io\nfrom skimage import measure\nfrom skimage.transform import resize\n\nimport tensorflow as tf\nfrom tensorflow import keras\n\n\nfrom matplotlib import pyplot as plt\nimport matplotlib.patches as patches","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:53:41.688484Z","iopub.execute_input":"2021-07-13T06:53:41.688816Z","iopub.status.idle":"2021-07-13T06:53:42.871579Z","shell.execute_reply.started":"2021-07-13T06:53:41.688778Z","shell.execute_reply":"2021-07-13T06:53:42.870581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_ls = pd.read_csv('../input/view-paap/PA.csv')\nap_ls = pd.read_csv('../input/view-paap/AP.csv')\npa_ls = [i for i in np.array(pa_ls)[:,0]]\nap_ls = [i for i in np.array(ap_ls)[:,0]]","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:54:13.109835Z","iopub.execute_input":"2021-07-13T06:54:13.110254Z","iopub.status.idle":"2021-07-13T06:54:13.198854Z","shell.execute_reply.started":"2021-07-13T06:54:13.110223Z","shell.execute_reply":"2021-07-13T06:54:13.19797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# empty dictionary\npneumonia_locations = {}\n# load table\nwith open(os.path.join('../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv'), mode='r') as infile:\n    # open reader\n    reader = csv.reader(infile)\n    # skip header\n    next(reader, None)\n    # loop through rows\n    for rows in reader:\n        # retrieve information\n        filename = rows[0]\n        location = rows[1:5]\n        pneumonia = rows[5]\n        # if row contains pneumonia add label to dictionary\n        # which contains a list of pneumonia locations per filename\n        if pneumonia == '1':\n            # convert string to float to int\n            location = [int(float(i)) for i in location]\n            # save pneumonia location in dictionary\n            if filename in pneumonia_locations:\n                pneumonia_locations[filename].append(location)\n            else:\n                pneumonia_locations[filename] = [location]","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:54:20.892202Z","iopub.execute_input":"2021-07-13T06:54:20.892545Z","iopub.status.idle":"2021-07-13T06:54:21.005738Z","shell.execute_reply.started":"2021-07-13T06:54:20.892516Z","shell.execute_reply":"2021-07-13T06:54:21.004767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_ls = pa_ls\n# load and shuffle filenames\nfolder = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images'\nfilenames = os.listdir(folder)","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:54:31.312929Z","iopub.execute_input":"2021-07-13T06:54:31.313291Z","iopub.status.idle":"2021-07-13T06:54:31.809853Z","shell.execute_reply.started":"2021-07-13T06:54:31.313262Z","shell.execute_reply":"2021-07-13T06:54:31.808966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_files = []\nfor i in filenames:\n    if i.split('.')[0] in file_ls:\n        selected_files.append(i)","metadata":{"execution":{"iopub.status.busy":"2021-07-13T06:54:41.365182Z","iopub.execute_input":"2021-07-13T06:54:41.365548Z","iopub.status.idle":"2021-07-13T06:54:48.936976Z","shell.execute_reply.started":"2021-07-13T06:54:41.365514Z","shell.execute_reply":"2021-07-13T06:54:48.936143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\n\nfor i in range(5):\n    img_ = pydicom.dcmread(os.path.join(folder, selected_files[i])).pixel_array\n    img_ = img_/255\n    img_ = cv2.resize(img_, (512,512))\n    plt.figure(figsize=(10,10))\n    plt.subplot(1,2,1)\n    plt.imshow(img_,cmap='gray')\n    plt.title('Original Image')\n    img_ = np.repeat(img_[..., np.newaxis], 3, -1)\n    img_ = preprocess_input(img_)\n    img_ = img_[np.newaxis, :, :, :]\n    \n    pred_ = model.predict(img_)\n    plt.subplot(1,2,2)\n    plt.imshow(np.squeeze(pred_) > .5)\n    plt.title('Prediction')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-13T07:04:26.16108Z","iopub.execute_input":"2021-07-13T07:04:26.161451Z","iopub.status.idle":"2021-07-13T07:04:46.484538Z","shell.execute_reply.started":"2021-07-13T07:04:26.16142Z","shell.execute_reply":"2021-07-13T07:04:46.483556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}