{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nsample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nsample_submission","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:14:52.618662Z","iopub.execute_input":"2025-06-30T08:14:52.618883Z","iopub.status.idle":"2025-06-30T08:14:54.458469Z","shell.execute_reply.started":"2025-06-30T08:14:52.618861Z","shell.execute_reply":"2025-06-30T08:14:54.457629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\npath_to_zip_file = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip'\nwith zipfile.ZipFile(path_to_zip_file,'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:14:54.460358Z","iopub.execute_input":"2025-06-30T08:14:54.460600Z","iopub.status.idle":"2025-06-30T08:15:02.942775Z","shell.execute_reply.started":"2025-06-30T08:14:54.460572Z","shell.execute_reply":"2025-06-30T08:15:02.941974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path_to_zip_file = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip'\nwith zipfile.ZipFile(path_to_zip_file,'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:02.943527Z","iopub.execute_input":"2025-06-30T08:15:02.943801Z","iopub.status.idle":"2025-06-30T08:15:06.883780Z","shell.execute_reply.started":"2025-06-30T08:15:02.943777Z","shell.execute_reply":"2025-06-30T08:15:06.883202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os \nfrom PIL import Image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:06.884391Z","iopub.execute_input":"2025-06-30T08:15:06.884604Z","iopub.status.idle":"2025-06-30T08:15:06.888217Z","shell.execute_reply.started":"2025-06-30T08:15:06.884582Z","shell.execute_reply":"2025-06-30T08:15:06.887370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset, DataLoader\nimport torch.optim as optim\nimport torch.nn as nn\nfrom torchvision import transforms\nimport numpy as np \nimport pandas as pd \nfrom PIL import Image\nfrom matplotlib import pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:06.889228Z","iopub.execute_input":"2025-06-30T08:15:06.889650Z","iopub.status.idle":"2025-06-30T08:15:15.107139Z","shell.execute_reply.started":"2025-06-30T08:15:06.889626Z","shell.execute_reply":"2025-06-30T08:15:15.106478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"a = os.listdir('/kaggle/working/train')\nimg1 = Image.open(os.path.join('/kaggle/working/train',a[0]))\nprint(img1.size)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:15.107793Z","iopub.execute_input":"2025-06-30T08:15:15.108097Z","iopub.status.idle":"2025-06-30T08:15:15.159175Z","shell.execute_reply.started":"2025-06-30T08:15:15.108078Z","shell.execute_reply":"2025-06-30T08:15:15.158621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"b=[]\nfor line in os.listdir('/kaggle/working/train'):\n    b.append(line.split('.')[0])\nfrom sklearn.preprocessing import LabelEncoder\n\nle = LabelEncoder()\nle.fit(b)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:15.160974Z","iopub.execute_input":"2025-06-30T08:15:15.161387Z","iopub.status.idle":"2025-06-30T08:15:15.634249Z","shell.execute_reply.started":"2025-06-30T08:15:15.161369Z","shell.execute_reply":"2025-06-30T08:15:15.633501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self,directorie,transform):\n        label1=[]\n        filename_image=[]\n        self.directory = directorie\n        for line in os.listdir(directorie):\n            k = line\n            k = k.split('.')\n            label1.append(k[0])\n            filename_image.append(line)\n        self.label = label1\n        self.filename = filename_image\n        self.transform = transform\n    def __len__(self):\n        return len(self.label)\n    def __getitem__(self,idx):\n        img = Image.open(os.path.join(self.directory,self.filename[idx]))\n        if self.transform:\n            img = self.transform(img)\n        val = le.transform([self.label[idx]])[0]\n        return img,val","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:15.635056Z","iopub.execute_input":"2025-06-30T08:15:15.635482Z","iopub.status.idle":"2025-06-30T08:15:15.641332Z","shell.execute_reply.started":"2025-06-30T08:15:15.635455Z","shell.execute_reply":"2025-06-30T08:15:15.640611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((64,64)),\n    transforms.ToTensor(),\n])\ntrain_dataset = CustomDataset('/kaggle/working/train',transform=transform)\ntrain_dataloader = DataLoader(train_dataset,batch_size = 32,shuffle = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:15.642127Z","iopub.execute_input":"2025-06-30T08:15:15.642483Z","iopub.status.idle":"2025-06-30T08:15:15.675041Z","shell.execute_reply.started":"2025-06-30T08:15:15.642454Z","shell.execute_reply":"2025-06-30T08:15:15.674528Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CNN(nn.Module):\n    def __init__(self):\n        super(CNN, self).__init__()\n        self.conv = nn.Sequential(\n            nn.Conv2d(3,20,3,padding = 1),\n            nn.ReLU(),\n            nn.MaxPool2d(2),\n            nn.MaxPool2d(2)\n        )\n        self.flatten = nn.Flatten()\n        self.edges1 = nn.Linear(16*16*20,64)\n        self.edges2 = nn.Linear(64,2)\n        self.activation = nn.ReLU()\n\n    def forward(self,x):\n        x = self.conv(x)\n        x = self.flatten(x)\n        x = self.edges1(x)\n        x = self.activation(x)\n        x = self.edges2(x)\n        return x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:15.675656Z","iopub.execute_input":"2025-06-30T08:15:15.675897Z","iopub.status.idle":"2025-06-30T08:15:15.680875Z","shell.execute_reply.started":"2025-06-30T08:15:15.675872Z","shell.execute_reply":"2025-06-30T08:15:15.680143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = 'cuda'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:15.681470Z","iopub.execute_input":"2025-06-30T08:15:15.681748Z","iopub.status.idle":"2025-06-30T08:15:15.696784Z","shell.execute_reply.started":"2025-06-30T08:15:15.681731Z","shell.execute_reply":"2025-06-30T08:15:15.696201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = CNN().to(device)\n\ncr = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(),lr=0.01)\nepochs = 10\n\ncorrects = 0\nfor i in range(epochs):\n    print(i)\n    cost = 0\n    for image,answer in train_dataloader:\n        image, answer = image.to(device), answer.to(device)\n        optimizer.zero_grad()\n        predictions = model(image)\n        error = cr(predictions,answer)\n        error.backward()\n        optimizer.step()\n        cost += error.item()\n        if i == epochs - 1:\n            for j in range(len(answer)):\n                if torch.argmax(predictions[j]).item() == answer[j].item():\n                    corrects += 1\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:15:15.697436Z","iopub.execute_input":"2025-06-30T08:15:15.697634Z","iopub.status.idle":"2025-06-30T08:23:16.833791Z","shell.execute_reply.started":"2025-06-30T08:15:15.697619Z","shell.execute_reply":"2025-06-30T08:23:16.833220Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:44:00.020338Z","iopub.execute_input":"2025-06-30T08:44:00.021119Z","iopub.status.idle":"2025-06-30T08:44:00.029766Z","shell.execute_reply.started":"2025-06-30T08:44:00.021095Z","shell.execute_reply":"2025-06-30T08:44:00.029180Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CustomDataset1(Dataset):\n    def __init__(self,directorie,transform):\n        filename_image=[]\n        self.directory = directorie\n        for line in os.listdir(directorie):\n            filename_image.append(line)\n        self.filename = filename_image\n        self.transform = transform\n    def __len__(self):\n        return len(self.filename)\n    def __getitem__(self,idx):\n        img = Image.open(os.path.join(self.directory,self.filename[idx]))\n        if self.transform:\n            img = self.transform(img)\n        f = self.filename[idx]\n        f = str(f)\n        f = f[0:len(f)-4]\n        return img,f","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:34:14.772592Z","iopub.execute_input":"2025-06-30T08:34:14.773037Z","iopub.status.idle":"2025-06-30T08:34:14.777928Z","shell.execute_reply.started":"2025-06-30T08:34:14.773015Z","shell.execute_reply":"2025-06-30T08:34:14.777197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dataset = CustomDataset1('/kaggle/working/test',transform=transform)\ntest_dataloader = DataLoader(test_dataset,batch_size = 32,shuffle = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:34:16.240377Z","iopub.execute_input":"2025-06-30T08:34:16.240999Z","iopub.status.idle":"2025-06-30T08:34:16.251454Z","shell.execute_reply.started":"2025-06-30T08:34:16.240971Z","shell.execute_reply":"2025-06-30T08:34:16.250708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for image,filename in test_dataloader:\n    image, filename = image.to(device), filename\n    optimizer.zero_grad()\n    predictions = model(image)\n    for j in range(len(filename)):\n        sample_submission.loc[int(filename[j])-1,'label'] = torch.softmax(predictions[j], dim=0)[1].item()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:46:20.550167Z","iopub.execute_input":"2025-06-30T08:46:20.550872Z","iopub.status.idle":"2025-06-30T08:46:46.137142Z","shell.execute_reply.started":"2025-06-30T08:46:20.550849Z","shell.execute_reply":"2025-06-30T08:46:46.136382Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:46:46.138374Z","iopub.execute_input":"2025-06-30T08:46:46.138738Z","iopub.status.idle":"2025-06-30T08:46:46.168694Z","shell.execute_reply.started":"2025-06-30T08:46:46.138713Z","shell.execute_reply":"2025-06-30T08:46:46.168030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T08:48:48.542690Z","iopub.execute_input":"2025-06-30T08:48:48.542983Z","iopub.status.idle":"2025-06-30T08:48:48.551997Z","shell.execute_reply.started":"2025-06-30T08:48:48.542963Z","shell.execute_reply":"2025-06-30T08:48:48.551291Z"}},"outputs":[],"execution_count":null}]}