{"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":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-03-23T21:34:05.519443Z","iopub.execute_input":"2024-03-23T21:34:05.519863Z","iopub.status.idle":"2024-03-23T21:34:05.524164Z","shell.execute_reply.started":"2024-03-23T21:34:05.519828Z","shell.execute_reply":"2024-03-23T21:34:05.523332Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os,random\nfrom PIL import Image\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.transforms import transforms","metadata":{"execution":{"iopub.status.busy":"2024-03-23T21:59:55.407317Z","iopub.execute_input":"2024-03-23T21:59:55.407757Z","iopub.status.idle":"2024-03-23T21:59:55.412898Z","shell.execute_reply.started":"2024-03-23T21:59:55.407724Z","shell.execute_reply":"2024-03-23T21:59:55.411525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/train'\ndirs = os.listdir(train_path)\nlen(dirs)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T21:59:55.622856Z","iopub.execute_input":"2024-03-23T21:59:55.623325Z","iopub.status.idle":"2024-03-23T21:59:55.648828Z","shell.execute_reply.started":"2024-03-23T21:59:55.623290Z","shell.execute_reply":"2024-03-23T21:59:55.647707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_paths = [path for path in dirs if path.startswith('cat')]\ndog_paths = [path for path in dirs if path.startswith('dog')]\nall_paths= cat_paths+dog_paths\n\ntrain_paths = cat_paths[2500:] + dog_paths[2500:]\nvalid_paths = cat_paths[:2500] + dog_paths[:2500]\n\nlen(cat_paths), len(dog_paths)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T21:59:56.033937Z","iopub.execute_input":"2024-03-23T21:59:56.034374Z","iopub.status.idle":"2024-03-23T21:59:56.051548Z","shell.execute_reply.started":"2024-03-23T21:59:56.034344Z","shell.execute_reply":"2024-03-23T21:59:56.050462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(train_paths)\nrandom.shuffle(valid_paths)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T21:59:56.712165Z","iopub.execute_input":"2024-03-23T21:59:56.712557Z","iopub.status.idle":"2024-03-23T21:59:56.734524Z","shell.execute_reply.started":"2024-03-23T21:59:56.712529Z","shell.execute_reply":"2024-03-23T21:59:56.733665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_path = os.path.join(train_path,cat_paths[0])\ncat_image = Image.open(cat_path)\ncat_image","metadata":{"execution":{"iopub.status.busy":"2024-03-23T21:59:58.962966Z","iopub.execute_input":"2024-03-23T21:59:58.963901Z","iopub.status.idle":"2024-03-23T21:59:59.065108Z","shell.execute_reply.started":"2024-03-23T21:59:58.963859Z","shell.execute_reply":"2024-03-23T21:59:59.064024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_path = os.path.join(train_path,dog_paths[0])\ndog_image = Image.open(dog_path)\ndog_image","metadata":{"execution":{"iopub.status.busy":"2024-03-23T22:00:00.055640Z","iopub.execute_input":"2024-03-23T22:00:00.056083Z","iopub.status.idle":"2024-03-23T22:00:00.170624Z","shell.execute_reply.started":"2024-03-23T22:00:00.056050Z","shell.execute_reply":"2024-03-23T22:00:00.169385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we can see inconsistent in sizes of pictures..","metadata":{}},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.PILToTensor()\n])","metadata":{"execution":{"iopub.status.busy":"2024-03-23T22:00:01.886386Z","iopub.execute_input":"2024-03-23T22:00:01.886783Z","iopub.status.idle":"2024-03-23T22:00:01.891940Z","shell.execute_reply.started":"2024-03-23T22:00:01.886754Z","shell.execute_reply":"2024-03-23T22:00:01.890862Z"},"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,index):\n        img = Image.open(os.path.join(train_path,self.paths[index]))\n        img = img.resize((500,500))\n        label =self.paths[index][:3]\n        if self.transform:\n            img = self.transform(img)\n#             img = img.float()\n    \n        return img, int(label == 'cat')\n    def __len__(self):\n        return len(self.paths)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T22:11:12.540244Z","iopub.execute_input":"2024-03-23T22:11:12.540669Z","iopub.status.idle":"2024-03-23T22:11:12.548924Z","shell.execute_reply.started":"2024-03-23T22:11:12.540638Z","shell.execute_reply":"2024-03-23T22:11:12.547533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindata = dataset(paths=train_paths,transform = transform)\nvaliddata = dataset(paths=valid_paths,transform = transform)\n\ntrain_loader = DataLoader(dataset = traindata,shuffle=True,batch_size=32)\nvalid_loader = DataLoader(dataset = validdata,shuffle=False,batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T22:11:12.912407Z","iopub.execute_input":"2024-03-23T22:11:12.913709Z","iopub.status.idle":"2024-03-23T22:11:12.920396Z","shell.execute_reply.started":"2024-03-23T22:11:12.913661Z","shell.execute_reply":"2024-03-23T22:11:12.918942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(train_loader))[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-03-23T22:11:13.459834Z","iopub.execute_input":"2024-03-23T22:11:13.460296Z","iopub.status.idle":"2024-03-23T22:11:13.800094Z","shell.execute_reply.started":"2024-03-23T22:11:13.460261Z","shell.execute_reply":"2024-03-23T22:11:13.798788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(train_loader))[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-23T22:11:48.583612Z","iopub.execute_input":"2024-03-23T22:11:48.586529Z","iopub.status.idle":"2024-03-23T22:11:48.950599Z","shell.execute_reply.started":"2024-03-23T22:11:48.586472Z","shell.execute_reply":"2024-03-23T22:11:48.949089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(train_loader))[1]","metadata":{"execution":{"iopub.status.busy":"2024-03-23T22:11:13.814053Z","iopub.execute_input":"2024-03-23T22:11:13.814509Z","iopub.status.idle":"2024-03-23T22:11:14.148826Z","shell.execute_reply.started":"2024-03-23T22:11:13.814476Z","shell.execute_reply":"2024-03-23T22:11:14.147508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimage = traindata[0]\nplt.imshow(image[0].permute(2,1,0))\n\nprint(image[1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-23T22:13:32.010032Z","iopub.execute_input":"2024-03-23T22:13:32.010491Z","iopub.status.idle":"2024-03-23T22:13:32.285572Z","shell.execute_reply.started":"2024-03-23T22:13:32.010458Z","shell.execute_reply":"2024-03-23T22:13:32.284262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}