{"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":"markdown","source":"<a href=\"https://colab.research.google.com/github/harikishorep122/UNET/blob/main/RSNA.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>","metadata":{"id":"view-in-github"}},{"cell_type":"markdown","source":"#Transferring data from kaggle to gdrive","metadata":{"id":"s2aIhOt_B-lM"}},{"cell_type":"code","source":"\nfrom google.colab import files\n\n#files.upload()","metadata":{"id":"e1idYqM8hsTF","outputId":"c68c366b-ecf2-4839-bd9f-4c261e72c975","execution":{"iopub.status.busy":"2022-04-09T07:19:18.860764Z","iopub.execute_input":"2022-04-09T07:19:18.861059Z","iopub.status.idle":"2022-04-09T07:19:18.877531Z","shell.execute_reply.started":"2022-04-09T07:19:18.861026Z","shell.execute_reply":"2022-04-09T07:19:18.876123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir \"/content/gdrive/My Drive/RSNA\"\n! cp kaggle.json \"/content/gdrive/My Drive/RSNA\"\n! chmod 600 ~/.kaggle/kaggle.json\n!pip install --upgrade --force-reinstall --no-deps kaggle\n! kaggle datasets list\n!kaggle competitions download -c rsna-pneumonia-detection-challenge\n!unzip rsna-pneumonia-detection-challenge.zip -d \"/content/gdrive/My Drive/RSNA\"","metadata":{"id":"ewcEPHc4966I"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pydicom\nimport os\nimport tensorflow as tf\n#mkdir \"../input/rsna-pneumonia-detection-challenge/train1\"\n#os.makedirs('../input/rsna-pneumonia-detection-challenge/train1')\ntf.io.gfile.makedirs('/kaggle/working/train1')\nimport pydicom as dicom\nimport cv2      \nfolder_path = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images'\njpg_path = \"'/kaggle/working/train1\"\nimages_path = os.listdir(folder_path)\nfor n, image in enumerate(images_path):\n    ds = dicom.dcmread(os.path.join(folder_path, image))\n    pixel_array_numpy = ds.pixel_array\n    image = image.replace('.dcm', '.jpg')\n    cv2.imwrite(os.path.join(jpg_path, image), pixel_array_numpy)\n    if n % 1000 == 0:\n        print('{} image converted'.format(n))","metadata":{"id":"cQXmGPzFku2e","execution":{"iopub.status.busy":"2022-04-09T07:11:36.590885Z","iopub.execute_input":"2022-04-09T07:11:36.591176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Creating Mask images","metadata":{"id":"3y6Ae3DUvyqu"}},{"cell_type":"code","source":"import numpy as np\nimport cv2\nimport cv2_imshow\nimport csv\nimport os\n\ntf.io.gfile.makedirs('/kaggle/working/masks')\nmask_path = \"/kaggle/working/masks/\"\ncsv_path = \"/content/gdrive/My Drive/RSNA/stage_2_train_labels.csv\"\n\nsheet = open(csv_path, newline='')\n\nimages_path = os.listdir(\"/content/gdrive/My Drive/RSNA/train\")\nimages_path = [os.path.splitext(x)[0] for x in images_path]\nimages_path = [x.strip() for x in images_path]\ni=1\nfor image_name in images_path: \n    sheet = open(csv_path, newline='')\n    reader = csv.reader(sheet)\n    for row in reader:\n      if row[0]==image_name:\n        print(image_name)\n        print(i)\n        i+=1\n        if row[5]=='1':\n          pt1 = (int(row[1].split('.')[0]),int(row[2].split('.')[0]))\n          pt2 = int(row[1].split('.')[0])+int(row[3].split('.')[0]),int(row[2].split('.')[0])+int(row[4].split('.')[0])\n\n          if os.path.isfile(mask_path+image_name+'.jpg')==True:       \n            img = cv2.imread(mask_path+image_name+'.jpg')\n          else:\n            img = np.zeros((1024, 1024, 3), dtype = \"uint8\")\n          cv2.rectangle(img, pt1, pt2, (255,255,255), -1)\n          cv2.imwrite(mask_path+image_name+'.jpg', img)  \n          #cv2_imshow(img)\n        else:  \n          img = np.zeros((1024, 1024, 3), dtype = \"uint8\")\n          cv2.imwrite(mask_path+row[0]+'.jpg', img)\n    sheet.close()","metadata":{"id":"064ZyYfivCeU","execution":{"iopub.status.busy":"2022-04-09T07:18:36.565133Z","iopub.execute_input":"2022-04-09T07:18:36.565446Z","iopub.status.idle":"2022-04-09T07:18:36.621397Z","shell.execute_reply.started":"2022-04-09T07:18:36.565413Z","shell.execute_reply":"2022-04-09T07:18:36.619615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\njpg_path = \"/content/gdrive/My Drive/RSNA/stage_2_train_images\"\nimages_path = os.listdir(jpg_path)\nlen(images_path)","metadata":{"id":"rd2PFj7F22s5","outputId":"abd5bc89-bbcf-4b39-9913-83494671453d"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = \"/content/gdrive/My Drive/RSNA/train\"\nids = next(os.walk(images_path))[2] # list of names all images in the given path\nprint(\"No. of images = \", len(ids))","metadata":{"id":"0r-Uyc--t91j","outputId":"d296f567-045d-4db1-9b5b-b2ada5b1277a"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Loading images and masks into arrays","metadata":{"id":"pVFloPBn-jKo"}},{"cell_type":"code","source":"# tqdm is used to display the progress bar\nimages_path = \"/content/gdrive/My Drive/RSNA/train/\"\nmask_path = \"/content/gdrive/My Drive/RSNA/masks/\"\nerr =1\nfor n, id_ in tqdm_notebook(enumerate(ids), total=len(ids)):\n    # Load images\n    img = load_img(images_path+id_, grayscale=True)\n    x_img = img_to_array(img)\n    x_img = resize(x_img, (256, 256, 1), mode = 'constant', preserve_range = True)\n    # Load masks\n    try:\n      mask = load_img(mask_path+id_, grayscale=True)\n      mask = img_to_array(mask)\n      mask = resize(mask, (256, 256, 1), mode = 'constant', preserve_range = True)\n    except Exception as e:\n      err+=1\n      #print(e)\n      continue  \n    # Save images\n    X[n] = x_img/255.0\n    y[n] = mask/255.0\nprint(err)","metadata":{"id":"9_j-kwuo-om4"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Mounting Drive","metadata":{"id":"TfcQcvvjfV-c"}},{"cell_type":"code","source":"from google.colab import drive\ndrive.mount('/content/gdrive')","metadata":{"id":"kG-7gielfUwN","outputId":"041ddb16-17d9-4e4f-9187-af27e1626648"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Importing necessary packages","metadata":{"id":"Ik2_GSiUc9Uw"}},{"cell_type":"code","source":"import os\nimport random\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nplt.style.use(\"ggplot\")\n%matplotlib inline\n!pip install pydicom\nimport pydicom as dicom\nimport cv2\nimport os\nfrom google.colab.patches import cv2_imshow\nimport csv\nimport keras\n\nfrom tqdm import tqdm_notebook, tnrange\nfrom itertools import chain\nfrom skimage.io import imread, imshow, concatenate_images\nfrom skimage.transform import resize\nfrom skimage.morphology import label\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\n\nfrom keras.models import Model, load_model\nfrom keras.layers import Input, BatchNormalization, Activation, Dense, Dropout\nfrom keras.layers.core import Lambda, RepeatVector, Reshape\nfrom keras.layers.convolutional import Conv2D, Conv2DTranspose\nfrom keras.layers.pooling import MaxPooling2D, GlobalMaxPool2D\nfrom keras.layers.merge import concatenate, add\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img\nfrom keras.metrics import MeanIoU\nfrom keras.utils import to_categorical\nfrom tensorflow.keras import layers\nfrom keras.callbacks import CSVLogger\nimport datetime","metadata":{"id":"uQKP4hGTs-yY","outputId":"60291cf5-fd59-4fdb-a80d-379e856f991c"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Set some parameters","metadata":{"id":"VesUXPvkcgaj"}},{"cell_type":"code","source":"im_height = 256 \nim_width = 256","metadata":{"id":"b7y85gKOcgHF"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Data generator class for efficient data flow","metadata":{"id":"L0tu1ooRnfsI"}},{"cell_type":"code","source":"class DataGenerator(keras.utils.Sequence):\n    # Inherites keras.utils.Sequence for using functionalities such as multiprocessing\n    'Generates data for Keras'\n    def __init__(self, folder_path, csv_path, ratio, batch_size=32, img_height=256, img_width=256, shuffle=True, val= False):\n        'Initialization'\n        self.batch_size = batch_size\n        self.folder_path = folder_path\n        self.csv_path = csv_path\n        self.ratio = ratio\n        if val == False:\n          self.image_names = os.listdir(folder_path)\n          random.seed(2)\n          random.shuffle(self.image_names)\n          self.image_names = self.image_names[0:int(ratio*len(self.image_names))]\n        else:\n          self.image_names = os.listdir(folder_path)\n          random.seed(2)\n          random.shuffle(self.image_names)\n          self.image_names = self.image_names[int(ratio*len(self.image_names)):-1]\n        self.img_height = img_height\n        self.img_width = img_width\n        self.shuffle = shuffle\n        self.on_epoch_end()\n\n    def __len__(self):\n        'Denotes the number of batches per epoch'\n        return int(np.floor(len(self.image_names) / self.batch_size))\n\n    def __getitem__(self, index):\n        'Generate one batch of data'\n        # Generate indexes of the batch\n        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n\n        # Find list of IDs\n        list_IDs_temp = [self.image_names[k] for k in indexes]\n\n        # Generate data\n        X, y = self.__data_generation(list_IDs_temp)\n\n        return X, y\n\n    def on_epoch_end(self):\n        'Updates indexes after each epoch'\n        self.indexes = np.arange(len(self.image_names))\n        if self.shuffle == True:\n            np.random.shuffle(self.indexes)\n\n    def __data_generation(self, list_IDs_temp):\n        'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)\n        # Initialization\n        X = np.zeros((self.batch_size, self.img_height, self.img_width, 1), dtype=np.float32)\n        y = np.zeros((self.batch_size, self.img_height, self.img_width, 2), dtype=np.float32)\n\n        # Generate data\n        for n, image_name in enumerate(list_IDs_temp):\n            ds = dicom.dcmread(os.path.join(self.folder_path, image_name))\n            pixel_array_numpy = ds.pixel_array\n            x_img = pixel_array_numpy\n            x_img = resize(x_img, (self.img_height, self.img_width, 1), mode = 'constant', preserve_range = True)\n\n            # Draw masks\n            sheet = open(self.csv_path, newline='')\n            reader = csv.reader(sheet)\n            mask = np.zeros((1024, 1024, 1), dtype = \"uint8\")\n\n            image_name = os.path.splitext(image_name)[0]\n            image_name = image_name.strip()\n\n            for row in reader:\n              if row[0]==image_name:\n                if row[5]=='1':\n                  pt1 = (int(row[1].split('.')[0]),int(row[2].split('.')[0]))\n                  pt2 = (int(row[1].split('.')[0])+int(row[3].split('.')[0]),int(row[2].split('.')[0])+int(row[4].split('.')[0]))\n                  cv2.rectangle(mask, pt1, pt2, 255, -1)     \n            sheet.close()\n            mask = resize(mask, (self.img_height, self.img_width, 1), mode = 'constant', preserve_range = True)\n            mask.astype(int)\n            mask /= 255\n            mask = tf.keras.utils.to_categorical(mask, num_classes=2, dtype='float32')\n            #cv2_imshow(x_img)\n            #cv2_imshow(mask)\n\n            # Save images\n            X[n] = x_img\n            y[n] = mask\n\n        return X, y","metadata":{"id":"R4uOGR-Gnm81"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Creating UNET Model","metadata":{"id":"HTjZXR7UEvA2"}},{"cell_type":"code","source":"# Datasets\nimages_path = \"/content/gdrive/My Drive/RSNA/stage_2_train_images\"\ncsv_path = \"/content/gdrive/My Drive/RSNA/stage_2_train_labels.csv\"","metadata":{"id":"o9z1BQ1003oC"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generators\ntraining_generator = DataGenerator(images_path, csv_path, 0.8, batch_size=32, img_height=256, img_width=256, shuffle=True, val=False)\nvalidation_generator = DataGenerator(images_path, csv_path, 0.8, batch_size=32, img_height=256, img_width=256, shuffle=True, val=True)","metadata":{"id":"j9OXtSahi9ud"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visualize any random image along with the mask","metadata":{"id":"prpgVDVKH0ZD"}},{"cell_type":"code","source":"gen = DataGenerator(images_path,csv_path, 0.8, batch_size = 32, img_height = 256, img_width = 256, shuffle = True, val = False)\nx, y = gen.__getitem__(0)","metadata":{"id":"X4IU2SD38a6K"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize any random image along with the mask\nix = random.randint(0, len(x)-1)\n\nfig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize = (10, 10))\n\nax1.imshow(x[ix].squeeze(), cmap = 'gray', interpolation = 'bilinear')\nax1.set_title('image')\n\nax2.imshow(y[ix][0].squeeze(), cmap = 'gray', interpolation = 'bilinear')\nax2.set_title('mask')\n\nax3.imshow(y[ix][1].squeeze(), cmap = 'gray', interpolation = 'bilinear')\nax3.set_title('mask')","metadata":{"id":"YpgXJqjQBAjJ"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Functions for dice coefficient metric and loss","metadata":{"id":"uOcuvCXhLbGz"}},{"cell_type":"code","source":"from tensorflow.keras.losses import BinaryCrossentropy\ndef dice_coef(y_true, y_pred):\n\n  smooth=1.0e-6\n  #y_true_f_1 = tf.keras.backend.flatten(y_true[:,:,:,0])\n  #y_pred_f_1 = tf.keras.backend.flatten(y_pred[:,:,:,0])\n  y_true_f_2 = tf.keras.backend.flatten(y_true[:,:,:,1])\n  y_pred_f_2 = tf.keras.backend.flatten(y_pred[:,:,:,1])\n  #intersection_1 = tf.keras.backend.sum(y_true_f_1 * y_pred_f_1)\n  intersection_2 = tf.keras.backend.sum(y_true_f_2 * y_pred_f_2)\n  #union_1 = tf.keras.backend.sum(y_true_f_1) + tf.keras.backend.sum(y_pred_f_1)\n  union_2 = tf.keras.backend.sum(y_true_f_2) + tf.keras.backend.sum(y_pred_f_2)\n  \n  score = 2*(intersection_2+smooth)/(union_2+smooth)\n  return score\n  \ndef DiceLoss(y_true, y_pred):\n\n  score = dice_coef(y_true, y_pred)\n  \n  return 1 - score","metadata":{"id":"Cr1YBhM-kldt"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Model architecture","metadata":{"id":"fLObi0jM8vta"}},{"cell_type":"code","source":"def get_model(img_size, num_classes):\n    inputs = keras.Input(shape=img_size + (1,))\n\n    ### [First half of the network: downsampling inputs] ###\n\n    # Entry block\n    x = layers.Conv2D(32, 3, strides=2, padding=\"same\")(inputs)\n    x = layers.BatchNormalization()(x)\n    x = layers.Activation(\"relu\")(x)\n\n    previous_block_activation = x  # Set aside residual\n\n    # Blocks 1, 2, 3 are identical apart from the feature depth.\n    for filters in [64, 128, 256]:\n        x = layers.Activation(\"relu\")(x)\n        x = layers.SeparableConv2D(filters, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.Activation(\"relu\")(x)\n        x = layers.SeparableConv2D(filters, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.MaxPooling2D(3, strides=2, padding=\"same\")(x)\n\n        # Project residual\n        residual = layers.Conv2D(filters, 1, strides=2, padding=\"same\")(\n            previous_block_activation\n        )\n        x = layers.add([x, residual])  # Add back residual\n        previous_block_activation = x  # Set aside next residual\n\n    ### [Second half of the network: upsampling inputs] ###\n\n    for filters in [256, 128, 64, 32]:\n        x = layers.Activation(\"relu\")(x)\n        x = layers.Conv2DTranspose(filters, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.Activation(\"relu\")(x)\n        x = layers.Conv2DTranspose(filters, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.UpSampling2D(2)(x)\n\n        # Project residual\n        residual = layers.UpSampling2D(2)(previous_block_activation)\n        residual = layers.Conv2D(filters, 1, padding=\"same\")(residual)\n        x = layers.add([x, residual])  # Add back residual\n        previous_block_activation = x  # Set aside next residual\n\n    # Add a per-pixel classification layer\n    outputs = layers.Conv2D(num_classes, 3, activation=\"softmax\", padding=\"same\")(x)\n\n    # Define the model\n    model = keras.Model(inputs, outputs)\n    return model\n\n\n# Free up RAM in case the model definition cells were run multiple times\n#keras.backend.clear_session()","metadata":{"id":"CF1Y8kQV8sc2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = (im_height, im_width)\nnum_classes = 2\nmodel = get_model(img_size, num_classes)\nmodel.compile(optimizer=Adam(), loss = DiceLoss, metrics=[MeanIoU(num_classes=2), dice_coef]) ","metadata":{"id":"ASSdDPZC_uRN"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Model Summary","metadata":{"id":"U-vK2Wl_UHHp"}},{"cell_type":"code","source":"model.summary()","metadata":{"id":"BX-G-HzQUGj6","outputId":"a4f9b42a-d5ab-4e0d-d070-4b77101b13dc"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Callbacks for the model","metadata":{"id":"uNAtwfefVSGG"}},{"cell_type":"code","source":"#callbacks = [ReduceLROnPlateau(factor=0.1, patience=5, min_lr=0.000001, verbose=1),]\nearlystopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0.0001, patience = 52, verbose=1, restore_best_weights=True)\ncheckpoint = ModelCheckpoint(\"/content/gdrive/My Drive/RSNA/best_model.h5\", monitor='val_loss', verbose=1,\n    save_best_only=True, mode='auto', save_freq = 'epoch')\ncsv_logger = CSVLogger(\"/content/gdrive/My Drive/RSNA/training.csv\", separator=',', append=True)\nlogdir = \"/content/gdrive/My Drive/RSNA/\" + \"logs/scalars/\" + datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\ntensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)\n\n\ncallbacks = [earlystopping, checkpoint, csv_logger, tensorboard_callback]","metadata":{"id":"8LEqlfIkVUgh"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the model","metadata":{"id":"JltLc83fCB6H"}},{"cell_type":"code","source":"#results = model.fit(X_train, y_train, batch_size=32, epochs=50, callbacks=callbacks, validation_data=(X_valid, y_valid))\n\nresults = model.fit(training_generator,\n                    validation_data=validation_generator,\n                    epochs = 50,\n                    callbacks=callbacks)","metadata":{"id":"9Zhlj_dYCEi6"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Saving the model to drive","metadata":{"id":"wUiWB6QKCFYg"}},{"cell_type":"code","source":"model.save_weights('/content/gdrive/My Drive/RSNA/unet_model.h5')","metadata":{"id":"Sm9d76ZWCIaq"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Plotting learning curve","metadata":{"id":"bdQhBIyACV-8"}},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.title(\"Learning curve\")\nplt.plot(results.history[\"loss\"], label=\"loss\")\nplt.plot(results.history[\"val_loss\"], label=\"val_loss\")\nplt.plot( np.argmin(results.history[\"val_loss\"]), np.min(results.history[\"val_loss\"]), marker=\"x\", color=\"r\", label=\"best model\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"log_loss\")\nplt.legend();","metadata":{"id":"qfyI9fIVCWXx","outputId":"125059ea-99ec-4e7b-858a-ece25c9f2580"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Plotting Dice coefficient curve","metadata":{"id":"05HqALjVL1eq"}},{"cell_type":"code","source":"plt.figure(figsize=(10,8))\nplt.plot(results.history['dice_coef'],label='dice_coef')\nplt.plot(results.history['val_dice_coef'],label='val_dice_coef')\nplt.legend()","metadata":{"id":"rf3rF2cAL0hp","outputId":"f5649b22-066b-42de-ee59-e5bea8991fc8"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Evaluate on validation set","metadata":{"id":"oiYReB3efibm"}},{"cell_type":"code","source":"model.evaluate(validation_generator, verbose=1)","metadata":{"id":"SdsNz2aYfmKQ","outputId":"c83c1397-5808-4abb-ea60-daed48fb0b42"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading saved model","metadata":{"id":"Y0YXn2R0SJ5P"}},{"cell_type":"code","source":"from keras.models import load_model\n\nmodel = load_model('/content/gdrive/My Drive/RSNA/best_model.h5', compile = False)","metadata":{"id":"QiXkHddLhC2D"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Predictions on train and validation sets","metadata":{"id":"5FIN9SPzgYPI"}},{"cell_type":"code","source":"def plot_sample1():\n    \"\"\"Function to plot the results\"\"\"\n    \n    fig, ax = plt.subplots(1, 3, figsize=(20, 20))\n    folder_path = '/content/gdrive/My Drive/RSNA/stage_2_train_images'\n    csv_path = \"/content/gdrive/My Drive/RSNA/stage_2_train_labels.csv\" \n    #loading a random image\n\n    ix = random.randint(0, len(ids))\n    image_name = ids[ix]\n    ds = dicom.dcmread(os.path.join(folder_path, image_name))\n    pixel_array_numpy = ds.pixel_array\n    x_img = pixel_array_numpy\n\n    # Draw masks\n    mask = np.zeros((1024, 1024, 1), dtype = \"uint8\")\n\n    image_name = os.path.splitext(image_name)[0]\n    image_name = image_name.strip()\n\n    sheet = open(csv_path, newline='')\n    reader = csv.reader(sheet)\n    for row in reader:\n        if row[0]==image_name:\n          if row[5]=='1':\n            pt1 = (int(row[1].split('.')[0]),int(row[2].split('.')[0]))\n            pt2 = (int(row[1].split('.')[0])+int(row[3].split('.')[0]),int(row[2].split('.')[0])+int(row[4].split('.')[0]))\n            cv2.rectangle(mask, pt1, pt2, (255), -1)     \n    sheet.close()\n    mask = resize(mask, (im_height, im_width, 1), mode = 'constant', preserve_range = True)\n\n    #ground truth plot---------------------------------------------------------\n\n    ax[0].imshow(x_img, cmap='gray', interpolation = 'bilinear')\n    #ax[0].contour(mask.squeeze(), colors='black', levels=[0.5])\n    ax[0].set_title('x_ray ground truth')\n\n    #Mask ground truth plot---------------------------------------------------\n\n    ax[1].imshow(mask.squeeze(), cmap='gray')\n    ax[1].set_title('Mask ground truth')\n\n    #Predicted mask plot --------------------------------------------------\n\n    x_img = resize(x_img, (256, 256), mode = 'constant', preserve_range = True)\n    img = np.expand_dims(x_img, axis=-1)\n    x_test= np.zeros((1, 256, 256, 1), dtype=np.uint8)\n    x_test[0]=img\n\n    pred_test= model.predict(x_test, verbose=1)\n    pred_test = np.argmax(pred_test, axis=-1)\n    pred_test = pred_test[0]\n    print(pred_test.shape)\n    for i in range(pred_test.shape[0]):\n            for j in range(pred_test.shape[1]):\n                if pred_test[i][j] == 1:\n                    pred_test[i][j] = 255\n                else:\n                    pred_test[i][j] = 0\n    pred_test = cv2.merge((pred_test,pred_test, pred_test))\n    '''\n    contours, hierarchy = cv2.findContours(pred_test,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n    for each_contour in contours:\n        x,y,w,h = cv2.boundingRect(each_contour)\n        cv2.rectangle(pred_test,(x,y),(x+w,y+h),(0,0,255),4)\n        print (x,y,w,h)\n    '''\n    ax[2].imshow(pred_test, cmap='gray')\n    \n    ax[2].set_title('Predicted x_ray')","metadata":{"id":"qgLvywkZha-A"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Predictions on training set","metadata":{"id":"Iine9H5ginud"}},{"cell_type":"code","source":"ids = os.listdir(images_path)\nprint(len(ids))","metadata":{"id":"h5D7izVGRtev","outputId":"59f38d1b-4d46-4e67-fc91-d735b4f07e22"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):plot_sample1()","metadata":{"id":"HjfyMRjXmaqI","outputId":"9c55104e-25a4-418c-a94c-c09350720fda"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):plot_sample1()","metadata":{"id":"U0y5KeBTmCFO","outputId":"1e13f08f-756b-40dd-89f1-f60e13eceaf1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):plot_sample1()","metadata":{"id":"1HHpHDdAaSjs","outputId":"8df3af83-8523-4967-d89d-043318530627"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Plotting mean IoU curve","metadata":{"id":"CzkWGWKiMrbV"}},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\nplt.plot(results.history['mean_iou'],label='iou')\nplt.legend()","metadata":{"id":"gb50CW4JMv_7"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"while True: pass","metadata":{"id":"8_5VuFYwSMBV"},"execution_count":null,"outputs":[]}]}