{"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\n\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 tensorflow.keras.applications.densenet import DenseNet121\nfrom tensorflow.keras.layers import *\nfrom sklearn.utils.class_weight import compute_class_weight\n\ndef calculating_class_weights(y_true):\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):\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):\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(2, activation='softmax', 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    ]\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","metadata":{"execution":{"iopub.status.busy":"2021-12-20T05:11:42.17122Z","iopub.execute_input":"2021-12-20T05:11:42.171647Z","iopub.status.idle":"2021-12-20T05:11:48.353341Z","shell.execute_reply.started":"2021-12-20T05:11:42.171525Z","shell.execute_reply":"2021-12-20T05:11:48.352583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.mobilenet import MobileNet\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.models import Sequential, Model\nimport tensorflow.keras as keras\nfrom tensorflow.keras.layers import Layer\nfrom tensorflow.keras import activations\nfrom tensorflow.keras import utils\nimport tensorflow as tf\nimport numpy as np\nimport os\n\n\nclass Capsule(Layer):\n    def __init__(self, k, num_capsule, dim_capsule, routings=3, share_weights=True, activation='squash', **kwargs):\n        super(Capsule, self).__init__(**kwargs)\n        self.num_capsule = num_capsule\n        self.dim_capsule = dim_capsule\n        self.routings = routings\n        self.share_weights = share_weights\n        self.k = k\n        if activation == 'squash':\n            self.activation = self.squash\n        else:\n            self.activation = activations.get(activation)\n\n    def squash(self, x, axis=-1):\n        s_squared_norm = K.sum(K.square(x), axis, keepdims=True) + K.epsilon()\n        scale = K.sqrt(s_squared_norm)/ (0.5 + s_squared_norm)\n        return scale * x\n    \n    def softmax(self, x, axis=-1):\n        ex = K.exp(x - K.max(x, axis=axis, keepdims=True))\n        return ex/K.sum(ex, axis=axis, keepdims=True)\n    \n    def build(self, input_shape):\n        super(Capsule, self).build(input_shape)\n        input_dim_capsule = input_shape[-1]\n        if self.share_weights:\n            self.W = self.add_weight(name='capsule_kernel',\n                                     shape=(1, input_dim_capsule,\n                                            self.num_capsule * self.dim_capsule),\n                                     initializer='glorot_uniform',\n                                     trainable=True)\n        else:\n            input_num_capsule = input_shape[-2]\n            self.W = self.add_weight(name='capsule_kernel',\n                                     shape=(input_num_capsule,\n                                            input_dim_capsule,\n                                            self.num_capsule * self.dim_capsule),\n                                     initializer='glorot_uniform',\n                                     trainable=True)\n\n    def call(self, u_vecs):\n        if self.share_weights:\n            u_hat_vecs = K.conv1d(u_vecs, self.W)\n        else:\n            u_hat_vecs = K.local_conv1d(u_vecs, self.W, [1], [1])\n\n        batch_size = K.shape(u_vecs)[0]\n        input_num_capsule = K.shape(u_vecs)[1]\n        u_hat_vecs = K.reshape(u_hat_vecs, (batch_size, input_num_capsule,\n                                            self.num_capsule, self.dim_capsule))\n        u_hat_vecs = K.permute_dimensions(u_hat_vecs, (0, 2, 1, 3))\n        #final u_hat_vecs.shape = [None, num_capsule, input_num_capsule, dim_capsule]\n\n        b = K.zeros_like(u_hat_vecs[:,:,:,0]) #shape = [None, num_capsule, input_num_capsule]\n        for i in range(self.routings):\n            c = self.softmax(b, 1)\n            # o = K.batch_dot(c, u_hat_vecs, [2, 2])\n            o = tf.einsum('bin,binj->bij', c, u_hat_vecs)\n            if K.backend() == 'theano':\n                o = K.sum(o, axis=1)\n            if i < self.routings - 1:\n                o = K.l2_normalize(o, -1)\n                # b = K.batch_dot(o, u_hat_vecs, [2, 3])\n                b = tf.einsum('bij,binj->bin', o, u_hat_vecs)\n                if K.backend() == 'theano':\n                    b = K.sum(b, axis=1)\n\n        return self.activation(o)\n    \n    def get_config(self):\n        config = super(Capsule, self).get_config()\n        config.update({\"k\": self.k,\n        'num_capsule': self.num_capsule,\n        'dim_capsule': self.dim_capsule,\n        'routings': self.routings,\n        'share_weights': self.share_weights,\n        'activation': self.activation\n        })\n        return config\n    \n    def compute_output_shape(self, input_shape):\n        return (None, self.num_capsule, self.dim_capsule)\n    \ndef mobile_capsule_model(num_classes, shape):\n\n    input_tensor = Input(shape)\n    baseModel = MobileNet(weights='imagenet', include_top=False, input_shape=shape, input_tensor=input_tensor)\n    headModel = baseModel.output\n\n    headModel = Sequential()(headModel)\n    headModel = Convolution2D(128, (3, 3),input_shape=shape, name='CONV2D')(headModel)\n    headModel = Activation('relu', name='CONV2D_Relu')(headModel)\n    headModel = MaxPooling2D(pool_size = (2,2), name='MAXPOOL')(headModel)\n    headModel = BatchNormalization(name='BatchNorm')(headModel)\n\n    x = headModel\n    x = Reshape((-1, 128))(x)\n    x = Capsule(1, 32, 16, 3, True)(x)  \n    x = Capsule(2, 32, 16, 3, True)(x)   \n    capsule = Capsule(3, num_classes, 16, 3, True)(x)\n    output = Lambda(lambda z: K.sqrt(K.sum(K.square(z), 2)))(capsule)\n    output = Lambda(lambda z: z/K.sum(z, axis=-1)[:, np.newaxis])(output)\n    model1 = Model(inputs=input_tensor, outputs=[output])\n    for layer in model1.layers:\n        layer.trainable = True\n\n    return model1","metadata":{"execution":{"iopub.status.busy":"2021-12-20T05:11:48.419493Z","iopub.execute_input":"2021-12-20T05:11:48.419747Z","iopub.status.idle":"2021-12-20T05:11:48.447363Z","shell.execute_reply.started":"2021-12-20T05:11:48.419722Z","shell.execute_reply":"2021-12-20T05:11:48.446676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _read_png(path, SHAPE):\n    img = cv2.imread(path)\n#     print(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):\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):\n        return int(ceil(len(self.ids) / self.batch_size))\n\n    def __getitem__(self, 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):\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):\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 'part' in ID:\n                ID = '..'+ID.split('..')[2]\n            image = _read_png(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","metadata":{"execution":{"iopub.status.busy":"2021-12-20T05:11:53.489565Z","iopub.execute_input":"2021-12-20T05:11:53.48982Z","iopub.status.idle":"2021-12-20T05:11:53.506573Z","shell.execute_reply.started":"2021-12-20T05:11:53.489787Z","shell.execute_reply":"2021-12-20T05:11:53.505619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from prettytable import PrettyTable\nfrom sklearn import metrics\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_confusion_matrix(y_test, y_pred, class_names):\n    matrix = confusion_matrix(y_test, y_pred)\n    plt.figure(figsize=(6, 4))\n    cm = matrix.astype('float') / matrix.sum(axis=1)[:, np.newaxis]\n    sns.heatmap(cm,cmap='crest',linecolor='white',linewidths=1,annot=True, xticklabels = class_names, yticklabels = class_names)\n    plt.title('Confusion Matrix')\n    plt.ylabel('True Label')\n    plt.xlabel('Predicted Label')\n    plt.show()\n\ndef print_performance_metrics(y_test, y_pred, class_names):\n    print('Accuracy:', np.round(metrics.accuracy_score(y_test, y_pred),4))\n    print('Precision:', np.round(metrics.precision_score(y_test, y_pred, average='weighted'),4))\n    print('Recall:', np.round(metrics.recall_score(y_test, y_pred, average='weighted'),4))\n    print('F1 Score:', np.round(metrics.f1_score(y_test, y_pred, average='weighted'),4))\n    print('Cohen Kappa Score:', np.round(metrics.cohen_kappa_score(y_test, y_pred), 4))\n    print('Matthews Corrcoef:', np.round(metrics.matthews_corrcoef(y_test, y_pred), 4))\n    if len(np.unique(y_test)) == 2:\n        print('ROC AUC:',roc_auc_score(y_test,y_pred))\n    print('\\t\\tClassification Report:\\n', metrics.classification_report(y_test, y_pred, target_names=class_names))","metadata":{"execution":{"iopub.status.busy":"2021-12-20T05:11:57.954074Z","iopub.execute_input":"2021-12-20T05:11:57.954612Z","iopub.status.idle":"2021-12-20T05:11:58.067727Z","shell.execute_reply.started":"2021-12-20T05:11:57.954576Z","shell.execute_reply":"2021-12-20T05:11:58.06685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import shuffle\nfold_num = 1\n\n\ntrain_df = pd.read_csv('../input/stroke-normal-abnormal-splits-new/Train_f{}.csv'.format(fold_num))\nabnormal = train_df.loc[train_df['Abnormal']==1]\ntrain_df = train_df.append(abnormal, ignore_index=True)\ntrain_df = shuffle(train_df)\n\nval_df = pd.read_csv('../input/stroke-normal-abnormal-splits-new/Validation_f{}.csv'.format(fold_num))","metadata":{"execution":{"iopub.status.busy":"2021-12-20T05:12:01.413124Z","iopub.execute_input":"2021-12-20T05:12:01.413899Z","iopub.status.idle":"2021-12-20T05:12:04.503212Z","shell.execute_reply.started":"2021-12-20T05:12:01.413846Z","shell.execute_reply":"2021-12-20T05:12:04.50247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT = 256\nWIDTH = 256\nCHANNELS = 3\nTRAIN_BATCH_SIZE = 32\nVALID_BATCH_SIZE = 64\nSHAPE = (HEIGHT, WIDTH, CHANNELS)\n\nclass_names = ['Normal', 'Abnormal']\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 = True,\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 = 1e-4","metadata":{"execution":{"iopub.status.busy":"2021-12-20T05:12:06.423627Z","iopub.execute_input":"2021-12-20T05:12:06.42388Z","iopub.status.idle":"2021-12-20T05:12:07.030764Z","shell.execute_reply.started":"2021-12-20T05:12:06.423854Z","shell.execute_reply":"2021-12-20T05:12:07.029956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Metrics = metrics_define(len(class_names))\nmodel = mobile_capsule_model(len(class_names), (HEIGHT, WIDTH, CHANNELS))\n# model.load_weights('../input/stroke-normal-abnormal-classification/model_alldata_densenet121_f1_run1.h5')\nmodel.compile(optimizer = Adam(learning_rate = LR),\n              loss = get_weighted_loss(weights),\n              metrics = Metrics)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T05:12:12.270763Z","iopub.execute_input":"2021-12-20T05:12:12.271039Z","iopub.status.idle":"2021-12-20T05:12:15.637967Z","shell.execute_reply.started":"2021-12-20T05:12:12.271012Z","shell.execute_reply":"2021-12-20T05:12:15.637233Z"},"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 = 20,\n                    callbacks = [ModelCheckpointFull('model_alldata_capsnet_run1.h5')],\n                    verbose = 1, workers=4\n                    )","metadata":{"execution":{"iopub.status.busy":"2021-12-20T05:12:54.994788Z","iopub.execute_input":"2021-12-20T05:12:54.995043Z","iopub.status.idle":"2021-12-20T05:24:58.766837Z","shell.execute_reply.started":"2021-12-20T05:12:54.995017Z","shell.execute_reply":"2021-12-20T05:24:58.765558Z"},"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":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model auc_accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.load_weights('../input/stroke-normal-abnormal-classification/model_alldata_densenet121_f1_new_run3.h5')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T08:33:00.90638Z","iopub.execute_input":"2021-11-13T08:33:00.90674Z","iopub.status.idle":"2021-11-13T08:33:02.970855Z","shell.execute_reply.started":"2021-11-13T08:33:00.906707Z","shell.execute_reply":"2021-11-13T08:33:02.970097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# val_df = pd.read_csv('../input/stroke-normal-abnormal-splits-new/Validation_f{}.csv'.format(fold_num))\n# # abnormal = val_df.loc[val_df['Abnormal']==1]\n# # normal = val_df.loc[val_df['Normal']==1]\n# # normal=shuffle(normal)\n# # normal = normal.iloc[0:len(abnormal)]\n# # val_df = abnormal.append(normal, ignore_index=True)\n# val_df = shuffle(val_df)\n# print(len(val_df))\n\n# HEIGHT = 256\n# WIDTH = 256\n# CHANNELS = 3\n# VALID_BATCH_SIZE = 64\n# SHAPE = (HEIGHT, WIDTH, CHANNELS)\n\n# class_names = ['Normal', 'Abnormal']\n# data_generator_test = TrainDataGenerator(val_df,\n#                                         class_names, \n#                                         VALID_BATCH_SIZE, \n#                                         SHAPE,\n#                                         augment = False,\n#                                         shuffle = False\n#                                         )\n\n# y_true = val_df[class_names].values\n# y_hat = model.predict(data_generator_test, verbose=1)\n\n# y_hat = y_hat[0:len(y_true)]\n# y_pred = np.argmax(y_hat, axis=1)\n# y_true = np.argmax(y_true, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T08:33:16.105771Z","iopub.execute_input":"2021-11-13T08:33:16.106364Z","iopub.status.idle":"2021-11-13T09:37:31.858172Z","shell.execute_reply.started":"2021-11-13T08:33:16.106315Z","shell.execute_reply":"2021-11-13T09:37:31.855798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_confusion_matrix(y_true, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T09:37:40.26456Z","iopub.execute_input":"2021-11-13T09:37:40.26513Z","iopub.status.idle":"2021-11-13T09:37:40.736582Z","shell.execute_reply.started":"2021-11-13T09:37:40.265092Z","shell.execute_reply":"2021-11-13T09:37:40.735892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_performance_metrics(y_true, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T09:37:44.472978Z","iopub.execute_input":"2021-11-13T09:37:44.473253Z","iopub.status.idle":"2021-11-13T09:37:45.357523Z","shell.execute_reply.started":"2021-11-13T09:37:44.473223Z","shell.execute_reply":"2021-11-13T09:37:45.356419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# val_df = pd.read_csv('../input/stroke-normal-abnormal-splits-new/RSNA/Validation_f{}.csv'.format(fold_num))\n# # abnormal = val_df.loc[val_df['Abnormal']==1]\n# # normal = val_df.loc[val_df['Normal']==1]\n# # normal=shuffle(normal)\n# # normal = normal.iloc[0:len(abnormal)]\n# # val_df = abnormal.append(normal, ignore_index=True)\n# val_df = shuffle(val_df)\n# print(len(val_df))\n\n# HEIGHT = 256\n# WIDTH = 256\n# CHANNELS = 3\n# VALID_BATCH_SIZE = 64\n# SHAPE = (HEIGHT, WIDTH, CHANNELS)\n\n# class_names = ['Normal', 'Abnormal']\n# data_generator_test = TrainDataGenerator(val_df,\n#                                         class_names, \n#                                         VALID_BATCH_SIZE, \n#                                         SHAPE,\n#                                         augment = False,\n#                                         shuffle = False\n#                                         )\n\n# y_true = val_df[class_names].values\n# y_hat = model.predict(data_generator_test, verbose=1)\n\n# y_hat = y_hat[0:len(y_true)]\n# y_pred = np.argmax(y_hat, axis=1)\n# y_true = np.argmax(y_true, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T09:50:53.150962Z","iopub.execute_input":"2021-11-13T09:50:53.151473Z","iopub.status.idle":"2021-11-13T10:33:42.488556Z","shell.execute_reply.started":"2021-11-13T09:50:53.151437Z","shell.execute_reply":"2021-11-13T10:33:42.487828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_confusion_matrix(y_true, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T10:33:55.903304Z","iopub.execute_input":"2021-11-13T10:33:55.903892Z","iopub.status.idle":"2021-11-13T10:33:56.296846Z","shell.execute_reply.started":"2021-11-13T10:33:55.903851Z","shell.execute_reply":"2021-11-13T10:33:56.296155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_performance_metrics(y_true, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T10:34:00.543197Z","iopub.execute_input":"2021-11-13T10:34:00.543479Z","iopub.status.idle":"2021-11-13T10:34:01.26196Z","shell.execute_reply.started":"2021-11-13T10:34:00.543448Z","shell.execute_reply":"2021-11-13T10:34:01.261226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# val_df = pd.read_csv('../input/stroke-normal-abnormal-splits-new/CQ500/Validation_f{}.csv'.format(fold_num))\n# # abnormal = val_df.loc[val_df['Abnormal']==1]\n# # normal = val_df.loc[val_df['Normal']==1]\n# # normal=shuffle(normal)\n# # normal = normal.iloc[0:len(abnormal)]\n# # val_df = abnormal.append(normal, ignore_index=True)\n# val_df = shuffle(val_df)\n# print(len(val_df))\n\n# HEIGHT = 256\n# WIDTH = 256\n# CHANNELS = 3\n# VALID_BATCH_SIZE = 64\n# SHAPE = (HEIGHT, WIDTH, CHANNELS)\n\n# class_names = ['Normal', 'Abnormal']\n# data_generator_test = TrainDataGenerator(val_df,\n#                                         class_names, \n#                                         VALID_BATCH_SIZE, \n#                                         SHAPE,\n#                                         augment = False,\n#                                         shuffle = False\n#                                         )\n\n# y_true = val_df[class_names].values\n# y_hat = model.predict(data_generator_test, verbose=1)\n\n# y_hat = y_hat[0:len(y_true)]\n# y_pred = np.argmax(y_hat, axis=1)\n# y_true = np.argmax(y_true, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T10:36:54.608846Z","iopub.execute_input":"2021-11-13T10:36:54.609414Z","iopub.status.idle":"2021-11-13T10:41:36.97885Z","shell.execute_reply.started":"2021-11-13T10:36:54.609364Z","shell.execute_reply":"2021-11-13T10:41:36.978068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_confusion_matrix(y_true, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T10:42:21.643677Z","iopub.execute_input":"2021-11-13T10:42:21.643937Z","iopub.status.idle":"2021-11-13T10:42:22.008386Z","shell.execute_reply.started":"2021-11-13T10:42:21.643908Z","shell.execute_reply":"2021-11-13T10:42:22.007668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print_performance_metrics(y_true, y_pred, class_names)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T10:42:25.489341Z","iopub.execute_input":"2021-11-13T10:42:25.489942Z","iopub.status.idle":"2021-11-13T10:42:25.655793Z","shell.execute_reply.started":"2021-11-13T10:42:25.489903Z","shell.execute_reply":"2021-11-13T10:42:25.65511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}