{"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-29T10:43:07.017231Z","iopub.execute_input":"2023-10-29T10:43:07.017539Z","iopub.status.idle":"2023-10-29T10:43:07.021753Z","shell.execute_reply.started":"2023-10-29T10:43:07.017515Z","shell.execute_reply":"2023-10-29T10:43:07.021073Z"},"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-30T06:19:42.528027Z","iopub.execute_input":"2023-10-30T06:19:42.528477Z","iopub.status.idle":"2023-10-30T06:19:42.540809Z","shell.execute_reply.started":"2023-10-30T06:19:42.528437Z","shell.execute_reply":"2023-10-30T06:19:42.539411Z"},"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-30T06:19:42.696447Z","iopub.execute_input":"2023-10-30T06:19:42.696868Z","iopub.status.idle":"2023-10-30T06:19:42.702431Z","shell.execute_reply.started":"2023-10-30T06:19:42.696835Z","shell.execute_reply":"2023-10-30T06:19:42.701091Z"},"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-30T06:19:43.168847Z","iopub.execute_input":"2023-10-30T06:19:43.169744Z","iopub.status.idle":"2023-10-30T06:19:43.204742Z","shell.execute_reply.started":"2023-10-30T06:19:43.169688Z","shell.execute_reply":"2023-10-30T06:19:43.203402Z"},"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-30T06:19:43.356019Z","iopub.execute_input":"2023-10-30T06:19:43.35641Z","iopub.status.idle":"2023-10-30T06:19:43.373Z","shell.execute_reply.started":"2023-10-30T06:19:43.356377Z","shell.execute_reply":"2023-10-30T06:19:43.37195Z"},"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-30T06:19:43.677495Z","iopub.execute_input":"2023-10-30T06:19:43.679199Z","iopub.status.idle":"2023-10-30T06:19:43.712815Z","shell.execute_reply.started":"2023-10-30T06:19:43.679083Z","shell.execute_reply":"2023-10-30T06:19:43.71118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"Test_Images\")","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:19:43.964147Z","iopub.execute_input":"2023-10-30T06:19:43.965128Z","iopub.status.idle":"2023-10-30T06:19:43.970986Z","shell.execute_reply.started":"2023-10-30T06:19:43.965079Z","shell.execute_reply":"2023-10-30T06:19:43.969744Z"},"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-30T06:19:44.858689Z","iopub.execute_input":"2023-10-30T06:19:44.859715Z","iopub.status.idle":"2023-10-30T06:19:44.869219Z","shell.execute_reply.started":"2023-10-30T06:19:44.859665Z","shell.execute_reply":"2023-10-30T06:19:44.868005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n#image_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, 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-30T06:19:53.978801Z","iopub.execute_input":"2023-10-30T06:19:53.979241Z","iopub.status.idle":"2023-10-30T06:19:55.31123Z","shell.execute_reply.started":"2023-10-30T06:19:53.979203Z","shell.execute_reply":"2023-10-30T06:19:55.309622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Images Visualization","metadata":{}},{"cell_type":"code","source":"id_folder_41 = os.listdir(\"/kaggle/working/Test_Images/41\")\nlen(id_folder_41)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:21:37.523349Z","iopub.execute_input":"2023-10-30T06:21:37.523821Z","iopub.status.idle":"2023-10-30T06:21:37.532871Z","shell.execute_reply.started":"2023-10-30T06:21:37.523784Z","shell.execute_reply":"2023-10-30T06:21:37.531652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = os.listdir(\"/kaggle/working/Test_Images/41\")\nplt.figure(figsize=(36,60))\ni=1\nfor img in path[:60]:\n    plt.subplot(10,6,i)\n    image = Image.open(\"/kaggle/working/Test_Images/41/\"+img)\n    #image = np.array(image)\n    plt.imshow(image)\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:20:17.724605Z","iopub.execute_input":"2023-10-30T06:20:17.725194Z","iopub.status.idle":"2023-10-30T06:20:32.714567Z","shell.execute_reply.started":"2023-10-30T06:20:17.725147Z","shell.execute_reply":"2023-10-30T06:20:32.71352Z"},"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-29T10:43:11.663356Z","iopub.status.idle":"2023-10-29T10:43:11.664246Z","shell.execute_reply.started":"2023-10-29T10:43:11.664055Z","shell.execute_reply":"2023-10-29T10:43:11.664078Z"},"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-29T10:43:11.665105Z","iopub.status.idle":"2023-10-29T10:43:11.665355Z","shell.execute_reply.started":"2023-10-29T10:43:11.665226Z","shell.execute_reply":"2023-10-29T10:43:11.665239Z"},"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-29T10:43:11.665941Z","iopub.status.idle":"2023-10-29T10:43:11.667292Z","shell.execute_reply.started":"2023-10-29T10:43:11.667149Z","shell.execute_reply":"2023-10-29T10:43:11.667162Z"},"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-29T10:43:11.668431Z","iopub.status.idle":"2023-10-29T10:43:11.668829Z","shell.execute_reply.started":"2023-10-29T10:43:11.668637Z","shell.execute_reply":"2023-10-29T10:43:11.668655Z"},"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-30T06:23:55.967529Z","iopub.execute_input":"2023-10-30T06:23:55.968024Z","iopub.status.idle":"2023-10-30T06:24:00.413072Z","shell.execute_reply.started":"2023-10-30T06:23:55.967984Z","shell.execute_reply":"2023-10-30T06:24:00.411877Z"},"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-30T06:24:02.161272Z","iopub.execute_input":"2023-10-30T06:24:02.161716Z","iopub.status.idle":"2023-10-30T06:24:02.1684Z","shell.execute_reply.started":"2023-10-30T06:24:02.161679Z","shell.execute_reply":"2023-10-30T06:24:02.167296Z"},"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-29T10:43:11.674511Z","iopub.status.idle":"2023-10-29T10:43:11.67538Z","shell.execute_reply.started":"2023-10-29T10:43:11.675187Z","shell.execute_reply":"2023-10-29T10:43:11.675207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array([1,3])\nnp.random.choice(a,3)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:38:13.477803Z","iopub.execute_input":"2023-10-30T06:38:13.47822Z","iopub.status.idle":"2023-10-30T06:38:13.486272Z","shell.execute_reply.started":"2023-10-30T06:38:13.478188Z","shell.execute_reply":"2023-10-30T06:38:13.485061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#id_no = []\nimg_id_data = []\ntest_prediction = []\nfor 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-30T06:41:06.165895Z","iopub.execute_input":"2023-10-30T06:41:06.166329Z","iopub.status.idle":"2023-10-30T06:41:06.638403Z","shell.execute_reply.started":"2023-10-30T06:41:06.166291Z","shell.execute_reply":"2023-10-30T06:41:06.637465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#count.most_common()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:41:43.167346Z","iopub.execute_input":"2023-10-30T06:41:43.167798Z","iopub.status.idle":"2023-10-30T06:41:43.176241Z","shell.execute_reply.started":"2023-10-30T06:41:43.167762Z","shell.execute_reply":"2023-10-30T06:41:43.174899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_prediction","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:41:56.691674Z","iopub.execute_input":"2023-10-30T06:41:56.692195Z","iopub.status.idle":"2023-10-30T06:41:56.700825Z","shell.execute_reply.started":"2023-10-30T06:41:56.692149Z","shell.execute_reply":"2023-10-30T06:41:56.699482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"50*2000\n# 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-29T10:43:11.683236Z","iopub.execute_input":"2023-10-29T10:43:11.683463Z","iopub.status.idle":"2023-10-29T10:43:11.687199Z","shell.execute_reply.started":"2023-10-29T10:43:11.683442Z","shell.execute_reply":"2023-10-29T10:43:11.686433Z"},"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-29T10:43:11.68845Z","iopub.execute_input":"2023-10-29T10:43:11.689209Z","iopub.status.idle":"2023-10-29T10:43:11.698189Z","shell.execute_reply.started":"2023-10-29T10:43:11.68917Z","shell.execute_reply":"2023-10-29T10:43:11.697524Z"},"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-29T10:43:11.960326Z","iopub.execute_input":"2023-10-29T10:43:11.96066Z","iopub.status.idle":"2023-10-29T10:43:11.964706Z","shell.execute_reply.started":"2023-10-29T10:43:11.960637Z","shell.execute_reply":"2023-10-29T10:43:11.963873Z"},"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-29T10:43:12.019794Z","iopub.execute_input":"2023-10-29T10:43:12.020155Z","iopub.status.idle":"2023-10-29T10:43:12.02331Z","shell.execute_reply.started":"2023-10-29T10:43:12.020135Z","shell.execute_reply":"2023-10-29T10:43:12.022732Z"},"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-29T10:43:15.252692Z","iopub.execute_input":"2023-10-29T10:43:15.253052Z","iopub.status.idle":"2023-10-29T10:43:15.257357Z","shell.execute_reply.started":"2023-10-29T10:43:15.253029Z","shell.execute_reply":"2023-10-29T10:43:15.256223Z"},"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-29T10:43:15.25882Z","iopub.execute_input":"2023-10-29T10:43:15.259043Z","iopub.status.idle":"2023-10-29T10:43:15.26978Z","shell.execute_reply.started":"2023-10-29T10:43:15.259022Z","shell.execute_reply":"2023-10-29T10:43:15.268818Z"},"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-29T10:43:15.959719Z","iopub.execute_input":"2023-10-29T10:43:15.960007Z","iopub.status.idle":"2023-10-29T10:43:15.963827Z","shell.execute_reply.started":"2023-10-29T10:43:15.959986Z","shell.execute_reply":"2023-10-29T10:43:15.962919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#count.most_common()","metadata":{"execution":{"iopub.status.busy":"2023-10-29T10:43:15.974078Z","iopub.execute_input":"2023-10-29T10:43:15.974308Z","iopub.status.idle":"2023-10-29T10:43:15.978006Z","shell.execute_reply.started":"2023-10-29T10:43:15.97429Z","shell.execute_reply":"2023-10-29T10:43:15.977026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_submission_label","metadata":{"execution":{"iopub.status.busy":"2023-10-29T10:43:15.979192Z","iopub.execute_input":"2023-10-29T10:43:15.979414Z","iopub.status.idle":"2023-10-29T10:43:15.987127Z","shell.execute_reply.started":"2023-10-29T10:43:15.979393Z","shell.execute_reply":"2023-10-29T10:43:15.986535Z"},"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-29T10:43:16.003043Z","iopub.execute_input":"2023-10-29T10:43:16.003457Z","iopub.status.idle":"2023-10-29T10:43:16.006756Z","shell.execute_reply.started":"2023-10-29T10:43:16.00343Z","shell.execute_reply":"2023-10-29T10:43:16.006026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-29T10:43:17.342487Z","iopub.execute_input":"2023-10-29T10:43:17.343017Z","iopub.status.idle":"2023-10-29T10:43:17.346717Z","shell.execute_reply.started":"2023-10-29T10:43:17.342989Z","shell.execute_reply":"2023-10-29T10:43:17.345886Z"},"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-29T10:43:17.348726Z","iopub.execute_input":"2023-10-29T10:43:17.349158Z","iopub.status.idle":"2023-10-29T10:43:17.357218Z","shell.execute_reply.started":"2023-10-29T10:43:17.349137Z","shell.execute_reply":"2023-10-29T10:43:17.356441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_pred_test\nclass_labels","metadata":{"execution":{"iopub.status.busy":"2023-10-29T10:43:17.35798Z","iopub.execute_input":"2023-10-29T10:43:17.35819Z","iopub.status.idle":"2023-10-29T10:43:17.3683Z","shell.execute_reply.started":"2023-10-29T10:43:17.358171Z","shell.execute_reply":"2023-10-29T10:43:17.367614Z"},"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_prediction })\nsubmission.to_csv('submission.csv', index=False)  # Save the CSV file\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:42:08.894641Z","iopub.execute_input":"2023-10-30T06:42:08.89504Z","iopub.status.idle":"2023-10-30T06:42:08.903842Z","shell.execute_reply.started":"2023-10-30T06:42:08.895009Z","shell.execute_reply":"2023-10-30T06:42:08.902745Z"},"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-30T06:42:12.641463Z","iopub.execute_input":"2023-10-30T06:42:12.641918Z","iopub.status.idle":"2023-10-30T06:42:12.656834Z","shell.execute_reply.started":"2023-10-30T06:42:12.641881Z","shell.execute_reply":"2023-10-30T06:42:12.655606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.rmtree(\"/kaggle/working/Test_Images\")","metadata":{"execution":{"iopub.status.busy":"2023-10-29T10:43:17.435192Z","iopub.execute_input":"2023-10-29T10:43:17.435753Z","iopub.status.idle":"2023-10-29T10:43:17.439042Z","shell.execute_reply.started":"2023-10-29T10:43:17.435727Z","shell.execute_reply":"2023-10-29T10:43:17.438148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}