{"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\n#import numpy as np # linear algebra\n#import 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\n#import os\n#for 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-19T09:11:28.014875Z","iopub.execute_input":"2023-10-19T09:11:28.015256Z","iopub.status.idle":"2023-10-19T09:11:28.019591Z","shell.execute_reply.started":"2023-10-19T09:11:28.015227Z","shell.execute_reply":"2023-10-19T09:11:28.018781Z"},"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\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-19T11:00:37.015701Z","iopub.execute_input":"2023-10-19T11:00:37.016084Z","iopub.status.idle":"2023-10-19T11:00:49.545499Z","shell.execute_reply.started":"2023-10-19T11:00:37.016053Z","shell.execute_reply":"2023-10-19T11:00:49.544415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-19T09:30:40.218681Z","iopub.execute_input":"2023-10-19T09:30:40.219222Z","iopub.status.idle":"2023-10-19T09:30:40.223356Z","shell.execute_reply.started":"2023-10-19T09:30:40.219192Z","shell.execute_reply":"2023-10-19T09:30:40.222408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Image Data Preprocessing ","metadata":{}},{"cell_type":"code","source":"path = \"/kaggle/input/UBC-OCEAN/test.csv\"\ntest_df = pd.read_csv(path)\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:00:54.89571Z","iopub.execute_input":"2023-10-19T11:00:54.896119Z","iopub.status.idle":"2023-10-19T11:00:54.92553Z","shell.execute_reply.started":"2023-10-19T11:00:54.896085Z","shell.execute_reply":"2023-10-19T11:00:54.92437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['is_tma']=((test_df['image_width'] < 5000) & (test_df['image_height'] < 5000))\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:00:55.490928Z","iopub.execute_input":"2023-10-19T11:00:55.491627Z","iopub.status.idle":"2023-10-19T11:00:55.506463Z","shell.execute_reply.started":"2023-10-19T11:00:55.491583Z","shell.execute_reply":"2023-10-19T11:00:55.505355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['is_tma']=test_df['is_tma'].astype(\"int8\")\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:00:57.296671Z","iopub.execute_input":"2023-10-19T11:00:57.297091Z","iopub.status.idle":"2023-10-19T11:00:57.306344Z","shell.execute_reply.started":"2023-10-19T11:00:57.297025Z","shell.execute_reply":"2023-10-19T11:00:57.305535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.MAX_IMAGE_PIXELS = 10000000000\n\nimage_data_1 = []\n#empty_img=0\nfor img_id, tma in zip(test_df['image_id'], test_df['is_tma']):\n    #print(img_id, label,  tma)\n    if tma==0:\n        img_name = str(img_id)+\"_thumbnail.png\"\n        image = Image.open(\"/kaggle/input/UBC-OCEAN/test_thumbnails/\"+img_name)\n        image = image.resize((512,512))\n        image = np.array(image)\n        image_data_1.append(image)\n        \n        \n    elif tma==1:\n        img_name = str(img_id)+\".png\"\n        image = Image.open(\"/kaggle/input/UBC-OCEAN/test_images/\"+img_name)\n        image = image.resize((512,512))\n        image = np.array(image)\n        image_data_1.append(image)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:01:04.618909Z","iopub.execute_input":"2023-10-19T11:01:04.619319Z","iopub.status.idle":"2023-10-19T11:01:04.945848Z","shell.execute_reply.started":"2023-10-19T11:01:04.619285Z","shell.execute_reply":"2023-10-19T11:01:04.944775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(image_data_1)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:01:05.933539Z","iopub.execute_input":"2023-10-19T11:01:05.933894Z","iopub.status.idle":"2023-10-19T11:01:05.941609Z","shell.execute_reply.started":"2023-10-19T11:01:05.933868Z","shell.execute_reply":"2023-10-19T11:01:05.940511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(image_data_1[0])","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:01:10.227931Z","iopub.execute_input":"2023-10-19T11:01:10.22894Z","iopub.status.idle":"2023-10-19T11:01:10.235695Z","shell.execute_reply.started":"2023-10-19T11:01:10.2289Z","shell.execute_reply":"2023-10-19T11:01:10.234573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img_name = str(41)+\".png\"\n# image = Image.open(\"/kaggle/input/UBC-OCEAN/test_images/\"+img_name)\n# image = image.resize((512,512))\n# image = np.array(image)\n# image.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:05:08.22693Z","iopub.execute_input":"2023-10-19T11:05:08.227309Z","iopub.status.idle":"2023-10-19T11:05:08.232322Z","shell.execute_reply.started":"2023-10-19T11:05:08.227279Z","shell.execute_reply":"2023-10-19T11:05:08.230603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:05:08.736956Z","iopub.execute_input":"2023-10-19T11:05:08.737362Z","iopub.status.idle":"2023-10-19T11:05:08.742Z","shell.execute_reply.started":"2023-10-19T11:05:08.737332Z","shell.execute_reply":"2023-10-19T11:05:08.740882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert into Array","metadata":{}},{"cell_type":"code","source":"test_data = np.array(image_data_1)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:01:40.05266Z","iopub.execute_input":"2023-10-19T11:01:40.053593Z","iopub.status.idle":"2023-10-19T11:01:40.057975Z","shell.execute_reply.started":"2023-10-19T11:01:40.053556Z","shell.execute_reply":"2023-10-19T11:01:40.056791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:02:11.623147Z","iopub.execute_input":"2023-10-19T11:02:11.623569Z","iopub.status.idle":"2023-10-19T11:02:11.630244Z","shell.execute_reply.started":"2023-10-19T11:02:11.623532Z","shell.execute_reply":"2023-10-19T11:02:11.629093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-19T03:16:04.714451Z","iopub.execute_input":"2023-10-19T03:16:04.714865Z","iopub.status.idle":"2023-10-19T03:16:04.730536Z","shell.execute_reply.started":"2023-10-19T03:16:04.714837Z","shell.execute_reply":"2023-10-19T03:16:04.729206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Image Visualization","metadata":{}},{"cell_type":"code","source":"# plt.figure(figsize=(6,6))\n# for i in range(1):\n#     plt.subplot(1,1,i+1)\n#     plt.imshow(test_data[i])","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:02:35.532057Z","iopub.execute_input":"2023-10-19T11:02:35.532457Z","iopub.status.idle":"2023-10-19T11:02:35.536416Z","shell.execute_reply.started":"2023-10-19T11:02:35.532424Z","shell.execute_reply":"2023-10-19T11:02:35.535689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load The Model ","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as hub","metadata":{"execution":{"iopub.status.busy":"2023-10-19T03:16:04.748682Z","iopub.execute_input":"2023-10-19T03:16:04.749763Z","iopub.status.idle":"2023-10-19T03:16:04.765136Z","shell.execute_reply.started":"2023-10-19T03:16:04.749715Z","shell.execute_reply":"2023-10-19T03:16:04.763891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Recreate the exact same model, including its weights and the optimizer\n# path = \"/kaggle/input/mobilenet-model/mobilenet_model_1.h5\"\n# path2 = \"/kaggle/input/mobilenet-model-2/mobilenet_model_2.h5\"\n# path3 = \"/kaggle/input/mobilenet-model-3/mobilenet_model_3.h5\"\npath4 = \"/kaggle/input/new-train-dataset/EfficientNet_512x512_model_5.h5\"\nmy_model = tf.keras.models.load_model(\n       (path4),\n       custom_objects={'KerasLayer':hub.KerasLayer}\n)\n# Show the model architecture\nmy_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:02:46.495547Z","iopub.execute_input":"2023-10-19T11:02:46.495905Z","iopub.status.idle":"2023-10-19T11:03:30.447322Z","shell.execute_reply.started":"2023-10-19T11:02:46.495878Z","shell.execute_reply":"2023-10-19T11:03:30.44598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predicting Testing Images for Submission","metadata":{}},{"cell_type":"code","source":"## Scale The Data\ntest_data_scaled = test_data/255","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:03:55.343307Z","iopub.execute_input":"2023-10-19T11:03:55.343704Z","iopub.status.idle":"2023-10-19T11:03:55.350936Z","shell.execute_reply.started":"2023-10-19T11:03:55.34367Z","shell.execute_reply":"2023-10-19T11:03:55.349853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_scaled.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:03:56.339891Z","iopub.execute_input":"2023-10-19T11:03:56.340923Z","iopub.status.idle":"2023-10-19T11:03:56.34766Z","shell.execute_reply.started":"2023-10-19T11:03:56.340887Z","shell.execute_reply":"2023-10-19T11:03:56.346169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\n\nclass_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC','Other']","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:03:57.948794Z","iopub.execute_input":"2023-10-19T11:03:57.949983Z","iopub.status.idle":"2023-10-19T11:03:57.956082Z","shell.execute_reply.started":"2023-10-19T11:03:57.949938Z","shell.execute_reply":"2023-10-19T11:03:57.954781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Final submission","metadata":{}},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:04:00.620832Z","iopub.execute_input":"2023-10-19T11:04:00.621227Z","iopub.status.idle":"2023-10-19T11:04:00.631252Z","shell.execute_reply.started":"2023-10-19T11:04:00.621196Z","shell.execute_reply":"2023-10-19T11:04:00.630103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = my_model.predict(test_data_scaled)\ny_pred_test = [class_labels[np.argmax(i)] for i in y_pred]","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:04:03.811198Z","iopub.execute_input":"2023-10-19T11:04:03.811717Z","iopub.status.idle":"2023-10-19T11:04:11.862679Z","shell.execute_reply.started":"2023-10-19T11:04:03.811671Z","shell.execute_reply":"2023-10-19T11:04:11.861495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_pred_test","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:04:11.86502Z","iopub.execute_input":"2023-10-19T11:04:11.866269Z","iopub.status.idle":"2023-10-19T11:04:11.874886Z","shell.execute_reply.started":"2023-10-19T11:04:11.866233Z","shell.execute_reply":"2023-10-19T11:04:11.873569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming 'test_ids' are the IDs of test samples, and 'predictions' are the predicted values\nsubmission = pd.DataFrame({'image_id': test_df['image_id'] , 'label': y_pred_test })\nsubmission.to_csv('submission.csv', index=False)  # Save the CSV file","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:04:25.913908Z","iopub.execute_input":"2023-10-19T11:04:25.914312Z","iopub.status.idle":"2023-10-19T11:04:25.922817Z","shell.execute_reply.started":"2023-10-19T11:04:25.914283Z","shell.execute_reply":"2023-10-19T11:04:25.921362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_submission = pd.read_csv(\"submission.csv\")\nfinal_submission","metadata":{"execution":{"iopub.status.busy":"2023-10-19T11:04:27.255831Z","iopub.execute_input":"2023-10-19T11:04:27.256215Z","iopub.status.idle":"2023-10-19T11:04:27.268824Z","shell.execute_reply.started":"2023-10-19T11:04:27.256184Z","shell.execute_reply":"2023-10-19T11:04:27.267436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}