{"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":7314263,"sourceType":"datasetVersion","datasetId":4244439},{"sourceId":7314842,"sourceType":"datasetVersion","datasetId":4244789},{"sourceId":7320099,"sourceType":"datasetVersion","datasetId":4247992}],"dockerImageVersionId":30626,"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\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":"2024-01-01T23:24:01.23941Z","iopub.execute_input":"2024-01-01T23:24:01.240296Z","iopub.status.idle":"2024-01-01T23:24:01.874387Z","shell.execute_reply.started":"2024-01-01T23:24:01.240249Z","shell.execute_reply":"2024-01-01T23:24:01.87357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:01.875891Z","iopub.execute_input":"2024-01-01T23:24:01.876969Z","iopub.status.idle":"2024-01-01T23:24:05.738914Z","shell.execute_reply.started":"2024-01-01T23:24:01.876936Z","shell.execute_reply":"2024-01-01T23:24:05.737988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/ubc-ocean-tma-augm","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:05.740111Z","iopub.execute_input":"2024-01-01T23:24:05.741441Z","iopub.status.idle":"2024-01-01T23:24:06.849101Z","shell.execute_reply.started":"2024-01-01T23:24:05.741403Z","shell.execute_reply":"2024-01-01T23:24:06.847912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BalancedAccuracy(tf.keras.metrics.Metric):\n    def __init__(self, num_classes=None, name='balanced_accuracy', **kwargs):\n        super(BalancedAccuracy, self).__init__(name=name, **kwargs)\n        self.num_classes = num_classes\n        if num_classes is not None:\n            self.true_positives = self.add_weight(name='tp', shape=(num_classes,), initializer='zeros')\n            self.false_negatives = self.add_weight(name='fn', shape=(num_classes,), initializer='zeros')\n\n    def update_state(self, y_true, y_pred, sample_weight=None):\n        if self.num_classes is None:\n            # Handle the case where num_classes is not set\n            # This could involve setting num_classes based on y_true or y_pred\n            # For now, we'll just raise an error\n            raise ValueError(\"num_classes must be set to use BalancedAccuracy\")\n\n        y_pred = tf.argmax(y_pred, axis=1)\n        y_true = tf.argmax(y_true, axis=1)  # Adjusting for one-hot encoded labels\n\n        for i in range(self.num_classes):\n            tp = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(y_true, i), tf.equal(y_pred, i)), tf.float32))\n            fn = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(y_true, i), tf.not_equal(y_pred, i)), tf.float32))\n\n            # Update using tensor_scatter_nd_add\n            indices = tf.reshape(tf.constant([i]), (1, 1))\n            self.true_positives.assign(tf.tensor_scatter_nd_add(self.true_positives, indices, tf.reshape(tp, (1,))))\n            self.false_negatives.assign(tf.tensor_scatter_nd_add(self.false_negatives, indices, tf.reshape(fn, (1,))))\n\n    def result(self):\n        recall_per_class = self.true_positives / (self.true_positives + self.false_negatives + tf.keras.backend.epsilon())\n        return tf.reduce_mean(recall_per_class)\n\n    def reset_state(self):\n        self.true_positives.assign(tf.zeros_like(self.true_positives))\n        self.false_negatives.assign(tf.zeros_like(self.false_negatives))\n","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:06.852174Z","iopub.execute_input":"2024-01-01T23:24:06.853282Z","iopub.status.idle":"2024-01-01T23:24:06.983399Z","shell.execute_reply.started":"2024-01-01T23:24:06.853234Z","shell.execute_reply":"2024-01-01T23:24:06.982387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes=5\nbalanced_accuracy_metric = BalancedAccuracy(num_classes=num_classes)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:06.984705Z","iopub.execute_input":"2024-01-01T23:24:06.985041Z","iopub.status.idle":"2024-01-01T23:24:07.031366Z","shell.execute_reply.started":"2024-01-01T23:24:06.985011Z","shell.execute_reply":"2024-01-01T23:24:07.030063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model('/kaggle/input/ubc-ocean-tma-augm/best_model_tma.h5', custom_objects={'BalancedAccuracy': balanced_accuracy_metric})\n","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:07.032838Z","iopub.execute_input":"2024-01-01T23:24:07.03327Z","iopub.status.idle":"2024-01-01T23:24:11.737584Z","shell.execute_reply.started":"2024-01-01T23:24:07.033227Z","shell.execute_reply":"2024-01-01T23:24:11.736503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:11.739309Z","iopub.execute_input":"2024-01-01T23:24:11.739684Z","iopub.status.idle":"2024-01-01T23:24:12.678284Z","shell.execute_reply.started":"2024-01-01T23:24:11.739651Z","shell.execute_reply":"2024-01-01T23:24:12.677514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_data = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:12.67956Z","iopub.execute_input":"2024-01-01T23:24:12.679888Z","iopub.status.idle":"2024-01-01T23:24:12.695805Z","shell.execute_reply.started":"2024-01-01T23:24:12.679859Z","shell.execute_reply":"2024-01-01T23:24:12.69482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_data","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:12.697335Z","iopub.execute_input":"2024-01-01T23:24:12.69769Z","iopub.status.idle":"2024-01-01T23:24:12.727553Z","shell.execute_reply.started":"2024-01-01T23:24:12.697659Z","shell.execute_reply":"2024-01-01T23:24:12.726404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_indices = {'CC': 0, 'EC': 1, 'HGSC': 2, 'LGSC': 3, 'MC': 4}","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:12.731871Z","iopub.execute_input":"2024-01-01T23:24:12.732209Z","iopub.status.idle":"2024-01-01T23:24:12.737507Z","shell.execute_reply.started":"2024-01-01T23:24:12.732177Z","shell.execute_reply":"2024-01-01T23:24:12.736294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path1 = \"/kaggle/input/UBC-OCEAN/test.csv\"\np1 = pd.read_csv(path1)\np1","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:12.739527Z","iopub.execute_input":"2024-01-01T23:24:12.739878Z","iopub.status.idle":"2024-01-01T23:24:12.758546Z","shell.execute_reply.started":"2024-01-01T23:24:12.739837Z","shell.execute_reply":"2024-01-01T23:24:12.75743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = list(class_indices.keys())\nclass_labels","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:12.760034Z","iopub.execute_input":"2024-01-01T23:24:12.760368Z","iopub.status.idle":"2024-01-01T23:24:12.768721Z","shell.execute_reply.started":"2024-01-01T23:24:12.760337Z","shell.execute_reply":"2024-01-01T23:24:12.767262Z"},"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":"2024-01-01T23:24:12.770287Z","iopub.execute_input":"2024-01-01T23:24:12.770863Z","iopub.status.idle":"2024-01-01T23:24:12.785569Z","shell.execute_reply.started":"2024-01-01T23:24:12.770832Z","shell.execute_reply":"2024-01-01T23:24:12.784495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image, ImageFile\nfrom tensorflow.keras.preprocessing import image\nimport matplotlib.pyplot as plt\n\n# Increase the pixel limit\nImage.MAX_IMAGE_PIXELS = None  # Removes the limit entirely\nImageFile.LOAD_TRUNCATED_IMAGES = True  # To prevent errors with truncated images\n\npredictions=[]\nfor index, row in p1.iterrows():\n    print(index, row['image_id'])\n    \n    image_path ='/kaggle/input/UBC-OCEAN/test_images/' + str(row['image_id']) +'.png'  \n    img = image.load_img(image_path, target_size=(2048, 1536))\n    # Convert the image to a numpy array\n    img_array = image.img_to_array(img)\n    \n    # Expand dimensions to match the model's input format\n    img_array = np.expand_dims(img_array, axis=0)\n    \n    # Normalize the image\n    img_array /= 255.0\n\n    # Make a prediction\n    prediction = model.predict(img_array)\n    predictions.append(prediction)\n\npredictions                                                           ","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:12.787634Z","iopub.execute_input":"2024-01-01T23:24:12.788082Z","iopub.status.idle":"2024-01-01T23:24:43.100208Z","shell.execute_reply.started":"2024-01-01T23:24:12.78804Z","shell.execute_reply":"2024-01-01T23:24:43.099384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_predicted = [np.argmax(i) for i in predictions]\nlabel_predicted","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:43.101514Z","iopub.execute_input":"2024-01-01T23:24:43.102007Z","iopub.status.idle":"2024-01-01T23:24:43.107586Z","shell.execute_reply.started":"2024-01-01T23:24:43.101977Z","shell.execute_reply":"2024-01-01T23:24:43.106836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file = pd.DataFrame()\nsubmission_file['image_id'] = p.image_id\nsubmission_file['label'] = pd.Series([class_labels[i] for i in label_predicted])\n\nsubmission_file","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:43.108817Z","iopub.execute_input":"2024-01-01T23:24:43.109287Z","iopub.status.idle":"2024-01-01T23:24:43.124841Z","shell.execute_reply.started":"2024-01-01T23:24:43.109257Z","shell.execute_reply":"2024-01-01T23:24:43.124022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-01T23:24:43.126107Z","iopub.execute_input":"2024-01-01T23:24:43.126659Z","iopub.status.idle":"2024-01-01T23:24:43.143049Z","shell.execute_reply.started":"2024-01-01T23:24:43.126627Z","shell.execute_reply":"2024-01-01T23:24:43.141572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}