{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        break\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-09T03:49:29.906768Z","iopub.execute_input":"2023-10-09T03:49:29.907472Z","iopub.status.idle":"2023-10-09T03:49:30.391364Z","shell.execute_reply.started":"2023-10-09T03:49:29.907442Z","shell.execute_reply":"2023-10-09T03:49:30.390558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import All Libraries","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nfrom sklearn.metrics import classification_report , confusion_matrix , accuracy_score , auc\nfrom sklearn.model_selection import train_test_split\n\nimport cv2\n#from google.colab.patches import cv2_imshow\nfrom PIL import Image \nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import Sequential\nfrom keras.layers import Input, Dense,Conv2D , MaxPooling2D, Flatten,BatchNormalization,Dropout\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nimport tensorflow_hub as hub ","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:49:57.231744Z","iopub.execute_input":"2023-10-09T03:49:57.232466Z","iopub.status.idle":"2023-10-09T03:50:08.159006Z","shell.execute_reply.started":"2023-10-09T03:49:57.232431Z","shell.execute_reply":"2023-10-09T03:50:08.157959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/input/UBC-OCEAN/sample_submission.csv\n# /kaggle/input/UBC-OCEAN/test_thumbnails/41_thumbnail.png\n# /kaggle/input/UBC-OCEAN/train_images/14127.png\n# /kaggle/input/UBC-OCEAN/train_thumbnails/52275_thumbnail.png\n# /kaggle/input/UBC-OCEAN/test_images/41.png","metadata":{"execution":{"iopub.status.busy":"2023-10-07T15:00:17.841595Z","iopub.execute_input":"2023-10-07T15:00:17.842213Z","iopub.status.idle":"2023-10-07T15:00:17.847338Z","shell.execute_reply.started":"2023-10-07T15:00:17.842144Z","shell.execute_reply":"2023-10-07T15:00:17.845951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Collection","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:08.161165Z","iopub.execute_input":"2023-10-09T03:50:08.161775Z","iopub.status.idle":"2023-10-09T03:50:08.206774Z","shell.execute_reply.started":"2023-10-09T03:50:08.161746Z","shell.execute_reply":"2023-10-09T03:50:08.20577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:08.207813Z","iopub.execute_input":"2023-10-09T03:50:08.208101Z","iopub.status.idle":"2023-10-09T03:50:08.225273Z","shell.execute_reply.started":"2023-10-09T03:50:08.208077Z","shell.execute_reply":"2023-10-09T03:50:08.224197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:08.228099Z","iopub.execute_input":"2023-10-09T03:50:08.228453Z","iopub.status.idle":"2023-10-09T03:50:08.250943Z","shell.execute_reply.started":"2023-10-09T03:50:08.228424Z","shell.execute_reply":"2023-10-09T03:50:08.24975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:08.252376Z","iopub.execute_input":"2023-10-09T03:50:08.252791Z","iopub.status.idle":"2023-10-09T03:50:08.263201Z","shell.execute_reply.started":"2023-10-09T03:50:08.252756Z","shell.execute_reply":"2023-10-09T03:50:08.262441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=train_df, x=\"label\")\nplt.title(\"Ovarian Cancer Types Distributions\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:08.264352Z","iopub.execute_input":"2023-10-09T03:50:08.264629Z","iopub.status.idle":"2023-10-09T03:50:08.527362Z","shell.execute_reply.started":"2023-10-09T03:50:08.264605Z","shell.execute_reply":"2023-10-09T03:50:08.526149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['label'].value_counts().plot(kind='pie',autopct=\"%.1f%%\")\nplt.title(\"Ovarian Cancer Types Distributions\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:30:30.667661Z","iopub.execute_input":"2023-10-09T04:30:30.668188Z","iopub.status.idle":"2023-10-09T04:30:30.927034Z","shell.execute_reply.started":"2023-10-09T04:30:30.668147Z","shell.execute_reply":"2023-10-09T04:30:30.926146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:08.528632Z","iopub.execute_input":"2023-10-09T03:50:08.528945Z","iopub.status.idle":"2023-10-09T03:50:08.540329Z","shell.execute_reply.started":"2023-10-09T03:50:08.528921Z","shell.execute_reply":"2023-10-09T03:50:08.538974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.subplot(1,2,1)\nsns.kdeplot(train_df['image_width'])\nplt.subplot(1,2,2)\nsns.kdeplot(train_df['image_height'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:08.54206Z","iopub.execute_input":"2023-10-09T03:50:08.542347Z","iopub.status.idle":"2023-10-09T03:50:09.066071Z","shell.execute_reply.started":"2023-10-09T03:50:08.542323Z","shell.execute_reply":"2023-10-09T03:50:09.065002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18,6))\nplt.subplot(1,2,1)\nplt.hist(x=train_df['image_width'])\nplt.title(\"Image Width Distributions\")\nplt.subplot(1,2,2)\nplt.hist(x=train_df['image_height'])\nplt.title(\"Image Height Distributions\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:09.068013Z","iopub.execute_input":"2023-10-09T03:50:09.068563Z","iopub.status.idle":"2023-10-09T03:50:09.461663Z","shell.execute_reply.started":"2023-10-09T03:50:09.068534Z","shell.execute_reply":"2023-10-09T03:50:09.460847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_width'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:09.463617Z","iopub.execute_input":"2023-10-09T03:50:09.464705Z","iopub.status.idle":"2023-10-09T03:50:09.474615Z","shell.execute_reply.started":"2023-10-09T03:50:09.46466Z","shell.execute_reply":"2023-10-09T03:50:09.473177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_height'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:09.475919Z","iopub.execute_input":"2023-10-09T03:50:09.476357Z","iopub.status.idle":"2023-10-09T03:50:09.493985Z","shell.execute_reply.started":"2023-10-09T03:50:09.47632Z","shell.execute_reply":"2023-10-09T03:50:09.492776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:09.495324Z","iopub.execute_input":"2023-10-09T03:50:09.495651Z","iopub.status.idle":"2023-10-09T03:50:09.516023Z","shell.execute_reply.started":"2023-10-09T03:50:09.495624Z","shell.execute_reply":"2023-10-09T03:50:09.514862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Outliers in The Data","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,6)) \nplt.subplot(1,2,1)\nsns.boxplot(data=train_df,x=\"image_width\")\nplt.subplot(1,2,2)\nsns.boxplot(data=train_df,x=\"image_height\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:10.519751Z","iopub.execute_input":"2023-10-09T03:50:10.520748Z","iopub.status.idle":"2023-10-09T03:50:10.72801Z","shell.execute_reply.started":"2023-10-09T03:50:10.520713Z","shell.execute_reply":"2023-10-09T03:50:10.726701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1,2,1)\ntrain_df[['image_width']].boxplot()\nplt.subplot(1,2,2)\ntrain_df[['image_width']].boxplot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:10.729867Z","iopub.execute_input":"2023-10-09T03:50:10.730187Z","iopub.status.idle":"2023-10-09T03:50:10.973206Z","shell.execute_reply.started":"2023-10-09T03:50:10.73016Z","shell.execute_reply":"2023-10-09T03:50:10.972034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_train = \"/kaggle/input/UBC-OCEAN/train_images\"\npath_test = \"/kaggle/input/UBC-OCEAN/test_images\"\ntrain_folder = os.listdir(path_train)\ntest_folder = os.listdir(path_test)\n\nprint(len(train_folder))\nprint(len(test_folder))","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:10.974464Z","iopub.execute_input":"2023-10-09T03:50:10.974832Z","iopub.status.idle":"2023-10-09T03:50:10.982367Z","shell.execute_reply.started":"2023-10-09T03:50:10.974805Z","shell.execute_reply":"2023-10-09T03:50:10.980058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:18.129127Z","iopub.execute_input":"2023-10-09T03:50:18.129522Z","iopub.status.idle":"2023-10-09T03:50:18.136667Z","shell.execute_reply.started":"2023-10-09T03:50:18.129483Z","shell.execute_reply":"2023-10-09T03:50:18.135457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_folder","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:19.27202Z","iopub.execute_input":"2023-10-09T03:50:19.272414Z","iopub.status.idle":"2023-10-09T03:50:19.278697Z","shell.execute_reply.started":"2023-10-09T03:50:19.272383Z","shell.execute_reply":"2023-10-09T03:50:19.277764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Images in small size\npath_train_copy = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\npath_test_copy = \"/kaggle/input/UBC-OCEAN/test_thumbnails\"\ntrain_folder_copy = os.listdir(path_train_copy)\ntest_folder_copy = os.listdir(path_test_copy)\n\nprint(len(train_folder_copy))\nprint(len(test_folder_copy))","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:22.163571Z","iopub.execute_input":"2023-10-09T03:50:22.163942Z","iopub.status.idle":"2023-10-09T03:50:22.172997Z","shell.execute_reply.started":"2023-10-09T03:50:22.163916Z","shell.execute_reply":"2023-10-09T03:50:22.171917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:25.643945Z","iopub.execute_input":"2023-10-09T03:50:25.644321Z","iopub.status.idle":"2023-10-09T03:50:25.665588Z","shell.execute_reply.started":"2023-10-09T03:50:25.644292Z","shell.execute_reply":"2023-10-09T03:50:25.664606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['is_tma'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:27.011551Z","iopub.execute_input":"2023-10-09T03:50:27.012754Z","iopub.status.idle":"2023-10-09T03:50:27.020481Z","shell.execute_reply.started":"2023-10-09T03:50:27.012697Z","shell.execute_reply":"2023-10-09T03:50:27.019765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Image id >> Tissue Microarray\ntrain_df_tma = train_df[train_df['is_tma']==True]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:28.329299Z","iopub.execute_input":"2023-10-09T03:50:28.330487Z","iopub.status.idle":"2023-10-09T03:50:28.336097Z","shell.execute_reply.started":"2023-10-09T03:50:28.330449Z","shell.execute_reply":"2023-10-09T03:50:28.335026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:29.796708Z","iopub.execute_input":"2023-10-09T03:50:29.797068Z","iopub.status.idle":"2023-10-09T03:50:29.810234Z","shell.execute_reply.started":"2023-10-09T03:50:29.797043Z","shell.execute_reply":"2023-10-09T03:50:29.809044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma = train_df[train_df['is_tma']==False]\ntrain_df_no_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:31.957465Z","iopub.execute_input":"2023-10-09T03:50:31.957884Z","iopub.status.idle":"2023-10-09T03:50:31.974733Z","shell.execute_reply.started":"2023-10-09T03:50:31.957852Z","shell.execute_reply":"2023-10-09T03:50:31.973551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma['image_id_path'] = [f\"{i}_thumbnail.png\" for i in train_df_no_tma['image_id']]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:33.177536Z","iopub.execute_input":"2023-10-09T03:50:33.177919Z","iopub.status.idle":"2023-10-09T03:50:33.184501Z","shell.execute_reply.started":"2023-10-09T03:50:33.177892Z","shell.execute_reply":"2023-10-09T03:50:33.183291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:33.197257Z","iopub.execute_input":"2023-10-09T03:50:33.197938Z","iopub.status.idle":"2023-10-09T03:50:33.21275Z","shell.execute_reply.started":"2023-10-09T03:50:33.197906Z","shell.execute_reply":"2023-10-09T03:50:33.211793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma['image_id_path'] = [f\"{i}.png\" for i in train_df_tma['image_id']]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:34.538233Z","iopub.execute_input":"2023-10-09T03:50:34.5386Z","iopub.status.idle":"2023-10-09T03:50:34.54574Z","shell.execute_reply.started":"2023-10-09T03:50:34.538571Z","shell.execute_reply":"2023-10-09T03:50:34.544354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:35.123163Z","iopub.execute_input":"2023-10-09T03:50:35.123508Z","iopub.status.idle":"2023-10-09T03:50:35.138545Z","shell.execute_reply.started":"2023-10-09T03:50:35.123481Z","shell.execute_reply":"2023-10-09T03:50:35.137435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Train Images data Preprocessing","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:42.189555Z","iopub.execute_input":"2023-10-09T03:50:42.189977Z","iopub.status.idle":"2023-10-09T03:50:42.195067Z","shell.execute_reply.started":"2023-10-09T03:50:42.189947Z","shell.execute_reply":"2023-10-09T03:50:42.193916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_thumbnails_folder = train_folder_copy","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:42.759157Z","iopub.execute_input":"2023-10-09T03:50:42.759502Z","iopub.status.idle":"2023-10-09T03:50:42.764852Z","shell.execute_reply.started":"2023-10-09T03:50:42.759474Z","shell.execute_reply":"2023-10-09T03:50:42.763575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_thumbnails_folder[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:46.661309Z","iopub.execute_input":"2023-10-09T03:50:46.662452Z","iopub.status.idle":"2023-10-09T03:50:46.670219Z","shell.execute_reply.started":"2023-10-09T03:50:46.6624Z","shell.execute_reply":"2023-10-09T03:50:46.668901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:46.67231Z","iopub.execute_input":"2023-10-09T03:50:46.672651Z","iopub.status.idle":"2023-10-09T03:50:46.687946Z","shell.execute_reply.started":"2023-10-09T03:50:46.672618Z","shell.execute_reply":"2023-10-09T03:50:46.68677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:46.689024Z","iopub.execute_input":"2023-10-09T03:50:46.689302Z","iopub.status.idle":"2023-10-09T03:50:46.706815Z","shell.execute_reply.started":"2023-10-09T03:50:46.689279Z","shell.execute_reply":"2023-10-09T03:50:46.705782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_thumbnails_folder[:5]    ## length 513","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:47.304436Z","iopub.execute_input":"2023-10-09T03:50:47.304842Z","iopub.status.idle":"2023-10-09T03:50:47.311506Z","shell.execute_reply.started":"2023-10-09T03:50:47.30481Z","shell.execute_reply":"2023-10-09T03:50:47.310375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder[:5]     ## Length 538","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:50:48.15161Z","iopub.execute_input":"2023-10-09T03:50:48.152286Z","iopub.status.idle":"2023-10-09T03:50:48.159594Z","shell.execute_reply.started":"2023-10-09T03:50:48.152245Z","shell.execute_reply":"2023-10-09T03:50:48.158602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Images from Thumbnail Folder","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(16,24))\npath = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\nj=1\nfor img, lb in zip(train_df_no_tma['image_id_path'][:24],train_df_no_tma['label'][:24]):\n    plt.subplot(6,4,j)\n    path = os.path.join(\"/kaggle/input/UBC-OCEAN/train_thumbnails/\",img)\n    image = plt.imread(path)\n    image = plt.imshow(image)\n    plt.title(f\"Label:{lb}\")\n    j+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-07T15:00:20.033584Z","iopub.execute_input":"2023-10-07T15:00:20.034086Z","iopub.status.idle":"2023-10-07T15:01:08.283013Z","shell.execute_reply.started":"2023-10-07T15:00:20.03406Z","shell.execute_reply":"2023-10-07T15:01:08.28154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Images from Train folder original size","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(16,28))\npath = \"/kaggle/input/UBC-OCEAN/train_images\"\nj=1\nfor img, lb in zip(train_df_tma['image_id_path'],train_df_tma['label']):\n    plt.subplot(7,4,j)\n    path = os.path.join(\"/kaggle/input/UBC-OCEAN/train_images\",img)\n    image = plt.imread(path)\n    image = plt.imshow(image)\n    plt.title(f\"Label:{lb}\")\n    j+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-07T15:01:08.284606Z","iopub.execute_input":"2023-10-07T15:01:08.284978Z","iopub.status.idle":"2023-10-07T15:02:39.80536Z","shell.execute_reply.started":"2023-10-07T15:01:08.284944Z","shell.execute_reply":"2023-10-07T15:02:39.80367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:52:10.179888Z","iopub.execute_input":"2023-10-09T03:52:10.180329Z","iopub.status.idle":"2023-10-09T03:52:10.196033Z","shell.execute_reply.started":"2023-10-09T03:52:10.180295Z","shell.execute_reply":"2023-10-09T03:52:10.194821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df['image_width'].min())\nprint(train_df['image_width'].max())\nprint(train_df['image_width'].mean())\nprint()\nprint(train_df['image_height'].min())\nprint(train_df['image_height'].max())\nprint(train_df['image_height'].mean())","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:52:10.514441Z","iopub.execute_input":"2023-10-09T03:52:10.514871Z","iopub.status.idle":"2023-10-09T03:52:10.522196Z","shell.execute_reply.started":"2023-10-09T03:52:10.51484Z","shell.execute_reply":"2023-10-09T03:52:10.521009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:52:12.555579Z","iopub.execute_input":"2023-10-09T03:52:12.55599Z","iopub.status.idle":"2023-10-09T03:52:12.571016Z","shell.execute_reply.started":"2023-10-09T03:52:12.555958Z","shell.execute_reply":"2023-10-09T03:52:12.569717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Data Preprocessing","metadata":{}},{"cell_type":"code","source":"image_data = []\nimage_label = []\npath = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\npath1=\"/kaggle/input/UBC-OCEAN/train_images/\"\nfor img , label in zip(train_df_no_tma['image_id_path'],train_df_no_tma['label']):\n    image = Image.open(\"/kaggle/input/UBC-OCEAN/train_thumbnails/\"+img)\n    image = image.resize((224,224))\n    image = image.convert(\"RGB\")\n    image = np.array(image)\n    image_data.append(image)\n    image_label.append(label)\n\nfor img , label in zip(train_df_tma['image_id_path'],train_df_tma['label']):\n    image = Image.open(\"/kaggle/input/UBC-OCEAN/train_images/\"+img)\n    image = image.resize((224,224))\n    image = image.convert(\"RGB\")\n    image = np.array(image)\n    image_data.append(image)\n    image_label.append(label)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:52:22.434281Z","iopub.execute_input":"2023-10-09T03:52:22.434695Z","iopub.status.idle":"2023-10-09T03:55:02.121373Z","shell.execute_reply.started":"2023-10-09T03:52:22.434647Z","shell.execute_reply":"2023-10-09T03:55:02.120085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(image_data))\nprint(len(image_label))","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.123554Z","iopub.execute_input":"2023-10-09T03:55:02.12399Z","iopub.status.idle":"2023-10-09T03:55:02.131021Z","shell.execute_reply.started":"2023-10-09T03:55:02.123952Z","shell.execute_reply":"2023-10-09T03:55:02.129773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(image_label)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.132034Z","iopub.execute_input":"2023-10-09T03:55:02.132325Z","iopub.status.idle":"2023-10-09T03:55:02.150517Z","shell.execute_reply.started":"2023-10-09T03:55:02.132301Z","shell.execute_reply":"2023-10-09T03:55:02.149367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_label_1 = []\nfor i in image_label:\n    if i==\"CC\":\n        image_label_1.append(0)\n    elif i==\"EC\":\n        image_label_1.append(1)\n    elif i==\"HGSC\":\n        image_label_1.append(2)\n    elif i==\"LGSC\":\n        image_label_1.append(3)\n    elif i==\"MC\":\n        image_label_1.append(4)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.152903Z","iopub.execute_input":"2023-10-09T03:55:02.153288Z","iopub.status.idle":"2023-10-09T03:55:02.167954Z","shell.execute_reply.started":"2023-10-09T03:55:02.153248Z","shell.execute_reply":"2023-10-09T03:55:02.166702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(image_label_1)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.169014Z","iopub.execute_input":"2023-10-09T03:55:02.169305Z","iopub.status.idle":"2023-10-09T03:55:02.18628Z","shell.execute_reply.started":"2023-10-09T03:55:02.169281Z","shell.execute_reply":"2023-10-09T03:55:02.185064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_label_1[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.187602Z","iopub.execute_input":"2023-10-09T03:55:02.187935Z","iopub.status.idle":"2023-10-09T03:55:02.203814Z","shell.execute_reply.started":"2023-10-09T03:55:02.187907Z","shell.execute_reply":"2023-10-09T03:55:02.202953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split The Data for Train and Test ","metadata":{}},{"cell_type":"code","source":"x = np.array(image_data)\ny = np.array(image_label_1)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.205511Z","iopub.execute_input":"2023-10-09T03:55:02.205854Z","iopub.status.idle":"2023-10-09T03:55:02.261961Z","shell.execute_reply.started":"2023-10-09T03:55:02.205827Z","shell.execute_reply":"2023-10-09T03:55:02.260645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train , x_test, y_train, y_test = train_test_split(x,y,test_size=0.15,shuffle=True)\nprint(x_train.shape)\nprint(x_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.263836Z","iopub.execute_input":"2023-10-09T03:55:02.264259Z","iopub.status.idle":"2023-10-09T03:55:02.314215Z","shell.execute_reply.started":"2023-10-09T03:55:02.264219Z","shell.execute_reply":"2023-10-09T03:55:02.312907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.315895Z","iopub.execute_input":"2023-10-09T03:55:02.316354Z","iopub.status.idle":"2023-10-09T03:55:02.326101Z","shell.execute_reply.started":"2023-10-09T03:55:02.316316Z","shell.execute_reply":"2023-10-09T03:55:02.325287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:02.328668Z","iopub.execute_input":"2023-10-09T03:55:02.329634Z","iopub.status.idle":"2023-10-09T03:55:02.343115Z","shell.execute_reply.started":"2023-10-09T03:55:02.329595Z","shell.execute_reply":"2023-10-09T03:55:02.341749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Image Visualization","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12,16))\nclass_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC']\nfor i in range(12):\n    plt.subplot(4,3,i+1)\n    plt.imshow(x_train[i])\n    plt.title(f\"Label:{class_labels[y_train[i]]}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-07T15:13:27.354101Z","iopub.execute_input":"2023-10-07T15:13:27.35468Z","iopub.status.idle":"2023-10-07T15:13:30.611541Z","shell.execute_reply.started":"2023-10-07T15:13:27.354653Z","shell.execute_reply":"2023-10-07T15:13:30.61025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Scale the data","metadata":{}},{"cell_type":"code","source":"x_train_scaled = x_train/255\nx_test_scaled = x_test/255","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:55:59.843542Z","iopub.execute_input":"2023-10-09T03:55:59.844015Z","iopub.status.idle":"2023-10-09T03:56:00.174616Z","shell.execute_reply.started":"2023-10-09T03:55:59.843969Z","shell.execute_reply":"2023-10-09T03:56:00.173528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building CNN Model","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=120,kernel_size=(3,3),strides=(1,1),activation='relu',input_shape=(224,224,3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=100,kernel_size=(3,3),strides=(1,1),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=80,kernel_size=(3,3),strides=(1,1),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),strides=(1,1),activation='relu',input_shape=(224,224,3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(units=600,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(units=600,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(units=5,activation='softmax'))\n\nmodel.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",\n             metrics=[\"accuracy\"])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:16:18.687429Z","iopub.execute_input":"2023-10-09T04:16:18.687875Z","iopub.status.idle":"2023-10-09T04:16:18.97618Z","shell.execute_reply.started":"2023-10-09T04:16:18.687841Z","shell.execute_reply":"2023-10-09T04:16:18.974879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train_scaled,y_train,epochs=8,\n         batch_size=64,validation_data=(x_test_scaled,y_test))","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:16:38.886138Z","iopub.execute_input":"2023-10-09T04:16:38.886547Z","iopub.status.idle":"2023-10-09T04:28:33.998152Z","shell.execute_reply.started":"2023-10-09T04:16:38.886517Z","shell.execute_reply":"2023-10-09T04:28:33.997205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Evaluation on Test and Train Data","metadata":{}},{"cell_type":"code","source":"loss, acc = model.evaluate(x_test_scaled,y_test)\nprint(\"Accuracy on Test Data:\",acc)\nprint()\nloss, acc = model.evaluate(x_train_scaled,y_train)\nprint(\"Accuracy on Train Data:\",acc)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:28:34.000515Z","iopub.execute_input":"2023-10-09T04:28:34.001527Z","iopub.status.idle":"2023-10-09T04:29:03.218742Z","shell.execute_reply.started":"2023-10-09T04:28:34.001485Z","shell.execute_reply":"2023-10-09T04:29:03.217919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x_test_scaled)\ny_pred_label = [np.argmax(i) for i in y_pred]\ny_pred_label[:10]  ## Predicted Labels","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:29:03.21959Z","iopub.execute_input":"2023-10-09T04:29:03.21991Z","iopub.status.idle":"2023-10-09T04:29:08.581354Z","shell.execute_reply.started":"2023-10-09T04:29:03.219884Z","shell.execute_reply":"2023-10-09T04:29:08.580332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[:10]  ## Actual Labels","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:29:08.583528Z","iopub.execute_input":"2023-10-09T04:29:08.58461Z","iopub.status.idle":"2023-10-09T04:29:08.591343Z","shell.execute_reply.started":"2023-10-09T04:29:08.584511Z","shell.execute_reply":"2023-10-09T04:29:08.59005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"-----Metrics Evaluation on Test Data-----\")\nprint()\nprint(\"Confusion Matrix:\\n\",confusion_matrix(y_test,y_pred_label))\nprint()\nprint(\"Classification Report:\\n\",classification_report(y_test,y_pred_label))","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:29:08.592578Z","iopub.execute_input":"2023-10-09T04:29:08.592962Z","iopub.status.idle":"2023-10-09T04:29:08.628837Z","shell.execute_reply.started":"2023-10-09T04:29:08.59292Z","shell.execute_reply":"2023-10-09T04:29:08.627424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Compare Actual & Predicted Class Labels","metadata":{}},{"cell_type":"code","source":"class_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC']\nplt.figure(figsize=(16,16))\nfor i in range(16):\n    plt.subplot(4,4,i+1)\n    plt.imshow(x_test[i])\n    plt.title(f\"Actual Label:{class_labels[y_test[i]]}\\nPredicted Label:{class_labels[y_pred_label[i]]}\")\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:32:56.829174Z","iopub.execute_input":"2023-10-09T04:32:56.829557Z","iopub.status.idle":"2023-10-09T04:32:59.785564Z","shell.execute_reply.started":"2023-10-09T04:32:56.82953Z","shell.execute_reply":"2023-10-09T04:32:59.784361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC']\nplt.figure(figsize=(16,16))\nj=1\nfor i in range(65,81):\n    plt.subplot(4,4,j)\n    plt.imshow(x_test[i])\n    plt.title(f\"Actual Label:{class_labels[y_test[i]]}\\nPredicted Label:{class_labels[y_pred_label[i]]}\")\n    plt.axis(\"off\")\n    j+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-09T04:37:41.535343Z","iopub.execute_input":"2023-10-09T04:37:41.535832Z","iopub.status.idle":"2023-10-09T04:37:43.777357Z","shell.execute_reply.started":"2023-10-09T04:37:41.5358Z","shell.execute_reply":"2023-10-09T04:37:43.776182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}