{"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":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30732,"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-06-19T21:16:34.457163Z","iopub.execute_input":"2024-06-19T21:16:34.457541Z","iopub.status.idle":"2024-06-19T21:16:34.869566Z","shell.execute_reply.started":"2024-06-19T21:16:34.457509Z","shell.execute_reply":"2024-06-19T21:16:34.868495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport zipfile\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport random\n\nimport torch\nimport torch.nn as nn\nfrom PIL import Image\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.transforms import transforms","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:16:36.752833Z","iopub.execute_input":"2024-06-19T21:16:36.753353Z","iopub.status.idle":"2024-06-19T21:16:42.142779Z","shell.execute_reply.started":"2024-06-19T21:16:36.753324Z","shell.execute_reply":"2024-06-19T21:16:42.141845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nimport zipfile\npath = \"/kaggle/working/train\"\ntest_path = \"/kaggle/input/dogs-vs-cats/test1.zip\"\ntrain_path = \"/kaggle/input/dogs-vs-cats/train.zip\"\n\nwith zipfile.ZipFile(train_path, 'r') as zip_ref:\n    zip_ref.extractall(path)\n\nextracted_path = os.path.join(path, 'train')\nfilenames = os.listdir(extracted_path)\nprint(f\"Total files : {len(filenames)}\")\n\n# Getting the cats and dogs labels\ncat_images = [filename for filename in filenames if filename.__contains__(\"cat\")]\ndog_images = [filename for filename in filenames if filename.__contains__(\"dog\")]\n\nprint(f\"Total cat images : {len(cat_images)}, and Total dog images : {len(dog_images)}\")\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:16:49.983173Z","iopub.execute_input":"2024-06-19T21:16:49.984133Z","iopub.status.idle":"2024-06-19T21:16:59.595698Z","shell.execute_reply.started":"2024-06-19T21:16:49.9841Z","shell.execute_reply":"2024-06-19T21:16:59.594691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's see few of the images !\nnp.random.seed(109)\ncat_images_sample = np.random.choice(cat_images, 5)\ndog_images_sample = np.random.choice(dog_images, 5)\n\ndef plot_images(images:list, title:str):\n    plt.figure(figsize=(14,3))\n    for i, img_name in enumerate(images):\n        plt.subplot(1, 6, i+1)\n        img = cv2.imread(os.path.join(extracted_path, img_name))\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        plt.imshow(img)\n        plt.axis('off')\n    plt.suptitle(title, fontsize=20)\n    plt.show()\n    \n# Plot the images\nplot_images(cat_images_sample, \"Cats\")\nplot_images(dog_images_sample, \"Dogs\")\n","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:17:07.902469Z","iopub.execute_input":"2024-06-19T21:17:07.902819Z","iopub.status.idle":"2024-06-19T21:17:08.765066Z","shell.execute_reply.started":"2024-06-19T21:17:07.902791Z","shell.execute_reply":"2024-06-19T21:17:08.763927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = cat_images[2500:] + dog_images[2500:]\nval_data = cat_images[:2500] +  dog_images[:2500]\nnp.random.shuffle(train_data)\nnp.random.shuffle(val_data)\n\nsample_path = \"/kaggle/input/dogs-vs-cats/sampleSubmission.csv\"\n\nsubm_df = pd.read_csv(sample_path)\n\nsubm_df","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:17:11.13849Z","iopub.execute_input":"2024-06-19T21:17:11.139591Z","iopub.status.idle":"2024-06-19T21:17:11.179627Z","shell.execute_reply.started":"2024-06-19T21:17:11.139546Z","shell.execute_reply":"2024-06-19T21:17:11.178626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class dataset(Dataset):\n    def __init__(self, paths, transform=None):\n        self.paths = paths\n        self.transform = transform\n        \n    def __getitem__(self, idx):\n        full_path = os.path.join(extracted_path, self.paths[idx])\n        img = Image.open(full_path)\n        img = img.resize((227, 227))\n        label = self.paths[idx][:3]\n        if self.transform:\n            img = self.transform(img)\n            img = img.type(torch.float32)\n        return img, int(label==\"cat\")\n    \n    def __len__(self):\n        return len(self.paths)\n\ntransform = transforms.Compose([transforms.PILToTensor()])\n\ntrain_ds = dataset(train_data, transform)\nval_ds = dataset(val_data, transform)\n\ntrain_loader = DataLoader(train_ds, shuffle=True, batch_size=32)\nval_loader = DataLoader(val_ds, shuffle=True, batch_size=32)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:17:18.060228Z","iopub.execute_input":"2024-06-19T21:17:18.06055Z","iopub.status.idle":"2024-06-19T21:17:18.069571Z","shell.execute_reply.started":"2024-06-19T21:17:18.060525Z","shell.execute_reply":"2024-06-19T21:17:18.06843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i, (images,labels) in enumerate(train_loader):\n#     print(i)\n#     print(images, labels)     \n\ntrain_ds.__getitem__(0)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:17:20.910075Z","iopub.execute_input":"2024-06-19T21:17:20.910919Z","iopub.status.idle":"2024-06-19T21:17:21.01186Z","shell.execute_reply.started":"2024-06-19T21:17:20.910877Z","shell.execute_reply":"2024-06-19T21:17:21.011002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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(in_channels=3, out_channels=96, kernel_size=11, stride=4)\n        self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2)\n        self.conv2 = nn.Conv2d(in_channels=96, out_channels=256, kernel_size=5, padding=\"same\")\n        self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2)\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        self.pool3 = nn.MaxPool2d(kernel_size=3, stride=2)\n        \n        self.ln1 = nn.Linear(9216, 4096)\n#         self.ln2 = nn.Linear(4096, 4096)\n        self.ln3 = nn.Linear(4096, num_classes)\n        \n    def forward(self, x):\n        out = nn.ReLU()(self.conv1(x))\n        out = self.pool1(out)\n        out = self.pool2(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.pool3(out)\n        out = nn.Flatten()(out)\n        out = nn.ReLU()(self.ln1(out))\n#         out = nn.ReLU()(self.ln2(out))\n        out = self.ln3(out)\n        return out\n    \n        ","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:17:28.297949Z","iopub.execute_input":"2024-06-19T21:17:28.298774Z","iopub.status.idle":"2024-06-19T21:17:28.309309Z","shell.execute_reply.started":"2024-06-19T21:17:28.298741Z","shell.execute_reply":"2024-06-19T21:17:28.30832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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-19T21:17:31.08771Z","iopub.execute_input":"2024-06-19T21:17:31.088068Z","iopub.status.idle":"2024-06-19T21:17:31.145844Z","shell.execute_reply.started":"2024-06-19T21:17:31.08804Z","shell.execute_reply":"2024-06-19T21:17:31.144852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = AlexNet(2).to(device)\nnum_epochs = 5\nlr = 0.005\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=lr)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:17:34.109168Z","iopub.execute_input":"2024-06-19T21:17:34.110162Z","iopub.status.idle":"2024-06-19T21:17:34.754642Z","shell.execute_reply.started":"2024-06-19T21:17:34.110119Z","shell.execute_reply":"2024-06-19T21:17:34.753608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train():\n    epoch_loss = 0\n    for i, (images,labels) in enumerate(train_loader):\n        images = images.to(device)\n        labels = labels.to(device)\n        #forward\n        out = model(images)\n        loss= criterion(out, labels)\n        epoch_loss += loss\n        #backward\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n        if 1%5==0:\n            print(f\"++++ Step {i+1}/{len(train_loader)},  Epoch Loss : {epoch_loss}\")\n    return epoch_loss/len(train_loader)\n\n\ndef get_val_loss():\n    epoch_loss = 0\n    with torch.no_grad():\n        for i, (images, labels) in enumerate(val_loader):\n            images, labels = images.to(device), labels.to(device)\n            # forward\n            out = model(images)\n            loss = criterion(out, labels)\n            epoch_loss += loss\n    return epoch_loss/len(val_loader)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:17:36.750634Z","iopub.execute_input":"2024-06-19T21:17:36.751535Z","iopub.status.idle":"2024-06-19T21:17:36.75984Z","shell.execute_reply.started":"2024-06-19T21:17:36.751496Z","shell.execute_reply":"2024-06-19T21:17:36.75868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses = []\nval_losses = []\nbest_score = float(\"inf\")\ntol = 0\n\n\nfor epoch in range(num_epochs):\n    print(f\"epoch {epoch+1}\")\n    model.train(True)\n    avg_train_epoch_loss = train()\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    train_losses.append(avg_train_epoch_loss)\n    val_losses.append(avg_val_epoch_loss)\n    \n    print(f\"epoch : {epoch+1} ---> avg train loss : {avg_train_epoch_loss}, avg val loss : {avg_val_epoch_loss}\")\n    print(\"*\"*50)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-19T21:17:39.872906Z","iopub.execute_input":"2024-06-19T21:17:39.873278Z"},"trusted":true},"execution_count":null,"outputs":[]}]}