{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":6930923,"sourceType":"datasetVersion","datasetId":3979700},{"sourceId":6931060,"sourceType":"datasetVersion","datasetId":3979802}],"dockerImageVersionId":30558,"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ[\"KERAS_BACKEND\"] = \"jax\" # or \"tensorflow\", \"torch\"\n\nimport cv2\nimport pickle\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.models import load_model\n\n# Set the style for the plot\nsns.set(style=\"whitegrid\")\n\nimport tensorflow as tf\nimport keras_cv\nimport keras_core as keras\nfrom keras_core import ops","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:21:26.177993Z","iopub.execute_input":"2023-11-09T15:21:26.178497Z","iopub.status.idle":"2023-11-09T15:21:26.188967Z","shell.execute_reply.started":"2023-11-09T15:21:26.178459Z","shell.execute_reply":"2023-11-09T15:21:26.187871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Input,Dropout,GlobalAveragePooling2D,BatchNormalization,Dense\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications import EfficientNetB5\n\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom tensorflow.keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:30:06.67774Z","iopub.execute_input":"2023-11-09T15:30:06.678329Z","iopub.status.idle":"2023-11-09T15:30:06.687673Z","shell.execute_reply.started":"2023-11-09T15:30:06.678286Z","shell.execute_reply":"2023-11-09T15:30:06.68623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    is_submission = False\n    \n    # Reproducibility\n    SEED = 42\n    \n    # Training\n    train_csv_path = \"/kaggle/input/UBC-OCEAN/train.csv\"\n    train_thumbnail_paths = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\n    batch_size = 8\n    learning_rate = 1e-3\n    epochs = 2\n    \n    # Inference\n    test_csv_path = \"/kaggle/input/UBC-OCEAN/test.csv\"\n    test_thumbnail_paths = \"/kaggle/input/UBC-OCEAN/test_thumbnails\"\n\nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:20:44.366725Z","iopub.execute_input":"2023-11-09T15:20:44.367239Z","iopub.status.idle":"2023-11-09T15:20:44.374599Z","shell.execute_reply.started":"2023-11-09T15:20:44.367193Z","shell.execute_reply":"2023-11-09T15:20:44.373235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = EfficientNetB5(include_top=False, input_shape=(224, 224, 3), weights=\"/kaggle/input/efficientb5/efficientnetb5_notop.h5\")               #https://www.tensorflow.org/api_docs/python/tf/keras/applications/efficientnet/EfficientNetB5\nmodel.trainable = True\ninput_layer = model.output    #Get this method from classrom query resolution, https://www.tensorflow.org/api_docs/python/tf/keras/Model\n\nlayer1 = GlobalAveragePooling2D()(input_layer)    \n\nlayer2 = BatchNormalization()(layer1)\n\nlayer3 = Dropout(0.2)(layer2)\n\n#output layer\noutput = Dense(5, activation=\"softmax\")(layer3)\n\n# creating the final model \nmodel = Model(inputs=model.input, outputs = output)   #https://www.tensorflow.org/api_docs/python/tf/keras/Model\n\n# compile the model \noptimizer = Adam(beta_1 = 0.9, beta_2 = 0.999,learning_rate=0.001)\n\nmodel.compile(loss = \"categorical_crossentropy\", optimizer = optimizer, metrics=[\"accuracy\"])\n\n#model.load_weights('/content/drive/MyDrive/Code/weight_EfficientNet_81.hdf5')","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:30:09.26787Z","iopub.execute_input":"2023-11-09T15:30:09.268322Z","iopub.status.idle":"2023-11-09T15:30:19.463119Z","shell.execute_reply.started":"2023-11-09T15:30:09.268291Z","shell.execute_reply":"2023-11-09T15:30:19.461811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/UBC-OCEAN/test.csv\")\ndf[\"image_path\"] = df[\"image_id\"].apply(lambda x: f\"{config.test_thumbnail_paths}/{x}_thumbnail.png\")\n    \n# Load the model weights\nmodel.load_weights(\"/kaggle/input/keras-train-infer/weight.hdf5\")\n    \n# Load the id to name dictionary\nid_to_name = [\"CC\",\"EC\",\"HGSC\",\"LGSC\",\"MC\"]","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:30:26.097056Z","iopub.execute_input":"2023-11-09T15:30:26.097672Z","iopub.status.idle":"2023-11-09T15:30:28.552312Z","shell.execute_reply.started":"2023-11-09T15:30:26.097632Z","shell.execute_reply":"2023-11-09T15:30:28.550825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_image(path):\n    file = tf.io.read_file(path)\n    image = tf.io.decode_png(file, 3)\n    image = tf.image.resize(image, (224,224))\n    image = tf.image.per_image_standardization(image)\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:32:20.66222Z","iopub.execute_input":"2023-11-09T15:32:20.662646Z","iopub.status.idle":"2023-11-09T15:32:20.670718Z","shell.execute_reply.started":"2023-11-09T15:32:20.662615Z","shell.execute_reply":"2023-11-09T15:32:20.668671Z"},"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_image(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 = id_to_name[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":"2023-11-09T15:32:22.369341Z","iopub.execute_input":"2023-11-09T15:32:22.369815Z","iopub.status.idle":"2023-11-09T15:32:28.732718Z","shell.execute_reply.started":"2023-11-09T15:32:22.369782Z","shell.execute_reply":"2023-11-09T15:32:28.731452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:32:33.750619Z","iopub.execute_input":"2023-11-09T15:32:33.751051Z","iopub.status.idle":"2023-11-09T15:32:33.77344Z","shell.execute_reply.started":"2023-11-09T15:32:33.75102Z","shell.execute_reply":"2023-11-09T15:32:33.772092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the submission\nsubmission_df = df[[\"image_id\", \"label\"]]\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{},"execution_count":null,"outputs":[]}]}