{"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":"gpu","dataSources":[{"sourceId":3175,"databundleVersionId":44352,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\n\nfrom PIL import Image\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torchvision import datasets, transforms\nfrom torch.utils.data import DataLoader, Dataset\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error","metadata":{"_uuid":"12c4478e-ad7d-44ee-bdc7-860f2ca8266d","_cell_guid":"3bd5ee0d-42c8-484d-8efd-54a2f694d4f7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T19:11:29.666922Z","iopub.execute_input":"2024-08-15T19:11:29.667792Z","iopub.status.idle":"2024-08-15T19:11:29.673332Z","shell.execute_reply.started":"2024-08-15T19:11:29.667757Z","shell.execute_reply":"2024-08-15T19:11:29.672400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T18:38:45.307693Z","iopub.execute_input":"2024-08-15T18:38:45.308512Z","iopub.status.idle":"2024-08-15T18:38:45.343288Z","shell.execute_reply.started":"2024-08-15T18:38:45.308482Z","shell.execute_reply":"2024-08-15T18:38:45.342284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/galaxy-zoo-the-galaxy-challenge/training_solutions_rev1.zip',compression='zip')\ndf_train, df_test = train_test_split(df, test_size=.2)\ndf_train","metadata":{"_uuid":"663b908a-39bf-4b72-8449-9d20641067bb","_cell_guid":"c292685a-bd06-4ee4-9771-a14b68cce80d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T19:26:26.127136Z","iopub.execute_input":"2024-08-15T19:26:26.127889Z","iopub.status.idle":"2024-08-15T19:26:26.586291Z","shell.execute_reply.started":"2024-08-15T19:26:26.127856Z","shell.execute_reply":"2024-08-15T19:26:26.585101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q -n ../input/galaxy-zoo-the-galaxy-challenge/images_training_rev1.zip -d ../temp/\n!unzip -q -n ../input/galaxy-zoo-the-galaxy-challenge/images_test_rev1.zip -d ../temp/","metadata":{"_uuid":"2307d694-1ceb-4236-ad73-37fbd96cd1b4","_cell_guid":"3c421b3e-5f8c-489e-8143-94ae399265a5","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self, image_paths, labels, transform=None):\n        self.image_paths = image_paths\n        self.labels = labels\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.image_paths)\n\n    def __getitem__(self, idx):\n        image = Image.open(self.image_paths[idx])\n        label = self.labels[idx]\n        if self.transform:\n            image = self.transform(image)\n        return image, torch.tensor(label, dtype=torch.float32)","metadata":{"_uuid":"f2495b33-8cd6-405b-a31d-73a4ddd8bd63","_cell_guid":"c28bc6ec-69cb-42d1-a510-e86e18b294d5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T18:39:33.979244Z","iopub.execute_input":"2024-08-15T18:39:33.979694Z","iopub.status.idle":"2024-08-15T18:39:33.986932Z","shell.execute_reply.started":"2024-08-15T18:39:33.979652Z","shell.execute_reply":"2024-08-15T18:39:33.986123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((128, 128)),\n    transforms.ToTensor(),\n])","metadata":{"_uuid":"d640bc57-bdb1-4efa-9b3e-25f678b5ef4e","_cell_guid":"dc92598b-ab2d-4253-bf5c-5dee753132d3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T18:39:33.988470Z","iopub.execute_input":"2024-08-15T18:39:33.989128Z","iopub.status.idle":"2024-08-15T18:39:34.028109Z","shell.execute_reply.started":"2024-08-15T18:39:33.989082Z","shell.execute_reply":"2024-08-15T18:39:34.027135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths = [f'../temp/images_training_rev1/{id}.jpg' for id in df_train.GalaxyID]\nlabels = df_train.values[:,1:]","metadata":{"_uuid":"49911e1e-dce2-406a-8f29-a31fc868fa28","_cell_guid":"9f2991cb-0271-4c63-adb9-7ca8328a0287","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T18:39:34.029503Z","iopub.execute_input":"2024-08-15T18:39:34.030553Z","iopub.status.idle":"2024-08-15T18:39:34.067042Z","shell.execute_reply.started":"2024-08-15T18:39:34.030517Z","shell.execute_reply":"2024-08-15T18:39:34.065812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_paths, val_image_paths, train_labels, val_labels = train_test_split(\n    image_paths, labels, test_size=0.2, random_state=42)","metadata":{"_uuid":"965f79cc-d653-42b0-94d4-0719bca883a9","_cell_guid":"36ce0984-34c8-4b5a-8532-7b5907b6b095","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T18:39:34.068330Z","iopub.execute_input":"2024-08-15T18:39:34.068648Z","iopub.status.idle":"2024-08-15T18:39:34.108087Z","shell.execute_reply.started":"2024-08-15T18:39:34.068623Z","shell.execute_reply":"2024-08-15T18:39:34.107150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = CustomDataset(train_image_paths, train_labels, transform=transform)\nval_dataset = CustomDataset(val_image_paths, val_labels, transform=transform)\n\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)","metadata":{"_uuid":"0403bf07-3de8-4ddc-b359-4a6b0465ea64","_cell_guid":"b7409a85-563f-4d1e-a5e0-4ea62aa01f1a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T18:39:34.109189Z","iopub.execute_input":"2024-08-15T18:39:34.109499Z","iopub.status.idle":"2024-08-15T18:39:34.115339Z","shell.execute_reply.started":"2024-08-15T18:39:34.109472Z","shell.execute_reply":"2024-08-15T18:39:34.114292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CNN(nn.Module):\n    def __init__(self):\n        super(CNN, self).__init__()\n        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)\n        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)\n        self.fc1 = nn.Linear(64 * 32 * 32, 128)\n        self.fc2 = nn.Linear(128, 37)  \n\n    def forward(self, x):\n        x = torch.relu(self.conv1(x))\n        x = torch.max_pool2d(x, kernel_size=2, stride=2)\n        x = torch.relu(self.conv2(x))\n        x = torch.max_pool2d(x, kernel_size=2, stride=2)\n        x = x.view(x.size(0), -1)\n        x = torch.relu(self.fc1(x))\n        x = self.fc2(x)\n        return x\n\nmodel = CNN().to(device) \n\ncriterion = nn.MSELoss() \noptimizer = optim.Adam(model.parameters(), lr=0.001)","metadata":{"_uuid":"e05b44dc-fd5a-452e-ae9a-829a72819436","_cell_guid":"1d78907f-d06b-4c01-a302-898a00a4df51","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T18:39:34.116797Z","iopub.execute_input":"2024-08-15T18:39:34.117600Z","iopub.status.idle":"2024-08-15T18:39:34.414876Z","shell.execute_reply.started":"2024-08-15T18:39:34.117568Z","shell.execute_reply":"2024-08-15T18:39:34.413770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 10\nfor epoch in range(num_epochs):\n    model.train()\n    train_loss = 0.0\n    for images, labels in tqdm(train_loader):\n        images, labels = images.to(device), labels.to(device)  # Transferir dados para GPU\n        optimizer.zero_grad()\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        train_loss += loss.item()\n\n    # Validar o Modelo\n    model.eval()\n    val_loss = 0.0\n    with torch.no_grad():\n        for images, labels in (val_loader):\n            images, labels = images.to(device), labels.to(device)  # Transferir dados para GPU\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n            val_loss += loss.item()\n\n    print(f'Epoch {epoch+1}/{num_epochs}, Train Loss: {train_loss/len(train_loader):.4f}, Val Loss: {val_loss/len(val_loader):.4f}')","metadata":{"_uuid":"160afb5f-5be8-42e8-b130-79f78b23f938","_cell_guid":"a1bcbd9d-ac58-43ca-89b4-f2d3e358dd08","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T18:39:34.418258Z","iopub.execute_input":"2024-08-15T18:39:34.418641Z","iopub.status.idle":"2024-08-15T19:02:42.114502Z","shell.execute_reply.started":"2024-08-15T18:39:34.418608Z","shell.execute_reply":"2024-08-15T19:02:42.113501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_paths = [f'../temp/images_training_rev1/{id}.jpg' for id in df_test.GalaxyID]\ntest_labels = df_test.values[:,1:]\n\ntest_dataset = CustomDataset(test_image_paths, test_labels, transform=transform)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)","metadata":{"_uuid":"bde4609c-f2d2-4b86-9e84-0c26d644c6cc","_cell_guid":"d62bd06a-447a-4c15-a5a8-4888d655831e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T19:02:42.116135Z","iopub.execute_input":"2024-08-15T19:02:42.116770Z","iopub.status.idle":"2024-08-15T19:02:42.128324Z","shell.execute_reply.started":"2024-08-15T19:02:42.116740Z","shell.execute_reply":"2024-08-15T19:02:42.127410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntrue_labels = []\npredicted_labels = []\n\nwith torch.no_grad():\n    for images, labels in tqdm(test_loader):\n        images, labels = images.to(device), labels.to(device)  # Transferir dados para GPU\n        outputs = model(images)\n        true_labels.extend(labels.cpu().numpy())  # Transferir para CPU e converter para numpy\n        predicted_labels.extend(outputs.cpu().numpy())  # Transferir para CPU e converter para numpy\n\n# Calcular RMSE\nrmse = np.sqrt(mean_squared_error(true_labels, predicted_labels))\nprint(f'RMSE: {rmse:.4f}')","metadata":{"_uuid":"3a48d471-624c-453f-8e5b-2dd5309d6202","_cell_guid":"a89b7910-b4db-4fdd-8d50-ef306c382723","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-08-15T19:02:42.129759Z","iopub.execute_input":"2024-08-15T19:02:42.130114Z","iopub.status.idle":"2024-08-15T19:03:14.626348Z","shell.execute_reply.started":"2024-08-15T19:02:42.130082Z","shell.execute_reply":"2024-08-15T19:03:14.625413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\n\nfor image in tqdm(sub_image):\n    \n    image = Image.open(f'../temp/images_test_rev1/{image}')\n    image = transform(image).unsqueeze(0).to(device)\n    \n    with torch.no_grad():\n        outputs = model(image)\n        predictions.append(outputs.cpu().numpy())","metadata":{"execution":{"iopub.status.busy":"2024-08-15T19:44:24.036611Z","iopub.execute_input":"2024-08-15T19:44:24.036968Z","iopub.status.idle":"2024-08-15T19:48:24.656280Z","shell.execute_reply.started":"2024-08-15T19:44:24.036939Z","shell.execute_reply":"2024-08-15T19:48:24.655357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = np.vstack(predictions)\nids = np.array([image.split('.')[0] for image in sub_image]).reshape(len(sub_image),1)","metadata":{"execution":{"iopub.status.busy":"2024-08-15T19:53:40.955806Z","iopub.execute_input":"2024-08-15T19:53:40.956690Z","iopub.status.idle":"2024-08-15T19:53:41.090057Z","shell.execute_reply.started":"2024-08-15T19:53:40.956656Z","shell.execute_reply":"2024-08-15T19:53:41.089055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame(np.hstack((ids, results)), columns=df.columns)\nsubmission_df","metadata":{"execution":{"iopub.status.busy":"2024-08-15T19:53:43.275245Z","iopub.execute_input":"2024-08-15T19:53:43.275651Z","iopub.status.idle":"2024-08-15T19:53:45.948763Z","shell.execute_reply.started":"2024-08-15T19:53:43.275621Z","shell.execute_reply":"2024-08-15T19:53:45.947816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = submission_df.sort_values(by=['GalaxyID'])\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-15T19:55:07.455742Z","iopub.execute_input":"2024-08-15T19:55:07.456157Z","iopub.status.idle":"2024-08-15T19:55:10.221116Z","shell.execute_reply.started":"2024-08-15T19:55:07.456125Z","shell.execute_reply":"2024-08-15T19:55:10.220085Z"},"trusted":true},"execution_count":null,"outputs":[]}]}