{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport keras.backend as K #to define custom loss function\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom pprint import pprint\nfrom collections import defaultdict\nimport openslide\nfrom openslide import OpenSlide\n\nfrom glob import glob\n\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense\nfrom tensorflow.keras.layers import GlobalMaxPooling2D\nfrom keras.models import load_model","metadata":{"papermill":{"duration":6.579445,"end_time":"2022-12-13T12:03:24.381666","exception":false,"start_time":"2022-12-13T12:03:17.802221","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-05T15:33:42.656382Z","iopub.execute_input":"2023-01-05T15:33:42.656853Z","iopub.status.idle":"2023-01-05T15:33:42.673394Z","shell.execute_reply.started":"2023-01-05T15:33:42.65682Z","shell.execute_reply":"2023-01-05T15:33:42.672252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/mayo-clinic-strip-ai/train.csv')\ntest_df  = pd.read_csv('../input/mayo-clinic-strip-ai/test.csv')\ntrain_df.head()","metadata":{"papermill":{"duration":0.031325,"end_time":"2022-12-13T12:03:24.419596","exception":false,"start_time":"2022-12-13T12:03:24.388271","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-05T15:33:43.290549Z","iopub.execute_input":"2023-01-05T15:33:43.290912Z","iopub.status.idle":"2023-01-05T15:33:43.32509Z","shell.execute_reply.started":"2023-01-05T15:33:43.290881Z","shell.execute_reply":"2023-01-05T15:33:43.324242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data viusalization","metadata":{}},{"cell_type":"code","source":"##### CLASS DISTRIBUTION\n\nplt.style.use('Solarize_Light2')\nlabels = train_df.groupby('label')['label'].count().div(len(train_df)).mul(100)\ncenters = train_df.groupby(\"center_id\")['center_id'].count().div(len(train_df)).mul(100)\n\nfig, ax = plt.subplots(1,2, figsize=(16,5))\nsns.barplot(x=labels.index, y=labels.values, ax=ax[0])\nax[0].set_title(\"Distribution of a target variable\"), ax[0].set_ylabel(\"%\")\nsns.barplot(x=centers.index, y=centers.values, ax=ax[1])\nax[1].set_title(\"Images per clinic center\"), ax[1].set_ylabel(\"%\")\nplt.show()\n\nprint('Train Size = {}'.format(len(train_df)))\nprint('Test Size = {}'.format(len(test_df)))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T18:50:10.020576Z","iopub.execute_input":"2023-01-03T18:50:10.021164Z","iopub.status.idle":"2023-01-03T18:50:10.34405Z","shell.execute_reply.started":"2023-01-03T18:50:10.021124Z","shell.execute_reply":"2023-01-03T18:50:10.343167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## From this plot we notice that there is a class imbalance which we will deal with later in this notebook","metadata":{}},{"cell_type":"code","source":"train_images = glob(\"/kaggle/input/mayo-clinic-strip-ai/train/*\")\ntest_images = glob(\"/kaggle/input/mayo-clinic-strip-ai/test/*\")\nother_images = glob(\"/kaggle/input/mayo-clinic-strip-ai/other/*\")\nprint(f\"Number of images in a training set: {len(train_images)}\")\nprint(f\"Number of images in a training set: {len(test_images)}\")\nprint(f\"Number of other: {len(other_images)}\")","metadata":{"execution":{"iopub.status.busy":"2023-01-03T18:50:10.947473Z","iopub.execute_input":"2023-01-03T18:50:10.947806Z","iopub.status.idle":"2023-01-03T18:50:11.532706Z","shell.execute_reply.started":"2023-01-03T18:50:10.947777Z","shell.execute_reply":"2023-01-03T18:50:11.531535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_prop = defaultdict(list)\n\nfor i, path in enumerate(train_images):\n    img_path = train_images[i]\n    slide = OpenSlide(img_path)    \n    img_prop['image_id'].append(img_path[-12:-4])\n    img_prop['width'].append(slide.dimensions[0])\n    img_prop['height'].append(slide.dimensions[1])\n    img_prop['size'].append(round(os.path.getsize(img_path) / 1e6, 2))\n    img_prop['path'].append(img_path)\n\nimage_data = pd.DataFrame(img_prop)\nimage_data['img_aspect_ratio'] = image_data['width']/image_data['height']\nimage_data.sort_values(by='image_id', inplace=True)\nimage_data.reset_index(inplace=True, drop=True)\n\nimage_data = image_data.merge(train_df, on='image_id')\nimage_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T18:50:14.330855Z","iopub.execute_input":"2023-01-03T18:50:14.331234Z","iopub.status.idle":"2023-01-03T18:50:36.50762Z","shell.execute_reply.started":"2023-01-03T18:50:14.331196Z","shell.execute_reply":"2023-01-03T18:50:36.506162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('Solarize_Light2')\n\nfig, ax = plt.subplots(1,2, figsize=(16,5))\nsns.histplot(x='size', data = image_data, bins=100, ax=ax[0])\nax[0].set_title(\"Distribution of size\"), ax[0].set_ylabel(\"%\")\nsns.histplot(x='img_aspect_ratio', data = image_data, bins=100, ax=ax[1])\nax[1].set_title(\"Image aspect ratio\"), ax[1].set_ylabel(\"%\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T18:50:36.509645Z","iopub.execute_input":"2023-01-03T18:50:36.510041Z","iopub.status.idle":"2023-01-03T18:50:37.30944Z","shell.execute_reply.started":"2023-01-03T18:50:36.509978Z","shell.execute_reply":"2023-01-03T18:50:37.308366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nImage.MAX_IMAGE_PIXELS = None \n\nCE_imgs = image_data.loc[image_data['label']=='CE','path']\nLAA_imgs = image_data.loc[image_data['label']=='LAA','path']\n\n\nplt.style.use('default')\nfig, axes = plt.subplots(1,5, figsize=(16,16))\ntrain_images\nfor ax in axes.reshape(-1):\n    img_path = np.random.choice(CE_imgs)\n    img = Image.open(img_path)   \n    img.thumbnail((300,300), Image.Resampling.LANCZOS)\n    ax.imshow(img), ax.set_title(\"target: CE\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T18:52:05.819156Z","iopub.execute_input":"2023-01-03T18:52:05.820221Z","iopub.status.idle":"2023-01-03T18:52:46.857084Z","shell.execute_reply.started":"2023-01-03T18:52:05.820147Z","shell.execute_reply":"2023-01-03T18:52:46.852939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1,5, figsize=(16,16))\ntrain_images\nfor ax in axes.reshape(-1):\n    img_path = np.random.choice(LAA_imgs)\n    img = Image.open(img_path)   \n    img.thumbnail((300,300), Image.Resampling.LANCZOS)\n    ax.imshow(img), ax.set_title(\"target: LAA\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T18:53:09.21447Z","iopub.execute_input":"2023-01-03T18:53:09.214834Z","iopub.status.idle":"2023-01-03T18:54:11.104056Z","shell.execute_reply.started":"2023-01-03T18:53:09.214802Z","shell.execute_reply":"2023-01-03T18:54:11.10012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## First impressions:\n\n- Images sizes are from small ones to a high-resolution ones\n- Images have different aspect ratios\n- Backgrounds have different colours\n- Clots are usually in the form of multiple small pieces\n- Blood clots have different colours","metadata":{}},{"cell_type":"code","source":"slide = OpenSlide('/kaggle/input/mayo-clinic-strip-ai/train/026c97_0.tif') # opening a full slide\n\nregion = (2500, 2000) # location of the top left pixel\nlevel = 0 # level of the picture (we have only 0)\nsize = (3500, 3500) # region size in pixels\n\nregion = slide.read_region(region, level, size)\nimage = region.resize((512, 512))\nplt.figure(figsize=(10, 10))\nplt.imshow(image)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:05:40.666013Z","iopub.execute_input":"2023-01-03T19:05:40.66701Z","iopub.status.idle":"2023-01-03T19:05:42.226722Z","shell.execute_reply.started":"2023-01-03T19:05:40.666968Z","shell.execute_reply":"2023-01-03T19:05:42.223038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![image](https://upload.wikimedia.org/wikipedia/commons/0/06/Composition_of_a_fresh_thrombus.jpg)","metadata":{}},{"cell_type":"markdown","source":"## Approach 1","metadata":{}},{"cell_type":"markdown","source":"## Loading Data","metadata":{}},{"cell_type":"code","source":"train_df[\"file_path\"] = train_df[\"image_id\"].apply(lambda x: \"../input/mayo-clinic-strip-ai/train/\" + x + \".tif\")\ntest_df[\"file_path\"]  = test_df[\"image_id\"].apply(lambda x: \"../input/mayo-clinic-strip-ai/test/\" + x + \".tif\")","metadata":{"papermill":{"duration":0.028968,"end_time":"2022-12-13T12:03:24.495672","exception":false,"start_time":"2022-12-13T12:03:24.466704","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-05T15:33:51.714704Z","iopub.execute_input":"2023-01-05T15:33:51.715281Z","iopub.status.idle":"2023-01-05T15:33:51.726867Z","shell.execute_reply.started":"2023-01-05T15:33:51.715237Z","shell.execute_reply":"2023-01-05T15:33:51.72574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# labelling CE class as 1 and LAA as 0\ntrain_df[\"target\"] = train_df[\"label\"].apply(lambda x : 1 if x==\"CE\" else 0)\ntrain_df.head()","metadata":{"papermill":{"duration":0.017639,"end_time":"2022-12-13T12:03:24.519397","exception":false,"start_time":"2022-12-13T12:03:24.501758","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-05T15:33:52.622064Z","iopub.execute_input":"2023-01-05T15:33:52.623099Z","iopub.status.idle":"2023-01-05T15:33:52.641616Z","shell.execute_reply.started":"2023-01-05T15:33:52.623055Z","shell.execute_reply":"2023-01-05T15:33:52.640098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing","metadata":{}},{"cell_type":"code","source":"%%time\ndef preprocess(image_path):\n    slide=OpenSlide(image_path)\n    region= (2500,2500)    \n    size  = (5000, 5000)\n    image = slide.read_region(region, 0, size)\n    image = image.resize((128, 128))\n    image = np.array(image)    \n    return image\n\nX_train=[]\nfor i in tqdm(train_df['file_path']):\n    x1=preprocess(i)\n    X_train.append(x1)\n\nY_train=[]    \nY_train=train_df['target']","metadata":{"papermill":{"duration":2496.090688,"end_time":"2022-12-13T12:45:00.650948","exception":false,"start_time":"2022-12-13T12:03:24.56026","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-04T13:13:54.952719Z","iopub.execute_input":"2023-01-04T13:13:54.953354Z","iopub.status.idle":"2023-01-04T13:14:24.698404Z","shell.execute_reply.started":"2023-01-04T13:13:54.953315Z","shell.execute_reply":"2023-01-04T13:14:24.697373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=np.array(X_train)\nX_train=X_train/255.0\nY_train = np.array(Y_train)","metadata":{"papermill":{"duration":0.296197,"end_time":"2022-12-13T12:45:01.078179","exception":false,"start_time":"2022-12-13T12:45:00.781982","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-03T22:04:13.169536Z","iopub.execute_input":"2023-01-03T22:04:13.169902Z","iopub.status.idle":"2023-01-03T22:04:13.1864Z","shell.execute_reply.started":"2023-01-03T22:04:13.16987Z","shell.execute_reply":"2023-01-03T22:04:13.185425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Splitting data\nx_train,x_test,y_train,y_test=train_test_split(X_train,Y_train, test_size=0.2, random_state=42)","metadata":{"papermill":{"duration":0.049801,"end_time":"2022-12-13T12:45:01.170206","exception":false,"start_time":"2022-12-13T12:45:01.120405","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-03T22:08:31.360957Z","iopub.execute_input":"2023-01-03T22:08:31.361354Z","iopub.status.idle":"2023-01-03T22:08:31.386071Z","shell.execute_reply.started":"2023-01-03T22:08:31.36132Z","shell.execute_reply":"2023-01-03T22:08:31.385083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)","metadata":{"papermill":{"duration":0.050654,"end_time":"2022-12-13T12:45:01.261913","exception":false,"start_time":"2022-12-13T12:45:01.211259","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-03T22:08:31.612415Z","iopub.execute_input":"2023-01-03T22:08:31.612793Z","iopub.status.idle":"2023-01-03T22:08:31.618318Z","shell.execute_reply.started":"2023-01-03T22:08:31.612759Z","shell.execute_reply":"2023-01-03T22:08:31.616821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:08:31.933343Z","iopub.execute_input":"2023-01-03T22:08:31.934023Z","iopub.status.idle":"2023-01-03T22:08:32.211136Z","shell.execute_reply.started":"2023-01-03T22:08:31.933986Z","shell.execute_reply":"2023-01-03T22:08:32.210224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Our CNN approach","metadata":{"papermill":{"duration":0.040808,"end_time":"2022-12-13T12:45:01.435232","exception":false,"start_time":"2022-12-13T12:45:01.394424","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def f1_score(y_true, y_pred): \n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    recall = true_positives / (possible_positives + K.epsilon())\n    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())\n    return f1_val","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:33:32.605063Z","iopub.status.idle":"2023-01-05T15:33:32.605911Z","shell.execute_reply.started":"2023-01-05T15:33:32.605647Z","shell.execute_reply":"2023-01-05T15:33:32.605686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import metrics\n\nmodel = Sequential()\ninput_shape = (128, 128, 4)\n\nmodel.add(Conv2D(filters=64, kernel_size = (3,3), padding = 'valid', activation = 'relu', input_shape = input_shape))\nmodel.add(MaxPooling2D())\nmodel.add(Conv2D(filters=64, kernel_size = (3,3), strides =2, padding = 'valid', activation = 'relu'))\nmodel.add(Conv2D(filters=64, kernel_size = (3,3), strides =2, padding = 'valid', activation = 'relu'))\nmodel.add(Conv2D(filters=64, kernel_size = (3,3), strides =2, padding = 'valid', activation = 'relu'))\nmodel.add(Conv2D(filters=128, kernel_size = (3,3), strides =2, padding = 'valid', activation = 'relu'))\nmodel.add(Conv2D(filters=128, kernel_size = (3,3), strides =2, padding = 'valid', activation = 'relu'))\n\nmodel.add(Dropout(0.13))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = 'relu'))\nmodel.add(Dropout(0.13))\nmodel.add(Dense(100, activation = 'relu'))\nmodel.add(Dense(50, activation = 'relu'))\nmodel.add(Dense(1 , activation=\"sigmoid\"))\n\nmodel.compile(\n    loss = tf.keras.losses.BinaryCrossentropy(),\n    metrics=[metrics.binary_accuracy,f1_score],\n    optimizer = tf.keras.optimizers.Adam(1e-3))","metadata":{"papermill":{"duration":0.213269,"end_time":"2022-12-13T12:45:01.68956","exception":false,"start_time":"2022-12-13T12:45:01.476291","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-04T13:12:45.194151Z","iopub.execute_input":"2023-01-04T13:12:45.194489Z","iopub.status.idle":"2023-01-04T13:12:47.799357Z","shell.execute_reply.started":"2023-01-04T13:12:45.19446Z","shell.execute_reply":"2023-01-04T13:12:47.798356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dot_img_file = 'model.png'\ntf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T13:12:47.801175Z","iopub.execute_input":"2023-01-04T13:12:47.802215Z","iopub.status.idle":"2023-01-04T13:12:48.723885Z","shell.execute_reply.started":"2023-01-04T13:12:47.802175Z","shell.execute_reply":"2023-01-04T13:12:48.722737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## This is where we balance class weights","metadata":{}},{"cell_type":"code","source":"from sklearn.utils import compute_class_weight\ntrain_classes = Y_train\nclass_weights = compute_class_weight(\n                                        class_weight = \"balanced\",\n                                        classes = np.unique(train_classes),\n                                        y = train_classes                                                    \n                                    )\nclass_weights = dict(zip(np.unique(train_classes), class_weights))\nclass_weights","metadata":{"papermill":{"duration":0.054636,"end_time":"2022-12-13T12:45:01.787016","exception":false,"start_time":"2022-12-13T12:45:01.73238","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training the model","metadata":{}},{"cell_type":"code","source":"callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath='our_cnn_best.h5',\n    monitor='val_binary_accuracy',\n    mode='max',\n    save_best_only=True, verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    x_train,\n    y_train,\n    epochs = 10,\n    batch_size=20,\n    validation_data = (x_test,y_test),\n    class_weight= class_weights,\n    callbacks = callback\n)","metadata":{"papermill":{"duration":13.303133,"end_time":"2022-12-13T12:45:15.131496","exception":false,"start_time":"2022-12-13T12:45:01.828363","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_cnn = load_model('/kaggle/working/our_cnn_best.h5', custom_objects={\"f1_score\": f1_score })\nbest_cnn.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:14:11.933952Z","iopub.execute_input":"2023-01-03T22:14:11.934358Z","iopub.status.idle":"2023-01-03T22:14:12.328328Z","shell.execute_reply.started":"2023-01-03T22:14:11.934324Z","shell.execute_reply":"2023-01-03T22:14:12.327161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot confusion matrices for benchmark and transfer learning models\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\nplt.figure(figsize=(15, 5))\n\npreds = best_cnn.predict(x_test)\npreds = (preds >= 0.5).astype(np.int32)\n\ncm = confusion_matrix(y_test, preds)\ndf_cm = pd.DataFrame(cm, index=['LAA', 'CE'], columns=['LAA', 'CE'])\nplt.subplot(121)\nplt.title(\"Confusion matrix for our model\\n\")\nsns.heatmap(df_cm, annot=True, fmt=\"d\", cmap=\"YlGnBu\")\nplt.ylabel(\"Predicted\")\nplt.xlabel(\"Actual\")","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:14:22.469802Z","iopub.execute_input":"2023-01-03T22:14:22.470413Z","iopub.status.idle":"2023-01-03T22:14:22.938535Z","shell.execute_reply.started":"2023-01-03T22:14:22.470364Z","shell.execute_reply":"2023-01-03T22:14:22.937516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission for kaggle competition","metadata":{}},{"cell_type":"code","source":"test1=[]\nfor i in test_df['file_path']:\n    x1=preprocess(i)\n    test1.append(x1)\n    print(i)\n    \ntest1=np.array(test1)\n","metadata":{"papermill":{"duration":18.955522,"end_time":"2022-12-13T12:45:34.133527","exception":false,"start_time":"2022-12-13T12:45:15.178005","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-03T22:06:04.538989Z","iopub.execute_input":"2023-01-03T22:06:04.539374Z","iopub.status.idle":"2023-01-03T22:06:13.792079Z","shell.execute_reply.started":"2023-01-03T22:06:04.539342Z","shell.execute_reply":"2023-01-03T22:06:13.790979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_pred=model.predict(test1)\ncnn_pred","metadata":{"papermill":{"duration":0.212752,"end_time":"2022-12-13T12:45:34.392434","exception":false,"start_time":"2022-12-13T12:45:34.179682","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-03T22:06:13.795322Z","iopub.execute_input":"2023-01-03T22:06:13.796232Z","iopub.status.idle":"2023-01-03T22:06:13.927119Z","shell.execute_reply.started":"2023-01-03T22:06:13.796192Z","shell.execute_reply":"2023-01-03T22:06:13.926141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(test_df[\"patient_id\"].copy())\nsub[\"CE\"] = cnn_pred\nsub[\"LAA\"] = 1- sub[\"CE\"]\n\nsub = sub.groupby(\"patient_id\").mean()\nsub = sub[[\"CE\", \"LAA\"]].round(6).reset_index()\nsub","metadata":{"papermill":{"duration":0.083787,"end_time":"2022-12-13T12:45:34.524093","exception":false,"start_time":"2022-12-13T12:45:34.440306","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-03T22:06:17.875828Z","iopub.execute_input":"2023-01-03T22:06:17.876219Z","iopub.status.idle":"2023-01-03T22:06:17.897902Z","shell.execute_reply.started":"2023-01-03T22:06:17.87616Z","shell.execute_reply":"2023-01-03T22:06:17.8967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index = False)\n!head submission.csv","metadata":{"papermill":{"duration":1.063784,"end_time":"2022-12-13T12:45:35.635044","exception":false,"start_time":"2022-12-13T12:45:34.57126","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-03T22:06:22.041214Z","iopub.execute_input":"2023-01-03T22:06:22.04237Z","iopub.status.idle":"2023-01-03T22:06:23.051516Z","shell.execute_reply.started":"2023-01-03T22:06:22.042319Z","shell.execute_reply":"2023-01-03T22:06:23.050317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Approach 2","metadata":{}},{"cell_type":"markdown","source":"## DenseNET","metadata":{}},{"cell_type":"code","source":"del X_train\ndel Y_train","metadata":{"execution":{"iopub.status.busy":"2023-01-04T13:14:44.874156Z","iopub.execute_input":"2023-01-04T13:14:44.874534Z","iopub.status.idle":"2023-01-04T13:14:44.908974Z","shell.execute_reply.started":"2023-01-04T13:14:44.874501Z","shell.execute_reply":"2023-01-04T13:14:44.906821Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-04T13:14:58.771159Z","iopub.execute_input":"2023-01-04T13:14:58.771748Z","iopub.status.idle":"2023-01-04T13:14:58.790057Z","shell.execute_reply.started":"2023-01-04T13:14:58.771698Z","shell.execute_reply":"2023-01-04T13:14:58.788927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndef preprocess_RGB(image_path):\n    slide=OpenSlide(image_path)\n    region= (2500,2500)    \n    size  = (5000, 5000)\n    image = slide.read_region(region, 0, size).convert('RGB')\n    image = image.resize((128, 128))\n    image = np.array(image)    \n    return image\n\nX_train=[]\nfor i in tqdm(train_df['file_path']):\n    #print(i)\n    x1=preprocess_RGB(i)\n    X_train.append(x1)\n    \nY_train=train_df['target']","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:40:01.255637Z","iopub.execute_input":"2023-01-05T15:40:01.256013Z","iopub.status.idle":"2023-01-05T15:45:55.548723Z","shell.execute_reply.started":"2023-01-05T15:40:01.255981Z","shell.execute_reply":"2023-01-05T15:45:55.54774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=np.array(X_train)\nX_train=X_train/255.0\nY_train = np.array(Y_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:46:08.860437Z","iopub.execute_input":"2023-01-05T15:46:08.861144Z","iopub.status.idle":"2023-01-05T15:46:08.882872Z","shell.execute_reply.started":"2023-01-05T15:46:08.861107Z","shell.execute_reply":"2023-01-05T15:46:08.881891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Splitting data\nx_train,x_test,y_train,y_test=train_test_split(X_train,Y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:46:51.536634Z","iopub.execute_input":"2023-01-05T15:46:51.537027Z","iopub.status.idle":"2023-01-05T15:46:51.551915Z","shell.execute_reply.started":"2023-01-05T15:46:51.536993Z","shell.execute_reply":"2023-01-05T15:46:51.550669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CE = 0\nLAA = 0\nfor i in y_test:\n    if i ==1:\n        CE = CE+1\n    else:\n        LAA = LAA + 1\nprint(CE)\nprint(LAA)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:46:51.906429Z","iopub.execute_input":"2023-01-05T15:46:51.906789Z","iopub.status.idle":"2023-01-05T15:46:51.913629Z","shell.execute_reply.started":"2023-01-05T15:46:51.906757Z","shell.execute_reply":"2023-01-05T15:46:51.912524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_test[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:46:54.879208Z","iopub.execute_input":"2023-01-05T15:46:54.879878Z","iopub.status.idle":"2023-01-05T15:46:55.099099Z","shell.execute_reply.started":"2023-01-05T15:46:54.879841Z","shell.execute_reply":"2023-01-05T15:46:55.098093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def f1_score(y_true, y_pred): \n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    recall = true_positives / (possible_positives + K.epsilon())\n    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())\n    return f1_val","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:47:02.34076Z","iopub.execute_input":"2023-01-05T15:47:02.341122Z","iopub.status.idle":"2023-01-05T15:47:02.348417Z","shell.execute_reply.started":"2023-01-05T15:47:02.34109Z","shell.execute_reply":"2023-01-05T15:47:02.347077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import compute_class_weight\ntrain_classes = Y_train\nclass_weights = compute_class_weight(\n                                        class_weight = \"balanced\",\n                                        classes = np.unique(train_classes),\n                                        y = train_classes                                                    \n                                    )\nclass_weights = dict(zip(np.unique(train_classes), class_weights))\nclass_weights","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:47:03.024233Z","iopub.execute_input":"2023-01-05T15:47:03.025309Z","iopub.status.idle":"2023-01-05T15:47:03.036134Z","shell.execute_reply.started":"2023-01-05T15:47:03.025258Z","shell.execute_reply":"2023-01-05T15:47:03.034944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\nfrom keras.models import Sequential\nfrom tensorflow.keras.applications import DenseNet121\nfrom keras import layers\nfrom keras import metrics\n\neffnet = DenseNet121(\n        weights='imagenet',\n        include_top=False,\n        input_shape=(128,128,3)\n)\n\nprint(len(effnet.layers))\nfor layer in effnet.layers[0:200]:\n    layer.trainable = True\nfor layer in effnet.layers[201::]:\n    layer.trainable = False\n\nDense_model = Sequential()\nDense_model.add(effnet)\nDense_model.add(layers.GlobalAveragePooling2D())\nDense_model.add(layers.Dropout(0.5))\nDense_model.add(layers.Dense(512,activation='relu'))\nDense_model.add(layers.Dense(64,activation='relu'))\nDense_model.add(layers.Dense(1, activation='sigmoid'))\n\nDense_model.compile(\n    loss = tf.keras.losses.BinaryCrossentropy(),\n    metrics=[metrics.binary_accuracy,f1_score],\n    optimizer = tf.keras.optimizers.Adam(1e-3))\n\nDense_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:47:04.923955Z","iopub.execute_input":"2023-01-05T15:47:04.924333Z","iopub.status.idle":"2023-01-05T15:47:08.39497Z","shell.execute_reply.started":"2023-01-05T15:47:04.924301Z","shell.execute_reply":"2023-01-05T15:47:08.393898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dot_img_file = 'model.png'\ntf.keras.utils.plot_model(Dense_model, to_file=dot_img_file, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:47:08.39708Z","iopub.execute_input":"2023-01-05T15:47:08.397449Z","iopub.status.idle":"2023-01-05T15:47:08.591317Z","shell.execute_reply.started":"2023-01-05T15:47:08.397412Z","shell.execute_reply":"2023-01-05T15:47:08.590158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback2 = tf.keras.callbacks.ModelCheckpoint(\n    filepath='Dense_best.h5',\n    monitor='val_binary_accuracy',\n    mode='max',\n    save_best_only=True, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:47:08.593068Z","iopub.execute_input":"2023-01-05T15:47:08.594167Z","iopub.status.idle":"2023-01-05T15:47:08.600048Z","shell.execute_reply.started":"2023-01-05T15:47:08.594123Z","shell.execute_reply":"2023-01-05T15:47:08.598866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Dense_model.fit(\n    x_train,\n    y_train,\n    epochs = 10,\n    batch_size=20,\n    validation_data = (x_test,y_test),\n    class_weight= class_weights,\n    callbacks = callback2   \n)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:47:08.60239Z","iopub.execute_input":"2023-01-05T15:47:08.602934Z","iopub.status.idle":"2023-01-05T15:47:27.015223Z","shell.execute_reply.started":"2023-01-05T15:47:08.602783Z","shell.execute_reply":"2023-01-05T15:47:27.01429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_Dense = load_model('/kaggle/working/Dense_best.h5', custom_objects={\"f1_score\": f1_score })\nbest_Dense.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:47:29.935517Z","iopub.execute_input":"2023-01-05T15:47:29.935883Z","iopub.status.idle":"2023-01-05T15:47:35.638819Z","shell.execute_reply.started":"2023-01-05T15:47:29.935853Z","shell.execute_reply":"2023-01-05T15:47:35.637696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot confusion matrices for benchmark and transfer learning models\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\nplt.figure(figsize=(15, 5))\n\npreds = best_Dense.predict(x_test)\npreds = (preds >= 0.5).astype(np.int32)\n\ncm = confusion_matrix(y_test, preds)\ndf_cm = pd.DataFrame(cm, index=['LAA', 'CE'], columns=['LAA', 'CE'])\nplt.subplot(121)\nplt.title(\"Confusion matrix for our model\\n\")\nsns.heatmap(df_cm, annot=True, fmt=\"d\", cmap=\"YlGnBu\")\nplt.ylabel(\"Predicted\")\nplt.xlabel(\"Actual\")","metadata":{"execution":{"iopub.status.busy":"2023-01-05T15:47:42.430539Z","iopub.execute_input":"2023-01-05T15:47:42.4309Z","iopub.status.idle":"2023-01-05T15:47:44.944063Z","shell.execute_reply.started":"2023-01-05T15:47:42.43087Z","shell.execute_reply":"2023-01-05T15:47:44.943096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}