{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3175,"databundleVersionId":44352,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-08-16T15:41:38.773096Z","iopub.execute_input":"2024-08-16T15:41:38.774090Z","iopub.status.idle":"2024-08-16T15:41:40.033519Z","shell.execute_reply.started":"2024-08-16T15:41:38.774017Z","shell.execute_reply":"2024-08-16T15:41:40.032236Z"},"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","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:41:40.036019Z","iopub.execute_input":"2024-08-16T15:41:40.036697Z","iopub.status.idle":"2024-08-16T15:41:56.548239Z","shell.execute_reply.started":"2024-08-16T15:41:40.036628Z","shell.execute_reply":"2024-08-16T15:41:56.547197Z"},"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-08-16T15:41:56.549633Z","iopub.execute_input":"2024-08-16T15:41:56.550494Z","iopub.status.idle":"2024-08-16T15:41:57.903017Z","shell.execute_reply.started":"2024-08-16T15:41:56.550452Z","shell.execute_reply":"2024-08-16T15:41:57.901453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\n\n# Load the dataset\ndf = pd.read_csv('../input/galaxy-zoo-the-galaxy-challenge/training_solutions_rev1.zip', compression='zip')\n\n# Retrieve only the required columns\ndf_selected = df[['GalaxyID', 'Class1.1', 'Class1.2', 'Class1.3']]\n\n# Rename the columns\ndf_selected.rename(columns={\n    'Class1.1': 'elliptical',\n    'Class1.2': 'spiral',\n    'Class1.3': 'uncertain'\n}, inplace=True)\n\n# Split into train and test sets\ndf_train, df_test = train_test_split(df_selected, test_size=0.2)\n\n# Calculate the class column for the train set\ndf_train['class'] = np.argmax(df_train[['elliptical', 'spiral', 'uncertain']].values, axis=1) + 1\n\n# Calculate the class column for the test set\ndf_test['class'] = np.argmax(df_test[['elliptical', 'spiral', 'uncertain']].values, axis=1) + 1\n\n# Drop the original class columns\ndf_train = df_train.drop(columns=['elliptical', 'spiral', 'uncertain'])\ndf_test = df_test.drop(columns=['elliptical', 'spiral', 'uncertain'])\n\n# Display the shape of the training set\nprint(df_train.shape)\n\n# Display the first few rows of the training set\nprint(df_train.head())\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:41:57.906391Z","iopub.execute_input":"2024-08-16T15:41:57.906820Z","iopub.status.idle":"2024-08-16T15:41:58.409655Z","shell.execute_reply.started":"2024-08-16T15:41:57.906783Z","shell.execute_reply":"2024-08-16T15:41:58.408277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:41:58.411035Z","iopub.execute_input":"2024-08-16T15:41:58.411367Z","iopub.status.idle":"2024-08-16T15:41:58.425535Z","shell.execute_reply.started":"2024-08-16T15:41:58.411336Z","shell.execute_reply":"2024-08-16T15:41:58.424429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/images_training_rev1.zip\n!unzip -q /kaggle/input/galaxy-zoo-the-galaxy-challenge/images_test_rev1.zip","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:41:58.427019Z","iopub.execute_input":"2024-08-16T15:41:58.427390Z","iopub.status.idle":"2024-08-16T15:42:38.285504Z","shell.execute_reply.started":"2024-08-16T15:41:58.427358Z","shell.execute_reply":"2024-08-16T15:42:38.283934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainPath = '/kaggle/working/images_training_rev1'\ntestPath = '/kaggle/working/images_test_rev1'","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:42:38.287595Z","iopub.execute_input":"2024-08-16T15:42:38.288147Z","iopub.status.idle":"2024-08-16T15:42:38.295450Z","shell.execute_reply.started":"2024-08-16T15:42:38.288060Z","shell.execute_reply":"2024-08-16T15:42:38.293540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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) \nplot_random_galaxy(trainPath)","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:42:38.296820Z","iopub.execute_input":"2024-08-16T15:42:38.297131Z","iopub.status.idle":"2024-08-16T15:42:40.786818Z","shell.execute_reply.started":"2024-08-16T15:42:38.297105Z","shell.execute_reply":"2024-08-16T15:42:40.785504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.transform import resize\nfrom tqdm import tqdm # progress which the model\nimport matplotlib.pyplot as plt\n%matplotlib inline\n          \n\nORIG_SHAPE = (424,424)\nCROP_SIZE = (256,256)\nIMG_SHAPE = (64,64)\n\ndef get_image(path, x1,y1, shape, crop_size):\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    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    for i in tqdm(ids):\n        x = get_image('/kaggle/working/images_training_rev1/'+i+'.jpg', x1,y1, shape=shape, crop_size=crop_size)\n        x_batch.append(x)\n    x_batch = np.array(x_batch)\n    return x_batch, y_batch\n        \nX_train, y_train = get_all_images(df_train)\nX_test, y_test = get_all_images(df_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:42:40.788436Z","iopub.execute_input":"2024-08-16T15:42:40.788838Z","iopub.status.idle":"2024-08-16T15:51:07.269461Z","shell.execute_reply.started":"2024-08-16T15:42:40.788803Z","shell.execute_reply":"2024-08-16T15:51:07.267058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:51:07.276262Z","iopub.execute_input":"2024-08-16T15:51:07.278784Z","iopub.status.idle":"2024-08-16T15:51:07.291001Z","shell.execute_reply.started":"2024-08-16T15:51:07.278741Z","shell.execute_reply":"2024-08-16T15:51:07.289599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import Input\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nfrom tensorflow.keras.utils import to_categorical\n\n# Define the dimensions of your images\nimage_height = 64\nimage_width = 64\nnum_channels = 3  # Assuming RGB images\n\n# Check the maximum label value to determine the number of classes\nprint(\"Max label in y_train:\", np.max(y_train))\nprint(\"Max label in y_test:\", np.max(y_test))\n\n# Define the number of classes based on the maximum label value\nnum_classes = np.max([np.max(y_train), np.max(y_test)]) + 1\n\n# Define the CNN model with an explicit Input layer\nmodel = Sequential([\n    Input(shape=(image_height, image_width, num_channels)),  # Explicit Input layer\n    \n    Conv2D(16, (3, 3), padding='same', activation='relu'),\n    MaxPooling2D((2, 2), strides=2),\n    \n    Conv2D(8, (3, 3), padding='same', activation='relu'),\n    MaxPooling2D((2, 2), strides=2),\n    \n    Conv2D(8, (3, 3), padding='same', activation='relu'),\n    MaxPooling2D((2, 2), strides=2),\n    \n    Flatten(),\n    Dense(256, activation='relu'),\n    Dense(128, activation='relu'),\n    Dense(num_classes, activation='softmax')  # Use num_classes here\n])\n\n# Compile the model\nmodel.compile(optimizer=Adam(learning_rate=0.001), \n              loss=CategoricalCrossentropy(), \n              metrics=['accuracy'])\n\n# Convert integer labels to one-hot encoded format\ny_train_one_hot = to_categorical(y_train, num_classes=num_classes)\ny_test_one_hot = to_categorical(y_test, num_classes=num_classes)\n\n# Train the model\nmodel.fit(X_train, y_train_one_hot, \n          validation_data=(X_test, y_test_one_hot), \n          batch_size=32, epochs=40)\n\n# Evaluate the model on the test data\ntest_loss, test_accuracy = model.evaluate(X_test, y_test_one_hot)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-16T15:51:07.292496Z","iopub.execute_input":"2024-08-16T15:51:07.292885Z","iopub.status.idle":"2024-08-16T16:39:05.444396Z","shell.execute_reply.started":"2024-08-16T15:51:07.292853Z","shell.execute_reply":"2024-08-16T16:39:05.442925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Max label in y_train:\", np.max(y_train))\nprint(\"Max label in y_test:\", np.max(y_test))\n","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:39:05.446379Z","iopub.execute_input":"2024-08-16T16:39:05.446841Z","iopub.status.idle":"2024-08-16T16:39:05.454867Z","shell.execute_reply.started":"2024-08-16T16:39:05.446801Z","shell.execute_reply":"2024-08-16T16:39:05.453480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train[0])","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:39:05.456934Z","iopub.execute_input":"2024-08-16T16:39:05.457589Z","iopub.status.idle":"2024-08-16T16:39:05.487143Z","shell.execute_reply.started":"2024-08-16T16:39:05.457544Z","shell.execute_reply":"2024-08-16T16:39:05.485901Z"},"trusted":true},"execution_count":null,"outputs":[]}]}