{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"!pip install git+https://github.com/qubvel/classification_models.git\n\nimport 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\n\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 classification_models.tfkeras import Classifiers\nfrom tensorflow.keras.layers import *\nfrom sklearn.utils.class_weight import compute_class_weight\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 ModelCheckpointFull(model_name):   # save weights of the model that has the best result.\n    return ModelCheckpoint(model_name, \n                            monitor = 'val_accuracy', \n                            verbose = 1, \n                            save_best_only = True, \n                            save_weights_only = True, \n                            mode = 'max', \n                            period = 1)\n\n# Create Model\ndef create_model(num_classes):     # input : num of classes\n                               # output : pretrained mobilenet to classify num_classes classes.\n    K.clear_session()\n    \n    mobilenet, preprocess_input = Classifiers.get('mobilenet')\n    engine = mobilenet(include_top=False,\n                           input_shape=(256, 256, 3),\n                           backend = tf.keras.backend,\n                           layers = tf.keras.layers,\n                           models = tf.keras.models,\n                           utils = tf.keras.utils,\n                          weights = 'imagenet')\n\n    x = GlobalAveragePooling2D(name='avg_pool')(engine.output)\n    x = Dropout(0.15)(x)\n    out = Dense(num_classes, activation='sigmoid', name='new_output')(x)\n    model = Model(inputs=engine.input, outputs=out)\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    ]\n\n    return metrics_all\n\ndef get_weighted_loss(weights):\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\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\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\ndef _read_png(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\n# Image Augmentation\nsometimes = lambda aug: iaa.Sometimes(0.25, aug)\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            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                if 'NonHemo' in ID:\n                    image = _read_png('../input/cq500-normal-images-and-labels/'+ID, self.img_size)\n                else:\n                    if 'content' in ID:\n                        ID = ID[9:]\n                    image = _read_png('../input/rsna-cq500-abnormal-data/'+ID, self.img_size)\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    \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\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\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":{"execution":{"iopub.status.busy":"2021-11-09T22:42:42.505861Z","iopub.execute_input":"2021-11-09T22:42:42.506306Z","iopub.status.idle":"2021-11-09T22:43:01.413206Z","shell.execute_reply.started":"2021-11-09T22:42:42.506269Z","shell.execute_reply":"2021-11-09T22:43:01.412449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"creating train and validation dataframes on fold 5","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/kfold-splits-rsna-cq500/Train_f5.csv')\nval_df = pd.read_csv('../input/kfold-splits-rsna-cq500/Validation_f5.csv')\n","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:43:05.209221Z","iopub.execute_input":"2021-11-09T22:43:05.210049Z","iopub.status.idle":"2021-11-09T22:43:05.51522Z","shell.execute_reply.started":"2021-11-09T22:43:05.210003Z","shell.execute_reply":"2021-11-09T22:43:05.514486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['normal']=0 # add normal class to dataframes\nval_df['normal']=0","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:43:07.275446Z","iopub.execute_input":"2021-11-09T22:43:07.275706Z","iopub.status.idle":"2021-11-09T22:43:07.287151Z","shell.execute_reply.started":"2021-11-09T22:43:07.275676Z","shell.execute_reply":"2021-11-09T22:43:07.286434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import shuffle\n\ntrain_df1 = pd.read_csv('../input/cq500-normal-images-and-labels/NormalAbnormal/NormalAbnormal/Train_f1.csv')\nabnormal1 = train_df1.loc[train_df1['Abnormal']==1]\ntrain_df1 = train_df1.append(abnormal1, ignore_index=True)\ntrain_df1 = shuffle(train_df1)\nval_df1 = pd.read_csv('../input/cq500-normal-images-and-labels/NormalAbnormal/NormalAbnormal/Validation_f1.csv')\n","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:43:09.70299Z","iopub.execute_input":"2021-11-09T22:43:09.703255Z","iopub.status.idle":"2021-11-09T22:43:11.407885Z","shell.execute_reply.started":"2021-11-09T22:43:09.703224Z","shell.execute_reply":"2021-11-09T22:43:11.407137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df1","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:43:13.773768Z","iopub.execute_input":"2021-11-09T22:43:13.774153Z","iopub.status.idle":"2021-11-09T22:43:13.795631Z","shell.execute_reply.started":"2021-11-09T22:43:13.77412Z","shell.execute_reply":"2021-11-09T22:43:13.794904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df1","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:43:17.417492Z","iopub.execute_input":"2021-11-09T22:43:17.417755Z","iopub.status.idle":"2021-11-09T22:43:17.432854Z","shell.execute_reply.started":"2021-11-09T22:43:17.417723Z","shell.execute_reply":"2021-11-09T22:43:17.431646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest_df= pd.DataFrame( train_df1.loc[i][:3] for i in range(20000, 21000) if train_df1.loc[i]['Normal'].all()==True  ) \n","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:58:53.639462Z","iopub.execute_input":"2021-11-09T22:58:53.639723Z","iopub.status.idle":"2021-11-09T22:58:53.980545Z","shell.execute_reply.started":"2021-11-09T22:58:53.639691Z","shell.execute_reply":"2021-11-09T22:58:53.979804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['epidural']=0\ntest_df['intraparenchymal']=0\ntest_df['intraventricular']=0\t\ntest_df['subarachnoid']=0\t\ntest_df['subdural']=0\ntest_df['normal']=1\ntest_df.rename(columns={'Normal': 'normal'}, inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:59:41.908342Z","iopub.execute_input":"2021-11-09T22:59:41.908869Z","iopub.status.idle":"2021-11-09T22:59:41.919062Z","shell.execute_reply.started":"2021-11-09T22:59:41.90883Z","shell.execute_reply":"2021-11-09T22:59:41.918082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:59:45.580884Z","iopub.execute_input":"2021-11-09T22:59:45.581567Z","iopub.status.idle":"2021-11-09T22:59:45.601103Z","shell.execute_reply.started":"2021-11-09T22:59:45.581528Z","shell.execute_reply":"2021-11-09T22:59:45.600477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:58:01.400512Z","iopub.status.idle":"2021-11-09T22:58:01.400922Z","shell.execute_reply.started":"2021-11-09T22:58:01.400698Z","shell.execute_reply":"2021-11-09T22:58:01.40072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:58:01.40222Z","iopub.status.idle":"2021-11-09T22:58:01.402641Z","shell.execute_reply.started":"2021-11-09T22:58:01.402405Z","shell.execute_reply":"2021-11-09T22:58:01.402427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df2 = pd.DataFrame( train_df1.loc[i][:3] for i in range(0, 20000) if train_df1.loc[i]['Normal'].all()==True  ) \nval_df2= pd.DataFrame( val_df1.loc[i][:3] for i in range(0, 10000) if val_df1.loc[i]['Normal'].all()==True  ) ","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:48:07.371316Z","iopub.execute_input":"2021-11-09T22:48:07.371655Z","iopub.status.idle":"2021-11-09T22:48:17.543082Z","shell.execute_reply.started":"2021-11-09T22:48:07.371618Z","shell.execute_reply":"2021-11-09T22:48:17.542344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df2['epidural']=0\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-09T22:48:17.544569Z","iopub.execute_input":"2021-11-09T22:48:17.544819Z","iopub.status.idle":"2021-11-09T22:48:17.559278Z","shell.execute_reply.started":"2021-11-09T22:48:17.544785Z","shell.execute_reply":"2021-11-09T22:48:17.558319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"concate dataframe for having samples of 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-09T22:48:17.560891Z","iopub.execute_input":"2021-11-09T22:48:17.561353Z","iopub.status.idle":"2021-11-09T22:48:17.58974Z","shell.execute_reply.started":"2021-11-09T22:48:17.561313Z","shell.execute_reply":"2021-11-09T22:48:17.589062Z"},"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-09T22:48:17.591158Z","iopub.execute_input":"2021-11-09T22:48:17.591461Z","iopub.status.idle":"2021-11-09T22:48:17.605851Z","shell.execute_reply.started":"2021-11-09T22:48:17.591425Z","shell.execute_reply":"2021-11-09T22:48:17.605192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:48:17.606979Z","iopub.execute_input":"2021-11-09T22:48:17.607296Z","iopub.status.idle":"2021-11-09T22:48:17.62388Z","shell.execute_reply.started":"2021-11-09T22:48:17.607259Z","shell.execute_reply":"2021-11-09T22:48:17.623257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:48:17.62581Z","iopub.execute_input":"2021-11-09T22:48:17.626283Z","iopub.status.idle":"2021-11-09T22:48:17.640807Z","shell.execute_reply.started":"2021-11-09T22:48:17.626247Z","shell.execute_reply":"2021-11-09T22:48:17.639997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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)/2)\nprint(TRAIN_STEPS)\nVal_STEPS = int(len(data_generator_val)/2)\nprint(Val_STEPS)\nLR = 5e-5","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:48:17.644033Z","iopub.execute_input":"2021-11-09T22:48:17.64435Z","iopub.status.idle":"2021-11-09T22:48:17.897292Z","shell.execute_reply.started":"2021-11-09T22:48:17.644312Z","shell.execute_reply":"2021-11-09T22:48:17.896537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"create and fit model","metadata":{}},{"cell_type":"code","source":"Metrics = metrics_define(len(class_names))\n\nmodel = create_model(len(class_names))\n# model.load_weights('../input/rsna-cq500-abnormal-weight/model.h5')\nmodel.compile(optimizer = Adam(learning_rate = LR),\n              loss = get_weighted_loss(weights),\n              metrics = Metrics)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"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 = 30,\n                    callbacks = [ModelCheckpointFull('mobilenet_all_fold5.h5')],\n                    verbose = 1, workers=4\n                    )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"results","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model acc')\nplt.ylabel('acc')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"evaluate model","metadata":{}},{"cell_type":"code","source":"model = create_model(6)\nmodel.load_weights('mobilenet_all_fold5.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df = pd.read_csv('../input/kfold-splits-rsna-cq500/Validation_f5.csv')\nval_df['normal']=0\n\nval_df2= pd.DataFrame( val_df1.loc[i][:3] for i in range(10000, 12000) 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)\nframes = [val_df, val_df2]\nval_df = pd.concat(frames, ignore_index=True)\nval_df=shuffle(val_df)\n\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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_precision_recall_curves(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_auc_curves(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_confusion_matrix(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_on_vs_all_cmatrix(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df = pd.read_csv('../input/kfold-splits-rsna-cq500/CQ500_Validation_f4.csv')\nval_df['normal']=0\n\nval_df2= pd.DataFrame( val_df1.loc[i][:3] for i in range(12000, 14000) 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)\nframes = [val_df, val_df2]\nval_df = pd.concat(frames, ignore_index=True)\nval_df=shuffle(val_df)\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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_metrics_table(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_precision_recall_curves(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_auc_curves(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_confusion_matrix(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_on_vs_all_cmatrix(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df = pd.read_csv('../input/kfold-splits-rsna-cq500/RSNA_Validation_f4.csv')\nval_df['normal']=0\n\nval_df2= pd.DataFrame( val_df1.loc[i][:3] for i in range(14000, 16000) 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)\nframes = [val_df, val_df2]\nval_df = pd.concat(frames, ignore_index=True)\nval_df=shuffle(val_df)\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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_metrics_table(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_precision_recall_curves(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_auc_curves(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_confusion_matrix(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_on_vs_all_cmatrix(y_true, y_hat, y_pred, class_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test on Normal class ","metadata":{}},{"cell_type":"code","source":"\n\n\nprint(len(test_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_test1 = TrainDataGenerator(test_df,\n                                        class_names, \n                                        VALID_BATCH_SIZE, \n                                        SHAPE,\n                                        augment = False,\n                                        shuffle = False\n                                        )\n\ny_true = test_df[class_names].values\ny_hat = model.predict(data_generator_test1, verbose=1)\n\ny_hat = y_hat[0:len(y_true)]\ny_pred = np.round(y_hat)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T23:01:36.044585Z","iopub.execute_input":"2021-11-09T23:01:36.044838Z","iopub.status.idle":"2021-11-09T23:01:42.975429Z","shell.execute_reply.started":"2021-11-09T23:01:36.044808Z","shell.execute_reply":"2021-11-09T23:01:42.974697Z"},"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-09T23:01:42.977283Z","iopub.execute_input":"2021-11-09T23:01:42.977583Z","iopub.status.idle":"2021-11-09T23:01:43.020098Z","shell.execute_reply.started":"2021-11-09T23:01:42.977544Z","shell.execute_reply":"2021-11-09T23:01:43.01912Z"},"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-09T23:01:57.620219Z","iopub.execute_input":"2021-11-09T23:01:57.620485Z","iopub.status.idle":"2021-11-09T23:01:59.001852Z","shell.execute_reply.started":"2021-11-09T23:01:57.620454Z","shell.execute_reply":"2021-11-09T23:01:58.998983Z"},"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-09T23:02:05.023673Z","iopub.execute_input":"2021-11-09T23:02:05.024465Z","iopub.status.idle":"2021-11-09T23:02:05.064769Z","shell.execute_reply.started":"2021-11-09T23:02:05.024406Z","shell.execute_reply":"2021-11-09T23:02:05.063583Z"},"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-09T23:02:10.819469Z","iopub.execute_input":"2021-11-09T23:02:10.819992Z","iopub.status.idle":"2021-11-09T23:02:10.832353Z","shell.execute_reply.started":"2021-11-09T23:02:10.819938Z","shell.execute_reply":"2021-11-09T23:02:10.830863Z"},"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-09T23:02:14.348393Z","iopub.execute_input":"2021-11-09T23:02:14.348938Z","iopub.status.idle":"2021-11-09T23:02:14.986701Z","shell.execute_reply.started":"2021-11-09T23:02:14.348893Z","shell.execute_reply":"2021-11-09T23:02:14.986004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"GradCam","metadata":{}},{"cell_type":"code","source":"model = create_model(6)\nmodel.load_weights('../input/mobilenet-stroke-all/mobilenet_all_fold5.h5')","metadata":{"execution":{"iopub.status.busy":"2021-11-09T22:48:37.398851Z","iopub.execute_input":"2021-11-09T22:48:37.399537Z","iopub.status.idle":"2021-11-09T22:48:42.38871Z","shell.execute_reply.started":"2021-11-09T22:48:37.399498Z","shell.execute_reply":"2021-11-09T22:48:42.38801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\n\n# Display\nfrom IPython.display import Image, display\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_builder = model\nimg_size = (256, 256)\n# preprocess_input = keras.applications.xception.preprocess_input\ndecode_predictions = keras.applications.mobilenet.decode_predictions\n\nlast_conv_layer_name = \"conv_pw_13_relu\"\n\n","metadata":{"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    # First, we create a model that maps the input image to the activations\n    # of the last conv layer as well as the output predictions\n    grad_model = tf.keras.models.Model(\n        [model.inputs], [model.get_layer(last_conv_layer_name).output, model.output]\n    )\n\n    # Then, we compute the gradient of the top predicted class for our input image\n    # with respect to the activations of the last conv layer\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    # This is the gradient of the output neuron (top predicted or chosen)\n    # with regard to the output feature map of the last conv layer\n    grads = tape.gradient(class_channel, last_conv_layer_output)\n\n    # This is a vector where each entry is the mean intensity of the gradient\n    # over a specific feature map channel\n    pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))\n\n    # We multiply each channel in the feature map array\n    # by \"how important this channel is\" with regard to the top predicted class\n    # then sum all the channels to obtain the heatmap class activation\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    # For visualization purpose, we will also normalize the heatmap between 0 & 1\n    heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)\n    return heatmap.numpy()","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data_generator_test[1][0][0])\nplt.savefig( \"ok.jpg\")","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(preds)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare image\n# img_array = preprocess_input(get_img_array('../input/rsna-cq500-abnormal-data/CQ500_Images/CQ500CT10 CQ500CT10/CT PLAIN THIN_1.2.276.0.7230010.3.1.3.296485376.1.1521713955.1950576/1.2.276.0.7230010.3.1.4.296485376.1.1521713955.1950589.png', size=img_size))\n\n# Make model\n# model = model_builder(weights=\"imagenet\")\n\n# Remove last layer's softmax\n# model.layers[-1].activation = None\n\n# Print what the top predicted class is\npreds = model.predict(img_array)\n# print(\"Predicted:\", decode_predictions(preds, top=1)[0])\n\n# Generate class activation heatmap\nheatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name)\n\n# Display heatmap\nplt.matshow(heatmap)\nplt.show()","metadata":{"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    # Load the original image\n#     img = keras.preprocessing.image.load_img(img_path)\n#     img = keras.preprocessing.image.img_to_array(img)\n\n    # Rescale heatmap to a range 0-255\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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data_generator_test[1][0][1])","metadata":{"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":{"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":{"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*255, 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            \n\n\n        \n    \n    ","metadata":{"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(\"./\",\"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":{"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":{"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":{"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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}