{"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":6924515,"sourceType":"competition"},{"sourceId":7313915,"sourceType":"datasetVersion","datasetId":4218186},{"sourceId":156863430,"sourceType":"kernelVersion"}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport os, sys, random\nimport cv2\nfrom keras_core import ops\n\nprint('IMPORTED ALL MODULES!!')\ntf.config.run_functions_eagerly(True)\nfrom tensorflow.python.ops.numpy_ops import np_config\nnp_config.enable_numpy_behavior()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-01T06:40:44.369981Z","iopub.execute_input":"2024-01-01T06:40:44.370534Z","iopub.status.idle":"2024-01-01T06:40:59.539102Z","shell.execute_reply.started":"2024-01-01T06:40:44.370489Z","shell.execute_reply":"2024-01-01T06:40:59.538004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = '/kaggle/input/UBC-OCEAN'\nTEST_DF = '/kaggle/input/UBC-OCEAN/test.csv'","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:40:59.541229Z","iopub.execute_input":"2024-01-01T06:40:59.541847Z","iopub.status.idle":"2024-01-01T06:40:59.548463Z","shell.execute_reply.started":"2024-01-01T06:40:59.541813Z","shell.execute_reply":"2024-01-01T06:40:59.547417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_labels = {\n    'HGSC' : 0,\n    'EC' : 1,\n    'CC' : 2,\n    'LGSC' : 3,\n    'MC' : 4\n}\n\nclass_labels = {\n    0 : 'HGSC',\n    1 : 'EC',\n    2 : 'CC',\n    3 : 'LGSC',\n    4 : 'MC'\n}","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:40:59.549637Z","iopub.execute_input":"2024-01-01T06:40:59.549941Z","iopub.status.idle":"2024-01-01T06:40:59.597257Z","shell.execute_reply.started":"2024-01-01T06:40:59.549915Z","shell.execute_reply":"2024-01-01T06:40:59.596139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ndef get_png(img_id, train=False): #/kaggle/input/UBC-OCEAN/train_images #/kaggle/input/UBC-OCEAN/test_images\n    if train:                #/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png\n        return f\"/kaggle/input/UBC-OCEAN/train_images/{img_id}.png\"\n    else:\n        return f\"/kaggle/input/UBC-OCEAN/test_images/{img_id}.png\"\n\ndef get_thm(img_id, is_tma=True): #/kaggle/input/UBC-OCEAN/train_images #/kaggle/input/UBC-OCEAN/test_images\n    if is_tma:\n        return f\"/kaggle/input/UBC-OCEAN/test_thumbnails/{img_id}_thumbnail.png\"\n    else:\n        return None\n\ndf = pd.read_csv(TEST_DF)\ndf['img_path'] = df['image_id'].apply(get_png)\ndf['thumbnail'] = df['image_id'].apply(get_thm)\ndf\n'''","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:40:59.598459Z","iopub.execute_input":"2024-01-01T06:40:59.599239Z","iopub.status.idle":"2024-01-01T06:40:59.609922Z","shell.execute_reply.started":"2024-01-01T06:40:59.599203Z","shell.execute_reply":"2024-01-01T06:40:59.608929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(TEST_DF)\ndf[\"image_path\"] = df[\"image_id\"].apply(lambda x: f\"/kaggle/input/UBC-OCEAN/test_thumbnails/{x}_thumbnail.png\")\ndf","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:40:59.612815Z","iopub.execute_input":"2024-01-01T06:40:59.613912Z","iopub.status.idle":"2024-01-01T06:40:59.649982Z","shell.execute_reply.started":"2024-01-01T06:40:59.613868Z","shell.execute_reply":"2024-01-01T06:40:59.649089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_img(path):\n    img = cv2.imread(path)\n    img = cv2.resize(img, (256, 256))\n    src = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n    dst = cv2.equalizeHist(src)\n    img = cv2.cvtColor(dst, cv2.COLOR_GRAY2RGB)\n    img = (tf.cast(img, tf.float32))/255.0\n    return img","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:41:43.911066Z","iopub.execute_input":"2024-01-01T06:41:43.911495Z","iopub.status.idle":"2024-01-01T06:41:43.918388Z","shell.execute_reply.started":"2024-01-01T06:41:43.91146Z","shell.execute_reply":"2024-01-01T06:41:43.916976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('/kaggle/input/ova-effenet-models/inception_1_2.h5')\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:40:59.65881Z","iopub.execute_input":"2024-01-01T06:40:59.659719Z","iopub.status.idle":"2024-01-01T06:41:09.246015Z","shell.execute_reply.started":"2024-01-01T06:40:59.659681Z","shell.execute_reply":"2024-01-01T06:41:09.24486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels = []\n\nfor index, row in df.iterrows():\n    # Get the image path\n    image_path = row[\"image_path\"]\n\n    # Get the image\n    image = read_img(image_path)[None, ...]\n\n    # Predict the label\n    logits = model.predict(image)\n    pred = ops.argmax(logits, axis=-1).tolist()[0]\n\n    # Map the pred to the name\n    label = class_labels[pred]\n\n    predicted_labels.append(label)\n\n# Add the predicted labels to the csv\ndf[\"label\"] = predicted_labels","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:41:46.654523Z","iopub.execute_input":"2024-01-01T06:41:46.654934Z","iopub.status.idle":"2024-01-01T06:41:47.688946Z","shell.execute_reply.started":"2024-01-01T06:41:46.6549Z","shell.execute_reply":"2024-01-01T06:41:47.687585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = df[[\"image_id\", \"label\"]]\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-01T06:41:51.077084Z","iopub.execute_input":"2024-01-01T06:41:51.077438Z","iopub.status.idle":"2024-01-01T06:41:51.095064Z","shell.execute_reply.started":"2024-01-01T06:41:51.07741Z","shell.execute_reply":"2024-01-01T06:41:51.093849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}