{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!unzip \"/kaggle/input/dogs-vs-cats/train.zip\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-26T12:30:15.979684Z","iopub.execute_input":"2024-06-26T12:30:15.979990Z","iopub.status.idle":"2024-06-26T12:30:15.983928Z","shell.execute_reply.started":"2024-06-26T12:30:15.979963Z","shell.execute_reply":"2024-06-26T12:30:15.983061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport os\nimport random\nimport torch.nn as nn\nfrom PIL import Image\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.transforms import v2\nfrom torchvision import models\nimport matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:30:15.985299Z","iopub.execute_input":"2024-06-26T12:30:15.985866Z","iopub.status.idle":"2024-06-26T12:30:21.804784Z","shell.execute_reply.started":"2024-06-26T12:30:15.985842Z","shell.execute_reply":"2024-06-26T12:30:21.803850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"/kaggle/working/train\"\ndirs = os.listdir(train_path)\n\ncat_paths = [path for path in dirs if path.startswith(\"cat\")]\ndog_paths = [path for path in dirs if path.startswith(\"dog\")]\n\nall_paths = cat_paths + dog_paths\ntrain_paths = cat_paths[2500:] + dog_paths[2500:]\nval_paths = cat_paths[:2500] + dog_paths[:2500]\n\nrandom.seed(42)\nrandom.shuffle(train_paths)\nrandom.shuffle(val_paths)","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:30:21.806888Z","iopub.execute_input":"2024-06-26T12:30:21.807288Z","iopub.status.idle":"2024-06-26T12:30:21.859688Z","shell.execute_reply.started":"2024-06-26T12:30:21.807263Z","shell.execute_reply":"2024-06-26T12:30:21.859040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = v2.Compose(\n[\n  v2.PILToTensor(),\n  v2.RandomHorizontalFlip(p = 0.5),\n  v2.RandomVerticalFlip(p = 0.5),  \n  v2.RandomResizedCrop(size = [227, 227], antialias = True),  \n  v2.ToDtype(torch.float32, scale = True),\n  v2.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])\n]\n)\n\nval_transform = v2.Compose(\n[\n  v2.PILToTensor(),\n  v2.ToDtype(torch.float32, scale = True),\n  v2.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])\n]\n)\n\ntransform = {\n    \"train_transform\": train_transform,\n    \"val_transform\": val_transform\n}","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:30:21.860649Z","iopub.execute_input":"2024-06-26T12:30:21.860912Z","iopub.status.idle":"2024-06-26T12:30:21.945158Z","shell.execute_reply.started":"2024-06-26T12:30:21.860889Z","shell.execute_reply":"2024-06-26T12:30:21.944460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class dataset(Dataset):\n    def __init__(self, paths, transform = None, is_train = True) :\n        self.paths = paths\n        self.transform = transform\n        self.is_train = is_train\n        \n    \n    def __getitem__(self, index):\n        full_path = os.path.join(train_path, self.paths[index])\n        img = Image.open(full_path)\n        img = img.resize((227, 227))\n        label = self.paths[index][:3]\n        \n        if self.transform:\n            if self.is_train:\n                img = self.transform[\"train_transform\"](img)\n            else:\n                img = self.transform[\"val_transform\"](img)\n            \n        \n        return img, int(label == \"cat\")\n    \n    def __len__(self):\n        return len(self.paths)","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:30:21.946079Z","iopub.execute_input":"2024-06-26T12:30:21.946345Z","iopub.status.idle":"2024-06-26T12:30:21.954394Z","shell.execute_reply.started":"2024-06-26T12:30:21.946323Z","shell.execute_reply":"2024-06-26T12:30:21.953424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = dataset(train_paths, transform)\nval_data = dataset(val_paths, transform, is_train = False)","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:30:21.955573Z","iopub.execute_input":"2024-06-26T12:30:21.956187Z","iopub.status.idle":"2024-06-26T12:30:21.966878Z","shell.execute_reply.started":"2024-06-26T12:30:21.956161Z","shell.execute_reply":"2024-06-26T12:30:21.966207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_data[0][0].permute(1, 2, 0))","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:30:21.967859Z","iopub.execute_input":"2024-06-26T12:30:21.968144Z","iopub.status.idle":"2024-06-26T12:30:22.399605Z","shell.execute_reply.started":"2024-06-26T12:30:21.968115Z","shell.execute_reply":"2024-06-26T12:30:22.398689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_data, shuffle = True, batch_size = 32)\nval_loader = DataLoader(val_data, shuffle = False, batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:38:42.916380Z","iopub.execute_input":"2024-06-26T12:38:42.917187Z","iopub.status.idle":"2024-06-26T12:38:42.921583Z","shell.execute_reply.started":"2024-06-26T12:38:42.917154Z","shell.execute_reply":"2024-06-26T12:38:42.920588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:50:51.932687Z","iopub.execute_input":"2024-06-26T12:50:51.933530Z","iopub.status.idle":"2024-06-26T12:50:51.939547Z","shell.execute_reply.started":"2024-06-26T12:50:51.933497Z","shell.execute_reply":"2024-06-26T12:50:51.938548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class AlexNet(nn.Module):\n#     def __init__(self, num_classes):\n#         super(AlexNet, self).__init__()\n        \n#         self.conv1 = nn.Conv2d(3, 96, kernel_size = 11, stride = 4)\n#         self.mx1 = nn.MaxPool2d(kernel_size = 3, stride = 2)\n        \n#         self.conv2 = nn.Conv2d(96, 256, kernel_size = 5, padding = \"same\")\n#         self.mx2 = nn.MaxPool2d(kernel_size = 3, stride = 2)\n        \n#         self.conv3 = nn.Conv2d(256, 384, kernel_size = 3, padding = \"same\")\n#         self.conv4 = nn.Conv2d(384, 384, kernel_size = 3, padding = \"same\")\n#         self.conv5 = nn.Conv2d(384, 256, kernel_size = 3, padding = \"same\")\n        \n#         self.mx3 = nn.MaxPool2d(kernel_size = 3, stride = 2)\n        \n#         self.linear1 = nn.Linear(9216, 4096)\n#         self.linear2 = nn.Linear(4096, 4096)\n#         self.linear3 = nn.Linear(4096, num_classes)\n        \n     \n    \n#     def forward(self, x):\n#         out = nn.ReLU()(self.conv1(x))\n#         out = self.mx1(out)\n#         out = self.mx2(nn.ReLU()(self.conv2(out)))\n#         out = nn.ReLU()(self.conv3(out))\n#         out = nn.ReLU()(self.conv4(out))\n#         out = nn.ReLU()(self.conv5(out))\n#         out = self.mx3(out)\n#         out = nn.Flatten()(out)\n#         out = nn.ReLU()(self.linear1(out))\n#         out = nn.ReLU()(self.linear2(out))\n#         out = self.linear3(out)\n        \n#         return out","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:30:57.765109Z","iopub.execute_input":"2024-06-26T12:30:57.765930Z","iopub.status.idle":"2024-06-26T12:30:57.780232Z","shell.execute_reply.started":"2024-06-26T12:30:57.765883Z","shell.execute_reply":"2024-06-26T12:30:57.779285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load the model\nmodel = models.resnet50(weights = \"IMAGENET1K_V1\")\n\n# freeze the model\nfor parameter in model.parameters():\n    parameter.requires_grad = False\n    \n# change classification head\nmodel.fc = nn.Linear(model.fc.in_features, 2)\n\n# move model to cuda\nmodel.to(device)\n\n# hyperparamters\nnum_epochs = 10\nlr = 0.001\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD([p for p in model.parameters() if p.requires_grad], lr = lr)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:52:26.853284Z","iopub.execute_input":"2024-06-26T12:52:26.853636Z","iopub.status.idle":"2024-06-26T12:52:26.860228Z","shell.execute_reply.started":"2024-06-26T12:52:26.853610Z","shell.execute_reply":"2024-06-26T12:52:26.859025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_epoch():\n    epoch_loss = 0\n    for i, (images, labels) in enumerate(train_loader):\n        \n        images = images.to(device)\n        labels = labels.to(device)\n        \n        # forward pass\n        out = model(images)\n        loss = criterion(out, labels)\n        epoch_loss += loss\n        \n        \n        # backward pass\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n        if(i%25 ==0):\n            print(f\"   step[{i+1} / {len(train_loader)}]\")\n    return epoch_loss / len(train_loader)","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:54:36.741881Z","iopub.execute_input":"2024-06-26T12:54:36.742763Z","iopub.status.idle":"2024-06-26T12:54:36.749380Z","shell.execute_reply.started":"2024-06-26T12:54:36.742729Z","shell.execute_reply":"2024-06-26T12:54:36.748405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_val_loss():\n    epoch_loss = 0\n    with torch.no_grad():\n        for i, (images, labels) in enumerate(val_loader):\n\n            images = images.to(device)\n            labels = labels.to(device)\n\n            # forward pass\n            out = model(images)\n            loss = criterion(out, labels)\n            epoch_loss += loss\n            \n    return epoch_loss / len(val_loader)","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:54:38.382873Z","iopub.execute_input":"2024-06-26T12:54:38.383706Z","iopub.status.idle":"2024-06-26T12:54:38.389051Z","shell.execute_reply.started":"2024-06-26T12:54:38.383674Z","shell.execute_reply":"2024-06-26T12:54:38.388115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses = []\nval_losses = []\nbest_score = float(\"inf\")\ntol = 0\n\nfor epoch in range(num_epochs):\n    print(f\"epoch {epoch+1}:\")\n    \n    model.train(True)\n    avg_train_epoch_loss = train_epoch()\n    \n    model.eval()\n    avg_val_epoch_loss = get_val_loss()\n    \n    if(avg_val_epoch_loss < best_score):\n        best_score = avg_val_epoch_loss\n        torch.save(model.state_dict(), f\"model_{epoch+1}\")\n    else:\n        tol += 1\n        if(tol == 4):\n            break\n    \n    \n    train_losses.append(avg_train_epoch_loss)\n    val_losses.append(avg_val_epoch_loss)\n    \n    \n    print(f\"epoch: {epoch+1} --- avg_train_loss = {avg_train_epoch_loss} --- avg_val_loss = {avg_val_epoch_loss}\") \n    print(\"#\"*100)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-26T12:54:40.594837Z","iopub.execute_input":"2024-06-26T12:54:40.595492Z","iopub.status.idle":"2024-06-26T13:15:58.755835Z","shell.execute_reply.started":"2024-06-26T12:54:40.595459Z","shell.execute_reply":"2024-06-26T13:15:58.754854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses = [x.item() for x in train_losses]\ntrain_losses","metadata":{"execution":{"iopub.status.busy":"2024-06-26T13:18:13.084414Z","iopub.execute_input":"2024-06-26T13:18:13.086321Z","iopub.status.idle":"2024-06-26T13:18:13.092008Z","shell.execute_reply.started":"2024-06-26T13:18:13.086286Z","shell.execute_reply":"2024-06-26T13:18:13.091111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_losses = [ x.item() for x in val_losses]\nval_losses","metadata":{"execution":{"iopub.status.busy":"2024-06-26T13:18:18.270218Z","iopub.execute_input":"2024-06-26T13:18:18.271044Z","iopub.status.idle":"2024-06-26T13:18:18.276520Z","shell.execute_reply.started":"2024-06-26T13:18:18.271010Z","shell.execute_reply":"2024-06-26T13:18:18.275734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(train_losses, \"b--\", label = \"Train_loss\")\nplt.plot(val_losses, \"r-\", label = \"Val_loss\")\nplt.title(\"Train VS Val\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-26T14:30:26.056959Z","iopub.execute_input":"2024-06-26T14:30:26.057347Z","iopub.status.idle":"2024-06-26T14:30:26.409919Z","shell.execute_reply.started":"2024-06-26T14:30:26.057318Z","shell.execute_reply":"2024-06-26T14:30:26.408499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Unfreeze the model\nfor parameter in model.parameters():\n    parameter.requires_grad = True\n    \n# change learning rate to a small value\nlr = 0.001\n\n# optimizer\noptimizer = torch.optim.SGD([p for p in model.parameters() if p.requires_grad], lr = lr)\n\n\n# train\n#######################################################\n# I have just copied the code, You can make it better #\n#######################################################\n\ntrain_losses = []\nval_losses = []\nbest_score = float(\"inf\")\ntol = 0\n\nfor epoch in range(num_epochs):\n    print(f\"epoch {epoch+1}:\")\n    \n    model.train(True)\n    avg_train_epoch_loss = train_epoch()\n    \n    model.eval()\n    avg_val_epoch_loss = get_val_loss()\n    \n    if(avg_val_epoch_loss < best_score):\n        best_score = avg_val_epoch_loss\n        torch.save(model.state_dict(), f\"model_{epoch+1}\")\n    else:\n        tol += 1\n        if(tol == 4):\n            break\n    \n    \n    train_losses.append(avg_train_epoch_loss)\n    val_losses.append(avg_val_epoch_loss)\n    \n    \n    print(f\"epoch: {epoch+1} --- avg_train_loss = {avg_train_epoch_loss} --- avg_val_loss = {avg_val_epoch_loss}\") \n    print(\"#\"*100)","metadata":{"execution":{"iopub.status.busy":"2024-06-26T13:18:28.371501Z","iopub.execute_input":"2024-06-26T13:18:28.371852Z","iopub.status.idle":"2024-06-26T13:57:31.598256Z","shell.execute_reply.started":"2024-06-26T13:18:28.371822Z","shell.execute_reply":"2024-06-26T13:57:31.597209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses = [x.item() for x in train_losses]\ntrain_losses","metadata":{"execution":{"iopub.status.busy":"2024-06-26T14:30:41.317042Z","iopub.execute_input":"2024-06-26T14:30:41.317793Z","iopub.status.idle":"2024-06-26T14:30:41.324314Z","shell.execute_reply.started":"2024-06-26T14:30:41.317762Z","shell.execute_reply":"2024-06-26T14:30:41.323493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_losses = [ x.item() for x in val_losses]\nval_losses","metadata":{"execution":{"iopub.status.busy":"2024-06-26T14:30:42.212462Z","iopub.execute_input":"2024-06-26T14:30:42.212834Z","iopub.status.idle":"2024-06-26T14:30:42.219979Z","shell.execute_reply.started":"2024-06-26T14:30:42.212805Z","shell.execute_reply":"2024-06-26T14:30:42.219181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.plot(train_losses, \"b--\", label = \"Train_loss\")\nplt.plot(val_losses, \"r-\", label = \"Val_loss\")\nplt.title(\"Train VS Val\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-26T14:30:43.539732Z","iopub.execute_input":"2024-06-26T14:30:43.540099Z","iopub.status.idle":"2024-06-26T14:30:43.759952Z","shell.execute_reply.started":"2024-06-26T14:30:43.540069Z","shell.execute_reply":"2024-06-26T14:30:43.759052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}