{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport random\nimport cv2\nimport tensorflow as tf\nimport pydicom\nfrom math import ceil, floor\nfrom copy import deepcopy\nfrom tqdm.notebook import tqdm\nfrom imgaug import augmenters as iaa\nfrom sklearn.utils import shuffle\nimport tensorflow.keras as keras\nimport tensorflow.keras.backend as K\nfrom tensorflow.keras.callbacks import Callback, ModelCheckpoint\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.models import Model, load_model\nfrom tensorflow.keras.utils import Sequence\nfrom tensorflow.keras.losses import binary_crossentropy\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.metrics import AUC, Recall, Precision, BinaryCrossentropy\nfrom tensorflow.keras.applications.densenet import DenseNet121\nfrom tensorflow.keras.layers import *\nfrom sklearn.utils.class_weight import compute_class_weight\n\nfrom prettytable import PrettyTable\nfrom sklearn.metrics import roc_auc_score, accuracy_score, precision_score, recall_score, f1_score\nfrom sklearn.metrics import precision_recall_curve\nfrom sklearn.metrics import average_precision_score\nfrom sklearn.metrics import roc_curve, auc, roc_auc_score\nfrom sklearn.metrics import multilabel_confusion_matrix\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nimport seaborn as sns\n\n\n\ndef correct_dcm(dcm):\n    x = dcm.pixel_array + 1000\n    px_mode = 4096\n    x[x>=px_mode] = x[x>=px_mode] - px_mode\n    dcm.PixelData = x.tobytes()\n    dcm.RescaleIntercept = -1000\n\ndef window_image(dcm, window_center, window_width):    \n    if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n        correct_dcm(dcm)\n    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept\n    img = cv2.resize(img, SHAPE[:2], interpolation = cv2.INTER_LINEAR)\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    img = np.clip(img, img_min, img_max)\n    return img\n\ndef bsb_window(dcm):\n    brain_img = window_image(dcm, 40, 80)\n    subdural_img = window_image(dcm, 80, 200)\n    brain_img = (brain_img - 0) / 80\n    subdural_img = (subdural_img - (-20)) / 200\n    soft_img = window_image(dcm, 40, 380)\n    soft_img = (soft_img - (-150)) / 380\n    bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)\n    return bsb_img\n\n\n\n\n\ndef calculating_class_weights(y_true):    #  input : true labels  \n                                        # output : weights of each class\n    number_dim = np.shape(y_true)[1]\n    weights = np.empty([number_dim, 2])\n    for i in range(number_dim):\n        weights[i] = compute_class_weight('balanced', classes=np.unique(y_true[:, i]), y=y_true[:, i])\n    return weights\n\n\ndef _read(path, SHAPE):        # input : path of specific image and the shape that we want convert image to.\n                              # output : readed image/255\n    img = cv2.imread(path)\n    img = cv2.resize(img, dsize=(256, 256))\n    return img/255.0\n\ndef _read_dicom(path, SHAPE):        # input : the path of dicom image and its shape.\n                                  # output : image in numpy format.\n    dcm = pydicom.dcmread(path)\n    try:\n        img = bsb_window(dcm)\n    except:\n        img = np.zeros(SHAPE)\n    return img\n\n# Image Augmentation\nsometimes = lambda aug: iaa.Sometimes(0.25, aug)\n\naugmentation = iaa.Sequential([ iaa.Fliplr(0.25),\n                                iaa.Flipud(0.10),\n                                sometimes(iaa.Crop(px=(0, 25), keep_size = True, sample_independently = False))   \n                            ], random_order = True)       \n        \n# Generators\nclass TrainDataGenerator(keras.utils.Sequence):\n    def __init__(self, dataset, class_names, batch_size = 16, img_size = (256, 256, 3), \n                 augment = False, shuffle = True, *args, **kwargs):            # initialize datagenerator\n        self.dataset = dataset\n        self.ids = self.dataset['imgfile'].values\n        self.labels = self.dataset[class_names].values\n        self.batch_size = batch_size\n        self.img_size = img_size\n        self.augment = augment\n        self.shuffle = shuffle\n        self.on_epoch_end()\n\n    def __len__(self):        # size of datagenerator (number of batchs)\n        return int(ceil(len(self.ids) / self.batch_size))\n\n    def __getitem__(self, index):          # input : index of a batch\n                                    # output : specific batch with input index\n        indices = self.indices[index*self.batch_size:(index+1)*self.batch_size]\n        X, Y = self.__data_generation(indices)\n        return X, Y\n\n    def augmentor(self, image):        # input : image\n                                 # output : augmented image\n        augment_img = augmentation        \n        image_aug = augment_img.augment_image(image)\n        return image_aug\n\n    def on_epoch_end(self):\n        self.indices = np.arange(len(self.ids))\n        if self.shuffle:\n            np.random.shuffle(self.indices)\n\n    def __data_generation(self, indices):        # creating augmented images and their labels.\n                                          # input : /////////////////////////////////////\n                                          # output : augmented images and their labels.\n        X = np.empty((self.batch_size, *self.img_size))\n        Y = np.empty((self.batch_size, len(class_names)), dtype=np.float32)\n        \n        for i, index in enumerate(indices):\n            ID = self.ids[index]\n#             image = _read(ID, self.img_size)\n            if '.png' not in ID:\n                image = _read_dicom('../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/'+ID+'.dcm', self.img_size)\n            else:\n## just CQ500\n\n                if 'NonHemo' in ID:\n                        image = _read('../input/cq500-normal-images-and-labels/'+ID, self.img_size)\n                else:\n                        if 'content' in ID:\n                            ID = ID[9:]\n                        \n                        image = _read('../input/rsna-cq500-abnormal-data/'+ID, self.img_size)\n                        \n            if self.augment:\n                X[i,] = self.augmentor(image)\n            else:\n                X[i,] = image\n            Y[i,] = self.labels[index]        \n        return X, Y\n\ndef ModelCheckpointFull(model_name):        # save weights of the model that has the best result.\n    return ModelCheckpoint(model_name, \n                            monitor = 'val_loss', \n                            verbose = 1, \n                            save_best_only = True, \n                            save_weights_only = True, \n                            mode = 'min', \n                            period = 1)\n\n# Create Model\ndef create_model(num_classes):        # input : num of classes\n                               # output : pretrained densenet121 to classify num_classes classes.\n    K.clear_session()\n    \n    input_shape = (256, 256, 3)\n    img_input = Input(shape=input_shape)\n    base_model = DenseNet121(\n        include_top=False,\n        input_tensor=img_input,\n        input_shape=input_shape,\n        weights='imagenet',\n        pooling=\"avg\"\n    )\n    x = base_model.output\n    x = Dropout(0.15)(x)\n    predictions = Dense(num_classes, activation='sigmoid', name=\"new_predictions\")(x)\n    model = Model(inputs=img_input, outputs=predictions)\n\n    return model\n\ndef metrics_define(num_classes):\n    metrics_all = ['accuracy',\n    AUC(curve='PR',multi_label=True,name='auc_pr'),\n    AUC(multi_label=True, name='auc_roc'),\n    Recall(),\n    Precision(),\n    BinaryCrossentropy(name='bi_crent')\n    ]\n\n    return metrics_all\n\ndef get_weighted_loss(weights):        # input : weights of classes\n                                   # output : loss (weighted loss)\n    def weighted_loss(y_true, y_pred):\n        return K.mean((weights[:,0]**(1-y_true))*(weights[:,1]**(y_true))*K.binary_crossentropy(y_true, y_pred), axis=-1)\n    return weighted_loss\n\n\ndef print_metrics_table(y_true, y_hat, y_pred, class_names):         # input : true labels, predicted labels, array of class names, y hat \n                                                             # output : a table of results (roc_auc_score, precision_score, f1_score, recall_xcore, accuracy_score)\n    myTable = PrettyTable([\"Class Name\", \"ROC_AUC\", \"Precsion\", \"Recall\", \"F1_Score\", \"Accuracy\"])\n\n    for i in range(len(class_names)) :\n        \n        myTable.add_row([class_names[i], \"%.4f\" % roc_auc_score(y_true[:, i], y_hat[:, i]),\n                        \"%.4f\" % precision_score(y_true[:, i], y_pred[:, i]), \"%.4f\" % recall_score(y_true[:, i], y_pred[:, i]),\n                        \"%.4f\" % f1_score(y_true[:, i], y_pred[:, i]), \"%.4f\" % accuracy_score(y_true[:, i], y_pred[:, i])\n                        ])\n\n    myTable.add_row(['Average', \"%.4f\" % roc_auc_score(y_true, y_hat),\n                    \"%.4f\" % precision_score(y_true, y_pred, average='macro'), \"%.4f\" % recall_score(y_true, y_pred, average='macro'),\n                    \"%.4f\" % f1_score(y_true, y_pred, average='macro'), \"%.4f\" % accuracy_score(y_true, y_pred)\n                    ])\n    print(myTable)\n\ndef print_precision_recall_curves(y_true, y_hat, y_pred, class_names):   # input : true labels, predicted labels, array of class names, y hat \n                                                                        # print precision and recall curves\n        # For each class\n    precision = dict()\n    recall = dict()\n    average_precision = dict()\n    for i in range(len(class_names)):\n        precision[i], recall[i], _ = precision_recall_curve(y_true[:, i],\n                                                            y_hat[:, i])\n        average_precision[i] = average_precision_score(y_true[:, i], y_hat[:, i])\n\n    # A \"micro-average\": quantifying score on all classes jointly\n    precision[\"micro\"], recall[\"micro\"], _ = precision_recall_curve(y_true.ravel(),\n        y_hat.ravel())\n    average_precision[\"micro\"] = average_precision_score(y_true, y_hat,\n                                                        average=\"micro\")\n    print('Average precision score, micro-averaged over all classes: {0:0.2f}'\n        .format(average_precision[\"micro\"]))\n    plt.figure()\n    plt.step(recall['micro'], precision['micro'], where='post')\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.xlabel('Recall')\n    plt.ylabel('Precision')\n    plt.ylim([0.0, 1.05])\n    plt.xlim([0.0, 1.0])\n    plt.title(\n        'Average precision score, micro-averaged over all classes: AP={0:0.2f}'\n        .format(average_precision[\"micro\"]))\n    plt.show()\n    for i in range(len(class_names)):\n        plt.figure()\n        plt.plot(recall[i], precision[i], label='Precision-recall for class {0} (area = {1:0.2f})'.format(i, average_precision[i]))\n        plt.plot([0, 1], [0, 1], 'k--')\n        plt.xlim([0.0, 1.0])\n        plt.ylim([0.0, 1.05])\n        plt.xlabel('Recall')\n        plt.ylabel('Precision')\n        plt.title('Precision-Recall Curve for Class {}'.format(class_names[i]))\n        plt.legend(loc=\"lower right\")\n        plt.show()\n\ndef print_auc_curves(y_true, y_hat, y_pred, class_names):    # input : true labels, predicted labels, array of class names, y hat \n                                                           # print auc curves\n    fpr = dict()\n    tpr = dict()\n    roc_auc = dict()\n    roc_auc_sc = dict()\n    for i in range(len(class_names)):\n        fpr[i], tpr[i], _ = roc_curve(y_true[:, i], y_hat[:, i])\n        roc_auc[i] = auc(fpr[i], tpr[i])\n        roc_auc_sc[i] = roc_auc_score(y_true[:, i], y_hat[:, i])\n\n    # Compute micro-average ROC curve and ROC area\n    fpr[\"micro\"], tpr[\"micro\"], _ = roc_curve(y_true.ravel(), y_hat.ravel())\n    roc_auc[\"micro\"] = auc(fpr[\"micro\"], tpr[\"micro\"])\n    for i in range(len(class_names)):\n        plt.figure()\n        plt.plot(fpr[i], tpr[i], label='ROC curve (area = %0.2f)' % roc_auc[i])\n        plt.plot([0, 1], [0, 1], 'k--')\n        plt.xlim([0.0, 1.0])\n        plt.ylim([0.0, 1.05])\n        plt.xlabel('False Positive Rate')\n        plt.ylabel('True Positive Rate')\n        plt.title('Receiver operating characteristic for class {}'.format(class_names[i]))\n        plt.legend(loc=\"lower right\")\n        plt.show()\n\ndef print_confusion_matrix(y_true, y_hat, y_pred, class_names):        # input : true labels, predicted labels, array of class names, y hat \n                                                                # print confusion matrixes.\n    print(multilabel_confusion_matrix(y_true, y_pred))\n\ndef print_on_vs_all_cmatrix(y_true, y_hat, y_pred, class_names):    # input : true labels, predicted labels, array of class names, y hat\n                                                                  # print confusion matrix plots.\n    confusion = multilabel_confusion_matrix(y_true, y_pred)\n\n    # Plot confusion matrix \n    fig = plt.figure(figsize = (14, 8))\n    for i, (label, matrix) in enumerate(zip(class_names[0:6], confusion[0:6])):\n        plt.subplot(f'23{i+1}')\n        labels = [f'not_{label}', label]\n        cm = matrix.astype('float') / matrix.sum(axis=1)[:, np.newaxis]\n        sns.heatmap(cm, annot = True, square = True, cbar = False, cmap = 'Blues', \n                    xticklabels = labels, yticklabels = labels, linecolor = 'black', linewidth = 1)\n        plt.title(labels[0])\n\n    plt.tight_layout()\n    plt.show()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-14T17:38:30.497109Z","iopub.execute_input":"2021-11-14T17:38:30.49738Z","iopub.status.idle":"2021-11-14T17:38:30.559303Z","shell.execute_reply.started":"2021-11-14T17:38:30.49735Z","shell.execute_reply":"2021-11-14T17:38:30.558435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"creating train and validation dataframes on fold 4","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/kfold-splits-rsna-cq500/Train_f4.csv')\nval_df = pd.read_csv('../input/kfold-splits-rsna-cq500/Validation_f4.csv')\ntrain_df = pd.DataFrame( train_df.loc[i][2:] for i in range(0, len(train_df)) if 'CQ500' in train_df.loc[i]['imgfile']  )    # just for CQ500 images.\nval_df = pd.DataFrame( val_df.loc[i][2:] for i in range(0, len(val_df)) if 'CQ500' in val_df.loc[i]['imgfile']  ) \n","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:03:02.357682Z","iopub.execute_input":"2021-11-14T16:03:02.358166Z","iopub.status.idle":"2021-11-14T16:03:24.025473Z","shell.execute_reply.started":"2021-11-14T16:03:02.358128Z","shell.execute_reply":"2021-11-14T16:03:24.02468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:03:24.026831Z","iopub.execute_input":"2021-11-14T16:03:24.027104Z","iopub.status.idle":"2021-11-14T16:03:24.046348Z","shell.execute_reply.started":"2021-11-14T16:03:24.02707Z","shell.execute_reply":"2021-11-14T16:03:24.045232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:03:24.04928Z","iopub.execute_input":"2021-11-14T16:03:24.049545Z","iopub.status.idle":"2021-11-14T16:03:24.061167Z","shell.execute_reply.started":"2021-11-14T16:03:24.049504Z","shell.execute_reply":"2021-11-14T16:03:24.060232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['normal']=0   # add normal label to dataframes\nval_df['normal']=0","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:03:24.06284Z","iopub.execute_input":"2021-11-14T16:03:24.063252Z","iopub.status.idle":"2021-11-14T16:03:24.070574Z","shell.execute_reply.started":"2021-11-14T16:03:24.063217Z","shell.execute_reply":"2021-11-14T16:03:24.069699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:03:24.072522Z","iopub.execute_input":"2021-11-14T16:03:24.072867Z","iopub.status.idle":"2021-11-14T16:03:24.087186Z","shell.execute_reply.started":"2021-11-14T16:03:24.072832Z","shell.execute_reply":"2021-11-14T16:03:24.086495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"make dataframes for normal images.","metadata":{}},{"cell_type":"code","source":"\ntrain_df1 = pd.read_csv('../input/cq500-normal-images-and-labels/NormalAbnormal/NormalAbnormal/CQ500/Train_f1.csv')\ntrain_df1 = pd.DataFrame( train_df1.loc[i][1:] for i in range(0, len(train_df1))   ) \nabnormal1 = train_df1.loc[train_df1['Abnormal']==1]\n\ntrain_df1 = shuffle(train_df1)\nval_df1 = pd.read_csv('../input/cq500-normal-images-and-labels/NormalAbnormal/NormalAbnormal/CQ500/Validation_f1.csv')\nval_df1 = pd.DataFrame( val_df1.loc[i][1:] for i in range(0, len(val_df1))   ) ","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:03:24.08833Z","iopub.execute_input":"2021-11-14T16:03:24.08859Z","iopub.status.idle":"2021-11-14T16:04:10.459294Z","shell.execute_reply.started":"2021-11-14T16:03:24.088556Z","shell.execute_reply":"2021-11-14T16:04:10.458378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df1","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:10.460812Z","iopub.execute_input":"2021-11-14T16:04:10.461113Z","iopub.status.idle":"2021-11-14T16:04:10.475386Z","shell.execute_reply.started":"2021-11-14T16:04:10.461077Z","shell.execute_reply":"2021-11-14T16:04:10.474479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df1","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:10.476666Z","iopub.execute_input":"2021-11-14T16:04:10.477675Z","iopub.status.idle":"2021-11-14T16:04:10.490278Z","shell.execute_reply.started":"2021-11-14T16:04:10.477618Z","shell.execute_reply":"2021-11-14T16:04:10.489542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df2 = pd.DataFrame( train_df1.loc[i][:1] for i in range(0, 8000) if train_df1.loc[i]['Normal'].all()==True  ) \nval_df2= pd.DataFrame( val_df1.loc[i][:1] for i in range(0, 4000) if val_df1.loc[i]['Normal'].all()==True )  ","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:10.491374Z","iopub.execute_input":"2021-11-14T16:04:10.49172Z","iopub.status.idle":"2021-11-14T16:04:15.296491Z","shell.execute_reply.started":"2021-11-14T16:04:10.491684Z","shell.execute_reply":"2021-11-14T16:04:15.295765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df2['epidural']=0      # assign 0 to other classes.\ntrain_df2['intraparenchymal']=0\ntrain_df2['intraventricular']=0\t\ntrain_df2['subarachnoid']=0\t\ntrain_df2['subdural']=0\ntrain_df2['normal']=1\ntrain_df2.rename(columns={'Normal': 'normal'}, inplace=True)\n\n\nval_df2['epidural']=0\nval_df2['intraparenchymal']=0\nval_df2['intraventricular']=0\t\nval_df2['subarachnoid']=0\t\nval_df2['subdural']=0\nval_df2['normal']=1\nval_df2.rename(columns={'Normal': 'normal'}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.304Z","iopub.execute_input":"2021-11-14T16:04:15.304864Z","iopub.status.idle":"2021-11-14T16:04:15.320884Z","shell.execute_reply.started":"2021-11-14T16:04:15.304813Z","shell.execute_reply":"2021-11-14T16:04:15.320192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df2","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.325306Z","iopub.execute_input":"2021-11-14T16:04:15.325513Z","iopub.status.idle":"2021-11-14T16:04:15.341374Z","shell.execute_reply.started":"2021-11-14T16:04:15.32549Z","shell.execute_reply":"2021-11-14T16:04:15.340576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"concate 2 dataframes and creating a dataframe for all classes.","metadata":{}},{"cell_type":"code","source":"frames = [train_df, train_df2]\ntrain_df = pd.concat(frames, ignore_index=True)\ntrain_df=shuffle(train_df)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.342392Z","iopub.execute_input":"2021-11-14T16:04:15.342665Z","iopub.status.idle":"2021-11-14T16:04:15.353342Z","shell.execute_reply.started":"2021-11-14T16:04:15.342613Z","shell.execute_reply":"2021-11-14T16:04:15.352463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames = [val_df, val_df2]\nval_df = pd.concat(frames, ignore_index=True)\nval_df=shuffle(val_df)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.354847Z","iopub.execute_input":"2021-11-14T16:04:15.355129Z","iopub.status.idle":"2021-11-14T16:04:15.364322Z","shell.execute_reply.started":"2021-11-14T16:04:15.355094Z","shell.execute_reply":"2021-11-14T16:04:15.363511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.365913Z","iopub.execute_input":"2021-11-14T16:04:15.366234Z","iopub.status.idle":"2021-11-14T16:04:15.380661Z","shell.execute_reply.started":"2021-11-14T16:04:15.366195Z","shell.execute_reply":"2021-11-14T16:04:15.380002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.381671Z","iopub.execute_input":"2021-11-14T16:04:15.382355Z","iopub.status.idle":"2021-11-14T16:04:15.396715Z","shell.execute_reply.started":"2021-11-14T16:04:15.382319Z","shell.execute_reply":"2021-11-14T16:04:15.395964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"creating data generator for training the model.","metadata":{}},{"cell_type":"code","source":"\n\nHEIGHT = 256\nWIDTH = 256\nCHANNELS = 3\nTRAIN_BATCH_SIZE = 32\nVALID_BATCH_SIZE = 64\nSHAPE = (HEIGHT, WIDTH, CHANNELS)\n\nclass_names = ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural','normal']\n\nweights = calculating_class_weights((train_df[class_names].values).astype(np.float32))\nprint(weights)\n\ndata_generator_train = TrainDataGenerator(train_df,\n                                          class_names,\n                                          TRAIN_BATCH_SIZE,\n                                          SHAPE,\n                                          augment = True,\n                                          shuffle = True)\ndata_generator_val = TrainDataGenerator(val_df,\n                                        class_names, \n                                        VALID_BATCH_SIZE, \n                                        SHAPE,\n                                        augment = False,\n                                        shuffle = True\n                                        )\n\nTRAIN_STEPS = int(len(data_generator_train)/10)\nprint(TRAIN_STEPS)\nVal_STEPS = int(len(data_generator_val)/10)\nprint(Val_STEPS)\nLR = 5e-6\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.397948Z","iopub.execute_input":"2021-11-14T16:04:15.398271Z","iopub.status.idle":"2021-11-14T16:04:15.461303Z","shell.execute_reply.started":"2021-11-14T16:04:15.398237Z","shell.execute_reply":"2021-11-14T16:04:15.460406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Model","metadata":{}},{"cell_type":"code","source":"plt.imshow(data_generator_val[1][0][0])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.462678Z","iopub.execute_input":"2021-11-14T16:04:15.462925Z","iopub.status.idle":"2021-11-14T16:04:15.873678Z","shell.execute_reply.started":"2021-11-14T16:04:15.462893Z","shell.execute_reply":"2021-11-14T16:04:15.872993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Metrics = metrics_define(len(class_names))\n\nmodel = create_model(len(class_names))   \n\n\nfrom keras.models import Model\n\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:15.87509Z","iopub.execute_input":"2021-11-14T16:04:15.875354Z","iopub.status.idle":"2021-11-14T16:04:19.574139Z","shell.execute_reply.started":"2021-11-14T16:04:15.875319Z","shell.execute_reply":"2021-11-14T16:04:19.573324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel.compile(optimizer = Adam(learning_rate = LR),\n              loss = get_weighted_loss(weights),\n              metrics = Metrics)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:19.575633Z","iopub.execute_input":"2021-11-14T16:04:19.576008Z","iopub.status.idle":"2021-11-14T16:04:19.597798Z","shell.execute_reply.started":"2021-11-14T16:04:19.575964Z","shell.execute_reply":"2021-11-14T16:04:19.597021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"fit model","metadata":{}},{"cell_type":"code","source":"history = model.fit(data_generator_train,\n                    validation_data = data_generator_val,\n                    validation_steps = Val_STEPS,\n                    steps_per_epoch = TRAIN_STEPS,\n                    epochs = 100,\n                    callbacks = [ModelCheckpointFull('model_all_densenet_fold4_andNormal.h5')],\n                    verbose = 1, workers=4\n                    )","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:04:19.598961Z","iopub.execute_input":"2021-11-14T16:04:19.599299Z","iopub.status.idle":"2021-11-14T16:50:51.976069Z","shell.execute_reply.started":"2021-11-14T16:04:19.599261Z","shell.execute_reply":"2021-11-14T16:50:51.975322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"results","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:50:51.977607Z","iopub.execute_input":"2021-11-14T16:50:51.977966Z","iopub.status.idle":"2021-11-14T16:50:52.20032Z","shell.execute_reply.started":"2021-11-14T16:50:51.977925Z","shell.execute_reply":"2021-11-14T16:50:52.199492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['auc_pr'])\nplt.plot(history.history['val_auc_pr'])\nplt.title('model auc_precision')\nplt.ylabel('auc_pr')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:50:52.201722Z","iopub.execute_input":"2021-11-14T16:50:52.201986Z","iopub.status.idle":"2021-11-14T16:50:52.418171Z","shell.execute_reply.started":"2021-11-14T16:50:52.201949Z","shell.execute_reply":"2021-11-14T16:50:52.417467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"validate the model","metadata":{}},{"cell_type":"code","source":"model.load_weights('model_all_densenet_fold4_andNormal.h5')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:50:52.41944Z","iopub.execute_input":"2021-11-14T16:50:52.420786Z","iopub.status.idle":"2021-11-14T16:50:52.941426Z","shell.execute_reply.started":"2021-11-14T16:50:52.420746Z","shell.execute_reply":"2021-11-14T16:50:52.940689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nprint(len(val_df))\n\nHEIGHT = 256\nWIDTH = 256\nCHANNELS = 3\nVALID_BATCH_SIZE = 64\nSHAPE = (HEIGHT, WIDTH, CHANNELS)\n\nclass_names = ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'normal']\ndata_generator_test = TrainDataGenerator(val_df,\n                                        class_names, \n                                        VALID_BATCH_SIZE, \n                                        SHAPE,\n                                        augment = False,\n                                        shuffle = False\n                                        )\n\ny_true = val_df[class_names].values\ny_hat = model.predict(data_generator_test, verbose=1)\n\ny_hat = y_hat[0:len(y_true)]\ny_pred = np.round(y_hat)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:50:52.942836Z","iopub.execute_input":"2021-11-14T16:50:52.943091Z","iopub.status.idle":"2021-11-14T16:51:23.358163Z","shell.execute_reply.started":"2021-11-14T16:50:52.943055Z","shell.execute_reply":"2021-11-14T16:51:23.357252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:23.359687Z","iopub.execute_input":"2021-11-14T16:51:23.359954Z","iopub.status.idle":"2021-11-14T16:51:23.376132Z","shell.execute_reply.started":"2021-11-14T16:51:23.359918Z","shell.execute_reply":"2021-11-14T16:51:23.375384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_metrics_table(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:23.377631Z","iopub.execute_input":"2021-11-14T16:51:23.378176Z","iopub.status.idle":"2021-11-14T16:51:23.509059Z","shell.execute_reply.started":"2021-11-14T16:51:23.378137Z","shell.execute_reply":"2021-11-14T16:51:23.508328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_precision_recall_curves(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:23.510208Z","iopub.execute_input":"2021-11-14T16:51:23.510602Z","iopub.status.idle":"2021-11-14T16:51:25.128108Z","shell.execute_reply.started":"2021-11-14T16:51:23.510565Z","shell.execute_reply":"2021-11-14T16:51:25.127376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_auc_curves(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:25.1296Z","iopub.execute_input":"2021-11-14T16:51:25.129882Z","iopub.status.idle":"2021-11-14T16:51:26.329277Z","shell.execute_reply.started":"2021-11-14T16:51:25.129847Z","shell.execute_reply":"2021-11-14T16:51:26.328522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_confusion_matrix(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:26.330474Z","iopub.execute_input":"2021-11-14T16:51:26.330773Z","iopub.status.idle":"2021-11-14T16:51:26.342133Z","shell.execute_reply.started":"2021-11-14T16:51:26.330735Z","shell.execute_reply":"2021-11-14T16:51:26.341383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_on_vs_all_cmatrix(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:26.343378Z","iopub.execute_input":"2021-11-14T16:51:26.344006Z","iopub.status.idle":"2021-11-14T16:51:27.069149Z","shell.execute_reply.started":"2021-11-14T16:51:26.343968Z","shell.execute_reply":"2021-11-14T16:51:27.06848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"test another dataset","metadata":{}},{"cell_type":"code","source":"val_df2= pd.DataFrame( val_df1.loc[i][:1] for i in range(4000, 8000) if val_df1.loc[i]['Normal'].all()==True )  ","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:27.070583Z","iopub.execute_input":"2021-11-14T16:51:27.071071Z","iopub.status.idle":"2021-11-14T16:51:28.110336Z","shell.execute_reply.started":"2021-11-14T16:51:27.071032Z","shell.execute_reply":"2021-11-14T16:51:28.109544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nval_df2['epidural']=0\nval_df2['intraparenchymal']=0\nval_df2['intraventricular']=0\t\nval_df2['subarachnoid']=0\t\nval_df2['subdural']=0\nval_df2['normal']=1\nval_df2.rename(columns={'Normal': 'normal'}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:28.116218Z","iopub.execute_input":"2021-11-14T16:51:28.118417Z","iopub.status.idle":"2021-11-14T16:51:28.130873Z","shell.execute_reply.started":"2021-11-14T16:51:28.118374Z","shell.execute_reply":"2021-11-14T16:51:28.130112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"val_df = pd.read_csv('../input/kfold-splits-rsna-cq500/CQ500_Validation_f0.csv')\nval_df = pd.DataFrame( val_df.loc[i][1:] for i in range(0, len(val_df)) if 'CQ500' in val_df.loc[i]['imgfile']  ) \n\nframes = [val_df, val_df2]\nval_df = pd.concat(frames, ignore_index=True)\nval_df=shuffle(val_df)\n\nval_df['normal']=0\nprint(len(val_df))\n\nHEIGHT = 256\nWIDTH = 256\nCHANNELS = 3\nVALID_BATCH_SIZE = 64\nSHAPE = (HEIGHT, WIDTH, CHANNELS)\n\nclass_names = ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'normal']\ndata_generator_test = TrainDataGenerator(val_df,\n                                        class_names, \n                                        VALID_BATCH_SIZE, \n                                        SHAPE,\n                                        augment = False,\n                                        shuffle = False\n                                        )\n\n\ny_true = val_df[class_names].values\ny_hat = model.predict(data_generator_test, verbose=1)\n\ny_hat = y_hat[0:len(y_true)]\ny_pred = np.round(y_hat)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:53:34.305584Z","iopub.execute_input":"2021-11-14T16:53:34.306364Z","iopub.status.idle":"2021-11-14T16:53:59.142475Z","shell.execute_reply.started":"2021-11-14T16:53:34.306325Z","shell.execute_reply":"2021-11-14T16:53:59.141756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:28.160991Z","iopub.status.idle":"2021-11-14T16:51:28.163222Z","shell.execute_reply.started":"2021-11-14T16:51:28.16296Z","shell.execute_reply":"2021-11-14T16:51:28.162988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_metrics_table(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:53:59.144029Z","iopub.execute_input":"2021-11-14T16:53:59.144238Z","iopub.status.idle":"2021-11-14T16:53:59.526164Z","shell.execute_reply.started":"2021-11-14T16:53:59.144214Z","shell.execute_reply":"2021-11-14T16:53:59.524953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_precision_recall_curves(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:54:03.836307Z","iopub.execute_input":"2021-11-14T16:54:03.836581Z","iopub.status.idle":"2021-11-14T16:54:05.200945Z","shell.execute_reply.started":"2021-11-14T16:54:03.836543Z","shell.execute_reply":"2021-11-14T16:54:05.20023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_auc_curves(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:54:14.682744Z","iopub.execute_input":"2021-11-14T16:54:14.683517Z","iopub.status.idle":"2021-11-14T16:54:14.74133Z","shell.execute_reply.started":"2021-11-14T16:54:14.683467Z","shell.execute_reply":"2021-11-14T16:54:14.740079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_confusion_matrix(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:54:21.055264Z","iopub.execute_input":"2021-11-14T16:54:21.055531Z","iopub.status.idle":"2021-11-14T16:54:21.066969Z","shell.execute_reply.started":"2021-11-14T16:54:21.055501Z","shell.execute_reply":"2021-11-14T16:54:21.066144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_on_vs_all_cmatrix(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:54:24.719149Z","iopub.execute_input":"2021-11-14T16:54:24.719787Z","iopub.status.idle":"2021-11-14T16:54:25.416246Z","shell.execute_reply.started":"2021-11-14T16:54:24.719749Z","shell.execute_reply":"2021-11-14T16:54:25.415588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"test model on Normal dataset\n","metadata":{}},{"cell_type":"code","source":"val_df = pd.read_csv('../input/cq500-normal-images-and-labels/NormalAbnormal/NormalAbnormal/CQ500/Validation_f3.csv')\n\nval_df = pd.DataFrame( val_df.loc[i][1:2] for i in range(0, len(val_df)) if val_df.loc[i]['Normal'].all()==True)\n                         \nval_df['epidural']=0\nval_df['intraparenchymal']=0\nval_df['intraventricular']=0\t\nval_df['subarachnoid']=0\t\nval_df['subdural']=0\nval_df['normal']=1\n","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:54:32.296904Z","iopub.execute_input":"2021-11-14T16:54:32.29754Z","iopub.status.idle":"2021-11-14T16:54:45.646408Z","shell.execute_reply.started":"2021-11-14T16:54:32.297496Z","shell.execute_reply":"2021-11-14T16:54:45.645613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nprint(len(val_df))\n\nHEIGHT = 256\nWIDTH = 256\nCHANNELS = 3\nVALID_BATCH_SIZE = 64\nSHAPE = (HEIGHT, WIDTH, CHANNELS)\n\nclass_names = ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'normal']\ndata_generator_test = TrainDataGenerator(val_df,\n                                        class_names, \n                                        VALID_BATCH_SIZE, \n                                        SHAPE,\n                                        augment = False,\n                                        shuffle = False\n                                        )\n\n\ny_true = val_df[class_names].values\ny_hat = model.predict(data_generator_test, verbose=1)\n\ny_hat = y_hat[0:len(y_true)]\ny_pred = np.round(y_hat)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:54:48.629972Z","iopub.execute_input":"2021-11-14T16:54:48.630378Z","iopub.status.idle":"2021-11-14T16:59:36.243416Z","shell.execute_reply.started":"2021-11-14T16:54:48.630344Z","shell.execute_reply":"2021-11-14T16:59:36.242691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:59:36.819545Z","iopub.execute_input":"2021-11-14T16:59:36.822163Z","iopub.status.idle":"2021-11-14T16:59:36.844821Z","shell.execute_reply.started":"2021-11-14T16:59:36.822119Z","shell.execute_reply":"2021-11-14T16:59:36.844143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_metrics_table(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:59:50.482812Z","iopub.execute_input":"2021-11-14T16:59:50.483082Z","iopub.status.idle":"2021-11-14T16:59:50.526757Z","shell.execute_reply.started":"2021-11-14T16:59:50.483053Z","shell.execute_reply":"2021-11-14T16:59:50.52542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_precision_recall_curves(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:59:54.163503Z","iopub.execute_input":"2021-11-14T16:59:54.16385Z","iopub.status.idle":"2021-11-14T16:59:55.647441Z","shell.execute_reply.started":"2021-11-14T16:59:54.163819Z","shell.execute_reply":"2021-11-14T16:59:55.646719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_auc_curves(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:00:00.126028Z","iopub.execute_input":"2021-11-14T17:00:00.126691Z","iopub.status.idle":"2021-11-14T17:00:00.174532Z","shell.execute_reply.started":"2021-11-14T17:00:00.126628Z","shell.execute_reply":"2021-11-14T17:00:00.173403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_confusion_matrix(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:00:04.400471Z","iopub.execute_input":"2021-11-14T17:00:04.401039Z","iopub.status.idle":"2021-11-14T17:00:04.421515Z","shell.execute_reply.started":"2021-11-14T17:00:04.400994Z","shell.execute_reply":"2021-11-14T17:00:04.420792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_on_vs_all_cmatrix(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:00:08.342327Z","iopub.execute_input":"2021-11-14T17:00:08.342818Z","iopub.status.idle":"2021-11-14T17:00:09.023629Z","shell.execute_reply.started":"2021-11-14T17:00:08.342781Z","shell.execute_reply":"2021-11-14T17:00:09.022953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Grad Cam","metadata":{}},{"cell_type":"code","source":"model = create_model(6)\nmodel.load_weights('./model_all_densenet_fold4_andNormal.h5')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:00:36.682052Z","iopub.execute_input":"2021-11-14T17:00:36.68238Z","iopub.status.idle":"2021-11-14T17:00:40.112752Z","shell.execute_reply.started":"2021-11-14T17:00:36.682346Z","shell.execute_reply":"2021-11-14T17:00:40.111849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:00:41.706845Z","iopub.execute_input":"2021-11-14T17:00:41.707381Z","iopub.status.idle":"2021-11-14T17:00:41.918292Z","shell.execute_reply.started":"2021-11-14T17:00:41.707342Z","shell.execute_reply":"2021-11-14T17:00:41.917447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\n\nfrom IPython.display import Image, display\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:00:53.096308Z","iopub.execute_input":"2021-11-14T17:00:53.097227Z","iopub.status.idle":"2021-11-14T17:00:53.102322Z","shell.execute_reply.started":"2021-11-14T17:00:53.097176Z","shell.execute_reply":"2021-11-14T17:00:53.101166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_builder = model\nimg_size = (256, 256)\n\ndecode_predictions = keras.applications.mobilenet.decode_predictions\n\nlast_conv_layer_name = \"conv5_block16_concat\"","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:00:56.048741Z","iopub.execute_input":"2021-11-14T17:00:56.049491Z","iopub.status.idle":"2021-11-14T17:00:56.05419Z","shell.execute_reply.started":"2021-11-14T17:00:56.049451Z","shell.execute_reply":"2021-11-14T17:00:56.053261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_array(img_path, size):\n    \n    img = keras.preprocessing.image.load_img(img_path, target_size=size)\n    \n    array = keras.preprocessing.image.img_to_array(img)\n    \n    array = np.expand_dims(array, axis=0)\n    return array\n\n\ndef make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):\n    \n    grad_model = tf.keras.models.Model(\n        [model.inputs], [model.get_layer(last_conv_layer_name).output, model.output]\n    )\n\n    with tf.GradientTape() as tape:\n        last_conv_layer_output, preds = grad_model(img_array)\n        if pred_index is None:\n            pred_index = tf.argmax(preds[0])\n        class_channel = preds[:, pred_index]\n\n    \n    grads = tape.gradient(class_channel, last_conv_layer_output)\n\n    \n    pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))\n\n    \n    last_conv_layer_output = last_conv_layer_output[0]\n    heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]\n    heatmap = tf.squeeze(heatmap)\n\n   \n    heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)\n    return heatmap.numpy()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:01:00.529749Z","iopub.execute_input":"2021-11-14T17:01:00.530329Z","iopub.status.idle":"2021-11-14T17:01:00.540471Z","shell.execute_reply.started":"2021-11-14T17:01:00.53029Z","shell.execute_reply":"2021-11-14T17:01:00.539373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:01:07.079868Z","iopub.execute_input":"2021-11-14T17:01:07.080139Z","iopub.status.idle":"2021-11-14T17:01:07.089111Z","shell.execute_reply.started":"2021-11-14T17:01:07.080112Z","shell.execute_reply":"2021-11-14T17:01:07.088287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df = pd.read_csv('../input/kfold-splits-rsna-cq500/Validation_f4.csv')\nval_df = pd.DataFrame( val_df.loc[i][2:] for i in range(0, len(val_df)) if 'CQ500' in val_df.loc[i]['imgfile']  ) \nval_df['normal']=0\n\nval_df2= pd.DataFrame( val_df1.loc[i][:1] for i in range(0, 4000) if val_df1.loc[i]['Normal'].all()==True )\nval_df2['epidural']=0\nval_df2['intraparenchymal']=0\nval_df2['intraventricular']=0\t\nval_df2['subarachnoid']=0\t\nval_df2['subdural']=0\nval_df2['normal']=1\nval_df2.rename(columns={'Normal': 'normal'}, inplace=True)\n\nframes = [val_df, val_df2]\nval_df = pd.concat(frames, ignore_index=True)\nval_df=shuffle(val_df)\n\nval_df\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:07:24.590803Z","iopub.execute_input":"2021-11-14T17:07:24.591411Z","iopub.status.idle":"2021-11-14T17:07:30.428361Z","shell.execute_reply.started":"2021-11-14T17:07:24.591374Z","shell.execute_reply":"2021-11-14T17:07:30.427526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_generator_test = TrainDataGenerator(val_df,\n                                        class_names, \n                                        VALID_BATCH_SIZE, \n                                        SHAPE,\n                                        augment = False,\n                                        shuffle = False\n                                        )","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:08:03.252092Z","iopub.execute_input":"2021-11-14T17:08:03.252375Z","iopub.status.idle":"2021-11-14T17:08:03.257916Z","shell.execute_reply.started":"2021-11-14T17:08:03.252346Z","shell.execute_reply":"2021-11-14T17:08:03.257156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data_generator_test[1][0][0])\nplt.savefig( \"ok.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:08:07.748256Z","iopub.execute_input":"2021-11-14T17:08:07.748843Z","iopub.status.idle":"2021-11-14T17:08:08.177656Z","shell.execute_reply.started":"2021-11-14T17:08:07.748803Z","shell.execute_reply":"2021-11-14T17:08:08.176976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_array= data_generator_test[1][0][0]\nimg_array=img_array.reshape(1,256,256,3)\nnp.shape(img_array)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:08:11.807189Z","iopub.execute_input":"2021-11-14T17:08:11.807877Z","iopub.status.idle":"2021-11-14T17:08:11.980209Z","shell.execute_reply.started":"2021-11-14T17:08:11.807837Z","shell.execute_reply":"2021-11-14T17:08:11.979462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:28.213238Z","iopub.status.idle":"2021-11-14T16:51:28.213937Z","shell.execute_reply.started":"2021-11-14T16:51:28.213684Z","shell.execute_reply":"2021-11-14T16:51:28.21371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(img_array)\n\nheatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name)\n\n# Display heatmap\nplt.matshow(heatmap)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:08:19.833354Z","iopub.execute_input":"2021-11-14T17:08:19.834081Z","iopub.status.idle":"2021-11-14T17:08:22.336306Z","shell.execute_reply.started":"2021-11-14T17:08:19.834042Z","shell.execute_reply":"2021-11-14T17:08:22.335398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_and_display_gradcam(img, heatmap, cam_path=\"cam.jpg\", alpha=0.4):\n \n    heatmap = np.uint8(255 * heatmap)\n\n    # Use jet colormap to colorize heatmap\n    jet = cm.get_cmap(\"jet\")\n\n    # Use RGB values of the colormap\n    jet_colors = jet(np.arange(256))[:, :3]\n    jet_heatmap = jet_colors[heatmap]\n\n    # Create an image with RGB colorized heatmap\n    jet_heatmap = keras.preprocessing.image.array_to_img(jet_heatmap)\n    jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))\n    jet_heatmap = keras.preprocessing.image.img_to_array(jet_heatmap)\n\n    # Superimpose the heatmap on original image\n    superimposed_img = jet_heatmap * alpha + img\n    superimposed_img = keras.preprocessing.image.array_to_img(superimposed_img)\n\n    # Save the superimposed image\n    superimposed_img.save(cam_path)\n\n    # Display Grad CAM\n    display(Image(cam_path))\n\n\nsave_and_display_gradcam(img_array[0]*255, heatmap)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:08:25.846904Z","iopub.execute_input":"2021-11-14T17:08:25.847686Z","iopub.status.idle":"2021-11-14T17:08:25.864084Z","shell.execute_reply.started":"2021-11-14T17:08:25.847627Z","shell.execute_reply":"2021-11-14T17:08:25.863069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data_generator_test[1][0][1])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:08:30.161727Z","iopub.execute_input":"2021-11-14T17:08:30.162552Z","iopub.status.idle":"2021-11-14T17:08:30.542167Z","shell.execute_reply.started":"2021-11-14T17:08:30.1625Z","shell.execute_reply":"2021-11-14T17:08:30.541485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir 0\n!mkdir 1\n!mkdir 2\n!mkdir 3\n!mkdir 4\n!mkdir 5","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:08:34.213743Z","iopub.execute_input":"2021-11-14T17:08:34.214297Z","iopub.status.idle":"2021-11-14T17:08:38.579151Z","shell.execute_reply.started":"2021-11-14T17:08:34.214258Z","shell.execute_reply":"2021-11-14T17:08:38.578104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r 0\n!rm -r 1\n!rm -r 2\n!rm -r 3\n!rm -r 4\n!rm -r 5","metadata":{"execution":{"iopub.status.busy":"2021-11-14T16:51:28.223147Z","iopub.status.idle":"2021-11-14T16:51:28.223836Z","shell.execute_reply.started":"2021-11-14T16:51:28.223577Z","shell.execute_reply":"2021-11-14T16:51:28.223603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nh=0\nfor i in range(0,10):\n    for j in range (0,64):\n        x = data_generator_test[i][0][j]\n        x = x.reshape(1,256,256,3)\n        np.shape(x)\n        \n        h+=1\n        preds = model.predict(x)\n        heatmap = make_gradcam_heatmap(x, model, last_conv_layer_name)\n        p=os.path.join(\"./\", str(np.argmax(preds)))\n        if len(os.listdir(p)) < 21: \n            save_and_display_gradcam(x[0]*255, heatmap, str(np.argmax(preds))+\"/\"+\"cam_\"+str(len(os.listdir(p)))+\".jpg\")\n            print(\"--- \"+str(np.argmax(preds))+\" :  \"+ str(len(os.listdir(p))))\n            ","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:08:41.866062Z","iopub.execute_input":"2021-11-14T17:08:41.866351Z","iopub.status.idle":"2021-11-14T17:13:37.796007Z","shell.execute_reply.started":"2021-11-14T17:08:41.86632Z","shell.execute_reply":"2021-11-14T17:13:37.795147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nw = 256\nh = 256\nfig = plt.figure(figsize=(9, 13))\ncolumns = 3\nrows = 5\n\n# prep (x,y) for extra plotting\nxs = np.linspace(0, 2*np.pi, 60)  # from 0 to 2pi\nys = np.abs(np.sin(xs))           # absolute of sine\n\n# ax enables access to manipulate each of subplots\nax = []\np=os.path.join(\"./\",\"0\",)\nfor i in range(columns*rows):\n\n#     img = np.random.randint(10, size=(h,w))\n    img = cv2.imread(p+'/'+'cam_'+str(i)+'.jpg')\n\n    \n    # create subplot and append to ax\n    ax.append( fig.add_subplot(rows, columns, i+1) )\n    ax[-1].set_title(\"prediction : 0\")  # set title\n    plt.imshow(img, )\n\n# do extra plots on selected axes/subplots\n# note: index starts with 0\nax[2].plot(xs, 3*ys)\nax[19].plot(ys**2, xs)\n\nplt.show()  # finally, render the plot","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:13:37.797593Z","iopub.execute_input":"2021-11-14T17:13:37.798038Z","iopub.status.idle":"2021-11-14T17:13:39.840108Z","shell.execute_reply.started":"2021-11-14T17:13:37.797991Z","shell.execute_reply":"2021-11-14T17:13:39.83766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nw = 256\nh = 256\nfig = plt.figure(figsize=(9, 13))\ncolumns = 4\nrows = 5\n\n# prep (x,y) for extra plotting\nxs = np.linspace(0, 2*np.pi, 60)  # from 0 to 2pi\nys = np.abs(np.sin(xs))           # absolute of sine\n\n# ax enables access to manipulate each of subplots\nax = []\np=os.path.join(\"./\",\"1\",)\nfor i in range(columns*rows):\n\n#     img = np.random.randint(10, size=(h,w))\n    img = cv2.imread(p+'/'+'cam_'+str(i)+'.jpg')\n\n    \n    # create subplot and append to ax\n    ax.append( fig.add_subplot(rows, columns, i+1) )\n    ax[-1].set_title(\"prediction : 1\")  # set title\n    plt.imshow(img, )\n\n# do extra plots on selected axes/subplots\n# note: index starts with 0\nax[2].plot(xs, 3*ys)\nax[19].plot(ys**2, xs)\n\nplt.show()  # finally, render the plot","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:14:42.130847Z","iopub.execute_input":"2021-11-14T17:14:42.131198Z","iopub.status.idle":"2021-11-14T17:14:44.447112Z","shell.execute_reply.started":"2021-11-14T17:14:42.131163Z","shell.execute_reply":"2021-11-14T17:14:44.446476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nw = 256\nh = 256\nfig = plt.figure(figsize=(9, 13))\ncolumns = 4\nrows = 5\n\n# prep (x,y) for extra plotting\nxs = np.linspace(0, 2*np.pi, 60)  # from 0 to 2pi\nys = np.abs(np.sin(xs))           # absolute of sine\n\n# ax enables access to manipulate each of subplots\nax = []\np=os.path.join(\"./\",\"2\",)\nfor i in range(columns*rows):\n\n#     img = np.random.randint(10, size=(h,w))\n    img = cv2.imread(p+'/'+'cam_'+str(i)+'.jpg')\n\n    \n    # create subplot and append to ax\n    ax.append( fig.add_subplot(rows, columns, i+1) )\n    ax[-1].set_title(\"prediction : 2\")  # set title\n    plt.imshow(img, )\n\n# do extra plots on selected axes/subplots\n# note: index starts with 0\nax[2].plot(xs, 3*ys)\nax[19].plot(ys**2, xs)\n\nplt.show()  # finally, render the plot","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:14:51.364297Z","iopub.execute_input":"2021-11-14T17:14:51.364581Z","iopub.status.idle":"2021-11-14T17:14:53.21911Z","shell.execute_reply.started":"2021-11-14T17:14:51.364541Z","shell.execute_reply":"2021-11-14T17:14:53.218408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nw = 256\nh = 256\nfig = plt.figure(figsize=(9, 13))\ncolumns = 4\nrows = 5\n\n# prep (x,y) for extra plotting\nxs = np.linspace(0, 2*np.pi, 60)  # from 0 to 2pi\nys = np.abs(np.sin(xs))           # absolute of sine\n\n# ax enables access to manipulate each of subplots\nax = []\np=os.path.join(\"./\",\"3\",)\nfor i in range(columns*rows):\n\n#     img = np.random.randint(10, size=(h,w))\n    img = cv2.imread(p+'/'+'cam_'+str(i)+'.jpg')\n\n    \n    # create subplot and append to ax\n    ax.append( fig.add_subplot(rows, columns, i+1) )\n    ax[-1].set_title(\"prediction : 3\")  # set title\n    plt.imshow(img, )\n\n# do extra plots on selected axes/subplots\n# note: index starts with 0\nax[2].plot(xs, 3*ys)\nax[19].plot(ys**2, xs)\n\nplt.show()  # finally, render the plot","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:14:55.283174Z","iopub.execute_input":"2021-11-14T17:14:55.283623Z","iopub.status.idle":"2021-11-14T17:14:57.083088Z","shell.execute_reply.started":"2021-11-14T17:14:55.283588Z","shell.execute_reply":"2021-11-14T17:14:57.082492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nw = 256\nh = 256\nfig = plt.figure(figsize=(9, 13))\ncolumns = 4\nrows = 5\n\n# prep (x,y) for extra plotting\nxs = np.linspace(0, 2*np.pi, 60)  # from 0 to 2pi\nys = np.abs(np.sin(xs))           # absolute of sine\n\n# ax enables access to manipulate each of subplots\nax = []\np=os.path.join(\"./\",\"4\",)\nfor i in range(columns*rows):\n\n#     img = np.random.randint(10, size=(h,w))\n    img = cv2.imread(p+'/'+'cam_'+str(i)+'.jpg')\n\n    \n    # create subplot and append to ax\n    ax.append( fig.add_subplot(rows, columns, i+1) )\n    ax[-1].set_title(\"prediction : 4\")  # set title\n    plt.imshow(img, )\n\n# do extra plots on selected axes/subplots\n# note: index starts with 0\nax[2].plot(xs, 3*ys)\nax[19].plot(ys**2, xs)\n\nplt.show()  # finally, render the plot","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:15:03.375205Z","iopub.execute_input":"2021-11-14T17:15:03.375478Z","iopub.status.idle":"2021-11-14T17:15:05.191247Z","shell.execute_reply.started":"2021-11-14T17:15:03.37545Z","shell.execute_reply":"2021-11-14T17:15:05.190617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nw = 256\nh = 256\nfig = plt.figure(figsize=(9, 13))\ncolumns = 4\nrows = 5\n\n# prep (x,y) for extra plotting\nxs = np.linspace(0, 2*np.pi, 60)  # from 0 to 2pi\nys = np.abs(np.sin(xs))           # absolute of sine\n\n# ax enables access to manipulate each of subplots\nax = []\np=os.path.join(\"./\",\"5\",)\nfor i in range(columns*rows):\n\n#     img = np.random.randint(10, size=(h,w))\n    img = cv2.imread(p+'/'+'cam_'+str(i)+'.jpg')\n\n    \n    # create subplot and append to ax\n    ax.append( fig.add_subplot(rows, columns, i+1) )\n    ax[-1].set_title(\"prediction : 5\")  # set title\n    plt.imshow(img, )\n\n# do extra plots on selected axes/subplots\n# note: index starts with 0\nax[2].plot(xs, 3*ys)\nax[19].plot(ys**2, xs)\n\nplt.show()  # finally, render the plot","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:15:06.935031Z","iopub.execute_input":"2021-11-14T17:15:06.937283Z","iopub.status.idle":"2021-11-14T17:15:09.240989Z","shell.execute_reply.started":"2021-11-14T17:15:06.937246Z","shell.execute_reply":"2021-11-14T17:15:09.239647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"test on RSNA dataset","metadata":{}},{"cell_type":"code","source":"rsna_df=pd.read_csv('../input/kfold-splits-rsna-cq500/RSNA_Validation_f2.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:32:15.840044Z","iopub.execute_input":"2021-11-14T17:32:15.840834Z","iopub.status.idle":"2021-11-14T17:32:15.869888Z","shell.execute_reply.started":"2021-11-14T17:32:15.840794Z","shell.execute_reply":"2021-11-14T17:32:15.869069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_df=pd.DataFrame(rsna_df.loc[i][1:] for i in range(0,len(rsna_df)) )","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:32:19.345246Z","iopub.execute_input":"2021-11-14T17:32:19.345503Z","iopub.status.idle":"2021-11-14T17:32:25.463565Z","shell.execute_reply.started":"2021-11-14T17:32:19.345473Z","shell.execute_reply":"2021-11-14T17:32:25.462798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:32:26.52271Z","iopub.execute_input":"2021-11-14T17:32:26.523552Z","iopub.status.idle":"2021-11-14T17:32:26.53678Z","shell.execute_reply.started":"2021-11-14T17:32:26.523494Z","shell.execute_reply":"2021-11-14T17:32:26.536071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_df2=pd.read_csv('../input/cq500-normal-images-and-labels/NormalAbnormal/NormalAbnormal/RSNA/Validation_f2.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:35:32.026149Z","iopub.execute_input":"2021-11-14T17:35:32.026916Z","iopub.status.idle":"2021-11-14T17:35:32.130658Z","shell.execute_reply.started":"2021-11-14T17:35:32.026872Z","shell.execute_reply":"2021-11-14T17:35:32.129875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_df2","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:35:36.123739Z","iopub.execute_input":"2021-11-14T17:35:36.124685Z","iopub.status.idle":"2021-11-14T17:35:36.139339Z","shell.execute_reply.started":"2021-11-14T17:35:36.124612Z","shell.execute_reply":"2021-11-14T17:35:36.138559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"add normal data to rsna dataframe","metadata":{}},{"cell_type":"code","source":"rsna_df2=pd.DataFrame(rsna_df2.loc[i][1:2] for i in range(0,len(rsna_df2)) if rsna_df2.loc[i]['Normal'].all()==True)\nrsna_df2['epidural']=0\nrsna_df2['intraparenchymal']=0\nrsna_df2['intraventricular']=0\t\nrsna_df2['subarachnoid']=0\t\nrsna_df2['subdural']=0\nrsna_df2['normal']=1\n\nrsna_df2","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:35:45.30349Z","iopub.execute_input":"2021-11-14T17:35:45.303963Z","iopub.status.idle":"2021-11-14T17:36:42.172219Z","shell.execute_reply.started":"2021-11-14T17:35:45.303924Z","shell.execute_reply":"2021-11-14T17:36:42.171513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_df['normal']=0\n\nframes = [rsna_df, rsna_df2]\nrsna_df = pd.concat(frames, ignore_index=True)\nrsna_df=shuffle(rsna_df)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:36:52.546856Z","iopub.execute_input":"2021-11-14T17:36:52.547434Z","iopub.status.idle":"2021-11-14T17:36:52.580174Z","shell.execute_reply.started":"2021-11-14T17:36:52.547394Z","shell.execute_reply":"2021-11-14T17:36:52.579132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_df","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:36:55.522853Z","iopub.execute_input":"2021-11-14T17:36:55.523418Z","iopub.status.idle":"2021-11-14T17:36:55.536698Z","shell.execute_reply.started":"2021-11-14T17:36:55.523378Z","shell.execute_reply":"2021-11-14T17:36:55.535809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nprint(len(rsna_df))\n\nHEIGHT = 256\nWIDTH = 256\nCHANNELS = 3\nVALID_BATCH_SIZE = 64\nSHAPE = (HEIGHT, WIDTH, CHANNELS)\n\nclass_names = ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural','normal']\ndata_generator_test = TrainDataGenerator(rsna_df,\n                                        class_names, \n                                        VALID_BATCH_SIZE, \n                                        SHAPE,\n                                        augment = False,\n                                        shuffle = False\n                                        )\n\n\ny_true = rsna_df[class_names].values\ny_hat = model.predict(data_generator_test, verbose=1)\n\ny_hat = y_hat[0:len(y_true)]\ny_pred = np.round(y_hat)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:38:40.208438Z","iopub.execute_input":"2021-11-14T17:38:40.208748Z","iopub.status.idle":"2021-11-14T18:25:18.74977Z","shell.execute_reply.started":"2021-11-14T17:38:40.208713Z","shell.execute_reply":"2021-11-14T18:25:18.748184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data_generator_test[5][0][1])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:25:43.545109Z","iopub.execute_input":"2021-11-14T18:25:43.545886Z","iopub.status.idle":"2021-11-14T18:25:44.17525Z","shell.execute_reply.started":"2021-11-14T18:25:43.545828Z","shell.execute_reply":"2021-11-14T18:25:44.174514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(data_generator_test[6][0][0])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:25:48.231177Z","iopub.execute_input":"2021-11-14T18:25:48.23144Z","iopub.status.idle":"2021-11-14T18:25:49.338356Z","shell.execute_reply.started":"2021-11-14T18:25:48.231411Z","shell.execute_reply":"2021-11-14T18:25:49.337551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"results for testing on rsna dataframe","metadata":{}},{"cell_type":"code","source":"print_metrics_table(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:25:53.133264Z","iopub.execute_input":"2021-11-14T18:25:53.13406Z","iopub.status.idle":"2021-11-14T18:25:55.054955Z","shell.execute_reply.started":"2021-11-14T18:25:53.133997Z","shell.execute_reply":"2021-11-14T18:25:55.05352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_precision_recall_curves(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:26:01.82425Z","iopub.execute_input":"2021-11-14T18:26:01.82452Z","iopub.status.idle":"2021-11-14T18:26:04.021463Z","shell.execute_reply.started":"2021-11-14T18:26:01.824483Z","shell.execute_reply":"2021-11-14T18:26:04.020609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_auc_curves(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:26:10.025891Z","iopub.execute_input":"2021-11-14T18:26:10.026612Z","iopub.status.idle":"2021-11-14T18:26:11.75532Z","shell.execute_reply.started":"2021-11-14T18:26:10.026572Z","shell.execute_reply":"2021-11-14T18:26:11.754469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_confusion_matrix(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:26:16.361115Z","iopub.execute_input":"2021-11-14T18:26:16.3619Z","iopub.status.idle":"2021-11-14T18:26:16.43658Z","shell.execute_reply.started":"2021-11-14T18:26:16.361864Z","shell.execute_reply":"2021-11-14T18:26:16.435811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_on_vs_all_cmatrix(y_true, y_hat, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:26:20.164947Z","iopub.execute_input":"2021-11-14T18:26:20.165509Z","iopub.status.idle":"2021-11-14T18:26:20.984272Z","shell.execute_reply.started":"2021-11-14T18:26:20.16546Z","shell.execute_reply":"2021-11-14T18:26:20.983518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}