{"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-04T18:42:21.833633Z","iopub.execute_input":"2024-11-04T18:42:21.834265Z","iopub.status.idle":"2024-11-04T18:42:22.188602Z","shell.execute_reply.started":"2024-11-04T18:42:21.834216Z","shell.execute_reply":"2024-11-04T18:42:22.187702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(\"../input/galaxy-zoo-the-galaxy-challenge/training_solutions_rev1.zip\",\n                   compression=\"zip\")\n\n#data = pd.read_csv('training_solutions_rev1.csv')\ndata.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.190071Z","iopub.execute_input":"2024-11-04T18:42:22.190446Z","iopub.status.idle":"2024-11-04T18:42:22.706432Z","shell.execute_reply.started":"2024-11-04T18:42:22.190413Z","shell.execute_reply":"2024-11-04T18:42:22.705265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.707775Z","iopub.execute_input":"2024-11-04T18:42:22.708168Z","iopub.status.idle":"2024-11-04T18:42:22.716059Z","shell.execute_reply.started":"2024-11-04T18:42:22.708123Z","shell.execute_reply":"2024-11-04T18:42:22.714949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def append_ext(filename):\n    \"\"\" Appends `.jpg` file extension to a filename \"\"\"\n    return f\"{filename}.jpg\"\n    #return filename + '.jpg'\n\n#applying the function to the GalaxyID colunm\ndata[\"GalaxyID\"] = data[\"GalaxyID\"].apply(append_ext)\n\n#printing the first five row of the dataframe to view the changes\ndata.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.718860Z","iopub.execute_input":"2024-11-04T18:42:22.719289Z","iopub.status.idle":"2024-11-04T18:42:22.781509Z","shell.execute_reply.started":"2024-11-04T18:42:22.719241Z","shell.execute_reply":"2024-11-04T18:42:22.780595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_data = data[((data[\"Class1.1\"] >= .8) | (data[\"Class1.2\"] >= .8))].reset_index(drop=True)\nprint(new_data.shape)\nnew_data.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.782510Z","iopub.execute_input":"2024-11-04T18:42:22.782800Z","iopub.status.idle":"2024-11-04T18:42:22.821459Z","shell.execute_reply.started":"2024-11-04T18:42:22.782768Z","shell.execute_reply":"2024-11-04T18:42:22.819302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_data.drop_duplicates(inplace=True)\nnew_data.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.822982Z","iopub.execute_input":"2024-11-04T18:42:22.823522Z","iopub.status.idle":"2024-11-04T18:42:22.883906Z","shell.execute_reply.started":"2024-11-04T18:42:22.823465Z","shell.execute_reply":"2024-11-04T18:42:22.882368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_data.isnull().sum().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.885359Z","iopub.execute_input":"2024-11-04T18:42:22.887283Z","iopub.status.idle":"2024-11-04T18:42:22.896881Z","shell.execute_reply.started":"2024-11-04T18:42:22.887248Z","shell.execute_reply":"2024-11-04T18:42:22.895972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conditions = [new_data[\"Class1.1\"] >= 0.8, \n              new_data[\"Class1.2\"] >= 0.8]\n\n#define choices\nchoices = [\"elliptical\", \"spiral\"]\n\n#create new column in DataFrame that displays results of comparisons\nnew_data[\"class\"] = np.select(conditions, choices, default=\"Tie\")\n\nnew_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.897992Z","iopub.execute_input":"2024-11-04T18:42:22.898309Z","iopub.status.idle":"2024-11-04T18:42:22.940249Z","shell.execute_reply.started":"2024-11-04T18:42:22.898276Z","shell.execute_reply":"2024-11-04T18:42:22.939399Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(new_data[new_data[\"class\"] == \"elliptical\"].shape)\n\nprint(new_data[new_data[\"class\"] == \"spiral\"].shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.941215Z","iopub.execute_input":"2024-11-04T18:42:22.941517Z","iopub.status.idle":"2024-11-04T18:42:22.962674Z","shell.execute_reply.started":"2024-11-04T18:42:22.941462Z","shell.execute_reply":"2024-11-04T18:42:22.961724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_spiral = new_data[new_data[\"class\"] == \"spiral\"].sample(n=8000,random_state = 42)\nprint(\"This is a sample of spiral galaxies\")\nprint(df_spiral.head(5))\nprint(df_spiral.shape)\n\ndf_elliptical =new_data[new_data[\"class\"] == \"elliptical\"].sample(n=8000,random_state = 42)\nprint(\"And this is a sample of elliptical galaxies\")\nprint(df_elliptical.head(5))\nprint(df_elliptical.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:22.965724Z","iopub.execute_input":"2024-11-04T18:42:22.966009Z","iopub.status.idle":"2024-11-04T18:42:23.008213Z","shell.execute_reply.started":"2024-11-04T18:42:22.965977Z","shell.execute_reply":"2024-11-04T18:42:23.007365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"frames = [df_spiral, df_elliptical]\ndf_galaxies = pd.concat(frames)\ndf_galaxies = df_galaxies[[\"GalaxyID\", \"class\"]]\ndf_galaxies.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:23.009130Z","iopub.execute_input":"2024-11-04T18:42:23.009403Z","iopub.status.idle":"2024-11-04T18:42:23.022181Z","shell.execute_reply.started":"2024-11-04T18:42:23.009372Z","shell.execute_reply":"2024-11-04T18:42:23.021363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_galaxies.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:23.023207Z","iopub.execute_input":"2024-11-04T18:42:23.023481Z","iopub.status.idle":"2024-11-04T18:42:23.029291Z","shell.execute_reply.started":"2024-11-04T18:42:23.023450Z","shell.execute_reply":"2024-11-04T18:42:23.028295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"../input/galaxy-zoo-the-galaxy-challenge/images_training_rev1.zip\", 'r') as zip_ref:\n    zip_ref.extractall()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:23.030516Z","iopub.execute_input":"2024-11-04T18:42:23.030922Z","iopub.status.idle":"2024-11-04T18:42:40.957813Z","shell.execute_reply.started":"2024-11-04T18:42:23.030874Z","shell.execute_reply":"2024-11-04T18:42:40.956825Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nimport matplotlib.pyplot as plt\n\n#images\nimg= image.load_img(\"./images_training_rev1/104934.jpg\")\nimg2 = image.load_img(\"./images_training_rev1/582003.jpg\")\n\n#Subplots\nplt.figure(figsize=(12,4.5))\nplt.subplot(141)\nplt.imshow(img)\nplt.title(\"Spiral Galaxy\", y=1.02, fontsize=12)\n\nplt.subplot(142)\nplt.imshow(img2)\nplt.title(\"Elliptical Galaxy\", y=1.02, fontsize=12)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:40.959033Z","iopub.execute_input":"2024-11-04T18:42:40.959343Z","iopub.status.idle":"2024-11-04T18:42:53.911445Z","shell.execute_reply.started":"2024-11-04T18:42:40.959309Z","shell.execute_reply":"2024-11-04T18:42:53.910562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n\nprint(\"The images are of size: \")\ncv2.imread(\"./images_training_rev1/104934.jpg\").shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:53.912817Z","iopub.execute_input":"2024-11-04T18:42:53.913419Z","iopub.status.idle":"2024-11-04T18:42:54.128457Z","shell.execute_reply.started":"2024-11-04T18:42:53.913375Z","shell.execute_reply":"2024-11-04T18:42:54.127553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.optimizers import Adam, RMSprop\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.callbacks import EarlyStopping\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport cv2\n\nimport tensorflow.keras.backend as K\nfrom tensorflow.keras.applications.inception_v3 import preprocess_input\n\nimport numpy as np\nimport os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:54.129719Z","iopub.execute_input":"2024-11-04T18:42:54.130051Z","iopub.status.idle":"2024-11-04T18:42:54.145688Z","shell.execute_reply.started":"2024-11-04T18:42:54.130018Z","shell.execute_reply":"2024-11-04T18:42:54.144879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1./255.,\n                           validation_split=0.2,\n\n)\n\n\ntrain_generator = datagen.flow_from_dataframe(dataframe = df_galaxies,\n                                          target_size = (224,224),\n                                          directory = \"./images_training_rev1\",\n                                          batch_size = 32,\n                                          x_col=\"GalaxyID\",\n                                          y_col=\"class\",\n                                          color_mode=\"rgb\",\n                                          class_mode=\"categorical\",\n                                          subset=\"training\",\n                                          shuffle=True,\n                                          seed=42\n                                         )\nvalid_generator = datagen.flow_from_dataframe(dataframe = df_galaxies,\n                                            target_size=(224, 224),\n                                            directory = \"./images_training_rev1\",\n                                            batch_size=32,\n                                            x_col=\"GalaxyID\",\n                                            y_col=\"class\",\n                                            color_mode=\"rgb\",\n                                            class_mode=\"categorical\",\n                                            subset=\"validation\",\n                                            shuffle=True,\n                                            seed=42\n                                           ) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:54.146855Z","iopub.execute_input":"2024-11-04T18:42:54.147137Z","iopub.status.idle":"2024-11-04T18:42:54.493125Z","shell.execute_reply.started":"2024-11-04T18:42:54.147104Z","shell.execute_reply":"2024-11-04T18:42:54.492425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator.class_indices","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:54.494112Z","iopub.execute_input":"2024-11-04T18:42:54.494407Z","iopub.status.idle":"2024-11-04T18:42:54.500221Z","shell.execute_reply.started":"2024-11-04T18:42:54.494375Z","shell.execute_reply":"2024-11-04T18:42:54.499370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# LoRA-like adaptation layer\nclass LoRALayer(tf.keras.layers.Layer):\n    def __init__(self, units, rank=4, alpha=16):\n        super(LoRALayer, self).__init__()\n        self.rank = rank\n        self.alpha = alpha\n        self.units = units\n\n    def build(self, input_shape):\n        self.low_rank = self.add_weight(\n            shape=(input_shape[-1], self.rank),\n            initializer=\"random_normal\",\n            trainable=True,\n        )\n        self.proj_up = self.add_weight(\n            shape=(self.rank, self.units),\n            initializer=\"random_normal\",\n            trainable=True,\n        )\n        self.scaling_factor = self.alpha / self.rank\n\n    def call(self, inputs):\n        return inputs @ self.low_rank @ self.proj_up * self.scaling_factor\n\n# Function to build a model with LoRA on the classifier\ndef build_model_with_lora(base_model, num_classes):\n    base_model.trainable = False  # Freeze base model layers\n    inputs = tf.keras.Input(shape=(224, 224, 3))\n    x = base_model(inputs, training=False)\n    x = layers.GlobalAveragePooling2D()(x)\n\n    # Apply LoRA layer to classifier\n    lora_output = LoRALayer(num_classes)(x)\n    outputs = layers.Dense(num_classes, activation=\"softmax\")(lora_output)\n\n    model = models.Model(inputs, outputs)\n    return model\n\n# Choose a pre-trained model and add LoRA adaptation\ndef choose_model(model_name, num_classes):\n    if model_name == \"MobileNet\":\n        base_model = tf.keras.applications.MobileNet(weights=\"imagenet\", include_top=False, input_shape=(224, 224, 3))\n    elif model_name == \"InceptionV3\":\n        base_model = tf.keras.applications.InceptionV3(weights=\"imagenet\", include_top=False, input_shape=(224, 224, 3))\n    elif model_name == \"VGG16\":\n        base_model = tf.keras.applications.VGG16(weights=\"imagenet\", include_top=False, input_shape=(224, 224, 3))\n    else:\n        raise ValueError(\"Model not supported\")\n    \n    return build_model_with_lora(base_model, num_classes)\n\n# Replace this with the actual number of classes in Galaxy Zoo\nnum_classes = 2\n\n# Select the model name\nmodel_name = \"MobileNet\"  \nmodel = choose_model(model_name, num_classes)\n\nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n    loss=\"categorical_crossentropy\",  \n    metrics=[\"accuracy\"]\n)\n\n\n\n\n\n\n\n# Fine-tune the model\nhistory = model.fit(\n    train_generator,\n    epochs=10,\n    validation_data=valid_generator\n)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:42:54.501472Z","iopub.execute_input":"2024-11-04T18:42:54.502134Z","iopub.status.idle":"2024-11-04T18:47:29.582875Z","shell.execute_reply.started":"2024-11-04T18:42:54.502090Z","shell.execute_reply":"2024-11-04T18:47:29.582020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_name = \"InceptionV3\"  \nmodel = choose_model(model_name, num_classes)\n\nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n    loss=\"categorical_crossentropy\",  \n    metrics=[\"accuracy\"]\n)\n\n\n\n\n\n\n\n# Fine-tune the model\nhistory = model.fit(\n    train_generator,\n    epochs=10,\n    validation_data=valid_generator\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:47:29.584368Z","iopub.execute_input":"2024-11-04T18:47:29.585093Z","iopub.status.idle":"2024-11-04T18:53:15.870390Z","shell.execute_reply.started":"2024-11-04T18:47:29.585045Z","shell.execute_reply":"2024-11-04T18:53:15.869528Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_name = \"VGG16\"  \nmodel = choose_model(model_name, num_classes)\n\nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n    loss=\"categorical_crossentropy\",  \n    metrics=[\"accuracy\"]\n)\n\n\n\n\n\n\n\n# Fine-tune the model\nhistory = model.fit(\n    train_generator,\n    epochs=10,\n    validation_data=valid_generator\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-04T18:53:15.871784Z","iopub.execute_input":"2024-11-04T18:53:15.872126Z","iopub.status.idle":"2024-11-04T19:04:46.012609Z","shell.execute_reply.started":"2024-11-04T18:53:15.872092Z","shell.execute_reply":"2024-11-04T19:04:46.011595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}