{"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-05T02:36:38.661286Z","iopub.execute_input":"2024-11-05T02:36:38.661665Z","iopub.status.idle":"2024-11-05T02:36:39.947108Z","shell.execute_reply.started":"2024-11-05T02:36:38.661628Z","shell.execute_reply":"2024-11-05T02:36:39.946174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import Sequence\nimport cv2\nimport tensorflow as tf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:36:47.460351Z","iopub.execute_input":"2024-11-05T02:36:47.461233Z","iopub.status.idle":"2024-11-05T02:37:02.977126Z","shell.execute_reply.started":"2024-11-05T02:36:47.461190Z","shell.execute_reply":"2024-11-05T02:37:02.976139Z"}},"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\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:37:11.516641Z","iopub.execute_input":"2024-11-05T02:37:11.517623Z","iopub.status.idle":"2024-11-05T02:37:11.952940Z","shell.execute_reply.started":"2024-11-05T02:37:11.517577Z","shell.execute_reply":"2024-11-05T02:37:11.952095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['GalaxyID'] = data['GalaxyID'].astype(str)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:37:23.354903Z","iopub.execute_input":"2024-11-05T02:37:23.355782Z","iopub.status.idle":"2024-11-05T02:37:23.394131Z","shell.execute_reply.started":"2024-11-05T02:37:23.355741Z","shell.execute_reply":"2024-11-05T02:37:23.393242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:37:27.175906Z","iopub.execute_input":"2024-11-05T02:37:27.176354Z","iopub.status.idle":"2024-11-05T02:37:27.220276Z","shell.execute_reply.started":"2024-11-05T02:37:27.176318Z","shell.execute_reply":"2024-11-05T02:37:27.219315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:06.986075Z","iopub.execute_input":"2024-11-05T02:38:06.986468Z","iopub.status.idle":"2024-11-05T02:38:07.019605Z","shell.execute_reply.started":"2024-11-05T02:38:06.986432Z","shell.execute_reply":"2024-11-05T02:38:07.018710Z"}},"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-05T02:37:34.983558Z","iopub.execute_input":"2024-11-05T02:37:34.983959Z","iopub.status.idle":"2024-11-05T02:37:52.054728Z","shell.execute_reply.started":"2024-11-05T02:37:34.983906Z","shell.execute_reply":"2024-11-05T02:37:52.053927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_dir=\"/kaggle/working/images_training_rev1\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:37:52.056336Z","iopub.execute_input":"2024-11-05T02:37:52.056658Z","iopub.status.idle":"2024-11-05T02:37:52.060756Z","shell.execute_reply.started":"2024-11-05T02:37:52.056624Z","shell.execute_reply":"2024-11-05T02:37:52.059881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df, val_df = train_test_split(data, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:37:52.062010Z","iopub.execute_input":"2024-11-05T02:37:52.062361Z","iopub.status.idle":"2024-11-05T02:37:52.099814Z","shell.execute_reply.started":"2024-11-05T02:37:52.062310Z","shell.execute_reply":"2024-11-05T02:37:52.099092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CustomDataGenerator(Sequence):\n    def __init__(self, dataframe, image_dir, batch_size=32, target_cols=None, image_size=(224, 224), shuffle=True, is_train=True):\n        self.dataframe = dataframe\n        self.image_dir = image_dir\n        self.batch_size = batch_size\n        self.target_cols = target_cols\n        self.image_size = image_size\n        self.shuffle = shuffle\n        self.indexes = np.arange(len(self.dataframe))\n        self.is_train = is_train\n        \n        if self.is_train:\n            self.image_data_gen = ImageDataGenerator(\n                rescale=1./255,\n                rotation_range=20,\n                width_shift_range=0.2,\n                height_shift_range=0.2,\n                shear_range=0.2,\n                zoom_range=0.2,\n                horizontal_flip=True,\n                fill_mode='nearest'\n            )\n        self.on_epoch_end()\n\n    def __len__(self):\n        return int(np.floor(len(self.dataframe) / self.batch_size))\n\n    def __getitem__(self, index):\n        batch_indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size]\n        batch_df = self.dataframe.iloc[batch_indexes]\n        \n        X = np.array([self.load_image(row['GalaxyID']) for _, row in batch_df.iterrows()])\n        y = batch_df[self.target_cols].values\n        \n        return X, y\n\n    def on_epoch_end(self):\n        if self.shuffle:\n            np.random.shuffle(self.indexes)\n\n    def load_image(self, image_name):\n        image_path = os.path.join(self.image_dir, f\"{image_name}.jpg\")  \n        image = cv2.imread(image_path)\n        image = cv2.resize(image, self.image_size)\n        \n        if self.is_train:\n            image = self.image_data_gen.random_transform(image)\n        else:\n            # Just rescale for validation images\n            image = image / 255.0\n                \n        return image\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:37:52.101849Z","iopub.execute_input":"2024-11-05T02:37:52.102450Z","iopub.status.idle":"2024-11-05T02:37:52.114722Z","shell.execute_reply.started":"2024-11-05T02:37:52.102403Z","shell.execute_reply":"2024-11-05T02:37:52.113838Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"batch_size = 256\nimage_size = (224, 224)  \ntarget_cols = train_df.columns[1:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:37:52.115734Z","iopub.execute_input":"2024-11-05T02:37:52.116050Z","iopub.status.idle":"2024-11-05T02:37:52.128358Z","shell.execute_reply.started":"2024-11-05T02:37:52.116018Z","shell.execute_reply":"2024-11-05T02:37:52.127599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator = CustomDataGenerator(train_df, image_dir, batch_size, target_cols, image_size)\nval_generator = CustomDataGenerator(val_df, image_dir, batch_size, target_cols, image_size, shuffle=False,is_train=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:17.492834Z","iopub.execute_input":"2024-11-05T02:38:17.493243Z","iopub.status.idle":"2024-11-05T02:38:17.499588Z","shell.execute_reply.started":"2024-11-05T02:38:17.493201Z","shell.execute_reply":"2024-11-05T02:38:17.498724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications import MobileNetV2\nfrom keras.models import Model,Sequential\nfrom keras.layers import Dense, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.callbacks import EarlyStopping\nfrom tensorflow.keras import applications,layers,models","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:19.710901Z","iopub.execute_input":"2024-11-05T02:38:19.711303Z","iopub.status.idle":"2024-11-05T02:38:19.719295Z","shell.execute_reply.started":"2024-11-05T02:38:19.711266Z","shell.execute_reply":"2024-11-05T02:38:19.718375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predictions(model, test_image_paths, batch_size=32, image_size=(224, 224), progress_interval=50):\n    predictions = []\n    image_ids = []\n\n    for start in range(0, len(test_image_paths), batch_size):\n        end = min(start + batch_size, len(test_image_paths))\n        batch_paths = test_image_paths[start:end]\n        all_images = []\n\n        for image_path in batch_paths:\n            image_id = os.path.splitext(os.path.basename(image_path))[0]\n            image = cv2.imread(image_path)\n\n            image = cv2.resize(image, image_size)\n            image = image / 255.0  \n            all_images.append(image)\n            image_ids.append(image_id)\n\n        all_images_array = np.array(all_images)\n        batch_predictions = model.predict(all_images_array)\n        predictions.extend(batch_predictions.tolist())\n\n    return predictions, image_ids\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:21.177898Z","iopub.execute_input":"2024-11-05T02:38:21.178796Z","iopub.status.idle":"2024-11-05T02:38:21.186441Z","shell.execute_reply.started":"2024-11-05T02:38:21.178753Z","shell.execute_reply":"2024-11-05T02:38:21.185413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with zipfile.ZipFile(\"/kaggle/input/galaxy-zoo-the-galaxy-challenge/images_test_rev1.zip\", 'r') as zip_ref:\n    zip_ref.extractall()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:22.934268Z","iopub.execute_input":"2024-11-05T02:38:22.934659Z","iopub.status.idle":"2024-11-05T02:38:45.339455Z","shell.execute_reply.started":"2024-11-05T02:38:22.934621Z","shell.execute_reply":"2024-11-05T02:38:45.338376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_dir=\"/kaggle/working/images_test_rev1\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:45.341473Z","iopub.execute_input":"2024-11-05T02:38:45.341868Z","iopub.status.idle":"2024-11-05T02:38:45.346368Z","shell.execute_reply.started":"2024-11-05T02:38:45.341824Z","shell.execute_reply":"2024-11-05T02:38:45.345360Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import glob","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:45.347478Z","iopub.execute_input":"2024-11-05T02:38:45.347800Z","iopub.status.idle":"2024-11-05T02:38:45.357393Z","shell.execute_reply.started":"2024-11-05T02:38:45.347767Z","shell.execute_reply":"2024-11-05T02:38:45.356413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_size = (224, 224) \ntest_image_paths = glob.glob(os.path.join(test_image_dir, '*.jpg'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:45.359587Z","iopub.execute_input":"2024-11-05T02:38:45.360308Z","iopub.status.idle":"2024-11-05T02:38:45.646332Z","shell.execute_reply.started":"2024-11-05T02:38:45.360251Z","shell.execute_reply":"2024-11-05T02:38:45.645527Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(test_image_paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:45.647436Z","iopub.execute_input":"2024-11-05T02:38:45.647749Z","iopub.status.idle":"2024-11-05T02:38:45.653571Z","shell.execute_reply.started":"2024-11-05T02:38:45.647715Z","shell.execute_reply":"2024-11-05T02:38:45.652658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def save_predictions_to_csv(predictions, image_ids, num_features, filename):\n   \n    predictions_df = pd.DataFrame(predictions, columns=[f'feature_{i+1}' for i in range(num_features)])\n    predictions_df['GalaxyID'] = image_ids \n\n    predictions_df = predictions_df[['GalaxyID'] + [f'feature_{i+1}' for i in range(num_features)]]\n\n    predictions_df.to_csv(filename, index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:45.654714Z","iopub.execute_input":"2024-11-05T02:38:45.655050Z","iopub.status.idle":"2024-11-05T02:38:45.663308Z","shell.execute_reply.started":"2024-11-05T02:38:45.655011Z","shell.execute_reply":"2024-11-05T02:38:45.662469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nearly_stopping = EarlyStopping(\n    monitor='val_loss',  \n    patience=2,          \n    verbose=1,          \n    restore_best_weights=True \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:38:45.664243Z","iopub.execute_input":"2024-11-05T02:38:45.664517Z","iopub.status.idle":"2024-11-05T02:38:45.680469Z","shell.execute_reply.started":"2024-11-05T02:38:45.664487Z","shell.execute_reply":"2024-11-05T02:38:45.679699Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Model without FineTuning","metadata":{}},{"cell_type":"code","source":"def create_model(input_shape=(224,224,3), output_size=37):\n    base_model = applications.MobileNetV2(\n        input_shape=input_shape,\n        include_top=False,\n        weights='imagenet'\n    )\n    base_model.trainable = False \n    \n    model = models.Sequential([\n        base_model,\n        layers.GlobalAveragePooling2D(), \n        layers.Dense(256, activation='relu'), \n        layers.Dropout(0.5),\n        layers.Dense(output_size, activation='linear')  \n    ])\n    \n    model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae'])\n    \n    return model\n    \nmodel = create_model()\nmodel.summary()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T03:38:36.397329Z","iopub.execute_input":"2024-11-05T03:38:36.397649Z","iopub.status.idle":"2024-11-05T03:38:37.182828Z","shell.execute_reply.started":"2024-11-05T03:38:36.397615Z","shell.execute_reply":"2024-11-05T03:38:37.181480Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=3,  \n    verbose=1,\n    callbacks=[early_stopping]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T03:38:37.184611Z","iopub.execute_input":"2024-11-05T03:38:37.185397Z","iopub.status.idle":"2024-11-05T03:57:37.771895Z","shell.execute_reply.started":"2024-11-05T03:38:37.185319Z","shell.execute_reply":"2024-11-05T03:57:37.770967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions, image_ids = predictions(model, test_image_paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T04:11:34.896357Z","iopub.execute_input":"2024-11-05T04:11:34.896678Z","iopub.status.idle":"2024-11-05T04:18:02.529277Z","shell.execute_reply.started":"2024-11-05T04:11:34.896643Z","shell.execute_reply":"2024-11-05T04:18:02.528434Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"save_predictions_to_csv(predictions, image_ids, len(predictions_finetuned[0]), 'predictions_no_finetuning.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T04:43:17.776253Z","iopub.execute_input":"2024-11-05T04:43:17.776592Z","iopub.status.idle":"2024-11-05T04:43:24.019536Z","shell.execute_reply.started":"2024-11-05T04:43:17.776556Z","shell.execute_reply":"2024-11-05T04:43:24.018455Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Using Model with FineTuning","metadata":{}},{"cell_type":"code","source":"def create_model_finetuning(input_shape=(224, 224, 3), output_size=37):\n    base_model = applications.MobileNetV2(\n        input_shape=input_shape,\n        include_top=False,\n        weights='imagenet'\n    )\n    \n    base_model.trainable = True\n    \n    for layer in base_model.layers[-10:]:\n        layer.trainable = True\n\n    model = models.Sequential([\n        base_model,\n        layers.GlobalAveragePooling2D(),  \n        layers.Dense(256, activation='relu'),\n        layers.Dropout(0.5),\n        layers.Dense(output_size, activation='linear') \n    ])\n    \n    model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae'])\n    print(model.summary())\n    return model\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:49:02.092742Z","iopub.execute_input":"2024-11-05T02:49:02.093508Z","iopub.status.idle":"2024-11-05T02:49:02.101057Z","shell.execute_reply.started":"2024-11-05T02:49:02.093463Z","shell.execute_reply":"2024-11-05T02:49:02.100030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfine_tuned_model = create_model_finetuning()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:49:04.387076Z","iopub.execute_input":"2024-11-05T02:49:04.387475Z","iopub.status.idle":"2024-11-05T02:49:05.204634Z","shell.execute_reply.started":"2024-11-05T02:49:04.387438Z","shell.execute_reply":"2024-11-05T02:49:05.203539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fine_tuned_model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=3, \n    verbose=1,\n    callbacks=[early_stopping]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T02:49:09.129900Z","iopub.execute_input":"2024-11-05T02:49:09.130362Z","iopub.status.idle":"2024-11-05T03:19:32.192440Z","shell.execute_reply.started":"2024-11-05T02:49:09.130319Z","shell.execute_reply":"2024-11-05T03:19:32.191475Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_finetuned, image_ids_finetuned = predictions(fine_tuned_model, test_image_paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T03:57:37.773127Z","iopub.execute_input":"2024-11-05T03:57:37.773475Z","iopub.status.idle":"2024-11-05T04:04:46.888874Z","shell.execute_reply.started":"2024-11-05T03:57:37.773439Z","shell.execute_reply":"2024-11-05T04:04:46.888110Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"save_predictions_to_csv(predictions_finetuned, image_ids_finetuned, len(predictions_finetuned[0]), 'predictions_finetuning.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T04:43:11.400902Z","iopub.execute_input":"2024-11-05T04:43:11.401280Z","iopub.status.idle":"2024-11-05T04:43:17.585281Z","shell.execute_reply.started":"2024-11-05T04:43:11.401245Z","shell.execute_reply":"2024-11-05T04:43:17.584245Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"FineTuning With LoRA","metadata":{}},{"cell_type":"code","source":"class LoRALayer(layers.Layer):\n    def __init__(self, original_layer, rank=4):\n        super(LoRALayer, self).__init__()\n        self.original_layer = original_layer\n        self.rank = rank\n        self.W_r = self.add_weight(shape=(self.original_layer.kernel.shape[0], self.rank), initializer=\"random_normal\", trainable=True)\n        self.W_l = self.add_weight(shape=(self.rank, self.original_layer.kernel.shape[1]), initializer=\"random_normal\", trainable=True)\n\n    def call(self, inputs):\n        return self.original_layer(inputs) + tf.matmul(tf.matmul(inputs, self.W_r), self.W_l)\n\ndef model_with_lora(input_shape=(224, 224, 3), output_size=37, rank=4):\n    base_model = MobileNetV2(input_shape=input_shape, include_top=False, weights=\"imagenet\")\n    base_model.trainable = False \n\n    for layer in base_model.layers[-10:]: \n        if isinstance(layer, layers.Conv2D):\n            lora_layer = LoRALayer(layer, rank=rank)\n            setattr(base_model, layer.name, lora_layer)\n            \n    x = base_model.output\n    x = layers.GlobalAveragePooling2D()(x)\n    x = layers.Dense(256, activation=\"relu\")(x)\n    x = layers.Dropout(0.5)(x)\n    outputs = layers.Dense(output_size, activation=\"linear\")(x)\n\n    model = Model(inputs=base_model.input, outputs=outputs)\n\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4), loss=\"mean_squared_error\", metrics=[\"mae\"])\n    print(model.summary())\n    return model\n\nmodel_with_lora = model_with_lora()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T03:19:32.194043Z","iopub.execute_input":"2024-11-05T03:19:32.194373Z","iopub.status.idle":"2024-11-05T03:19:33.234376Z","shell.execute_reply.started":"2024-11-05T03:19:32.194339Z","shell.execute_reply":"2024-11-05T03:19:33.233419Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_with_lora.fit(train_generator, validation_data=val_generator, epochs=3,callbacks=[early_stopping])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T03:19:33.235605Z","iopub.execute_input":"2024-11-05T03:19:33.235944Z","iopub.status.idle":"2024-11-05T03:38:36.394982Z","shell.execute_reply.started":"2024-11-05T03:19:33.235888Z","shell.execute_reply":"2024-11-05T03:38:36.393896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_lora, image_ids_lora = predictions(model_with_lora, test_image_paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T04:04:53.186185Z","iopub.execute_input":"2024-11-05T04:04:53.186608Z","iopub.status.idle":"2024-11-05T04:11:28.784004Z","shell.execute_reply.started":"2024-11-05T04:04:53.186569Z","shell.execute_reply":"2024-11-05T04:11:28.783174Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"save_predictions_to_csv(predictions_lora, image_ids_lora, len(predictions_lora[0]), 'predictions_lora.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T04:43:00.487127Z","iopub.execute_input":"2024-11-05T04:43:00.488022Z","iopub.status.idle":"2024-11-05T04:43:06.588202Z","shell.execute_reply.started":"2024-11-05T04:43:00.487980Z","shell.execute_reply":"2024-11-05T04:43:06.586971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = '/kaggle/working/predictions_no_finetuning.csv'\ndata = pd.read_csv(file_path)\ndisplay(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T05:41:51.325739Z","iopub.execute_input":"2024-11-05T05:41:51.326113Z","iopub.status.idle":"2024-11-05T05:41:52.128083Z","shell.execute_reply.started":"2024-11-05T05:41:51.326078Z","shell.execute_reply":"2024-11-05T05:41:52.127124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = '/kaggle/working/predictions_finetuning.csv'\ndata = pd.read_csv(file_path)\ndisplay(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T05:42:18.521417Z","iopub.execute_input":"2024-11-05T05:42:18.522380Z","iopub.status.idle":"2024-11-05T05:42:19.255965Z","shell.execute_reply.started":"2024-11-05T05:42:18.522335Z","shell.execute_reply":"2024-11-05T05:42:19.254697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = '/kaggle/working/predictions_lora.csv'\ndata = pd.read_csv(file_path)\ndisplay(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-05T05:42:35.269630Z","iopub.execute_input":"2024-11-05T05:42:35.270042Z","iopub.status.idle":"2024-11-05T05:42:36.030738Z","shell.execute_reply.started":"2024-11-05T05:42:35.270001Z","shell.execute_reply":"2024-11-05T05:42:36.029735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}