{"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":152983114,"sourceType":"kernelVersion"}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import joblib\nfrom tensorflow.keras.models import  Model\nfrom tensorflow.keras.models import load_model\nfrom PIL import Image\nimport pandas as pd\nimport numpy as np\n\ndef load_images(image_paths):\n    images = [np.array(Image.open(path).resize((256, 256))) for path in image_paths]\n    return np.array(images)\n\nlabel_encoder = joblib.load('/kaggle/input/train-version/label_encoder.joblib')\n# Load the model weights\nmodel = load_model(\"/kaggle/input/train-version/UBC_model.h5\")\nmodel.load_weights(\"/kaggle/input/train-version/ucb_ocean_checkpoint.weights.h5\")\ndf = pd.read_csv('/kaggle/input/UBC-OCEAN/test.csv')\ndf[\"image_path\"] = df[\"image_id\"].apply(lambda x: f\"{'/kaggle/input/UBC-OCEAN/test_thumbnails'}/{x}_thumbnail.png\")\npredicted_labels = []\nfor index, row in df.iterrows():\n    # Get the image path\n    image_path = row[\"image_path\"]\n\n    # Get the image\n    image = load_images([image_path])\n\n    # Convert predicted probabilities to class labels\n    logits = model.predict(image)\n    predicted_labels = [np.argmax(prediction) for prediction in logits]\n\n    # Decode predicted labels using the label_encoder\n    predicted_subtypes = label_encoder.inverse_transform(predicted_labels)\n\n# Add the predicted labels to the csv\ndf[\"label\"] = predicted_labels\n# Create the submission\nsubmission_df = df[[\"image_id\", \"label\"]]\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T07:30:42.902974Z","iopub.execute_input":"2023-11-30T07:30:42.903563Z","iopub.status.idle":"2023-11-30T07:30:51.773968Z","shell.execute_reply.started":"2023-11-30T07:30:42.903523Z","shell.execute_reply":"2023-11-30T07:30:51.772837Z"},"trusted":true},"execution_count":null,"outputs":[]}]}