{"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":5441,"databundleVersionId":38425,"sourceType":"competition"},{"sourceId":183098811,"sourceType":"kernelVersion"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install --upgrade lightning\n!pip install wandb","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:30:39.084975Z","iopub.execute_input":"2024-06-12T17:30:39.085323Z","iopub.status.idle":"2024-06-12T17:31:09.417664Z","shell.execute_reply.started":"2024-06-12T17:30:39.085294Z","shell.execute_reply":"2024-06-12T17:31:09.416659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport lightning as pl\nfrom torchmetrics.classification import Accuracy, F1Score\nfrom torch.utils.data import DataLoader, Dataset\nfrom sklearn.model_selection import train_test_split\nfrom torchvision.transforms import ToTensor\nimport albumentations as A\nfrom albumentations import Compose, RandomCrop, HorizontalFlip, Normalize, Resize\nfrom albumentations.pytorch import ToTensorV2\nfrom sklearn.metrics import accuracy_score, f1_score\nfrom lightning.pytorch.callbacks import ModelCheckpoint, EarlyStopping, TQDMProgressBar\nfrom lightning.pytorch.loggers import WandbLogger\nimport wandb","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:31:09.420080Z","iopub.execute_input":"2024-06-12T17:31:09.420479Z","iopub.status.idle":"2024-06-12T17:31:18.474954Z","shell.execute_reply.started":"2024-06-12T17:31:09.420423Z","shell.execute_reply":"2024-06-12T17:31:18.474160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip -d /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:31:18.479543Z","iopub.execute_input":"2024-06-12T17:31:18.479816Z","iopub.status.idle":"2024-06-12T17:31:26.209687Z","shell.execute_reply.started":"2024-06-12T17:31:18.479793Z","shell.execute_reply":"2024-06-12T17:31:26.208667Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomImageDataset(Dataset):\n    def __init__(self, image_dir, transform=None):\n        self.image_dir = image_dir\n        self.transform = transform\n        self.classes = os.listdir(image_dir)\n        self.image_paths = []\n        self.labels = []\n        self.ids = []\n        \n        for img_name in os.listdir(image_dir):\n            self.image_paths.append(os.path.join(image_dir, img_name))\n            self.labels.append(0)\n            self.ids.append(int(img_name.split('.')[0]))\n        \n    def __len__(self):\n        return len(self.image_paths)\n    \n    def __getitem__(self, idx):\n        img_path = self.image_paths[idx]\n        image = cv2.imread(img_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        label = self.labels[idx]\n        \n        if self.transform:\n            augmented = self.transform(image=image)\n            image = augmented['image']\n        \n        return image, label","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:31:26.211124Z","iopub.execute_input":"2024-06-12T17:31:26.211471Z","iopub.status.idle":"2024-06-12T17:31:26.221056Z","shell.execute_reply.started":"2024-06-12T17:31:26.211405Z","shell.execute_reply":"2024-06-12T17:31:26.220075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimpleCNN(pl.LightningModule):\n    def __init__(self, lr):\n        super(SimpleCNN, self).__init__()\n        self.model = torch.nn.Sequential(\n            torch.nn.Conv2d(3, 32, kernel_size=3, padding=\"same\"),\n            torch.nn.ReLU(),\n            torch.torch.nn.MaxPool2d(kernel_size=2),\n            torch.nn.Dropout(p=0.25),\n            torch.nn.Conv2d(32, 64, kernel_size=3, padding=\"same\"),\n            torch.nn.ReLU(),\n            torch.nn.MaxPool2d(kernel_size=2),\n            torch.nn.Dropout(p=0.25),\n            torch.nn.Flatten(),\n            torch.nn.Linear(64 * 64 * 64, 512),\n            torch.nn.ReLU(),\n            torch.nn.Linear(512, 64),\n            torch.nn.ReLU(),\n            torch.nn.Linear(64, 1),\n            torch.nn.Sigmoid(),\n        )\n        self.loss_fn = torch.nn.BCELoss()\n        self.lr = lr\n        \n        self.train_acc = Accuracy(task=\"binary\")\n        self.val_acc = Accuracy(task=\"binary\")\n        self.train_f1 = F1Score(task=\"binary\", average='macro')\n        self.val_f1 = F1Score(task=\"binary\", average='macro')\n    \n    def forward(self, x):\n        return self.model(x)\n    \n    def training_step(self, batch, batch_idx):\n        x, y = batch\n        y_hat = self(x).squeeze()\n        y = y.type(torch.float)\n        loss = self.loss_fn(y_hat, y)\n        y_hat = ((y_hat > 0.5) == y).type(torch.float)\n        \n        self.log('train_loss', loss)\n        self.train_acc.update(y_hat, y)\n        self.train_f1.update(y_hat, y)\n        self.log('train_acc', self.train_acc)\n        self.log('train_f1', self.train_f1)\n        \n        return loss\n    \n    def validation_step(self, batch, batch_idx):\n        x, y = batch\n        y_hat = self(x).squeeze()\n        y = y.type(torch.float)\n        val_loss = self.loss_fn(y_hat, y)\n        y_hat = ((y_hat > 0.5) == y).type(torch.float)\n        \n        self.val_acc.update(y_hat, y)\n        self.val_f1.update(y_hat, y)\n        self.log('val_loss', val_loss, prog_bar=True)\n        self.log('val_acc', self.val_acc)\n        self.log('val_f1', self.val_f1)\n        \n        return val_loss\n    \n    def predict_step(self, batch, batch_idx):\n        x, y = batch\n        y_hat = self(x).squeeze().type(torch.float)\n        \n        return y_hat\n    \n    def configure_optimizers(self):\n        optimizer = torch.optim.Adam(self.parameters(), lr=self.lr, weight_decay=0.0001)\n        return optimizer\n    \n    def on_train_epoch_end(self):\n        self.log(\"train_acc_epoch\", self.train_acc.compute(), on_epoch=True)\n        self.log(\"train_f1_epoch\", self.train_f1.compute(), on_epoch=True)\n        self.train_acc.reset()\n        self.train_f1.reset()\n    \n    def on_validation_epoch_end(self):\n        self.log(\"val_acc_epoch\", self.val_acc.compute(), on_epoch=True)\n        self.log(\"val_f1_epoch\", self.val_f1.compute(), on_epoch=True)\n        self.val_acc.reset()\n        self.val_f1.reset()","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:31:26.222561Z","iopub.execute_input":"2024-06-12T17:31:26.222944Z","iopub.status.idle":"2024-06-12T17:31:26.264313Z","shell.execute_reply.started":"2024-06-12T17:31:26.222920Z","shell.execute_reply":"2024-06-12T17:31:26.263560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose([\n    A.Resize(256, 256),\n    A.Normalize(),\n    ToTensorV2(),\n])\ntest_dataset = CustomImageDataset(\"/kaggle/working/test\", transform=transform)\ntest_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False)\n\nmodel = SimpleCNN.load_from_checkpoint(\"/kaggle/input/m3-l5-2/models/best-checkpoint.ckpt\", lr=0.0001)\ntrainer = pl.Trainer(accelerator=\"gpu\", devices=[0])\ntest_predictions = trainer.predict(model, test_dataloader)","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:31:26.265388Z","iopub.execute_input":"2024-06-12T17:31:26.265704Z","iopub.status.idle":"2024-06-12T17:32:34.622040Z","shell.execute_reply.started":"2024-06-12T17:31:26.265681Z","shell.execute_reply":"2024-06-12T17:32:34.621009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:32:34.623274Z","iopub.execute_input":"2024-06-12T17:32:34.623889Z","iopub.status.idle":"2024-06-12T17:32:34.824091Z","shell.execute_reply.started":"2024-06-12T17:32:34.623842Z","shell.execute_reply":"2024-06-12T17:32:34.823126Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = torch.cat(test_predictions)","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:32:34.825359Z","iopub.execute_input":"2024-06-12T17:32:34.825717Z","iopub.status.idle":"2024-06-12T17:32:34.830530Z","shell.execute_reply.started":"2024-06-12T17:32:34.825684Z","shell.execute_reply":"2024-06-12T17:32:34.829493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:32:34.833283Z","iopub.execute_input":"2024-06-12T17:32:34.833580Z","iopub.status.idle":"2024-06-12T17:32:34.845451Z","shell.execute_reply.started":"2024-06-12T17:32:34.833556Z","shell.execute_reply":"2024-06-12T17:32:34.844641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/test","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:32:34.846396Z","iopub.execute_input":"2024-06-12T17:32:34.846707Z","iopub.status.idle":"2024-06-12T17:32:34.853042Z","shell.execute_reply.started":"2024-06-12T17:32:34.846684Z","shell.execute_reply":"2024-06-12T17:32:34.852029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:32:34.854131Z","iopub.execute_input":"2024-06-12T17:32:34.854458Z","iopub.status.idle":"2024-06-12T17:32:35.896744Z","shell.execute_reply.started":"2024-06-12T17:32:34.854418Z","shell.execute_reply":"2024-06-12T17:32:35.895764Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:33:28.798373Z","iopub.execute_input":"2024-06-12T17:33:28.799284Z","iopub.status.idle":"2024-06-12T17:33:28.803597Z","shell.execute_reply.started":"2024-06-12T17:33:28.799250Z","shell.execute_reply":"2024-06-12T17:33:28.802592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"id\": [test_dataset.ids[i] for i in range(len(test_dataset))], \"label\": test_predictions.tolist()})","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:33:30.185757Z","iopub.execute_input":"2024-06-12T17:33:30.186361Z","iopub.status.idle":"2024-06-12T17:33:30.204602Z","shell.execute_reply.started":"2024-06-12T17:33:30.186332Z","shell.execute_reply":"2024-06-12T17:33:30.203629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:33:35.086303Z","iopub.execute_input":"2024-06-12T17:33:35.087172Z","iopub.status.idle":"2024-06-12T17:33:35.113765Z","shell.execute_reply.started":"2024-06-12T17:33:35.087138Z","shell.execute_reply":"2024-06-12T17:33:35.112897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.sort_values(by=['id']).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:33:43.315306Z","iopub.execute_input":"2024-06-12T17:33:43.316229Z","iopub.status.idle":"2024-06-12T17:33:43.326891Z","shell.execute_reply.started":"2024-06-12T17:33:43.316193Z","shell.execute_reply":"2024-06-12T17:33:43.325931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:33:45.134964Z","iopub.execute_input":"2024-06-12T17:33:45.135650Z","iopub.status.idle":"2024-06-12T17:33:45.147150Z","shell.execute_reply.started":"2024-06-12T17:33:45.135620Z","shell.execute_reply":"2024-06-12T17:33:45.146199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:34:02.811399Z","iopub.execute_input":"2024-06-12T17:34:02.811761Z","iopub.status.idle":"2024-06-12T17:34:02.840236Z","shell.execute_reply.started":"2024-06-12T17:34:02.811735Z","shell.execute_reply":"2024-06-12T17:34:02.839478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/test","metadata":{"execution":{"iopub.status.busy":"2024-06-12T17:34:04.941584Z","iopub.execute_input":"2024-06-12T17:34:04.941918Z","iopub.status.idle":"2024-06-12T17:34:06.271588Z","shell.execute_reply.started":"2024-06-12T17:34:04.941896Z","shell.execute_reply":"2024-06-12T17:34:06.270418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}