{"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","execution":{"iopub.status.busy":"2024-11-02T11:56:58.753988Z","iopub.execute_input":"2024-11-02T11:56:58.754302Z","iopub.status.idle":"2024-11-02T11:56:59.876871Z","shell.execute_reply.started":"2024-11-02T11:56:58.754267Z","shell.execute_reply":"2024-11-02T11:56:59.875821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/training_solutions_rev1.zip","metadata":{"execution":{"iopub.status.busy":"2024-11-02T11:57:27.938830Z","iopub.execute_input":"2024-11-02T11:57:27.939728Z","iopub.status.idle":"2024-11-02T11:57:29.093149Z","shell.execute_reply.started":"2024-11-02T11:57:27.939688Z","shell.execute_reply":"2024-11-02T11:57:29.091974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport os\nimport random\nimport matplotlib.pyplot as plt\ndf = pd.read_csv('/kaggle/working/training_solutions_rev1.csv')\ndf_train, df_test = train_test_split(df[:15000], test_size=.2)\ndf_train.shape, df_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-02T13:03:35.446656Z","iopub.execute_input":"2024-11-02T13:03:35.447734Z","iopub.status.idle":"2024-11-02T13:03:46.595792Z","shell.execute_reply.started":"2024-11-02T13:03:35.447678Z","shell.execute_reply":"2024-11-02T13:03:46.594821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/images_training_rev1.zip","metadata":{"execution":{"iopub.status.busy":"2024-11-02T11:58:51.292553Z","iopub.execute_input":"2024-11-02T11:58:51.293215Z","iopub.status.idle":"2024-11-02T11:59:06.866245Z","shell.execute_reply.started":"2024-11-02T11:58:51.293176Z","shell.execute_reply":"2024-11-02T11:59:06.864873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainPath = '/kaggle/working/images_training_rev1'\ndef plot_random_galaxy(path, sample = 5):\n    random_image=random.sample(os.listdir(path),sample)\n\n    plt.figure(figsize=(16,5))\n    for i in range(sample):\n\n        plt.subplot(1,sample,i+1)\n        img=tf.io.read_file(os.path.join(path,random_image[i]))\n        img=tf.io.decode_image(img)\n        plt.imshow(img)\n        plt.title(f'Class: {random_image[i]}\\nShape: {img.shape}')\n        plt.axis(False) ","metadata":{"execution":{"iopub.status.busy":"2024-11-02T13:03:46.597713Z","iopub.execute_input":"2024-11-02T13:03:46.598615Z","iopub.status.idle":"2024-11-02T13:03:46.606386Z","shell.execute_reply.started":"2024-11-02T13:03:46.598563Z","shell.execute_reply":"2024-11-02T13:03:46.605420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_random_galaxy(trainPath)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T12:37:27.563236Z","iopub.execute_input":"2024-11-02T12:37:27.563582Z","iopub.status.idle":"2024-11-02T12:37:29.608746Z","shell.execute_reply.started":"2024-11-02T12:37:27.563546Z","shell.execute_reply":"2024-11-02T12:37:29.607762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout\nfrom skimage.transform import resize\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\n# Constants\nIMG_SHAPE = (224, 224)  # ResNet50 expected input size\nCROP_SIZE = (256, 256)\nORIG_SHAPE = (424, 424)\nBATCH_SIZE = 32\n\n# Image processing functions\ndef get_image(path, x1, y1, shape, crop_size):\n    \"\"\"Process a single image: read, crop, resize, and normalize\"\"\"\n    x = plt.imread(path)\n    x = x[x1:x1+crop_size[0], y1:y1+crop_size[1]]\n    x = resize(x, shape)\n    x = x/255.\n    return x\n\ndef get_all_images(dataframe, shape=IMG_SHAPE, crop_size=CROP_SIZE):\n    \"\"\"Process all images in the dataframe\"\"\"\n    x1 = (ORIG_SHAPE[0]-CROP_SIZE[0])//2\n    y1 = (ORIG_SHAPE[1]-CROP_SIZE[1])//2\n    \n    sel = dataframe.values\n    ids = sel[:,0].astype(int).astype(str)\n    y_batch = sel[:,1:]\n    x_batch = []\n    \n    print(\"Processing images...\")\n    for i in tqdm(ids):\n        x = get_image(f'/kaggle/working/images_training_rev1/{i}.jpg', \n                     x1, y1, shape=shape, crop_size=crop_size)\n        x_batch.append(x)\n    \n    return np.array(x_batch), y_batch\n\n# Custom metrics\ndef root_mean_squared_error(y_true, y_pred):\n    return tf.sqrt(tf.reduce_mean(tf.square(y_pred - y_true)))","metadata":{"execution":{"iopub.status.busy":"2024-11-02T13:05:55.573829Z","iopub.execute_input":"2024-11-02T13:05:55.574817Z","iopub.status.idle":"2024-11-02T13:05:55.691093Z","shell.execute_reply.started":"2024-11-02T13:05:55.574750Z","shell.execute_reply":"2024-11-02T13:05:55.690109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/working/training_solutions_rev1.csv')\ndf_train, df_test = train_test_split(df[:15000], test_size=0.2, random_state=42)\nX_train, y_train = get_all_images(df_train)\nX_test, y_test = get_all_images(df_test)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T12:55:41.991878Z","iopub.execute_input":"2024-11-02T12:55:41.992373Z","iopub.status.idle":"2024-11-02T12:59:23.023122Z","shell.execute_reply.started":"2024-11-02T12:55:41.992340Z","shell.execute_reply":"2024-11-02T12:59:23.021910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n# Define file paths for saving arrays in Kaggle's output directory\nX_train_path = '/kaggle/working/X_train.npy'\nX_test_path = '/kaggle/working/X_test.npy'\ny_train_path = '/kaggle/working/y_train.npy'\ny_test_path = '/kaggle/working/y_test.npy'\n\n# Save the arrays\nnp.save(X_train_path, X_train)\nnp.save(X_test_path, X_test)\nnp.save(y_train_path, y_train)\nnp.save(y_test_path, y_test)\n\nprint(\"Datasets saved successfully.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-11-02T12:59:53.758809Z","iopub.execute_input":"2024-11-02T12:59:53.759761Z","iopub.status.idle":"2024-11-02T13:01:05.275556Z","shell.execute_reply.started":"2024-11-02T12:59:53.759719Z","shell.execute_reply":"2024-11-02T13:01:05.274148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n# Define paths for the saved data files\nX_train_path = '/kaggle/working/X_train.npy'\nX_test_path = '/kaggle/working/X_test.npy'\ny_train_path = '/kaggle/working/y_train.npy'\ny_test_path = '/kaggle/working/y_test.npy'\n\n# Load the datasets\nX_train = np.load(X_train_path)\nX_test = np.load(X_test_path)\ny_train = np.load(y_train_path)\ny_test = np.load(y_test_path)\n\nprint(\"Datasets loaded successfully.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-11-02T13:04:06.526896Z","iopub.execute_input":"2024-11-02T13:04:06.527848Z","iopub.status.idle":"2024-11-02T13:05:29.880927Z","shell.execute_reply.started":"2024-11-02T13:04:06.527795Z","shell.execute_reply":"2024-11-02T13:05:29.879572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model():\n    # Load pre-trained ResNet50\n    base_model = ResNet50(\n        weights='imagenet',\n        include_top=False,\n        input_shape=(IMG_SHAPE[0], IMG_SHAPE[1], 3)\n    )\n    \n    # Freeze the pre-trained weights\n    base_model.trainable = False\n    \n    # Add custom layers\n    x = base_model.output\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(512, activation='relu')(x)\n    x = Dropout(0.2)(x)\n    x = Dense(256, activation='relu')(x)\n    x = Dropout(0.2)(x)\n    predictions = Dense(37, activation='sigmoid')(x)\n    \n    model = Model(inputs=base_model.input, outputs=predictions)\n    \n    return model\n\n# Training strategy for multiple GPUs\nstrategy = tf.distribute.MirroredStrategy()\n\nwith strategy.scope():\n    model = build_model()\n    model.compile(\n        optimizer='adam',\n        loss='binary_crossentropy',\n        metrics=['accuracy', root_mean_squared_error]\n    )","metadata":{"execution":{"iopub.status.busy":"2024-11-02T13:06:00.971228Z","iopub.execute_input":"2024-11-02T13:06:00.971963Z","iopub.status.idle":"2024-11-02T13:06:04.272506Z","shell.execute_reply.started":"2024-11-02T13:06:00.971921Z","shell.execute_reply":"2024-11-02T13:06:04.271676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    batch_size=BATCH_SIZE,\n    epochs=8,\n    validation_data=(X_test, y_test),\n    \n)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T13:06:46.580688Z","iopub.execute_input":"2024-11-02T13:06:46.581141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}