{"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-03T19:39:28.246968Z","iopub.execute_input":"2024-11-03T19:39:28.247920Z","iopub.status.idle":"2024-11-03T19:39:29.290794Z","shell.execute_reply.started":"2024-11-03T19:39:28.247874Z","shell.execute_reply":"2024-11-03T19:39:29.289885Z"}},"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-03T19:39:29.292752Z","iopub.execute_input":"2024-11-03T19:39:29.293185Z","iopub.status.idle":"2024-11-03T19:39:29.816833Z","shell.execute_reply.started":"2024-11-03T19:39:29.293149Z","shell.execute_reply":"2024-11-03T19:39:29.815621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:39:29.819019Z","iopub.execute_input":"2024-11-03T19:39:29.819468Z","iopub.status.idle":"2024-11-03T19:39:29.831691Z","shell.execute_reply.started":"2024-11-03T19:39:29.819421Z","shell.execute_reply":"2024-11-03T19:39:29.830520Z"}},"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-03T19:39:29.833579Z","iopub.execute_input":"2024-11-03T19:39:29.833984Z","iopub.status.idle":"2024-11-03T19:39:29.917905Z","shell.execute_reply.started":"2024-11-03T19:39:29.833917Z","shell.execute_reply":"2024-11-03T19:39:29.916589Z"}},"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-03T19:39:29.922888Z","iopub.execute_input":"2024-11-03T19:39:29.923684Z","iopub.status.idle":"2024-11-03T19:39:29.960104Z","shell.execute_reply.started":"2024-11-03T19:39:29.923631Z","shell.execute_reply":"2024-11-03T19:39:29.959152Z"}},"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-03T19:39:29.961452Z","iopub.execute_input":"2024-11-03T19:39:29.961862Z","iopub.status.idle":"2024-11-03T19:39:30.020382Z","shell.execute_reply.started":"2024-11-03T19:39:29.961813Z","shell.execute_reply":"2024-11-03T19:39:30.018731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_data.isnull().sum().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:39:30.022350Z","iopub.execute_input":"2024-11-03T19:39:30.022768Z","iopub.status.idle":"2024-11-03T19:39:30.037689Z","shell.execute_reply.started":"2024-11-03T19:39:30.022719Z","shell.execute_reply":"2024-11-03T19:39:30.035376Z"}},"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-03T19:39:30.038920Z","iopub.execute_input":"2024-11-03T19:39:30.039228Z","iopub.status.idle":"2024-11-03T19:39:30.092633Z","shell.execute_reply.started":"2024-11-03T19:39:30.039197Z","shell.execute_reply":"2024-11-03T19:39:30.091714Z"}},"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-03T19:39:30.093891Z","iopub.execute_input":"2024-11-03T19:39:30.094283Z","iopub.status.idle":"2024-11-03T19:39:30.117290Z","shell.execute_reply.started":"2024-11-03T19:39:30.094239Z","shell.execute_reply":"2024-11-03T19:39:30.116335Z"}},"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-03T19:39:30.118501Z","iopub.execute_input":"2024-11-03T19:39:30.118846Z","iopub.status.idle":"2024-11-03T19:39:30.171588Z","shell.execute_reply.started":"2024-11-03T19:39:30.118812Z","shell.execute_reply":"2024-11-03T19:39:30.170503Z"}},"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-03T19:39:30.172997Z","iopub.execute_input":"2024-11-03T19:39:30.173444Z","iopub.status.idle":"2024-11-03T19:39:30.190928Z","shell.execute_reply.started":"2024-11-03T19:39:30.173392Z","shell.execute_reply":"2024-11-03T19:39:30.189978Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_galaxies.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:39:30.192045Z","iopub.execute_input":"2024-11-03T19:39:30.192363Z","iopub.status.idle":"2024-11-03T19:39:30.198591Z","shell.execute_reply.started":"2024-11-03T19:39:30.192329Z","shell.execute_reply":"2024-11-03T19:39:30.197640Z"}},"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-03T19:39:30.199808Z","iopub.execute_input":"2024-11-03T19:39:30.200180Z","iopub.status.idle":"2024-11-03T19:39:50.277843Z","shell.execute_reply.started":"2024-11-03T19:39:30.200137Z","shell.execute_reply":"2024-11-03T19:39:50.276794Z"}},"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-03T19:39:50.282515Z","iopub.execute_input":"2024-11-03T19:39:50.282837Z","iopub.status.idle":"2024-11-03T19:40:03.302920Z","shell.execute_reply.started":"2024-11-03T19:39:50.282804Z","shell.execute_reply":"2024-11-03T19:40:03.301980Z"}},"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-03T19:40:03.304011Z","iopub.execute_input":"2024-11-03T19:40:03.304469Z","iopub.status.idle":"2024-11-03T19:40:03.523817Z","shell.execute_reply.started":"2024-11-03T19:40:03.304437Z","shell.execute_reply":"2024-11-03T19:40:03.522927Z"}},"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-03T19:40:03.525093Z","iopub.execute_input":"2024-11-03T19:40:03.525840Z","iopub.status.idle":"2024-11-03T19:40:03.542102Z","shell.execute_reply.started":"2024-11-03T19:40:03.525795Z","shell.execute_reply":"2024-11-03T19:40:03.541354Z"}},"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-03T19:40:03.543097Z","iopub.execute_input":"2024-11-03T19:40:03.543353Z","iopub.status.idle":"2024-11-03T19:40:03.897692Z","shell.execute_reply.started":"2024-11-03T19:40:03.543322Z","shell.execute_reply":"2024-11-03T19:40:03.896972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator.class_indices","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:40:03.898686Z","iopub.execute_input":"2024-11-03T19:40:03.898968Z","iopub.status.idle":"2024-11-03T19:40:03.904990Z","shell.execute_reply.started":"2024-11-03T19:40:03.898919Z","shell.execute_reply":"2024-11-03T19:40:03.904067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16,(6,6),activation = 'relu',\n                                                           input_shape=(224,224,3)),\n                                  #tf.keras.layers.BatchNormalization(),\n                                  tf.keras.layers.MaxPool2D(2,2),\n                                  #tf.keras.layers.Dropout(0.1),\n                                    \n                                  #\n                                  tf.keras.layers.Conv2D(32,(5,5),activation = 'relu'),\n                                  #tf.keras.layers.BatchNormalization(),\n                                  tf.keras.layers.MaxPool2D(2,2),\n                                  tf.keras.layers.Dropout(0.1),\n                                    \n                                  #\n                                  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu'),\n                                  #tf.keras.layers.BatchNormalization(),\n                                  tf.keras.layers.MaxPool2D(2,2),\n                                  #tf.keras.layers.Dropout(0.1),\n                                    \n                                  #\n                                  tf.keras.layers.Conv2D(128,(3,3),activation = 'relu'),\n                                  #tf.keras.layers.BatchNormalization(),\n                                  tf.keras.layers.MaxPool2D(2,2),\n                                  tf.keras.layers.Dropout(0.1),\n                                    \n                                  #\n                                  tf.keras.layers.Flatten(),\n                                  \n                                  #\n                                  tf.keras.layers.Dense(512, activation='relu'),\n                                  #tf.keras.layers.Dropout(0.1),\n                                    \n                                  tf.keras.layers.Dense(256, activation='relu'),\n                                  #tf.keras.layers.Dropout(0.1),\n                                    \n                                   #\n                                  tf.keras.layers.Dense(2, activation='softmax')\n                                  ])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:40:03.906229Z","iopub.execute_input":"2024-11-03T19:40:03.906535Z","iopub.status.idle":"2024-11-03T19:40:04.755872Z","shell.execute_reply.started":"2024-11-03T19:40:03.906501Z","shell.execute_reply":"2024-11-03T19:40:04.754855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"summary = model.summary()\ntf.keras.utils.plot_model(model, to_file=\"model_plot3.png\", \n                          show_shapes=True, show_layer_names=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:40:04.757389Z","iopub.execute_input":"2024-11-03T19:40:04.758117Z","iopub.status.idle":"2024-11-03T19:40:05.545851Z","shell.execute_reply.started":"2024-11-03T19:40:04.758071Z","shell.execute_reply":"2024-11-03T19:40:05.544890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\nloss=\"categorical_crossentropy\",\noptimizer=Adam(learning_rate=0.001),\nmetrics=[\"accuracy\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:40:05.547252Z","iopub.execute_input":"2024-11-03T19:40:05.547542Z","iopub.status.idle":"2024-11-03T19:40:05.561660Z","shell.execute_reply.started":"2024-11-03T19:40:05.547511Z","shell.execute_reply":"2024-11-03T19:40:05.560728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"hist = model.fit(train_generator, epochs = 10, validation_data=valid_generator \n                )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:40:05.563047Z","iopub.execute_input":"2024-11-03T19:40:05.563343Z","iopub.status.idle":"2024-11-03T19:44:51.717737Z","shell.execute_reply.started":"2024-11-03T19:40:05.563312Z","shell.execute_reply":"2024-11-03T19:44:51.716905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.layers import GlobalAveragePooling2D,Dense\n\n# Load the MobileNetV2 model\nbase_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Unfreeze the last few layers\nfor layer in base_model.layers[-10:]:\n    layer.trainable = True\n\n# Define the model\nmodel2 = tf.keras.models.Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(512, activation='relu'),\n    Dense(256, activation='relu'),\n    Dense(2, activation='softmax')\n])\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:44:52.079414Z","iopub.execute_input":"2024-11-03T19:44:52.079720Z","iopub.status.idle":"2024-11-03T19:44:53.424157Z","shell.execute_reply.started":"2024-11-03T19:44:52.079688Z","shell.execute_reply":"2024-11-03T19:44:53.423100Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model2.compile(loss=\"categorical_crossentropy\",\n              optimizer=Adam(learning_rate=1e-4),\n              metrics=[\"accuracy\"])\n\nhist = model2.fit(train_generator,\n                 epochs=10,\n                 validation_data=valid_generator\n                 )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:44:53.425630Z","iopub.execute_input":"2024-11-03T19:44:53.426419Z","iopub.status.idle":"2024-11-03T19:53:23.867289Z","shell.execute_reply.started":"2024-11-03T19:44:53.426383Z","shell.execute_reply":"2024-11-03T19:53:23.866307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionV3\n\nbase_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Unfreeze the last few layers for fine-tuning\nfor layer in base_model.layers[-10:]:\n    layer.trainable = True\n\n# Create the model\nmodel3 = tf.keras.models.Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(512, activation='relu'),\n    Dense(256, activation='relu'),\n    Dense(2, activation='softmax')  # Assuming binary classification\n])\n\nmodel3.compile(\n    loss=\"categorical_crossentropy\",\n    optimizer=Adam(learning_rate=1e-5),\n    metrics=[\"accuracy\"]\n)\n\nhist = model3.fit(\n    train_generator,\n    epochs=10,\n    validation_data=valid_generator\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T19:53:23.868728Z","iopub.execute_input":"2024-11-03T19:53:23.869093Z","iopub.status.idle":"2024-11-03T20:11:20.439300Z","shell.execute_reply.started":"2024-11-03T19:53:23.869057Z","shell.execute_reply":"2024-11-03T20:11:20.438321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\n\n# Load the VGG16 model, without the top layers\nbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Unfreeze the last few layers\nfor layer in base_model.layers[-10:]:\n    layer.trainable = True\n\n# Define the model\nmodel4 = tf.keras.models.Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(512, activation='relu'),\n    Dense(256, activation='relu'),\n    Dense(2, activation='softmax')  # For binary classification\n])\nmodel4.compile(\n    loss=\"categorical_crossentropy\",\n    optimizer=Adam(learning_rate=1e-5),\n    metrics=[\"accuracy\"]\n)\n\nhist = model4.fit(\n    train_generator,\n    epochs=10,\n    validation_data=valid_generator\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T20:14:31.525241Z","iopub.execute_input":"2024-11-03T20:14:31.525657Z","iopub.status.idle":"2024-11-03T20:45:04.861499Z","shell.execute_reply.started":"2024-11-03T20:14:31.525618Z","shell.execute_reply":"2024-11-03T20:45:04.860676Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}