{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"dockerImageVersionId":30009,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install visualkeras","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:01.201313Z","iopub.execute_input":"2023-06-09T04:25:01.201674Z","iopub.status.idle":"2023-06-09T04:25:10.673016Z","shell.execute_reply.started":"2023-06-09T04:25:01.201642Z","shell.execute_reply":"2023-06-09T04:25:10.672012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from numpy.random import seed\nseed(101)\n\nimport pandas as pd\nimport numpy as np\n\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Activation\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\nfrom tensorflow.keras.optimizers import Adam\n\nimport os\nimport cv2\n\nfrom sklearn.utils import shuffle\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nimport itertools\nimport shutil\nimport matplotlib.pyplot as plt\n%matplotlib inline\ntf.random.set_seed(101)","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2023-06-09T04:25:10.675945Z","iopub.execute_input":"2023-06-09T04:25:10.67639Z","iopub.status.idle":"2023-06-09T04:25:16.347135Z","shell.execute_reply.started":"2023-06-09T04:25:10.67634Z","shell.execute_reply":"2023-06-09T04:25:16.346228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE=96\nIMAGE_CHANNELS=3\nSAMPLE_SIZE=80000","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:16.348647Z","iopub.execute_input":"2023-06-09T04:25:16.349065Z","iopub.status.idle":"2023-06-09T04:25:16.354662Z","shell.execute_reply.started":"2023-06-09T04:25:16.349022Z","shell.execute_reply":"2023-06-09T04:25:16.353573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/histopathologic-cancer-detection')","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:16.356483Z","iopub.execute_input":"2023-06-09T04:25:16.357226Z","iopub.status.idle":"2023-06-09T04:25:16.37083Z","shell.execute_reply.started":"2023-06-09T04:25:16.357183Z","shell.execute_reply":"2023-06-09T04:25:16.369934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('../input/histopathologic-cancer-detection/train')))\nprint(len(os.listdir('../input/histopathologic-cancer-detection/test')))","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:16.375056Z","iopub.execute_input":"2023-06-09T04:25:16.375349Z","iopub.status.idle":"2023-06-09T04:25:19.526941Z","shell.execute_reply.started":"2023-06-09T04:25:16.375318Z","shell.execute_reply":"2023-06-09T04:25:19.526025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_data = pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv')\ndf_data[df_data['id'] != 'dd6dfed324f9fcb6f93f46f32fc800f2ec196be2']\ndf_data[df_data['id'] != '9369c7278ec8bcc6c880d99194de09fc2bd4efbe']\nprint(df_data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:19.530508Z","iopub.execute_input":"2023-06-09T04:25:19.530916Z","iopub.status.idle":"2023-06-09T04:25:19.93982Z","shell.execute_reply.started":"2023-06-09T04:25:19.530879Z","shell.execute_reply":"2023-06-09T04:25:19.938054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_data['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:19.941427Z","iopub.execute_input":"2023-06-09T04:25:19.941801Z","iopub.status.idle":"2023-06-09T04:25:19.955005Z","shell.execute_reply.started":"2023-06-09T04:25:19.941763Z","shell.execute_reply":"2023-06-09T04:25:19.953674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_category_images(col_name,figure_cols, df, IMAGE_PATH):\n    categories = (df.groupby([col_name])[col_name].nunique()).index\n    f, ax = plt.subplots(nrows=len(categories),ncols=figure_cols, \n                         figsize=(4*figure_cols,4*len(categories)))\n    for i, cat in enumerate(categories):\n        sample = df[df[col_name]==cat].sample(figure_cols)\n        for j in range(0,figure_cols):\n            file=IMAGE_PATH + sample.iloc[j]['id'] + '.tif'\n            im=cv2.imread(file)\n            ax[i, j].imshow(im, resample=True, cmap='gray')\n            ax[i, j].set_title(cat, fontsize=16)  \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:19.956652Z","iopub.execute_input":"2023-06-09T04:25:19.957044Z","iopub.status.idle":"2023-06-09T04:25:19.968632Z","shell.execute_reply.started":"2023-06-09T04:25:19.956999Z","shell.execute_reply":"2023-06-09T04:25:19.966721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_PATH = '../input/histopathologic-cancer-detection/train/' \ndraw_category_images('label',4, df_data, IMAGE_PATH)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:19.970556Z","iopub.execute_input":"2023-06-09T04:25:19.97123Z","iopub.status.idle":"2023-06-09T04:25:21.605803Z","shell.execute_reply.started":"2023-06-09T04:25:19.971192Z","shell.execute_reply":"2023-06-09T04:25:21.601972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_0=df_data[df_data['label']==0].sample(SAMPLE_SIZE,random_state=101)\ndf_1=df_data[df_data['label']==1].sample(SAMPLE_SIZE,random_state=101)\ndf_data = pd.concat([df_0, df_1], axis=0).reset_index(drop=True)\ndf_data = shuffle(df_data)\ndf_data['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:21.606941Z","iopub.execute_input":"2023-06-09T04:25:21.607283Z","iopub.status.idle":"2023-06-09T04:25:21.686569Z","shell.execute_reply.started":"2023-06-09T04:25:21.60724Z","shell.execute_reply":"2023-06-09T04:25:21.685421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df_data['label']\ndf_train, df_val = train_test_split(df_data, test_size=0.10, random_state=101, stratify=y)\nprint(df_train.shape)\nprint(df_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:21.688233Z","iopub.execute_input":"2023-06-09T04:25:21.688633Z","iopub.status.idle":"2023-06-09T04:25:21.798799Z","shell.execute_reply.started":"2023-06-09T04:25:21.688593Z","shell.execute_reply":"2023-06-09T04:25:21.797896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir='base_dir'\nos.mkdir(base_dir)\ntrain_dir = os.path.join(base_dir, 'train_dir')\nos.mkdir(train_dir)\nval_dir = os.path.join(base_dir, 'val_dir')\nos.mkdir(val_dir)\nno_tumor_tissue = os.path.join(train_dir, 'a_no_tumor_tissue')\nos.mkdir(no_tumor_tissue)\nhas_tumor_tissue = os.path.join(train_dir, 'b_has_tumor_tissue')\nos.mkdir(has_tumor_tissue)\nno_tumor_tissue = os.path.join(val_dir, 'a_no_tumor_tissue')\nos.mkdir(no_tumor_tissue)\nhas_tumor_tissue = os.path.join(val_dir, 'b_has_tumor_tissue')\nos.mkdir(has_tumor_tissue)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:21.80122Z","iopub.execute_input":"2023-06-09T04:25:21.801782Z","iopub.status.idle":"2023-06-09T04:25:21.811091Z","shell.execute_reply.started":"2023-06-09T04:25:21.80174Z","shell.execute_reply":"2023-06-09T04:25:21.810403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('base_dir/train_dir')","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:21.8125Z","iopub.execute_input":"2023-06-09T04:25:21.813019Z","iopub.status.idle":"2023-06-09T04:25:21.822908Z","shell.execute_reply.started":"2023-06-09T04:25:21.812953Z","shell.execute_reply":"2023-06-09T04:25:21.821882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_data.set_index('id', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:21.824468Z","iopub.execute_input":"2023-06-09T04:25:21.825233Z","iopub.status.idle":"2023-06-09T04:25:21.84123Z","shell.execute_reply.started":"2023-06-09T04:25:21.825188Z","shell.execute_reply":"2023-06-09T04:25:21.840265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_list = list(df_train['id'])\nval_list = list(df_val['id'])\n\nfor image in train_list:\n    fname = image + '.tif'\n    target = df_data.loc[image,'label']\n    if target == 0:\n        label = 'a_no_tumor_tissue'\n    if target == 1:\n        label = 'b_has_tumor_tissue'\n    src = os.path.join('../input/histopathologic-cancer-detection/train', fname)\n    dst = os.path.join(train_dir, label, fname)\n    shutil.copyfile(src, dst)\n\nfor image in val_list:\n    fname = image + '.tif'\n    target = df_data.loc[image,'label']\n    if target == 0:\n        label = 'a_no_tumor_tissue'\n    if target == 1:\n        label = 'b_has_tumor_tissue'\n    src = os.path.join('../input/histopathologic-cancer-detection/train', fname)\n    dst = os.path.join(val_dir, label, fname)\n    shutil.copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:25:21.842859Z","iopub.execute_input":"2023-06-09T04:25:21.843285Z","iopub.status.idle":"2023-06-09T04:41:24.206793Z","shell.execute_reply.started":"2023-06-09T04:25:21.843248Z","shell.execute_reply":"2023-06-09T04:41:24.205747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('base_dir/train_dir/a_no_tumor_tissue')))\nprint(len(os.listdir('base_dir/train_dir/b_has_tumor_tissue')))","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:24.208454Z","iopub.execute_input":"2023-06-09T04:41:24.208832Z","iopub.status.idle":"2023-06-09T04:41:24.322392Z","shell.execute_reply.started":"2023-06-09T04:41:24.208792Z","shell.execute_reply":"2023-06-09T04:41:24.321256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('base_dir/val_dir/a_no_tumor_tissue')))\nprint(len(os.listdir('base_dir/val_dir/b_has_tumor_tissue')))","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:24.323973Z","iopub.execute_input":"2023-06-09T04:41:24.324365Z","iopub.status.idle":"2023-06-09T04:41:24.344052Z","shell.execute_reply.started":"2023-06-09T04:41:24.324327Z","shell.execute_reply":"2023-06-09T04:41:24.343266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = 'base_dir/train_dir'\nvalid_path = 'base_dir/val_dir'\ntest_path = '../input/histopathologic-cancer-detection/test'\nnum_train_samples = len(df_train)\nnum_val_samples = len(df_val)\ntrain_batch_size = 10\nval_batch_size = 10\ntrain_steps = np.ceil(num_train_samples / train_batch_size)\nval_steps = np.ceil(num_val_samples / val_batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:24.347143Z","iopub.execute_input":"2023-06-09T04:41:24.347423Z","iopub.status.idle":"2023-06-09T04:41:24.355279Z","shell.execute_reply.started":"2023-06-09T04:41:24.347396Z","shell.execute_reply":"2023-06-09T04:41:24.354534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1.0/255)\ntrain_gen = datagen.flow_from_directory(train_path,\n                                        target_size=(IMAGE_SIZE,IMAGE_SIZE),\n                                        batch_size=train_batch_size,\n                                        class_mode='categorical')\nval_gen = datagen.flow_from_directory(valid_path,\n                                        target_size=(IMAGE_SIZE,IMAGE_SIZE),\n                                        batch_size=val_batch_size,\n                                        class_mode='categorical')\ntest_gen = datagen.flow_from_directory(valid_path,\n                                        target_size=(IMAGE_SIZE,IMAGE_SIZE),\n                                        batch_size=1,\n                                        class_mode='categorical',\n                                        shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:24.357413Z","iopub.execute_input":"2023-06-09T04:41:24.357909Z","iopub.status.idle":"2023-06-09T04:41:34.83064Z","shell.execute_reply.started":"2023-06-09T04:41:24.35787Z","shell.execute_reply":"2023-06-09T04:41:34.829645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kernel_size = (3,3)\npool_size= (2,2)\nfirst_filters = 32\nsecond_filters = 64\nthird_filters = 128\ndropout_conv = 0.3\ndropout_dense = 0.3\nmodel = Sequential()\nmodel.add(Conv2D(first_filters, kernel_size, activation = 'relu', input_shape = (96, 96, 3)))\nmodel.add(Conv2D(first_filters, kernel_size, activation = 'relu'))\nmodel.add(Conv2D(first_filters, kernel_size, activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = pool_size)) \nmodel.add(Dropout(dropout_conv))\nmodel.add(Conv2D(second_filters, kernel_size, activation ='relu'))\nmodel.add(Conv2D(second_filters, kernel_size, activation ='relu'))\nmodel.add(Conv2D(second_filters, kernel_size, activation ='relu'))\nmodel.add(MaxPooling2D(pool_size = pool_size))\nmodel.add(Dropout(dropout_conv))\nmodel.add(Conv2D(third_filters, kernel_size, activation ='relu'))\nmodel.add(Conv2D(third_filters, kernel_size, activation ='relu'))\nmodel.add(Conv2D(third_filters, kernel_size, activation ='relu'))\nmodel.add(MaxPooling2D(pool_size = pool_size))\nmodel.add(Dropout(dropout_conv))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(dropout_dense))\nmodel.add(Dense(2, activation = \"softmax\"))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:34.832231Z","iopub.execute_input":"2023-06-09T04:41:34.832611Z","iopub.status.idle":"2023-06-09T04:41:37.187655Z","shell.execute_reply.started":"2023-06-09T04:41:34.832571Z","shell.execute_reply":"2023-06-09T04:41:37.185099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import visualkeras\nvisualkeras.layered_view(model, legend=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:37.189001Z","iopub.execute_input":"2023-06-09T04:41:37.189384Z","iopub.status.idle":"2023-06-09T04:41:37.334182Z","shell.execute_reply.started":"2023-06-09T04:41:37.189344Z","shell.execute_reply":"2023-06-09T04:41:37.333246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ntf.keras.utils.plot_model(model, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:37.335409Z","iopub.execute_input":"2023-06-09T04:41:37.335797Z","iopub.status.idle":"2023-06-09T04:41:37.883861Z","shell.execute_reply.started":"2023-06-09T04:41:37.335743Z","shell.execute_reply":"2023-06-09T04:41:37.882777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(Adam(lr=0.0001), loss='binary_crossentropy', \n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:37.885814Z","iopub.execute_input":"2023-06-09T04:41:37.886539Z","iopub.status.idle":"2023-06-09T04:41:37.906978Z","shell.execute_reply.started":"2023-06-09T04:41:37.886486Z","shell.execute_reply":"2023-06-09T04:41:37.906031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(val_gen.class_indices)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:37.908708Z","iopub.execute_input":"2023-06-09T04:41:37.909395Z","iopub.status.idle":"2023-06-09T04:41:37.916327Z","shell.execute_reply.started":"2023-06-09T04:41:37.909356Z","shell.execute_reply":"2023-06-09T04:41:37.915261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = \"model.h5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, \n                             save_best_only=True, mode='max')\nreduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=2, \n                                   verbose=1, mode='max', min_lr=0.00001)\ncallbacks_list = [checkpoint, reduce_lr]\nhistory = model.fit_generator(train_gen, steps_per_epoch=train_steps, \n                    validation_data=val_gen,\n                    validation_steps=val_steps,\n                    epochs=20, verbose=1,\n                   callbacks=callbacks_list)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T04:41:37.917849Z","iopub.execute_input":"2023-06-09T04:41:37.918405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.metrics_names","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss, val_acc = \\\nmodel.evaluate_generator(test_gen, \n                        steps=len(df_val))\nprint('val_loss:', val_loss)\nprint('val_acc:', val_acc)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\nplt.plot(epochs, loss, 'b', label='Training loss', color='Red')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.figure()\nplt.plot(epochs, acc, 'b', label='Training acc', color='Red')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict_generator(test_gen, steps=len(df_val), verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen.class_indices","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_preds = pd.DataFrame(predictions, columns=['no_tumor_tissue', 'has_tumor_tissue'])\ndf_preds.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = test_gen.classes\ny_pred = df_preds['has_tumor_tissue']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nroc_auc_score(y_true, y_pred)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels = test_gen.classes\ntest_labels.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(test_labels, predictions.argmax(axis=1))\ntest_gen.class_indices","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import plot_confusion_matrix","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('base_dir')\ntest_dir = 'test_dir'\nos.mkdir(test_dir)\ntest_images = os.path.join(test_dir, 'test_images')\nos.mkdir(test_images)\nos.listdir('test_dir')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_list = os.listdir('../input/histopathologic-cancer-detection/test')\nfor image in test_list:\n    fname = image\n    src = os.path.join('../input/histopathologic-cancer-detection/test', fname)\n    dst = os.path.join(test_images, fname)\n    shutil.copyfile(src, dst)\n\nlen(os.listdir('test_dir/test_images'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path ='test_dir'\ntest_gen = datagen.flow_from_directory(test_path,\n                                        target_size=(IMAGE_SIZE,IMAGE_SIZE),\n                                        batch_size=1,\n                                        class_mode='categorical',\n                                        shuffle=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_test_images = 57458\npredictions = model.predict_generator(test_gen, steps=num_test_images, verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predictions)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_preds = pd.DataFrame(predictions, columns=['no_tumor_tissue', 'has_tumor_tissue'])\ndf_preds.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = test_gen.filenames\ndf_preds['file_names'] = test_filenames\ndf_preds.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_id(x):\n    # split into a list\n    a = x.split('/')\n    # split into a list\n    b = a[1].split('.')\n    extracted_id = b[0]\n    return extracted_id\n\ndf_preds['id'] = df_preds['file_names'].apply(extract_id)\ndf_preds.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = df_preds['has_tumor_tissue']\nimage_id = df_preds['id']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion Matrix","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt     \n\nax= plt.subplot()\nsns.heatmap(cm, annot=True, ax = ax, fmt=',d', xticklabels=['no_tumor_tissue', 'has_tumor_tissue'], yticklabels=['no_tumor_tissue', 'has_tumor_tissue']);\nax.set_xlabel('Predicted labels');\nax.set_ylabel('True labels'); \nax.set_title('Confusion Matrix'); ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cm)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}