{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3175,"databundleVersionId":44352,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":{"iopub.status.busy":"2024-11-09T08:23:04.109217Z","iopub.execute_input":"2024-11-09T08:23:04.109975Z","iopub.status.idle":"2024-11-09T08:23:05.196270Z","shell.execute_reply.started":"2024-11-09T08:23:04.109932Z","shell.execute_reply":"2024-11-09T08:23:05.195332Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nimport random\nfrom importlib import reload\nimport glob\nfrom shutil import copyfile\nimport keras\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.layers import Flatten, Dense, Input, Add, Lambda\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras import optimizers","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:23:53.433235Z","iopub.execute_input":"2024-11-09T08:23:53.434562Z","iopub.status.idle":"2024-11-09T08:24:06.463101Z","shell.execute_reply.started":"2024-11-09T08:23:53.434518Z","shell.execute_reply":"2024-11-09T08:24:06.462195Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define the class labels\nclasses = [\n    'Class1.1', 'Class1.2', 'Class1.3', 'Class2.1', 'Class2.2', 'Class3.1',\n    'Class3.2', 'Class4.1', 'Class4.2', 'Class5.1', 'Class5.2', 'Class5.3',\n    'Class5.4', 'Class6.1', 'Class6.2', 'Class7.1', 'Class7.2', 'Class7.3',\n    'Class8.1', 'Class8.2', 'Class8.3', 'Class8.4', 'Class8.5', 'Class8.6',\n    'Class8.7', 'Class9.1', 'Class9.2', 'Class9.3', 'Class10.1', 'Class10.2',\n    'Class10.3', 'Class11.1', 'Class11.2', 'Class11.3', 'Class11.4',\n    'Class11.5', 'Class11.6'\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:25:55.468663Z","iopub.execute_input":"2024-11-09T08:25:55.469667Z","iopub.status.idle":"2024-11-09T08:25:55.474717Z","shell.execute_reply.started":"2024-11-09T08:25:55.469626Z","shell.execute_reply":"2024-11-09T08:25:55.473804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set image shape\nimg_shape = (224, 224, 3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:26:00.638353Z","iopub.execute_input":"2024-11-09T08:26:00.639299Z","iopub.status.idle":"2024-11-09T08:26:00.643186Z","shell.execute_reply.started":"2024-11-09T08:26:00.639258Z","shell.execute_reply":"2024-11-09T08:26:00.642180Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define LoRA injection function\ndef apply_lora(layer, rank=2):\n    input_dim = layer.output_shape[-1]\n    lora_down = Dense(rank, use_bias=False, name=f\"{layer.name}_lora_down\")\n    lora_up = Dense(input_dim, use_bias=False, name=f\"{layer.name}_lora_up\")\n\n    def lora_transform(x):\n        return layer(x) + lora_up(lora_down(x))\n\n    return Lambda(lora_transform, name=f\"{layer.name}_lora\")(layer.input)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:26:01.834208Z","iopub.execute_input":"2024-11-09T08:26:01.835107Z","iopub.status.idle":"2024-11-09T08:26:01.840841Z","shell.execute_reply.started":"2024-11-09T08:26:01.835065Z","shell.execute_reply":"2024-11-09T08:26:01.839830Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load ResNet50 model without top layers\nresnet_base = ResNet50(include_top=False, input_shape=img_shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:26:02.841867Z","iopub.execute_input":"2024-11-09T08:26:02.842519Z","iopub.status.idle":"2024-11-09T08:26:05.622708Z","shell.execute_reply.started":"2024-11-09T08:26:02.842466Z","shell.execute_reply":"2024-11-09T08:26:05.621821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Apply LoRA to selected layers in ResNet-50\nfor layer in resnet_base.layers:\n    if isinstance(layer, Dense):\n        layer.trainable = False  # Freeze the original Dense layers\n        layer = apply_lora(layer)  # Apply LoRA","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:26:07.473013Z","iopub.execute_input":"2024-11-09T08:26:07.473887Z","iopub.status.idle":"2024-11-09T08:26:07.478628Z","shell.execute_reply.started":"2024-11-09T08:26:07.473845Z","shell.execute_reply":"2024-11-09T08:26:07.477708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Add final layers\nx = Flatten()(resnet_base.output)\nx = Dense(len(classes), activation='sigmoid')(x)\n\n# Construct the final model\nmodel = Model(inputs=resnet_base.input, outputs=x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:26:10.833114Z","iopub.execute_input":"2024-11-09T08:26:10.834053Z","iopub.status.idle":"2024-11-09T08:26:10.875640Z","shell.execute_reply.started":"2024-11-09T08:26:10.834014Z","shell.execute_reply":"2024-11-09T08:26:10.874739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Print model summary\nprint(model.summary())\n\n!unzip /kaggle/input/galaxy-zoo-the-galaxy-challenge/images_training_rev1.zip \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:26:12.581335Z","iopub.execute_input":"2024-11-09T08:26:12.582243Z","iopub.status.idle":"2024-11-09T08:26:12.998134Z","shell.execute_reply.started":"2024-11-09T08:26:12.582206Z","shell.execute_reply":"2024-11-09T08:26:12.997296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/training_solutions_rev1.zip","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:33:19.556257Z","iopub.execute_input":"2024-11-09T08:33:19.556734Z","iopub.status.idle":"2024-11-09T08:33:20.743920Z","shell.execute_reply.started":"2024-11-09T08:33:19.556692Z","shell.execute_reply":"2024-11-09T08:33:20.742770Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Preparing dataframe and data generators\ndef append_ext(fn):\n    return fn + \".jpg\"\n\ntraindf = pd.read_csv('/kaggle/working/training_solutions_rev1.csv')\ntraindf[\"id\"] = traindf['GalaxyID'].astype(str).apply(append_ext)\n\ndatagen = ImageDataGenerator(\n    fill_mode='nearest',\n    cval=0,\n    rescale=1. / 255,\n    rotation_range=90,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=True,\n    vertical_flip=True,\n    validation_split=0.02\n)\n\ntrain_generator = datagen.flow_from_dataframe(\n    dataframe=traindf,\n    directory=\"/kaggle/working/images_training_rev1\",\n    x_col=\"id\",\n    y_col=classes,\n    subset=\"training\",\n    batch_size=64,\n    seed=123,\n    shuffle=True,\n    class_mode=\"other\",\n    target_size=(224, 224)\n)\n\nvalid_generator = datagen.flow_from_dataframe(\n    dataframe=traindf,\n    directory=\"/kaggle/working/images_training_rev1\",\n    x_col=\"id\",\n    y_col=classes,\n    subset=\"validation\",\n    batch_size=64,\n    seed=123,\n    shuffle=True,\n    class_mode=\"other\",\n    target_size=(224, 224)\n)\n\nSTEP_SIZE_TRAIN = train_generator.n // train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n // valid_generator.batch_size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:36:43.145026Z","iopub.execute_input":"2024-11-09T08:36:43.145944Z","iopub.status.idle":"2024-11-09T08:36:44.614766Z","shell.execute_reply.started":"2024-11-09T08:36:43.145903Z","shell.execute_reply":"2024-11-09T08:36:44.613788Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compile model with LoRA-adjusted layers\noptimizer = keras.optimizers.Adam(learning_rate=0.001, decay=5e-4)\nmodel.compile(optimizer, loss='mse', metrics=[\"accuracy\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:37:22.452135Z","iopub.execute_input":"2024-11-09T08:37:22.452833Z","iopub.status.idle":"2024-11-09T08:37:22.463538Z","shell.execute_reply.started":"2024-11-09T08:37:22.452793Z","shell.execute_reply":"2024-11-09T08:37:22.462651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\n\n\nclass LossHistory(Callback):\n    def on_train_begin(self, logs={}):\n        self.losses = []\n        self.val_losses = []\n\n    def on_batch_end(self, batch, logs={}):\n        self.losses.append(logs.get('loss'))\n        self.val_losses.append(logs.get('val_loss'))\n\n\nearly_stopping = EarlyStopping(\n    monitor='val_loss', patience=4, verbose=1, mode='auto')\n\nhistory = LossHistory()\n\nfrom keras.callbacks import ModelCheckpoint\ncheckpointer = ModelCheckpoint(\n    filepath='/kaggle/working/model/model.keras', verbose=2, save_best_only=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:39:08.318505Z","iopub.execute_input":"2024-11-09T08:39:08.319277Z","iopub.status.idle":"2024-11-09T08:39:08.326199Z","shell.execute_reply.started":"2024-11-09T08:39:08.319238Z","shell.execute_reply":"2024-11-09T08:39:08.325340Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training\nhist = model.fit(\n    train_generator,\n    steps_per_epoch=STEP_SIZE_TRAIN,\n    validation_data=valid_generator,\n    validation_steps=STEP_SIZE_VALID,\n    epochs=30,\n    callbacks=[checkpointer, early_stopping]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:39:24.563509Z","iopub.execute_input":"2024-11-09T08:39:24.564322Z","iopub.status.idle":"2024-11-09T10:03:18.124117Z","shell.execute_reply.started":"2024-11-09T08:39:24.564278Z","shell.execute_reply":"2024-11-09T10:03:18.123317Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plotting training and validation loss\nplt.figure(figsize=(12, 8))\nplt.plot(hist.epoch, hist.history['loss'], label='Training Loss')\nplt.plot(hist.epoch, hist.history['val_loss'], label='Validation', linestyle='--')\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"RMSE\")\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:03:18.125826Z","iopub.execute_input":"2024-11-09T10:03:18.126387Z","iopub.status.idle":"2024-11-09T10:03:18.450712Z","shell.execute_reply.started":"2024-11-09T10:03:18.126352Z","shell.execute_reply":"2024-11-09T10:03:18.448795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Re-create the ResNet50 model architecture with LoRA applied\nimg_shape = (224, 224, 3)\n\n# Load the ResNet50 base model without the top layers\nresnet_base = ResNet50(include_top=False, input_shape=img_shape)\n\n# Apply LoRA to selected layers in ResNet-50\nfor layer in resnet_base.layers:\n    if isinstance(layer, Dense):\n        layer.trainable = False  # Freeze the original Dense layers\n        layer = apply_lora(layer)  # Apply LoRA\n\n# Add the final layers\nx = Flatten()(resnet_base.output)\nx = Dense(len(classes), activation='sigmoid')(x)\n\n# Construct the final model\nmodel = Model(inputs=resnet_base.input, outputs=x)\n\n# Load the saved weights into the model\nmodel.load_weights('/kaggle/working/model/model.keras')\n\n# Confirm the model structure\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:03:18.451963Z","iopub.execute_input":"2024-11-09T10:03:18.452269Z","iopub.status.idle":"2024-11-09T10:03:24.962961Z","shell.execute_reply.started":"2024-11-09T10:03:18.452236Z","shell.execute_reply":"2024-11-09T10:03:24.961981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel = load_model('/kaggle/working/model/model.keras')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:03:24.965559Z","iopub.execute_input":"2024-11-09T10:03:24.966041Z","iopub.status.idle":"2024-11-09T10:03:37.533961Z","shell.execute_reply.started":"2024-11-09T10:03:24.965983Z","shell.execute_reply":"2024-11-09T10:03:37.533153Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/central_pixel_benchmark.zip","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:03:37.535340Z","iopub.execute_input":"2024-11-09T10:03:37.535699Z","iopub.status.idle":"2024-11-09T10:03:38.760230Z","shell.execute_reply.started":"2024-11-09T10:03:37.535662Z","shell.execute_reply":"2024-11-09T10:03:38.758946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/all_zeros_benchmark.zip","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:05:33.068385Z","iopub.execute_input":"2024-11-09T10:05:33.068820Z","iopub.status.idle":"2024-11-09T10:05:34.156368Z","shell.execute_reply.started":"2024-11-09T10:05:33.068782Z","shell.execute_reply":"2024-11-09T10:05:34.155114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/images_test_rev1.zip","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:07:23.132224Z","iopub.execute_input":"2024-11-09T10:07:23.132653Z","iopub.status.idle":"2024-11-09T10:08:05.233510Z","shell.execute_reply.started":"2024-11-09T10:07:23.132614Z","shell.execute_reply":"2024-11-09T10:08:05.232354Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir -p /kaggle/working//images\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:31:13.131259Z","iopub.execute_input":"2024-11-09T10:31:13.132050Z","iopub.status.idle":"2024-11-09T10:31:14.187437Z","shell.execute_reply.started":"2024-11-09T10:31:13.132009Z","shell.execute_reply":"2024-11-09T10:31:14.186204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/images_test_rev1.zip -d /kaggle/working/images","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:32:02.886244Z","iopub.execute_input":"2024-11-09T10:32:02.886995Z","iopub.status.idle":"2024-11-09T10:32:19.686969Z","shell.execute_reply.started":"2024-11-09T10:32:02.886953Z","shell.execute_reply":"2024-11-09T10:32:19.685665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Test Data Preparation\n# Define the path to your test data folder\ntest_data_path = '/kaggle/working/images'\n# Load the test data using image_dataset_from_directory\ntest_dataset = tf.keras.preprocessing.image_dataset_from_directory(\n    test_data_path,\n    image_size=(224, 224),  # ResNet-50 expects 224x224 input images\n    batch_size=32,\n    label_mode=None,  # No labels needed for test data\n    shuffle=False     # Ensures order is preserved\n)\n\n# Normalize the images by scaling pixel values\nnormalization_layer = tf.keras.layers.Rescaling(1./255)\ntest_dataset = test_dataset.map(lambda x: normalization_layer(x))\n\n# Make predictions\nmodel = tf.keras.models.load_model('/kaggle/working/model/model.keras')\npredictions = model.predict(test_dataset)\n\n# If you want class labels, get the argmax for each prediction\npredicted_classes = np.argmax(predictions, axis=-1)\n\n# Print predictions\nfor idx, pred_class in enumerate(predicted_classes):\n    print(f\"Image {idx}: Predicted class {pred_class}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:32:47.496428Z","iopub.execute_input":"2024-11-09T10:32:47.497190Z","iopub.status.idle":"2024-11-09T10:36:19.535791Z","shell.execute_reply.started":"2024-11-09T10:32:47.497142Z","shell.execute_reply":"2024-11-09T10:36:19.534736Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport os\n\n# Set up ImageDataGenerator for testing data\ntest_datagen = ImageDataGenerator(rescale=1.0/255)\ntest_generator = test_datagen.flow_from_directory(\n    '/kaggle/working/images',  # Update this path to your test data directory\n    target_size=(224, 224),  # Adjust target size based on your model's input size\n    batch_size=32,\n    class_mode=None,\n    shuffle=False\n)\n\n# Define get_id function\ndef get_id(filepath):\n    return os.path.splitext(os.path.basename(filepath))[0]\n\n# Get the current working directory\ncurrent_directory = os.getcwd()\nprint(\"Current directory:\", current_directory)\n\n# Construct the full path to the file\nfile_path = os.path.join(current_directory, '/kaggle/working/all_zeros_benchmark.csv')\n\n# Try opening the file and reading the header\ntry:\n    with open(file_path, 'r') as file:\n        header = file.readline()  # Read the first line as the header\nexcept FileNotFoundError:\n    print(f\"Error: File not found at {file_path}\")\n    header =\"GalaxyID, Class1.1, Class1.2, Class1.3, Class2.1, Class2.2, Class3.1, Class3.2, Class4.1, Class4.2, Class5.1, Class5.2, Class5.3, Class5.4, Class6.1, Class6.2, Class7.1, Class7.2, Class7.3, Class8.1, Class8.2, Class8.3, Class8.4, Class8.5, Class8.6, Class8.7, Class9.1, Class9.2, Class9.3, Class10.1, Class10.2, Class10.3, Class11.1, Class11.2, Class11.3, Class11.4, Class11.5, Class11.6\\n\"  # Provide a default header if the file is missing\n\n# Assume predictions is defined (e.g., from model predictions on test_generator)\n# Example: predictions = model.predict(test_generator)\n\n# Write predictions to Output.csv\nwith open('/kaggle/working/Predictions_Lora.csv', 'w') as outfile:\n    outfile.write(header)\n    for i in range(len(test_generator.filenames)):\n        id_ = get_id(test_generator.filenames[i])  # Extract ID from filename\n        pred = predictions[i]  # predictions should be your model's output\n        outline = id_ + \",\" + \",\".join([str(x) for x in pred])\n        outfile.write(outline + \"\\n\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:38:03.854477Z","iopub.execute_input":"2024-11-09T10:38:03.855122Z","iopub.status.idle":"2024-11-09T10:38:08.329043Z","shell.execute_reply.started":"2024-11-09T10:38:03.855079Z","shell.execute_reply":"2024-11-09T10:38:08.327917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Load the actual and predicted values from the CSV files\nactual_df = pd.read_csv('/kaggle/working/central_pixel_benchmark.csv')\npredicted_df = pd.read_csv('/kaggle/working/Predictions_Lora.csv')\n\n# Ensure that both dataframes have the same shape\nassert actual_df.shape == predicted_df.shape, \"The actual and predicted files must have the same shape.\"\n\n# Exclude the 'GalaxyID' column and calculate RMSE for the remaining columns\nactual_values = actual_df.iloc[:, 1:].values  # Exclude GalaxyID\npredicted_values = predicted_df.iloc[:, 1:].values  # Exclude GalaxyID\n\n# Calculate RMSE across all columns and rows\nrmse = np.sqrt(np.mean((predicted_values - actual_values) ** 2))\nprint(\"Root Mean Squared Error:\", rmse)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:41:09.110784Z","iopub.execute_input":"2024-11-09T10:41:09.111150Z","iopub.status.idle":"2024-11-09T10:41:10.210583Z","shell.execute_reply.started":"2024-11-09T10:41:09.111116Z","shell.execute_reply":"2024-11-09T10:41:10.209493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"actual_df = pd.read_csv('/kaggle/working/central_pixel_benchmark.csv')\npredicted_df = pd.read_csv('/kaggle/working/Predictions_Lora.csv')\n\n# list of class columns\nclass_columns = actual_df.columns[1:]\n\n# Determining the predicted class based on the highest probability for each row\nactual_df['Predicted_Class'] = actual_df[class_columns].idxmax(axis=1)\npredicted_df['Predicted_Class'] = predicted_df[class_columns].idxmax(axis=1)\n\n# Merging on GalaxyID to align predictions with actual values\nmerged_df = pd.merge(actual_df[['GalaxyID', 'Predicted_Class']], predicted_df[['GalaxyID', 'Predicted_Class']], on='GalaxyID', suffixes=('_actual', '_predicted'))\n\n# Calculating accuracy\naccuracy = np.mean(merged_df['Predicted_Class_actual'] == merged_df['Predicted_Class_predicted'])\naccuracy ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:41:33.350527Z","iopub.execute_input":"2024-11-09T10:41:33.351394Z","iopub.status.idle":"2024-11-09T10:41:34.503546Z","shell.execute_reply.started":"2024-11-09T10:41:33.351355Z","shell.execute_reply":"2024-11-09T10:41:34.502624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T10:39:00.515816Z","iopub.execute_input":"2024-11-09T10:39:00.516222Z","iopub.status.idle":"2024-11-09T10:39:00.547384Z","shell.execute_reply.started":"2024-11-09T10:39:00.516183Z","shell.execute_reply":"2024-11-09T10:39:00.546476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}