{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6688004,"sourceType":"competition"},{"sourceId":830,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":695},{"sourceId":2659,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":1925}],"dockerImageVersionId":30558,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-09T10:07:37.501031Z","iopub.execute_input":"2023-10-09T10:07:37.501913Z","iopub.status.idle":"2023-10-09T10:07:37.999574Z","shell.execute_reply.started":"2023-10-09T10:07:37.501879Z","shell.execute_reply":"2023-10-09T10:07:37.998658Z"},"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-09T10:16:23.763332Z","iopub.execute_input":"2023-10-09T10:16:23.764541Z","iopub.status.idle":"2023-10-09T10:16:34.159774Z","shell.execute_reply.started":"2023-10-09T10:16:23.764505Z","shell.execute_reply":"2023-10-09T10:16:34.158907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:34.161807Z","iopub.execute_input":"2023-10-09T10:16:34.162596Z","iopub.status.idle":"2023-10-09T10:16:34.207835Z","shell.execute_reply.started":"2023-10-09T10:16:34.162556Z","shell.execute_reply":"2023-10-09T10:16:34.207021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:34.208954Z","iopub.execute_input":"2023-10-09T10:16:34.20983Z","iopub.status.idle":"2023-10-09T10:16:34.224914Z","shell.execute_reply.started":"2023-10-09T10:16:34.2098Z","shell.execute_reply":"2023-10-09T10:16:34.22416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:34.226607Z","iopub.execute_input":"2023-10-09T10:16:34.226923Z","iopub.status.idle":"2023-10-09T10:16:34.253295Z","shell.execute_reply.started":"2023-10-09T10:16:34.226898Z","shell.execute_reply":"2023-10-09T10:16:34.252173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:34.254503Z","iopub.execute_input":"2023-10-09T10:16:34.255439Z","iopub.status.idle":"2023-10-09T10:16:34.265854Z","shell.execute_reply.started":"2023-10-09T10:16:34.255406Z","shell.execute_reply":"2023-10-09T10:16:34.26471Z"},"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-09T10:16:34.267091Z","iopub.execute_input":"2023-10-09T10:16:34.267528Z","iopub.status.idle":"2023-10-09T10:16:34.512258Z","shell.execute_reply.started":"2023-10-09T10:16:34.267502Z","shell.execute_reply":"2023-10-09T10:16:34.511123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Width and Height Distributions","metadata":{}},{"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-09T10:16:34.513923Z","iopub.execute_input":"2023-10-09T10:16:34.514708Z","iopub.status.idle":"2023-10-09T10:16:34.953596Z","shell.execute_reply.started":"2023-10-09T10:16:34.514664Z","shell.execute_reply":"2023-10-09T10:16:34.952373Z"},"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-09T10:16:34.954802Z","iopub.execute_input":"2023-10-09T10:16:34.955091Z","iopub.status.idle":"2023-10-09T10:16:35.306249Z","shell.execute_reply.started":"2023-10-09T10:16:34.955066Z","shell.execute_reply":"2023-10-09T10:16:35.305174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_width'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:35.307804Z","iopub.execute_input":"2023-10-09T10:16:35.308429Z","iopub.status.idle":"2023-10-09T10:16:35.319471Z","shell.execute_reply.started":"2023-10-09T10:16:35.308392Z","shell.execute_reply":"2023-10-09T10:16:35.318161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_height'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:35.323585Z","iopub.execute_input":"2023-10-09T10:16:35.324123Z","iopub.status.idle":"2023-10-09T10:16:35.334092Z","shell.execute_reply.started":"2023-10-09T10:16:35.324093Z","shell.execute_reply":"2023-10-09T10:16:35.333098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:35.335717Z","iopub.execute_input":"2023-10-09T10:16:35.336126Z","iopub.status.idle":"2023-10-09T10:16:35.356439Z","shell.execute_reply.started":"2023-10-09T10:16:35.336089Z","shell.execute_reply":"2023-10-09T10:16:35.355658Z"},"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-09T10:16:35.357596Z","iopub.execute_input":"2023-10-09T10:16:35.358379Z","iopub.status.idle":"2023-10-09T10:16:35.566198Z","shell.execute_reply.started":"2023-10-09T10:16:35.358351Z","shell.execute_reply":"2023-10-09T10:16:35.564983Z"},"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-09T10:16:35.56817Z","iopub.execute_input":"2023-10-09T10:16:35.568615Z","iopub.status.idle":"2023-10-09T10:16:35.790312Z","shell.execute_reply.started":"2023-10-09T10:16:35.568576Z","shell.execute_reply":"2023-10-09T10:16:35.789143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Data Collection","metadata":{}},{"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-09T10:16:35.791739Z","iopub.execute_input":"2023-10-09T10:16:35.792549Z","iopub.status.idle":"2023-10-09T10:16:35.844088Z","shell.execute_reply.started":"2023-10-09T10:16:35.792507Z","shell.execute_reply":"2023-10-09T10:16:35.843055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:41.419495Z","iopub.execute_input":"2023-10-09T10:16:41.420589Z","iopub.status.idle":"2023-10-09T10:16:41.427577Z","shell.execute_reply.started":"2023-10-09T10:16:41.420539Z","shell.execute_reply":"2023-10-09T10:16:41.426614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_folder","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:41.429139Z","iopub.execute_input":"2023-10-09T10:16:41.429605Z","iopub.status.idle":"2023-10-09T10:16:41.448163Z","shell.execute_reply.started":"2023-10-09T10:16:41.429576Z","shell.execute_reply":"2023-10-09T10:16:41.447148Z"},"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-09T10:16:41.542968Z","iopub.execute_input":"2023-10-09T10:16:41.54334Z","iopub.status.idle":"2023-10-09T10:16:41.600023Z","shell.execute_reply.started":"2023-10-09T10:16:41.543311Z","shell.execute_reply":"2023-10-09T10:16:41.598699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:42.810017Z","iopub.execute_input":"2023-10-09T10:16:42.810443Z","iopub.status.idle":"2023-10-09T10:16:42.825796Z","shell.execute_reply.started":"2023-10-09T10:16:42.81041Z","shell.execute_reply":"2023-10-09T10:16:42.824573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['is_tma'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:42.828352Z","iopub.execute_input":"2023-10-09T10:16:42.828822Z","iopub.status.idle":"2023-10-09T10:16:42.839509Z","shell.execute_reply.started":"2023-10-09T10:16:42.828779Z","shell.execute_reply":"2023-10-09T10:16:42.838421Z"},"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-09T10:16:42.840556Z","iopub.execute_input":"2023-10-09T10:16:42.840916Z","iopub.status.idle":"2023-10-09T10:16:42.852598Z","shell.execute_reply.started":"2023-10-09T10:16:42.84088Z","shell.execute_reply":"2023-10-09T10:16:42.851674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:42.853773Z","iopub.execute_input":"2023-10-09T10:16:42.854088Z","iopub.status.idle":"2023-10-09T10:16:42.87276Z","shell.execute_reply.started":"2023-10-09T10:16:42.85406Z","shell.execute_reply":"2023-10-09T10:16:42.87189Z"},"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-09T10:16:55.459377Z","iopub.execute_input":"2023-10-09T10:16:55.459895Z","iopub.status.idle":"2023-10-09T10:16:55.476457Z","shell.execute_reply.started":"2023-10-09T10:16:55.45985Z","shell.execute_reply":"2023-10-09T10:16:55.475168Z"},"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-09T10:16:56.337965Z","iopub.execute_input":"2023-10-09T10:16:56.338468Z","iopub.status.idle":"2023-10-09T10:16:56.34559Z","shell.execute_reply.started":"2023-10-09T10:16:56.338426Z","shell.execute_reply":"2023-10-09T10:16:56.344572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:16:59.413769Z","iopub.execute_input":"2023-10-09T10:16:59.414833Z","iopub.status.idle":"2023-10-09T10:16:59.429359Z","shell.execute_reply.started":"2023-10-09T10:16:59.414791Z","shell.execute_reply":"2023-10-09T10:16:59.428009Z"},"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-09T10:17:00.061421Z","iopub.execute_input":"2023-10-09T10:17:00.062617Z","iopub.status.idle":"2023-10-09T10:17:00.069489Z","shell.execute_reply.started":"2023-10-09T10:17:00.062571Z","shell.execute_reply":"2023-10-09T10:17:00.068402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:00.817274Z","iopub.execute_input":"2023-10-09T10:17:00.81761Z","iopub.status.idle":"2023-10-09T10:17:00.830588Z","shell.execute_reply.started":"2023-10-09T10:17:00.817586Z","shell.execute_reply":"2023-10-09T10:17:00.829634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Train Images data Preprocessing","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:02.760585Z","iopub.execute_input":"2023-10-09T10:17:02.76098Z","iopub.status.idle":"2023-10-09T10:17:02.765661Z","shell.execute_reply.started":"2023-10-09T10:17:02.760949Z","shell.execute_reply":"2023-10-09T10:17:02.764711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_thumbnails_folder = train_folder_copy","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:03.650696Z","iopub.execute_input":"2023-10-09T10:17:03.65114Z","iopub.status.idle":"2023-10-09T10:17:03.65622Z","shell.execute_reply.started":"2023-10-09T10:17:03.65111Z","shell.execute_reply":"2023-10-09T10:17:03.654988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_thumbnails_folder[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:04.447401Z","iopub.execute_input":"2023-10-09T10:17:04.447781Z","iopub.status.idle":"2023-10-09T10:17:04.454179Z","shell.execute_reply.started":"2023-10-09T10:17:04.447753Z","shell.execute_reply":"2023-10-09T10:17:04.453183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:12.845675Z","iopub.execute_input":"2023-10-09T10:17:12.846042Z","iopub.status.idle":"2023-10-09T10:17:12.852775Z","shell.execute_reply.started":"2023-10-09T10:17:12.846013Z","shell.execute_reply":"2023-10-09T10:17:12.851767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:12.854517Z","iopub.execute_input":"2023-10-09T10:17:12.854862Z","iopub.status.idle":"2023-10-09T10:17:12.87376Z","shell.execute_reply.started":"2023-10-09T10:17:12.854837Z","shell.execute_reply":"2023-10-09T10:17:12.872486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_thumbnails_folder[:5]    ## length 513","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:12.875682Z","iopub.execute_input":"2023-10-09T10:17:12.876291Z","iopub.status.idle":"2023-10-09T10:17:12.890356Z","shell.execute_reply.started":"2023-10-09T10:17:12.876249Z","shell.execute_reply":"2023-10-09T10:17:12.889053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder[:5]     ## Length 538","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:14.216493Z","iopub.execute_input":"2023-10-09T10:17:14.217108Z","iopub.status.idle":"2023-10-09T10:17:14.222289Z","shell.execute_reply.started":"2023-10-09T10:17:14.217078Z","shell.execute_reply":"2023-10-09T10:17:14.221534Z"},"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-09T10:12:04.562346Z","iopub.execute_input":"2023-10-09T10:12:04.562702Z","iopub.status.idle":"2023-10-09T10:12:48.404826Z","shell.execute_reply.started":"2023-10-09T10:12:04.562675Z","shell.execute_reply":"2023-10-09T10:12:48.403521Z"},"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-09T10:12:48.406443Z","iopub.execute_input":"2023-10-09T10:12:48.406795Z","iopub.status.idle":"2023-10-09T10:14:11.345477Z","shell.execute_reply.started":"2023-10-09T10:12:48.406754Z","shell.execute_reply":"2023-10-09T10:14:11.344241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:16.25565Z","iopub.execute_input":"2023-10-09T10:17:16.256808Z","iopub.status.idle":"2023-10-09T10:17:16.269704Z","shell.execute_reply.started":"2023-10-09T10:17:16.256769Z","shell.execute_reply":"2023-10-09T10:17:16.268585Z"},"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-09T10:17:16.819824Z","iopub.execute_input":"2023-10-09T10:17:16.820186Z","iopub.status.idle":"2023-10-09T10:17:16.827186Z","shell.execute_reply.started":"2023-10-09T10:17:16.82016Z","shell.execute_reply":"2023-10-09T10:17:16.826095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:17:17.165493Z","iopub.execute_input":"2023-10-09T10:17:17.166532Z","iopub.status.idle":"2023-10-09T10:17:17.180196Z","shell.execute_reply.started":"2023-10-09T10:17:17.166499Z","shell.execute_reply":"2023-10-09T10:17:17.179194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train 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((600,600))\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((600,600))\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-09T10:17:19.635108Z","iopub.execute_input":"2023-10-09T10:17:19.635528Z","iopub.status.idle":"2023-10-09T10:20:06.906245Z","shell.execute_reply.started":"2023-10-09T10:17:19.635491Z","shell.execute_reply":"2023-10-09T10:20:06.905269Z"},"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-09T10:20:17.649079Z","iopub.execute_input":"2023-10-09T10:20:17.650007Z","iopub.status.idle":"2023-10-09T10:20:17.654846Z","shell.execute_reply.started":"2023-10-09T10:20:17.64997Z","shell.execute_reply":"2023-10-09T10:20:17.653807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(image_label)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:22.002403Z","iopub.execute_input":"2023-10-09T10:20:22.00334Z","iopub.status.idle":"2023-10-09T10:20:22.008594Z","shell.execute_reply.started":"2023-10-09T10:20:22.003308Z","shell.execute_reply":"2023-10-09T10:20:22.00789Z"},"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-09T10:20:22.742508Z","iopub.execute_input":"2023-10-09T10:20:22.74391Z","iopub.status.idle":"2023-10-09T10:20:22.749198Z","shell.execute_reply.started":"2023-10-09T10:20:22.743872Z","shell.execute_reply":"2023-10-09T10:20:22.748357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(image_label_1)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:25.564886Z","iopub.execute_input":"2023-10-09T10:20:25.565242Z","iopub.status.idle":"2023-10-09T10:20:25.571972Z","shell.execute_reply.started":"2023-10-09T10:20:25.565216Z","shell.execute_reply":"2023-10-09T10:20:25.570834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_label_1[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:25.574536Z","iopub.execute_input":"2023-10-09T10:20:25.574967Z","iopub.status.idle":"2023-10-09T10:20:25.588387Z","shell.execute_reply.started":"2023-10-09T10:20:25.574865Z","shell.execute_reply":"2023-10-09T10:20:25.587274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preprocessing on test Image","metadata":{}},{"cell_type":"code","source":"test_image_data = []\npath = \"/kaggle/input/UBC-OCEAN/test_thumbnails\"\n#path1 = \"/kaggle/input/UBC-OCEAN/test_images\"\n\ntest_thumbnail_folder = os.listdir(path)\n\nfor img in test_thumbnail_folder:\n    image = Image.open(\"/kaggle/input/UBC-OCEAN/test_thumbnails/\"+img)\n    image = image.resize((600,600))\n    image = image.convert(\"RGB\")\n    image = np.array(image)\n    test_image_data.append(image)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:35.29446Z","iopub.execute_input":"2023-10-09T10:20:35.294879Z","iopub.status.idle":"2023-10-09T10:20:35.647044Z","shell.execute_reply.started":"2023-10-09T10:20:35.294845Z","shell.execute_reply":"2023-10-09T10:20:35.645871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_data","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:41.728258Z","iopub.execute_input":"2023-10-09T10:20:41.728694Z","iopub.status.idle":"2023-10-09T10:20:41.736486Z","shell.execute_reply.started":"2023-10-09T10:20:41.728649Z","shell.execute_reply":"2023-10-09T10:20:41.7354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_data[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:41.73859Z","iopub.execute_input":"2023-10-09T10:20:41.739397Z","iopub.status.idle":"2023-10-09T10:20:41.7515Z","shell.execute_reply.started":"2023-10-09T10:20:41.739364Z","shell.execute_reply":"2023-10-09T10:20:41.75072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_test_array = np.array(test_image_data)\n\n\n## Scale The Test Images\npredict_test_array_scaled = predict_test_array/255","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:42.285658Z","iopub.execute_input":"2023-10-09T10:20:42.286532Z","iopub.status.idle":"2023-10-09T10:20:42.295376Z","shell.execute_reply.started":"2023-10-09T10:20:42.286497Z","shell.execute_reply":"2023-10-09T10:20:42.294165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_test_array_scaled.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:44.767516Z","iopub.execute_input":"2023-10-09T10:20:44.767915Z","iopub.status.idle":"2023-10-09T10:20:44.773592Z","shell.execute_reply.started":"2023-10-09T10:20:44.767886Z","shell.execute_reply":"2023-10-09T10:20:44.772832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split The Data For Training ang Testing Purpose","metadata":{}},{"cell_type":"code","source":"x = np.array(image_data)\ny = np.array(image_label_1)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:47.96054Z","iopub.execute_input":"2023-10-09T10:20:47.960955Z","iopub.status.idle":"2023-10-09T10:20:48.204177Z","shell.execute_reply.started":"2023-10-09T10:20:47.960924Z","shell.execute_reply":"2023-10-09T10:20:48.20293Z"},"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.10,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-09T10:20:48.455444Z","iopub.execute_input":"2023-10-09T10:20:48.455825Z","iopub.status.idle":"2023-10-09T10:20:48.699553Z","shell.execute_reply.started":"2023-10-09T10:20:48.455796Z","shell.execute_reply":"2023-10-09T10:20:48.698458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:49.65681Z","iopub.execute_input":"2023-10-09T10:20:49.657197Z","iopub.status.idle":"2023-10-09T10:20:49.667209Z","shell.execute_reply.started":"2023-10-09T10:20:49.657167Z","shell.execute_reply":"2023-10-09T10:20:49.666353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:20:50.28856Z","iopub.execute_input":"2023-10-09T10:20:50.288994Z","iopub.status.idle":"2023-10-09T10:20:50.297656Z","shell.execute_reply.started":"2023-10-09T10:20:50.288961Z","shell.execute_reply":"2023-10-09T10:20:50.296327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-09T10:21:13.721708Z","iopub.execute_input":"2023-10-09T10:21:13.722106Z","iopub.status.idle":"2023-10-09T10:21:17.000541Z","shell.execute_reply.started":"2023-10-09T10:21:13.722077Z","shell.execute_reply":"2023-10-09T10:21:16.999097Z"},"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-09T10:21:22.135994Z","iopub.execute_input":"2023-10-09T10:21:22.136355Z","iopub.status.idle":"2023-10-09T10:21:24.174469Z","shell.execute_reply.started":"2023-10-09T10:21:22.136327Z","shell.execute_reply":"2023-10-09T10:21:24.173566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_scaled[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:21:24.176395Z","iopub.execute_input":"2023-10-09T10:21:24.176722Z","iopub.status.idle":"2023-10-09T10:21:24.183352Z","shell.execute_reply.started":"2023-10-09T10:21:24.176695Z","shell.execute_reply":"2023-10-09T10:21:24.182294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_scaled[0]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:21:24.184808Z","iopub.execute_input":"2023-10-09T10:21:24.18509Z","iopub.status.idle":"2023-10-09T10:21:24.199354Z","shell.execute_reply.started":"2023-10-09T10:21:24.185058Z","shell.execute_reply":"2023-10-09T10:21:24.198149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# efficientnet_600x600 = \"https://tfhub.dev/google/efficientnet/b7/classification/1\"\n# efficientnet_528x528 = \"https://tfhub.dev/tensorflow/efficientnet/b6/classification/1\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transfer Learning ","metadata":{}},{"cell_type":"raw","source":"# EfficientNet B7   Base Resolution : 600x600x3","metadata":{}},{"cell_type":"markdown","source":"## Create EfficientNet B7 Model ","metadata":{}},{"cell_type":"code","source":"#eff_path = \"https://tfhub.dev/google/efficientnet/b7/classification/1\"\npath1 = \"/kaggle/input/efficientnet/tensorflow1/b7-classification/1\"\npath2  = \"/kaggle/input/efficientnet/tensorflow2/b7-classification/1\"\n\neff_model = hub.KerasLayer(path2,  input_shape=(600,600,3), trainable=False)\n\nnum_class = 5\nefficientnet_model = Sequential()\n\nefficientnet_model.add(eff_model)\nefficientnet_model.add(Dense(units=600, activation=\"relu\"))\nefficientnet_model.add(Dropout(0.2))\nefficientnet_model.add(Dense(units=600, activation=\"relu\"))\nefficientnet_model.add(Dropout(0.2))\nefficientnet_model.add(Dense(units=num_class, activation=\"softmax\"))\n\nefficientnet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:21:38.731839Z","iopub.execute_input":"2023-10-09T10:21:38.732218Z","iopub.status.idle":"2023-10-09T10:22:13.732322Z","shell.execute_reply.started":"2023-10-09T10:21:38.73219Z","shell.execute_reply":"2023-10-09T10:22:13.731454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"efficientnet_model.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",\n                            metrics=[\"accuracy\"])\n\nhistory_2 = efficientnet_model.fit(x_train_scaled,y_train,epochs=1,batch_size=64,\n                        validation_data=(x_test_scaled,y_test))","metadata":{"execution":{"iopub.status.busy":"2023-10-09T10:22:24.662199Z","iopub.execute_input":"2023-10-09T10:22:24.662565Z","iopub.status.idle":"2023-10-09T10:44:30.739771Z","shell.execute_reply.started":"2023-10-09T10:22:24.662539Z","shell.execute_reply":"2023-10-09T10:44:30.738716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, acc = efficientnet_model.evaluate(x_test_scaled,y_test)\nprint(\"Accuracy on Test Data:\",acc) \nloss ,acc = efficientnet_model.evaluate(x_train_scaled,y_train)\nprint(\"Accuracy on Train Data:\",acc)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:02:36.159254Z","iopub.execute_input":"2023-10-09T11:02:36.159677Z","iopub.status.idle":"2023-10-09T11:24:28.480721Z","shell.execute_reply.started":"2023-10-09T11:02:36.15964Z","shell.execute_reply":"2023-10-09T11:24:28.479937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = efficientnet_model.predict(x_test_scaled)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:24:28.482363Z","iopub.execute_input":"2023-10-09T11:24:28.482939Z","iopub.status.idle":"2023-10-09T11:26:52.300324Z","shell.execute_reply.started":"2023-10-09T11:24:28.482912Z","shell.execute_reply":"2023-10-09T11:26:52.299303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_label = [np.argmax(i) for i in y_pred]","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:26:52.302047Z","iopub.execute_input":"2023-10-09T11:26:52.30282Z","iopub.status.idle":"2023-10-09T11:26:52.30745Z","shell.execute_reply.started":"2023-10-09T11:26:52.302784Z","shell.execute_reply":"2023-10-09T11:26:52.306668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_label[:10]   # Predicted Label","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:26:52.309005Z","iopub.execute_input":"2023-10-09T11:26:52.309824Z","iopub.status.idle":"2023-10-09T11:26:52.322983Z","shell.execute_reply.started":"2023-10-09T11:26:52.309795Z","shell.execute_reply":"2023-10-09T11:26:52.321704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[:10]  # Actual Label","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:26:52.324376Z","iopub.execute_input":"2023-10-09T11:26:52.325028Z","iopub.status.idle":"2023-10-09T11:26:52.336085Z","shell.execute_reply.started":"2023-10-09T11:26:52.324995Z","shell.execute_reply":"2023-10-09T11:26:52.334836Z"},"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-09T11:27:17.856784Z","iopub.execute_input":"2023-10-09T11:27:17.857176Z","iopub.status.idle":"2023-10-09T11:27:17.875873Z","shell.execute_reply.started":"2023-10-09T11:27:17.857138Z","shell.execute_reply":"2023-10-09T11:27:17.874759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predictions on Test Data","metadata":{}},{"cell_type":"code","source":"class_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC']","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:27:25.607261Z","iopub.execute_input":"2023-10-09T11:27:25.607874Z","iopub.status.idle":"2023-10-09T11:27:25.612836Z","shell.execute_reply.started":"2023-10-09T11:27:25.607839Z","shell.execute_reply":"2023-10-09T11:27:25.61172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = efficientnet_model.predict(predict_test_array_scaled)\npredictions = [np.argmax(i) for i in predictions]\npredictions\n#result = class_labels[predictions]\n#print(\"Predicted Image Label:\",result)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:27:27.777246Z","iopub.execute_input":"2023-10-09T11:27:27.777658Z","iopub.status.idle":"2023-10-09T11:27:29.482363Z","shell.execute_reply.started":"2023-10-09T11:27:27.777599Z","shell.execute_reply":"2023-10-09T11:27:29.481175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/UBC-OCEAN/sample_submission.csv\"\np = pd.read_csv(path)\np","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:27:53.110538Z","iopub.execute_input":"2023-10-09T11:27:53.111026Z","iopub.status.idle":"2023-10-09T11:27:53.135351Z","shell.execute_reply.started":"2023-10-09T11:27:53.110994Z","shell.execute_reply":"2023-10-09T11:27:53.134339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/UBC-OCEAN/test.csv\"\ntest = pd.read_csv(path)\ntest","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:27:57.260361Z","iopub.execute_input":"2023-10-09T11:27:57.261684Z","iopub.status.idle":"2023-10-09T11:27:57.277441Z","shell.execute_reply.started":"2023-10-09T11:27:57.261616Z","shell.execute_reply":"2023-10-09T11:27:57.275985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file = pd.DataFrame()\nsubmission_file['image_id'] = [41]\nsubmission_file['label'] = [class_labels[i] for i in predictions]\n\nsubmission_file.to_csv(\"submission.csv\",index_label=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:30:21.798135Z","iopub.execute_input":"2023-10-09T11:30:21.798997Z","iopub.status.idle":"2023-10-09T11:30:21.809369Z","shell.execute_reply.started":"2023-10-09T11:30:21.798958Z","shell.execute_reply":"2023-10-09T11:30:21.808102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"submission.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2023-10-09T11:30:25.772811Z","iopub.execute_input":"2023-10-09T11:30:25.773245Z","iopub.status.idle":"2023-10-09T11:30:25.785974Z","shell.execute_reply.started":"2023-10-09T11:30:25.773214Z","shell.execute_reply":"2023-10-09T11:30:25.785069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Practice","metadata":{}},{"cell_type":"code","source":"Image.MAX_IMAGE_PIXELS = 10000000000 ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}