{"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":"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-10T12:44:16.457171Z","iopub.execute_input":"2021-07-10T12:44:16.457633Z","iopub.status.idle":"2021-07-10T12:44:22.105789Z","shell.execute_reply.started":"2021-07-10T12:44:16.457545Z","shell.execute_reply":"2021-07-10T12:44:22.104846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2021-07-10T12:44:22.107413Z","iopub.execute_input":"2021-07-10T12:44:22.107834Z","iopub.status.idle":"2021-07-10T12:44:22.116473Z","shell.execute_reply.started":"2021-07-10T12:44:22.107794Z","shell.execute_reply":"2021-07-10T12:44:22.11536Z"},"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-10T12:44:22.118899Z","iopub.execute_input":"2021-07-10T12:44:22.119334Z","iopub.status.idle":"2021-07-10T12:44:22.222126Z","shell.execute_reply.started":"2021-07-10T12:44:22.119291Z","shell.execute_reply":"2021-07-10T12:44:22.221115Z"},"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-10T12:44:22.223829Z","iopub.execute_input":"2021-07-10T12:44:22.224157Z","iopub.status.idle":"2021-07-10T12:44:22.328378Z","shell.execute_reply.started":"2021-07-10T12:44:22.224123Z","shell.execute_reply":"2021-07-10T12:44:22.327329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_ls = ap_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-10T12:44:22.329781Z","iopub.execute_input":"2021-07-10T12:44:22.330136Z","iopub.status.idle":"2021-07-10T12:44:23.04427Z","shell.execute_reply.started":"2021-07-10T12:44:22.330101Z","shell.execute_reply":"2021-07-10T12:44:23.043349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nselected_files = []\nfor i in tqdm(filenames):\n    if i.split('.')[0] in file_ls:\n        selected_files.append(i)\n    \nlen(selected_files)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T12:44:23.045539Z","iopub.execute_input":"2021-07-10T12:44:23.045904Z","iopub.status.idle":"2021-07-10T12:44:32.464648Z","shell.execute_reply.started":"2021-07-10T12:44:23.04587Z","shell.execute_reply":"2021-07-10T12:44:32.463824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = selected_files\nrandom.shuffle(filenames)\n# split into train and validation filenames\nn_valid_samples = 1451\n# n_valid_samples = 1217\nn_test_samples = 2902\n# n_test_samples = 2435\ntrain_filenames = filenames[n_test_samples+n_valid_samples:]\nvalid_filenames = filenames[:n_valid_samples]\ntest_filenames = filenames[n_valid_samples:n_test_samples+n_valid_samples]\nprint('n train samples', len(train_filenames))\nprint('n valid samples', len(valid_filenames))\nprint('n test samples', len(test_filenames))\nn_train_samples = len(filenames) - n_valid_samples","metadata":{"execution":{"iopub.status.busy":"2021-07-10T12:44:32.46855Z","iopub.execute_input":"2021-07-10T12:44:32.470593Z","iopub.status.idle":"2021-07-10T12:44:32.529738Z","shell.execute_reply.started":"2021-07-10T12:44:32.470552Z","shell.execute_reply":"2021-07-10T12:44:32.528833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Total train images:',len(filenames))\nprint('Images with pneumonia:', len(pneumonia_locations))\n\nns = [len(value) for value in pneumonia_locations.values()]\nplt.figure()\nplt.hist(ns)\nplt.xlabel('Pneumonia per image')\nplt.xticks(range(1, np.max(ns)+1))\nplt.show()\n\nheatmap = np.zeros((1024, 1024))\nws = []\nhs = []\nfor values in pneumonia_locations.values():\n    for value in values:\n        x, y, w, h = value\n        heatmap[y:y+h, x:x+w] += 1\n        ws.append(w)\n        hs.append(h)\nplt.figure()\nplt.title('Pneumonia location heatmap')\nplt.imshow(heatmap)\nplt.figure()\nplt.title('Pneumonia height lengths')\nplt.hist(hs, bins=np.linspace(0,1000,50))\nplt.show()\nplt.figure()\nplt.title('Pneumonia width lengths')\nplt.hist(ws, bins=np.linspace(0,1000,50))\nplt.show()\nprint('Minimum pneumonia height:', np.min(hs))\nprint('Minimum pneumonia width: ', np.min(ws))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T12:44:32.534828Z","iopub.execute_input":"2021-07-10T12:44:32.537003Z","iopub.status.idle":"2021-07-10T12:44:35.087377Z","shell.execute_reply.started":"2021-07-10T12:44:32.53696Z","shell.execute_reply":"2021-07-10T12:44:35.084455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"height = 128\nclass generator(keras.utils.Sequence):\n    \n    def __init__(self, folder, filenames, pneumonia_locations=None, batch_size=32, image_size=256, shuffle=True, augment=False, predict=False):\n        self.folder = folder\n        self.filenames = filenames\n        self.pneumonia_locations = pneumonia_locations\n        self.batch_size = batch_size\n        self.image_size = image_size\n        self.shuffle = shuffle\n        self.augment = augment\n        self.predict = predict\n        self.on_epoch_end()\n        \n    def __load__(self, filename):\n        # load dicom file as numpy array\n        img = pydicom.dcmread(os.path.join(self.folder, filename)).pixel_array\n        # create empty mask\n        msk = np.zeros(img.shape)\n        # get filename without extension\n        filename = filename.split('.')[0]\n        cls_label = 0\n        # if image contains pneumonia\n        if filename in self.pneumonia_locations:\n            cls_label = 1\n            # loop through pneumonia\n            for location in self.pneumonia_locations[filename]:\n                # add 1's at the location of the pneumonia\n                x, y, w, h = location\n                msk[y:y+h, x:x+w] = 1\n        # resize both image and mask\n        img = resize(img, (self.image_size, self.image_size), mode='reflect')\n        msk = resize(msk, (self.image_size, self.image_size), mode='reflect') > 0.5\n        # if augment then horizontal flip half the time\n        if self.augment and random.random() > 0.5:\n            img = np.fliplr(img)\n            msk = np.fliplr(msk)\n        # add trailing channel dimension\n        img = np.expand_dims(img, -1)\n        msk = np.expand_dims(msk, -1)\n        \n        bbox = np.zeros((len(img), 15))\n        for i, m in enumerate(msk):\n            m_ = np.array(m, dtype='uint8')\n            _, thresh = cv2.threshold(m_,127,255,0)\n            contours, _ = cv2.findContours(thresh, 1, 2)\n            \n            for j, cnt in enumerate(contours):\n                bbox[i, 5*j] = 1\n                x,y,w,h = cv2.boundingRect(cnt)\n                bbox[i, 5*j+1:5*j+4] = x/height, y/height, w/height, h/height\n\n        # Assuming height == width\n        #bbox /= height\n        \n        img = np.repeat(img[..., np.newaxis], 3, -1)\n\n        return img, cls_label, bbox\n    \n    def __loadpredict__(self, filename):\n        # load dicom file as numpy array\n        img = pydicom.dcmread(os.path.join(self.folder, filename)).pixel_array\n        # resize image\n        img = resize(img, (self.image_size, self.image_size), mode='reflect')\n        # add trailing channel dimension\n        img = np.expand_dims(img, -1)\n        return img\n        \n    def __getitem__(self, index):\n        # select batch\n        filenames = self.filenames[index*self.batch_size:(index+1)*self.batch_size]\n        # predict mode: return images and filenames\n        if self.predict:\n            # load files\n            imgs = [self.__loadpredict__(filename) for filename in filenames]\n            # create numpy batch\n            imgs = np.array(imgs)\n            return imgs, filenames\n        # train mode: return images and masks\n        else:\n            # load files\n            items = [self.__load__(filename) for filename in filenames]\n            # unzip images and masks\n            imgs, cls_labels, bboxs = zip(*items)\n            # create numpy batch\n            imgs = np.array(imgs)\n            cls_labels = np.array(cls_labels)\n            bboxs = np.array(bboxs)\n            return imgs, cls_labels, bboxs\n        \n    def on_epoch_end(self):\n        if self.shuffle:\n            random.shuffle(self.filenames)\n        \n    def __len__(self):\n        if self.predict:\n            # return everything\n            return int(np.ceil(len(self.filenames) / self.batch_size))\n        else:\n            # return full batches only\n            return int(len(self.filenames) / self.batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T13:24:33.557244Z","iopub.execute_input":"2021-07-10T13:24:33.55763Z","iopub.status.idle":"2021-07-10T13:24:33.581164Z","shell.execute_reply.started":"2021-07-10T13:24:33.557599Z","shell.execute_reply":"2021-07-10T13:24:33.579927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model, load_model\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.layers import *\n\ndef TransCGUNet(input_size=(512, 512, 1)):\n    \"\"\"\n    TransCGUNet -- main architecture of TransCGUNet\n    \n    Arguments:\n    input_size {tuple} -- size of input image\n    \n    Returns:\n    model {<class 'tensorflow.python.keras.engine.training.Model'>} -- final model\n    \"\"\"\n    \n    # Input \n    inputs = Input(input_size)\n\n    # VGG16 with imagenet weights\n    encoder = VGG16(include_top=False, weights='imagenet', input_shape=input_size)\n       \n    # First encoder block\n    enc1 = encoder.get_layer(name='block1_conv1')(inputs)\n    enc1 = encoder.get_layer(name='block1_conv2')(enc1)\n    enc2 = MaxPooling2D(pool_size=(2, 2))(enc1)\n    \n    # Second encoder block\n    enc2 = encoder.get_layer(name='block2_conv1')(enc2)\n    enc2 = encoder.get_layer(name='block2_conv2')(enc2)\n    enc3 = MaxPooling2D(pool_size=(2, 2))(enc2)\n    \n    # Third encoder block\n    enc3 = encoder.get_layer(name='block3_conv1')(enc3)\n    enc3 = encoder.get_layer(name='block3_conv2')(enc3)\n    enc3 = encoder.get_layer(name='block3_conv3')(enc3)\n    center = MaxPooling2D(pool_size=(2, 2))(enc3)\n\n    # Center block\n    center = Conv2D(512, (3, 3), activation='relu', padding='same', name='center1')(center)\n    center = Conv2D(512, (3, 3), activation='relu', padding='same', name='center2')(center)\n    \n    # classification pred\n    cls = Conv2D(32, (3,3), activation='relu', padding='same')(center)\n    cls = Conv2D(1, (1,1))(cls)\n    cls = GlobalAveragePooling2D()(cls)\n    cls = Activation('sigmoid', name='class')(cls)\n    clsr = Reshape((1, 1, 1), name='reshape')(cls)\n\n#     # Decoder block corresponding to third encoder\n#     res_path3 = res_path(enc3,128,3)\n#     dec3 = decoder_block(center, res_path3, 256, 3)\n    \n#     # Decoder block corresponding to second encoder\n#     res_path2 = res_path(enc2,64,2)\n#     dec2 = decoder_block(dec3, res_path2, 128, 2)\n    \n#     # Final Block concatenation with first encoded feature \n#     res_path1 = res_path(enc1,32,1)\n#     dec1 = decoder_block(dec2, res_path1, 64, 1)\n\n#     # Output\n#     out = Conv2D(1, 1)(dec1)\n#     out = Activation('sigmoid', name='pre')(out)\n#     out_2 = multiply(inputs=[out,clsr], name='seg')\n    \n    # Final model\n    model = Model(inputs=[inputs], outputs= cls)\n    # Adding BBox\n    model = add_bbox(model)\n    \n    return model\n\ndef add_bbox(model):\n    # bbox branch\n    cls_ = Conv2D(256, (3,3), activation='relu', padding='same')(model.get_layer('center2').output)\n    cls_ = Conv2D(256, (3,3), activation='relu', padding='same')(cls_)\n    cls_ = MaxPooling2D(pool_size=(2, 2))(cls_)\n\n    cls_ = Conv2D(128, (3,3), activation='relu', padding='same')(cls_)\n    cls_ = Conv2D(128, (3,3), activation='relu', padding='same')(cls_)\n    cls_ = MaxPooling2D(pool_size=(2, 2))(cls_)\n\n    cls_ = Conv2D(64, (3,3), activation='relu', padding='same')(cls_)\n    cls_ = Conv2D(64, (3,3), activation='relu', padding='same')(cls_)\n    cls_ = MaxPooling2D(pool_size=(2, 2))(cls_)\n\n    cls_ = Conv2D(32, (3,3), activation='relu', padding='same')(cls_)\n    cls_ = Conv2D(32, (3,3), activation='relu', padding='same')(cls_)\n\n    bbox = Conv2D(15, (1,1))(cls_)\n    bbox = GlobalAveragePooling2D()(bbox)\n    bbox = Activation('sigmoid', name='bbox')(bbox)\n\n    return Model(inputs=[model.input], outputs=[model.output, bbox])","metadata":{"execution":{"iopub.status.busy":"2021-07-10T12:44:35.112423Z","iopub.execute_input":"2021-07-10T12:44:35.112921Z","iopub.status.idle":"2021-07-10T12:44:35.135129Z","shell.execute_reply.started":"2021-07-10T12:44:35.11288Z","shell.execute_reply":"2021-07-10T12:44:35.134236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\n\ndef recall_m(y_true, y_pred):\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n        recall = (true_positives + 1.) / (possible_positives + 1.)\n        return recall\n    \ndef recall(y_true, y_pred):\n    org = np.max(y_true)>0.5\n    pred = np.max(y_pred)>0.5\n    return np.float\n\ndef precision_m(y_true, y_pred):\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n        precision = (true_positives + 1.) / (predicted_positives + 1.)\n        return precision\n\ndef f1_m(y_true, y_pred):\n    precision = precision_m(y_true, y_pred)\n    recall = recall_m(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall))\n\n# define iou or jaccard loss function\ndef iou_loss(y_true, y_pred):\n    y_true = tf.reshape(y_true, [-1])\n    y_pred = tf.reshape(y_pred, [-1])\n    intersection = tf.reduce_sum(y_true * y_pred)\n    score = (intersection + 1.) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) - intersection + 1.)\n    return 1 - score\n\n# combine bce loss and iou loss\ndef iou_bce_loss(y_true, y_pred):\n    return 0.5 * keras.losses.binary_crossentropy(y_true, y_pred) + 0.5 * iou_loss(y_true, y_pred)\n\n# mean iou as a metric\ndef mean_iou(y_true, y_pred):\n    y_pred = tf.round(y_pred)\n    intersect = tf.reduce_sum(y_true * y_pred, axis=[1, 2, 3])\n    union = tf.reduce_sum(y_true, axis=[1, 2, 3]) + tf.reduce_sum(y_pred, axis=[1, 2, 3])\n    smooth = tf.ones(tf.shape(intersect))\n    return tf.reduce_mean((intersect + smooth) / (union - intersect + smooth))\n\n\ndef bbox_loss(y_true, y_pred):\n    for i in range(3):\n        loss_c = keras.losses.binary_crossentropy(y_true[5*i], y_pred[5*i])\n        if y_true[5*i] == 1:\n            loss_v = K.mean(K.square(y_true[5*i+1:5*i+4] - y_pred[5*i+1:5*i+4]), axis=-1)\n            \n    return loss_c*loss_v\n\n# create network and compiler\nmodel = TransCGUNet(input_size=(256, 256, 3))\n# model.compile(optimizer='adam',\n#               loss=iou_bce_loss,\n#               metrics=['accuracy',recall_m, precision_m, f1_m, mean_iou])\n\nmodel.compile(optimizer='adam',\n                    loss={'class':'binary_crossentropy', 'bbox':'mse'},\n                    loss_weights={'class':1, 'bbox':1},\n                    metrics={'class':['accuracy'], 'bbox':['accuracy']})\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T13:14:54.685666Z","iopub.execute_input":"2021-07-10T13:14:54.686014Z","iopub.status.idle":"2021-07-10T13:14:55.162373Z","shell.execute_reply.started":"2021-07-10T13:14:54.685985Z","shell.execute_reply":"2021-07-10T13:14:55.161508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n# cosine learning rate annealing\ndef cosine_annealing(x):\n    lr = 0.001\n    epochs = 1\n    return lr*(np.cos(np.pi*x/epochs)+1.)/2\nlearning_rate = tf.keras.callbacks.LearningRateScheduler(cosine_annealing)\n\n# create train and validation generators\nfolder = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images'\ntrain_gen = generator(folder, train_filenames, pneumonia_locations, batch_size=32, image_size=256, shuffle=True, augment=True, predict=False)\nvalid_gen = generator(folder, valid_filenames, pneumonia_locations, batch_size=32, image_size=256, shuffle=False, predict=False)\ntest_gen = generator(folder, test_filenames, pneumonia_locations, batch_size=32, image_size=256, shuffle=False, predict=False)\n\nhistory = model.fit(train_gen, validation_data=valid_gen, callbacks=[learning_rate], epochs=30, workers=4, use_multiprocessing=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T13:24:40.292318Z","iopub.execute_input":"2021-07-10T13:24:40.292694Z","iopub.status.idle":"2021-07-10T13:24:45.271609Z","shell.execute_reply.started":"2021-07-10T13:24:40.292663Z","shell.execute_reply":"2021-07-10T13:24:45.264571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('../input/modelweight/PA_FCN_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.411789Z","iopub.status.idle":"2021-07-09T16:41:46.4122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,4))\nplt.subplot(131)\nplt.plot(history.epoch, history.history[\"loss\"], label=\"Train loss\")\nplt.plot(history.epoch, history.history[\"val_loss\"], label=\"Valid loss\")\nplt.legend()\nplt.subplot(132)\nplt.plot(history.epoch, history.history[\"acc\"], label=\"Train accuracy\")\nplt.plot(history.epoch, history.history[\"val_acc\"], label=\"Valid accuracy\")\nplt.legend()\nplt.subplot(133)\nplt.plot(history.epoch, history.history[\"mean_iou\"], label=\"Train iou\")\nplt.plot(history.epoch, history.history[\"val_mean_iou\"], label=\"Valid iou\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.413351Z","iopub.status.idle":"2021-07-09T16:41:46.413783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.history[\"acc\"])","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.414736Z","iopub.status.idle":"2021-07-09T16:41:46.415126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.history[\"val_acc\"])","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.415926Z","iopub.status.idle":"2021-07-09T16:41:46.416316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.history[\"mean_iou\"])","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.417115Z","iopub.status.idle":"2021-07-09T16:41:46.417517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.history[\"val_mean_iou\"])","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.418484Z","iopub.status.idle":"2021-07-09T16:41:46.418909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_mean_iou = model.evaluate_generator(test_gen)[-1]\nprint(test_mean_iou)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.419787Z","iopub.status.idle":"2021-07-09T16:41:46.420173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_no = len(test_filenames)\nthresh = 0.5\nTotal = 0\nTP = 0\nTN = 0\nFP = 0\nFN = 0\ntrue = []\nprd = []\nfor imgs, msks in test_gen:\n    # predict batch of images\n    preds = model.predict(imgs)\n    for img, msk, pred in zip(imgs, msks, preds):\n        Total+=1\n        true.append(np.max(msk[:, :, 0]))\n        prd.append(np.max(pred[:, :, 0]))\n        # threshold true mask\n        comp1 = msk[:, :, 0] > thresh\n        comp2 = pred[:, :, 0] > thresh\n        comp1[comp1>thresh] = 1\n        comp2[comp2>thresh] = 1\n        predl = np.max(comp2)\n        orgl = np.max(comp1)\n        if(predl==1 and orgl==1):\n            TP += 1\n        elif(predl==1 and orgl==0):\n            FP += 1\n        elif(predl==0 and orgl==1):\n            FN += 1\n        else:\n            TN += 1\n        print(Total)\n        if Total>test_no: break\n    if Total>test_no: break","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.42093Z","iopub.status.idle":"2021-07-09T16:41:46.42132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom sklearn import metrics\ny = np.array(true)\nscores = np.array(prd)\nfpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.422111Z","iopub.status.idle":"2021-07-09T16:41:46.422499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_roc_curve(fpr, tpr):\n    plt.plot(fpr, tpr, color='orange', label='ROC')\n    plt.plot([0, 1], [0, 1], color='darkblue', linestyle='--')\n    plt.xlabel('False Positive Rate')\n    plt.ylabel('True Positive Rate')\n    plt.title('Receiver Operating Characteristic (ROC) Curve')\n    plt.legend()\n    plt.show()\nplot_roc_curve(fpr, tpr)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.423371Z","iopub.status.idle":"2021-07-09T16:41:46.423809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nroc_auc_score(y,scores)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.424762Z","iopub.status.idle":"2021-07-09T16:41:46.425153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Total, TP, FP, TN, FN)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.426017Z","iopub.status.idle":"2021-07-09T16:41:46.426437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sens = TP/(TP+FN)*1.0\nspec = TN/(TN+FP)*1.0\nF1 = (2*TP)/(2*TP+FP+FN)\nprint(sens, spec, F1)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.427186Z","iopub.status.idle":"2021-07-09T16:41:46.427593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sens = TP/(TP+FN)*1.0\nspec = TN/(TN+FP)*1.0\nF1 = (2*TP)/(2*TP+FP+FN)\nprint(sens, spec, F1)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.428437Z","iopub.status.idle":"2021-07-09T16:41:46.428863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for imgs, msks in test_gen:\n    # predict batch of images\n    preds = model.predict(imgs)\n    # create figure\n    f, axarr = plt.subplots(4, 8, figsize=(20,15))\n    axarr = axarr.ravel()\n    axidx = 0\n    # loop through batch\n    for img, msk, pred in zip(imgs, msks, preds):\n        # plot image\n        axarr[axidx].imshow(img[:, :, 0], cmap='gray')\n        axarr[axidx].axis('off')\n        # threshold true mask\n        comp = msk[:, :, 0] > 0.5\n        # apply connected components\n        comp = measure.label(comp)\n        # apply bounding boxes\n        predictionString = ''\n        for region in measure.regionprops(comp):\n            # retrieve x, y, height and width\n            y, x, y2, x2 = region.bbox\n            height = y2 - y\n            width = x2 - x\n            axarr[axidx].add_patch(patches.Rectangle((x,y),width,height,linewidth=2,edgecolor='b',facecolor='none'))\n        # threshold predicted mask\n        comp = pred[:, :, 0] > 0.5\n        # apply connected components\n        comp = measure.label(comp)\n        # apply bounding boxes\n        predictionString = ''\n        for region in measure.regionprops(comp):\n            # retrieve x, y, height and width\n            y, x, y2, x2 = region.bbox\n            height = y2 - y\n            width = x2 - x\n            axarr[axidx].add_patch(patches.Rectangle((x,y),width,height,linewidth=2,edgecolor='r',facecolor='none'))\n        axidx += 1\n    plt.savefig('result.png', dpi=500)\n    plt.show()\n    # only plot one batch\n    break","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.429678Z","iopub.status.idle":"2021-07-09T16:41:46.430083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnt = 0\nimages = []\nmasks = []\nfor imgs, msks in test_gen:\n    for img, msk, pred in zip(imgs, msks, preds):\n        images.append(img[:, :, 0])\n        masks.append(msk[:, :, 0] > 0.5)\n    cnt+=1\n    if cnt>1: break\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.430846Z","iopub.status.idle":"2021-07-09T16:41:46.431235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array([[1,1,1],[1,1,1], [1,1,1]])\nnp.repeat(x[..., np.newaxis], 3, -1).shape","metadata":{"execution":{"iopub.status.busy":"2021-07-10T13:23:41.252496Z","iopub.execute_input":"2021-07-10T13:23:41.252838Z","iopub.status.idle":"2021-07-10T13:23:41.259133Z","shell.execute_reply.started":"2021-07-10T13:23:41.252809Z","shell.execute_reply":"2021-07-10T13:23:41.258304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = images[5]\nimg","metadata":{"execution":{"iopub.status.busy":"2021-07-09T16:41:46.432074Z","iopub.status.idle":"2021-07-09T16:41:46.432466Z"},"trusted":true},"execution_count":null,"outputs":[]}]}