{"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-30T09:30:50.574117Z","iopub.execute_input":"2023-10-30T09:30:50.57477Z","iopub.status.idle":"2023-10-30T09:30:50.607682Z","shell.execute_reply.started":"2023-10-30T09:30:50.574735Z","shell.execute_reply":"2023-10-30T09:30:50.606236Z"},"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-30T09:30:50.619558Z","iopub.execute_input":"2023-10-30T09:30:50.620404Z","iopub.status.idle":"2023-10-30T09:31:04.291882Z","shell.execute_reply.started":"2023-10-30T09:30:50.620368Z","shell.execute_reply":"2023-10-30T09:31:04.290588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC','Other']","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.293917Z","iopub.execute_input":"2023-10-30T09:31:04.294566Z","iopub.status.idle":"2023-10-30T09:31:04.299765Z","shell.execute_reply.started":"2023-10-30T09:31:04.294526Z","shell.execute_reply":"2023-10-30T09:31:04.298113Z"},"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-30T09:31:04.300993Z","iopub.execute_input":"2023-10-30T09:31:04.301291Z","iopub.status.idle":"2023-10-30T09:31:04.347321Z","shell.execute_reply.started":"2023-10-30T09:31:04.301265Z","shell.execute_reply":"2023-10-30T09:31:04.346183Z"},"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-30T09:31:04.350198Z","iopub.execute_input":"2023-10-30T09:31:04.350587Z","iopub.status.idle":"2023-10-30T09:31:04.366901Z","shell.execute_reply.started":"2023-10-30T09:31:04.350554Z","shell.execute_reply":"2023-10-30T09:31:04.365811Z"},"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-30T09:31:04.368301Z","iopub.execute_input":"2023-10-30T09:31:04.369302Z","iopub.status.idle":"2023-10-30T09:31:04.383513Z","shell.execute_reply.started":"2023-10-30T09:31:04.369267Z","shell.execute_reply":"2023-10-30T09:31:04.382389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image.MAX_IMAGE_PIXELS = 10000000000\n# # Define patch size and overlap (if needed)\n# patch_size = (224,224)  # Adjust this according to your requirements\n# overlap = 0  # Adjust this if you want overlapping patches\n# num=0\n# #image_data_1 = []\n# empty_img=0\n# for 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#         large_image = Image.open(\"/kaggle/input/UBC-OCEAN/test_thumbnails/\"+img_name)\n#         #single_image_patches = []\n#         os.mkdir(f\"/kaggle/working/Test_Images/{img_id}\")\n#         for y in range(0, large_image.height, patch_size[0] - overlap): # (0,2523,192)\n#             for x in range(0, large_image.width, patch_size[1] - overlap):  # (0,3000,192)  224-32=192\n#                 patch = large_image.crop((x, y, x+patch_size[1], y+patch_size[0]))\n#                 image = np.array(patch)\n#                 if np.sum(image)==0:\n#                     empty_img+=1\n#                 elif (np.sum(image[0:,0:50])==0) or (np.sum(image[0:,180:])==0) or (np.sum(image[0:50,130:180])==0) or (np.sum(image[180:,130:180])==0) or (np.sum(image[0:50,0:])==0) or (np.sum(image[180:,0:])==0):\n#                     empty_img+=1\n#                 elif (np.sum(image[0:50,0:50])==0) or (np.sum(image[0:50,180:])==0) or (np.sum(image[180:,0:50])==0) or (np.sum(image[180:,180:])==0) or (np.sum(image[130:180,0:50])==0) or (np.sum(image[130:180,180:])==0):\n#                     empty_img+=1\n              \n\n#                 else:\n#                     image = Image.fromarray(image)\n#                     image.save(f\"/kaggle/working/Test_Images/{img_id}/{num}.png\")\n#                     num+=1\n#         # Close the large image\n#         large_image.close()\n#                     #single_image_patches.append(image)\n#         #image_data_1.append(single_image_patches)\n        \n#     elif tma==1:\n#         img_name = str(img_id)+\".png\"\n#         large_image = Image.open(\"/kaggle/input/UBC-OCEAN/test_images/\"+img_name)\n#         os.mkdir(f\"/kaggle/working/Test_Images/{img_id}\")\n#         for y in range(0, large_image.height, patch_size[0] - overlap): # (0,2523,192)\n#             for x in range(0, large_image.width, patch_size[1] - overlap):  # (0,3000,192)  224-32=192\n#                 patch = large_image.crop((x, y, x+patch_size[1], y+patch_size[0]))\n#                 image = np.array(patch)\n#                 if np.sum(image)==0:\n#                     empty_img+=1\n#                 elif (np.sum(image[0:,0:50])==0) or (np.sum(image[0:,180:])==0) or (np.sum(image[0:50,130:180])==0) or (np.sum(image[180:,130:180])==0) or (np.sum(image[0:50,0:])==0) or (np.sum(image[180:,0:])==0):\n#                     empty_img+=1\n#                 elif (np.sum(image[0:50,0:50])==0) or (np.sum(image[0:50,180:])==0) or (np.sum(image[180:,0:50])==0) or (np.sum(image[180:,180:])==0) or (np.sum(image[130:180,0:50])==0) or (np.sum(image[130:180,180:])==0):\n#                     empty_img+=1\n                 \n#                 else:\n#                     image = Image.fromarray(image)\n#                     image.save(f\"/kaggle/working/Test_Images/{img_id}/{num}.png\")\n#                     num+=1\n#         # Close the large image\n#         large_image.close()\n#                     #single_image_patches.append(image)\n#         #image_data_1.append(single_image_patches)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.385191Z","iopub.execute_input":"2023-10-30T09:31:04.385937Z","iopub.status.idle":"2023-10-30T09:31:04.396355Z","shell.execute_reply.started":"2023-10-30T09:31:04.385893Z","shell.execute_reply":"2023-10-30T09:31:04.395234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####################### OK ###########################\n\n# Image.MAX_IMAGE_PIXELS = 10000000000\n# # Define patch size and overlap (if needed)\n# patch_size = (128,128)  # Adjust this according to your requirements\n# overlap = 0  # Adjust this if you want overlapping patches\n# num=0\n# #image_data_1 = []\n# empty_img=0\n# for 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#         large_image = Image.open(\"/kaggle/input/UBC-OCEAN/test_thumbnails/\"+img_name)\n#         #single_image_patches = []\n#         os.mkdir(f\"/kaggle/working/Test_Images/{img_id}\")\n#         for y in range(0, large_image.height, patch_size[0] - overlap): # (0,2523,192)\n#             for x in range(0, large_image.width, patch_size[1] - overlap):  # (0,3000,192)  224-32=192\n#                 patch = large_image.crop((x, y, x+patch_size[1], y+patch_size[0]))\n#                 image = np.array(patch)\n#                 if np.sum(image)==0:\n#                     empty_img+=1\n#                 elif (np.sum(image[0:,0:25])==0) or (np.sum(image[0:,105:])==0) or (np.sum(image[0:25,0:])==0) or (np.sum(image[105:,0:])==0) or (np.sum(image[0:,50:75])==0) or (np.sum(image[50:75,0:])==0):\n#                     empty_img+=1\n#                 elif (np.sum(image[0:25,0:25])==0) or (np.sum(image[0:25,105:])==0) or (np.sum(image[105:,0:25])==0) or (np.sum(image[105:,105:])==0) or (np.sum(image[50:75,50:75])==0): # or (np.sum(image[130:180,180:])==0):\n#                     empty_img+=1\n#                 elif (np.sum(image[0:10,0:10])==0) or (np.sum(image[0:10,115:])==0) or (np.sum(image[115:,0:10])==0) or (np.sum(image[115:,115:])==0) or (np.sum(image[60:75,60:75])==0) or (np.sum(image[60:75,115:])==0):\n#                     empty_img+=1\n\n#                 else:\n#                     image = Image.fromarray(image)\n#                     image.save(f\"/kaggle/working/Test_Images/{img_id}/{num}.png\")\n#                     num+=1\n#         # Close the large image\n#         large_image.close()\n#                     #single_image_patches.append(image)\n#         #image_data_1.append(single_image_patches)\n        \n#     elif tma==1:\n#         img_name = str(img_id)+\".png\"\n#         large_image = Image.open(\"/kaggle/input/UBC-OCEAN/test_images/\"+img_name)\n#         os.mkdir(f\"/kaggle/working/Test_Images/{img_id}\")\n#         for y in range(0, large_image.height, patch_size[0] - overlap): # (0,2523,192)\n#             for x in range(0, large_image.width, patch_size[1] - overlap):  # (0,3000,192)  224-32=192\n#                 patch = large_image.crop((x, y, x+patch_size[1], y+patch_size[0]))\n#                 image = np.array(patch)\n#                 if np.sum(image)==0:\n#                     empty_img+=1\n#                 elif (np.sum(image[0:,0:25])==0) or (np.sum(image[0:,105:])==0) or (np.sum(image[0:25,0:])==0) or (np.sum(image[105:,0:])==0) or (np.sum(image[0:,50:75])==0) or (np.sum(image[50:75,0:])==0):\n#                     empty_img+=1\n#                 elif (np.sum(image[0:25,0:25])==0) or (np.sum(image[0:25,105:])==0) or (np.sum(image[105:,0:25])==0) or (np.sum(image[105:,105:])==0) or (np.sum(image[50:75,50:75])==0): # or (np.sum(image[130:180,180:])==0):\n#                     empty_img+=1\n#                 elif (np.sum(image[0:10,0:10])==0) or (np.sum(image[0:10,115:])==0) or (np.sum(image[115:,0:10])==0) or (np.sum(image[115:,115:])==0) or (np.sum(image[60:75,60:75])==0) or (np.sum(image[60:75,115:])==0):\n#                     empty_img+=1\n                 \n#                 else:\n#                     image = Image.fromarray(image)\n#                     image.save(f\"/kaggle/working/Test_Images/{img_id}/{num}.png\")\n#                     num+=1\n#         # Close the large image\n#         large_image.close()\n#                     #single_image_patches.append(image)\n#         #image_data_1.append(single_image_patches)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.398014Z","iopub.execute_input":"2023-10-30T09:31:04.398939Z","iopub.status.idle":"2023-10-30T09:31:04.414886Z","shell.execute_reply.started":"2023-10-30T09:31:04.398892Z","shell.execute_reply":"2023-10-30T09:31:04.413442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Images Visualization","metadata":{}},{"cell_type":"code","source":"Image.MAX_IMAGE_PIXELS = 10000000000\n# Define patch size and overlap (if needed)\npatch_size = (128,128)  # Adjust this according to your requirements\noverlap = 0  # Adjust this if you want overlapping patches\nnum=0\nimage_data_1 = []\nempty_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        large_image = Image.open(\"/kaggle/input/UBC-OCEAN/test_thumbnails/\"+img_name)\n        single_image_patches = []\n        #os.mkdir(f\"/kaggle/working/Test_Images/{img_id}\")\n        for y in range(0, large_image.height//2, patch_size[0] - overlap): # (0,2523,192)\n            for x in range(0, large_image.width//2, patch_size[1] - overlap):  # (0,3000,192)  224-32=192\n                patch = large_image.crop((x, y, x+patch_size[1], y+patch_size[0]))\n                image = np.array(patch)\n                if np.sum(image)==0:\n                    empty_img+=1\n                elif (np.sum(image[0:,0:25])==0) or (np.sum(image[0:,105:])==0) or (np.sum(image[0:25,0:])==0) or (np.sum(image[105:,0:])==0) or (np.sum(image[0:,50:75])==0) or (np.sum(image[50:75,0:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:25,0:25])==0) or (np.sum(image[0:25,105:])==0) or (np.sum(image[105:,0:25])==0) or (np.sum(image[105:,105:])==0) or (np.sum(image[50:75,50:75])==0): # or (np.sum(image[130:180,180:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:10,0:10])==0) or (np.sum(image[0:10,115:])==0) or (np.sum(image[115:,0:10])==0) or (np.sum(image[115:,115:])==0) or (np.sum(image[60:75,60:75])==0) or (np.sum(image[60:75,115:])==0):\n                    empty_img+=1\n\n                else:\n                    single_image_patches.append(image)\n        image_data_1.append(single_image_patches)\n        large_image.close()\n        \n    elif tma==1:\n        img_name = str(img_id)+\".png\"\n        large_image = Image.open(\"/kaggle/input/UBC-OCEAN/test_images/\"+img_name)\n        single_image_patches = []\n        #os.mkdir(f\"/kaggle/working/Test_Images/{img_id}\")\n        for y in range(0, large_image.height//2, patch_size[0] - overlap): # (0,2523,192)\n            for x in range(0, large_image.width//2, patch_size[1] - overlap):  # (0,3000,192)  224-32=192\n                patch = large_image.crop((x, y, x+patch_size[1], y+patch_size[0]))\n                image = np.array(patch)\n                if np.sum(image)==0:\n                    empty_img+=1\n                elif (np.sum(image[0:,0:25])==0) or (np.sum(image[0:,105:])==0) or (np.sum(image[0:25,0:])==0) or (np.sum(image[105:,0:])==0) or (np.sum(image[0:,50:75])==0) or (np.sum(image[50:75,0:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:25,0:25])==0) or (np.sum(image[0:25,105:])==0) or (np.sum(image[105:,0:25])==0) or (np.sum(image[105:,105:])==0) or (np.sum(image[50:75,50:75])==0): # or (np.sum(image[130:180,180:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:10,0:10])==0) or (np.sum(image[0:10,115:])==0) or (np.sum(image[115:,0:10])==0) or (np.sum(image[115:,115:])==0) or (np.sum(image[60:75,60:75])==0) or (np.sum(image[60:75,115:])==0):\n                    empty_img+=1\n                 \n                else:\n                    single_image_patches.append(image)\n        image_data_1.append(single_image_patches)\n        large_image.close()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.41698Z","iopub.execute_input":"2023-10-30T09:31:04.417343Z","iopub.status.idle":"2023-10-30T09:31:04.837614Z","shell.execute_reply.started":"2023-10-30T09:31:04.417312Z","shell.execute_reply":"2023-10-30T09:31:04.836604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#len(image_data_1[0])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.839039Z","iopub.execute_input":"2023-10-30T09:31:04.83935Z","iopub.status.idle":"2023-10-30T09:31:04.844579Z","shell.execute_reply.started":"2023-10-30T09:31:04.839323Z","shell.execute_reply":"2023-10-30T09:31:04.843675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image_data_1[0][0].shape","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.85003Z","iopub.execute_input":"2023-10-30T09:31:04.850446Z","iopub.status.idle":"2023-10-30T09:31:04.85645Z","shell.execute_reply.started":"2023-10-30T09:31:04.850414Z","shell.execute_reply":"2023-10-30T09:31:04.855053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(36,60))\n# i=1\n# for img in image_data_1[0]:\n#     plt.subplot(10,6,i)\n#     plt.imshow(img)\n#     i+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.858129Z","iopub.execute_input":"2023-10-30T09:31:04.859021Z","iopub.status.idle":"2023-10-30T09:31:04.867533Z","shell.execute_reply.started":"2023-10-30T09:31:04.858978Z","shell.execute_reply":"2023-10-30T09:31:04.866284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# id_folder_41 = os.listdir(\"/kaggle/working/Test_Images/41\")\n# len(id_folder_41)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.869098Z","iopub.execute_input":"2023-10-30T09:31:04.869439Z","iopub.status.idle":"2023-10-30T09:31:04.87822Z","shell.execute_reply.started":"2023-10-30T09:31:04.869411Z","shell.execute_reply":"2023-10-30T09:31:04.877099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(large_image.height)\n# print(large_image.width)\n# print(large_image.height//6)\n# print(large_image.width-large_image.height//6)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.880052Z","iopub.execute_input":"2023-10-30T09:31:04.88055Z","iopub.status.idle":"2023-10-30T09:31:04.890586Z","shell.execute_reply.started":"2023-10-30T09:31:04.880516Z","shell.execute_reply":"2023-10-30T09:31:04.889456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#len(os.listdir(\"/kaggle/working/Test_Images/41\"))","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.892108Z","iopub.execute_input":"2023-10-30T09:31:04.893216Z","iopub.status.idle":"2023-10-30T09:31:04.900149Z","shell.execute_reply.started":"2023-10-30T09:31:04.893182Z","shell.execute_reply":"2023-10-30T09:31:04.899231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(50,50))\n# path = os.listdir(\"/kaggle/working/Test_Images/41\")\n# i=1\n# for img in path:\n#     image = Image.open(\"/kaggle/working/Test_Images/41/\"+img)\n#     image = np.array(image)\n#     #print(image.shape)\n#     plt.subplot(10,10,i)\n#     plt.imshow(image)\n#     plt.axis(\"off\")\n#     i+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.90159Z","iopub.execute_input":"2023-10-30T09:31:04.901923Z","iopub.status.idle":"2023-10-30T09:31:04.911956Z","shell.execute_reply.started":"2023-10-30T09:31:04.901894Z","shell.execute_reply":"2023-10-30T09:31:04.910755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_folder = os.listdir(\"/kaggle/working/Test_Images\")\n# print(len(test_folder))\n# img_id = os.listdir(\"/kaggle/working/Test_Images/41\")\n# print(len(img_id))","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.91359Z","iopub.execute_input":"2023-10-30T09:31:04.913939Z","iopub.status.idle":"2023-10-30T09:31:04.928257Z","shell.execute_reply.started":"2023-10-30T09:31:04.913908Z","shell.execute_reply":"2023-10-30T09:31:04.926991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path = \"/kaggle/working/Test_Images/41\"\n# img_id = os.listdir(\"/kaggle/working/Test_Images/41\")\n\n# image = tf.io.read_file(\"/kaggle/working/Test_Images/41/2.png\")\n# image = tf.image.decode_jpeg(image, channels=3)\n# image = tf.image.convert_image_dtype(image,tf.float32)\n# image = tf.image.resize(image,size = [224,224])\n# image","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:04.92943Z","iopub.execute_input":"2023-10-30T09:31:04.929847Z","iopub.status.idle":"2023-10-30T09:31:04.94018Z","shell.execute_reply.started":"2023-10-30T09:31:04.929816Z","shell.execute_reply":"2023-10-30T09:31:04.938947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#path = \"/kaggle/input/train-data-224x224-patch-images-dataset-and-model/mobilenet_model_224x224_Acc_1.h5\"\npath = \"/kaggle/input/train-128x128-patch-images-imagedatagenerator/MobileNet_V2_128x128_29_10.h5\"\n\nmy_model = tf.keras.models.load_model(\n       (path),\n       custom_objects={'KerasLayer':hub.KerasLayer}\n)\n# Show the model architecture\nmy_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:04.533259Z","iopub.execute_input":"2023-10-30T09:33:04.533792Z","iopub.status.idle":"2023-10-30T09:33:08.964172Z","shell.execute_reply.started":"2023-10-30T09:33:04.533748Z","shell.execute_reply":"2023-10-30T09:33:08.963056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:08.966633Z","iopub.execute_input":"2023-10-30T09:33:08.967597Z","iopub.status.idle":"2023-10-30T09:33:08.974168Z","shell.execute_reply.started":"2023-10-30T09:33:08.967552Z","shell.execute_reply":"2023-10-30T09:33:08.973243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path = \"/kaggle/working/Test_Images/41/7.png\"\n# image = load_img(path)\n# img_array = img_to_array(image)\n# print(img_array.shape)\n# img_tensor = tf.convert_to_tensor(img_array,dtype=tf.float32)/255\n# img_tensor","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.955701Z","iopub.status.idle":"2023-10-30T09:31:05.956275Z","shell.execute_reply.started":"2023-10-30T09:31:05.956011Z","shell.execute_reply":"2023-10-30T09:31:05.956036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# a = np.array([1,3])\n# np.random.choice(a,3)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.958143Z","iopub.status.idle":"2023-10-30T09:31:05.959154Z","shell.execute_reply.started":"2023-10-30T09:31:05.958938Z","shell.execute_reply":"2023-10-30T09:31:05.958962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_patch_image_array_folder = []\nfor i in image_data_1:   # [[ar1,ar2,......]]\n    single_image = np.array(i)\n    single_image = single_image/255\n    test_patch_image_array_folder.append(single_image)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:11.574011Z","iopub.execute_input":"2023-10-30T09:33:11.574387Z","iopub.status.idle":"2023-10-30T09:33:11.592394Z","shell.execute_reply.started":"2023-10-30T09:33:11.574356Z","shell.execute_reply":"2023-10-30T09:33:11.591317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_patch_image_array_folder[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:12.136175Z","iopub.execute_input":"2023-10-30T09:33:12.136843Z","iopub.status.idle":"2023-10-30T09:33:12.144844Z","shell.execute_reply.started":"2023-10-30T09:33:12.136799Z","shell.execute_reply":"2023-10-30T09:33:12.143604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #id_no = []\n# img_id_data = []\n# test_prediction = []\n# for img_id  in os.listdir(\"/kaggle/working/Test_Images\"):\n#     #print(img_id)  # 41\n#     #id_no.append(int(img_id))\n#     path = os.path.join(\"/kaggle/working/Test_Images\",img_id)\n#     #print(path)    # /kaggle/working/Test_Images/41\n#     list_folder = os.listdir(path)\n#     if len(list_folder)>50:\n#         a=50\n#     elif len(list_folder)<50:\n#         a=len(list_folder)\n#     #print(a)  # 50\n#     for img in np.random.choice(list_folder,a):\n#         #print(img)  # 2.png\n#         image = Image.open(path+\"/\"+img)\n#         image = np.array(image)\n#         #print(image.shape)\n#         img_id_data.append(image)\n        \n#     img_id_data_array = np.array(img_id_data)\n#     #img_id_data_array = img_id_data_array/255    # Single id image array (63,224,224,3)\n#     test_img_tensor = tf.convert_to_tensor(img_id_data_array,dtype=tf.float32)/255\n\n    \n#     output = my_model.predict(test_img_tensor)\n#     output_1 = [np.argmax(i) for i in output]\n#     #print(output_1) #= [np.argmax(j) for j in output]\n    \n#     ## Use Counter to find most common\n#     count = Counter(output_1)\n#     result = count.most_common(1)[0][0]\n#     #print(result)\n#     test_prediction.append(class_labels[result])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.965869Z","iopub.status.idle":"2023-10-30T09:31:05.96671Z","shell.execute_reply.started":"2023-10-30T09:31:05.966413Z","shell.execute_reply":"2023-10-30T09:31:05.96644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#count.most_common()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.968016Z","iopub.status.idle":"2023-10-30T09:31:05.969048Z","shell.execute_reply.started":"2023-10-30T09:31:05.968757Z","shell.execute_reply":"2023-10-30T09:31:05.968787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_prediction","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.971058Z","iopub.status.idle":"2023-10-30T09:31:05.971468Z","shell.execute_reply.started":"2023-10-30T09:31:05.971266Z","shell.execute_reply":"2023-10-30T09:31:05.971284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_submission_label = []\nfor i in test_patch_image_array_folder:\n    output = my_model.predict(i)\n    output = [np.argmax(j) for j in output]\n    \n    ## Use Counter to find most common\n    count = Counter(output)\n    result = count.most_common(1)[0][0]\n    \n    test_submission_label.append(class_labels[result])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:16.54017Z","iopub.execute_input":"2023-10-30T09:33:16.54057Z","iopub.status.idle":"2023-10-30T09:33:17.925851Z","shell.execute_reply.started":"2023-10-30T09:33:16.540533Z","shell.execute_reply":"2023-10-30T09:33:17.924891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#count.most_common()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:22.080754Z","iopub.execute_input":"2023-10-30T09:33:22.081502Z","iopub.status.idle":"2023-10-30T09:33:22.090632Z","shell.execute_reply.started":"2023-10-30T09:33:22.081448Z","shell.execute_reply":"2023-10-30T09:33:22.089474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_submission_label","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:27.500453Z","iopub.execute_input":"2023-10-30T09:33:27.500863Z","iopub.status.idle":"2023-10-30T09:33:27.507667Z","shell.execute_reply.started":"2023-10-30T09:33:27.50083Z","shell.execute_reply":"2023-10-30T09:33:27.506656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# output = my_model.predict(test_patch_image_array_folder[0])\n# y_pred = [np.argmax(j) for j in output]\n# y_pred[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.979669Z","iopub.status.idle":"2023-10-30T09:31:05.980134Z","shell.execute_reply.started":"2023-10-30T09:31:05.979921Z","shell.execute_reply":"2023-10-30T09:31:05.979941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#type(image_data_1[0])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.981833Z","iopub.status.idle":"2023-10-30T09:31:05.982253Z","shell.execute_reply.started":"2023-10-30T09:31:05.982047Z","shell.execute_reply":"2023-10-30T09:31:05.982066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(30,165))\n# i=0\n# for l1 in image_data_1[0]:\n#     plt.subplot(33,6,i+1)\n#     plt.imshow(l1)\n#     plt.title(f\"Label:{class_labels[y_pred[i]]}\")\n#     i+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.983613Z","iopub.status.idle":"2023-10-30T09:31:05.984064Z","shell.execute_reply.started":"2023-10-30T09:31:05.983866Z","shell.execute_reply":"2023-10-30T09:31:05.983885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ## list of patch image array\n# test_patch_image_array_folder = []\n# for img in image_data_1:\n#     single_image = np.array(img)\n#     test_patch_image_array_folder.append(single_image)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.985108Z","iopub.status.idle":"2023-10-30T09:31:05.985509Z","shell.execute_reply.started":"2023-10-30T09:31:05.985317Z","shell.execute_reply":"2023-10-30T09:31:05.985336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(40,40))\n# j=1\n# for i in test_patch_image_array_folder[0][:100]:\n#     plt.subplot(10,10,j)\n#     plt.imshow(i)\n#     j+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.98702Z","iopub.status.idle":"2023-10-30T09:31:05.987571Z","shell.execute_reply.started":"2023-10-30T09:31:05.987283Z","shell.execute_reply":"2023-10-30T09:31:05.987309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load The Model ","metadata":{}},{"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\"\n# path4 = \"/kaggle/input/mobilenet-model-4/mobilenet_model_4.h5\"\n# my_model = tf.keras.models.load_model(\n#        (path4),\n#        custom_objects={'KerasLayer':hub.KerasLayer}\n# )\n# Show the model architecture\n# my_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.989191Z","iopub.status.idle":"2023-10-30T09:31:05.989833Z","shell.execute_reply.started":"2023-10-30T09:31:05.989522Z","shell.execute_reply":"2023-10-30T09:31:05.989551Z"},"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":"#test_patch_image_array_folder[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.991632Z","iopub.status.idle":"2023-10-30T09:31:05.992189Z","shell.execute_reply.started":"2023-10-30T09:31:05.991916Z","shell.execute_reply":"2023-10-30T09:31:05.991941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from collections import Counter\n\n#class_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC']","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.996945Z","iopub.status.idle":"2023-10-30T09:31:05.99757Z","shell.execute_reply.started":"2023-10-30T09:31:05.997261Z","shell.execute_reply":"2023-10-30T09:31:05.997291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_submission_label = []\n# for i in test_patch_image_array_folder:\n#     output = my_model.predict(i)\n#     output = [np.argmax(j) for j in output]\n    \n#     ## Use Counter to find most common\n#     count = Counter(output)\n#     result = count.most_common(1)[0][0]\n    \n#     test_submission_label.append(class_labels[result])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:05.99963Z","iopub.status.idle":"2023-10-30T09:31:06.000233Z","shell.execute_reply.started":"2023-10-30T09:31:05.999942Z","shell.execute_reply":"2023-10-30T09:31:05.999969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#count.most_common()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:06.001929Z","iopub.status.idle":"2023-10-30T09:31:06.002498Z","shell.execute_reply.started":"2023-10-30T09:31:06.00221Z","shell.execute_reply":"2023-10-30T09:31:06.002235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_submission_label","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:06.004001Z","iopub.status.idle":"2023-10-30T09:31:06.004581Z","shell.execute_reply.started":"2023-10-30T09:31:06.00428Z","shell.execute_reply":"2023-10-30T09:31:06.004305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Final submission","metadata":{}},{"cell_type":"code","source":"# ## Sample Submission\n# path = \"/kaggle/input/UBC-OCEAN/sample_submission.csv\"\n# sample_df = pd.read_csv(path)\n# sample_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:06.006181Z","iopub.status.idle":"2023-10-30T09:31:06.006762Z","shell.execute_reply.started":"2023-10-30T09:31:06.006456Z","shell.execute_reply":"2023-10-30T09:31:06.006492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:06.008526Z","iopub.status.idle":"2023-10-30T09:31:06.00911Z","shell.execute_reply.started":"2023-10-30T09:31:06.008823Z","shell.execute_reply":"2023-10-30T09:31:06.00885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# y_pred = my_model.predict(test_data)\n# y_pred_test = [class_labels[np.argmax(i)] for i in y_pred]","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:31:06.010654Z","iopub.status.idle":"2023-10-30T09:31:06.01111Z","shell.execute_reply.started":"2023-10-30T09:31:06.010897Z","shell.execute_reply":"2023-10-30T09:31:06.010917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_pred_test\nclass_labels","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:38.178732Z","iopub.execute_input":"2023-10-30T09:33:38.180115Z","iopub.status.idle":"2023-10-30T09:33:38.187637Z","shell.execute_reply.started":"2023-10-30T09:33:38.180063Z","shell.execute_reply":"2023-10-30T09:33:38.186472Z"},"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': test_submission_label })\nsubmission.to_csv('submission.csv', index=False)  # Save the CSV file\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:33:39.49752Z","iopub.execute_input":"2023-10-30T09:33:39.498755Z","iopub.status.idle":"2023-10-30T09:33:39.507248Z","shell.execute_reply.started":"2023-10-30T09:33:39.498703Z","shell.execute_reply":"2023-10-30T09:33:39.506309Z"},"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-30T09:33:40.8183Z","iopub.execute_input":"2023-10-30T09:33:40.818691Z","iopub.status.idle":"2023-10-30T09:33:40.832475Z","shell.execute_reply.started":"2023-10-30T09:33:40.818661Z","shell.execute_reply":"2023-10-30T09:33:40.831127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}