{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h3 bgcolor=\"gray\">Loading Data</h3>","metadata":{}},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:14:56.203696Z","iopub.execute_input":"2025-02-11T19:14:56.204044Z","iopub.status.idle":"2025-02-11T19:15:07.510324Z","shell.execute_reply.started":"2025-02-11T19:14:56.204013Z","shell.execute_reply":"2025-02-11T19:15:07.509373Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nimport os\nimport torch \nimport torchvision\nfrom torchvision.transforms import transforms,v2\nfrom torch.utils.data import Dataset,DataLoader","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:14:27.800748Z","iopub.execute_input":"2025-02-11T19:14:27.801053Z","iopub.status.idle":"2025-02-11T19:14:27.815074Z","shell.execute_reply.started":"2025-02-11T19:14:27.801023Z","shell.execute_reply":"2025-02-11T19:14:27.814426Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_path=\"/kaggle/working/train\"\ntrain_dir=os.listdir(train_path)\nprint(len(train_dir))\n\ncats_paths=[os.path.join(train_path,path) for path in train_dir if \"cat\" in path]\ndogs_paths=[os.path.join(train_path,path) for path in train_dir if \"dog\" in path]\nprint(len(cats_paths),len(dogs_paths))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:13.751545Z","iopub.execute_input":"2025-02-11T19:15:13.751874Z","iopub.status.idle":"2025-02-11T19:15:13.796598Z","shell.execute_reply.started":"2025-02-11T19:15:13.751842Z","shell.execute_reply":"2025-02-11T19:15:13.795883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_cats=cats_paths[:2500]\nval_dogs=dogs_paths[:2500]\n\ntrain_cats=cats_paths[2500:]\ntrain_dogs=dogs_paths[2500:]\n\nall_train=[]\nall_valid=[]\n\nall_train.extend(train_cats)\nall_train.extend(train_dogs)\n\nall_valid.extend(val_cats)\nall_valid.extend(val_dogs)\n\nprint(f\"length of training data: {len(all_train)} , length of validation data: {len(all_valid)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:27.598458Z","iopub.execute_input":"2025-02-11T19:15:27.598732Z","iopub.status.idle":"2025-02-11T19:15:27.604717Z","shell.execute_reply.started":"2025-02-11T19:15:27.598710Z","shell.execute_reply":"2025-02-11T19:15:27.603773Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img=Image.open(cats_paths[0])\nimg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:29.667332Z","iopub.execute_input":"2025-02-11T19:15:29.667656Z","iopub.status.idle":"2025-02-11T19:15:29.751683Z","shell.execute_reply.started":"2025-02-11T19:15:29.667627Z","shell.execute_reply":"2025-02-11T19:15:29.750859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img=Image.open(dogs_paths[0])\nimg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:32.252234Z","iopub.execute_input":"2025-02-11T19:15:32.252508Z","iopub.status.idle":"2025-02-11T19:15:32.292239Z","shell.execute_reply.started":"2025-02-11T19:15:32.252487Z","shell.execute_reply":"2025-02-11T19:15:32.291386Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_transforms=v2.Compose(\n    [\n        v2.PILToTensor(),\n        v2.RandomHorizontalFlip(p=0.5),\n        v2.RandomResizedCrop(size=[227,227],antialias=True ),\n        v2.ToDtype(torch.float32,scale=True)\n    ]\n)\n\nvalid_trasforms=v2.Compose(\n    [\n        v2.PILToTensor(),\n        v2.ToDtype(torch.float32,scale=True)\n\n    ]\n)\n\ntrasform={\n    \"train_transform\":train_transforms,\n    \"valid_transform\":valid_trasforms\n    \n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:33.801754Z","iopub.execute_input":"2025-02-11T19:15:33.802051Z","iopub.status.idle":"2025-02-11T19:15:33.846569Z","shell.execute_reply.started":"2025-02-11T19:15:33.802027Z","shell.execute_reply":"2025-02-11T19:15:33.845729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CatsDogs(Dataset):\n    def __init__(self,paths,transforms=None,is_train=True):\n        self.paths=paths\n        self.transforms=transforms\n        self.is_train=is_train\n\n    def __getitem__(self,index):\n        img=Image.open(self.paths[index])\n        img=img.resize((227,227))\n        if self.transforms:\n            if self.is_train:\n                img=self.transforms[\"train_transform\"](img)\n            else:\n                img=self.transforms[\"valid_transform\"](img)\n        label=''\n        if 'cat' in self.paths[index]:\n            label=0\n        else:\n            label=1\n        return img,label\n        \n    def __len__(self):\n        return len(self.paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:35.488665Z","iopub.execute_input":"2025-02-11T19:15:35.489005Z","iopub.status.idle":"2025-02-11T19:15:35.494755Z","shell.execute_reply.started":"2025-02-11T19:15:35.488976Z","shell.execute_reply":"2025-02-11T19:15:35.493858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset=CatsDogs(all_train,trasform)\nvalid_dataset=CatsDogs(all_valid,trasform,False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:36.212696Z","iopub.execute_input":"2025-02-11T19:15:36.213079Z","iopub.status.idle":"2025-02-11T19:15:36.217109Z","shell.execute_reply.started":"2025-02-11T19:15:36.213040Z","shell.execute_reply":"2025-02-11T19:15:36.216302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loader=DataLoader(train_dataset,shuffle=True,batch_size=50)\nvalid_loader=DataLoader(valid_dataset,shuffle=True,batch_size=50)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:36.554126Z","iopub.execute_input":"2025-02-11T19:15:36.554370Z","iopub.status.idle":"2025-02-11T19:15:36.558628Z","shell.execute_reply.started":"2025-02-11T19:15:36.554351Z","shell.execute_reply":"2025-02-11T19:15:36.557698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimg=train_dataset[1500][0]\nplt.imshow(img.permute(1,2,0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:36.782102Z","iopub.execute_input":"2025-02-11T19:15:36.782369Z","iopub.status.idle":"2025-02-11T19:15:37.126085Z","shell.execute_reply.started":"2025-02-11T19:15:36.782346Z","shell.execute_reply":"2025-02-11T19:15:37.125339Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimg=valid_dataset[1500][0]\nplt.imshow(img.permute(1,2,0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:37.127464Z","iopub.execute_input":"2025-02-11T19:15:37.127778Z","iopub.status.idle":"2025-02-11T19:15:37.356418Z","shell.execute_reply.started":"2025-02-11T19:15:37.127747Z","shell.execute_reply":"2025-02-11T19:15:37.355554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:37.357481Z","iopub.execute_input":"2025-02-11T19:15:37.357689Z","iopub.status.idle":"2025-02-11T19:15:37.444558Z","shell.execute_reply.started":"2025-02-11T19:15:37.357671Z","shell.execute_reply":"2025-02-11T19:15:37.443630Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn as nn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:37.445622Z","iopub.execute_input":"2025-02-11T19:15:37.445951Z","iopub.status.idle":"2025-02-11T19:15:37.457368Z","shell.execute_reply.started":"2025-02-11T19:15:37.445911Z","shell.execute_reply":"2025-02-11T19:15:37.456716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class AlexNet(nn.Module):\n    def __init__(self):\n        super(AlexNet,self).__init__()\n        self.conv1=nn.Conv2d(3,96,kernel_size=11,stride=4)\n        self.pool1=nn.MaxPool2d(kernel_size=3,stride=2)\n\n        self.conv2=nn.Conv2d(96,256,kernel_size=5,padding=\"same\")\n        self.pool2=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        self.pool3=nn.MaxPool2d(kernel_size=3,stride=2)\n\n        self.l1=nn.Linear(9216,4096)\n        self.l2=nn.Linear(4096,4096)\n        self.l3=nn.Linear(4096,2)\n        \n    def forward(self,data):\n        out=nn.ReLU()(self.conv1(data))\n        out=self.pool1(out)\n        \n        out=nn.ReLU()(self.conv2(out))\n        out=self.pool2(out)\n\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        \n        out=nn.Flatten()(out)\n        out=nn.ReLU()(self.l1(out))\n        out=nn.ReLU()(self.l2(out))\n        out=self.l3(out)\n        return out","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:37.548616Z","iopub.execute_input":"2025-02-11T19:15:37.548834Z","iopub.status.idle":"2025-02-11T19:15:37.555692Z","shell.execute_reply.started":"2025-02-11T19:15:37.548815Z","shell.execute_reply":"2025-02-11T19:15:37.555062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model=AlexNet()\nmodel.to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:37.824772Z","iopub.execute_input":"2025-02-11T19:15:37.825039Z","iopub.status.idle":"2025-02-11T19:15:38.737623Z","shell.execute_reply.started":"2025-02-11T19:15:37.825016Z","shell.execute_reply":"2025-02-11T19:15:38.736669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"criterion=nn.CrossEntropyLoss()\noptimizer=torch.optim.Adam(model.parameters(),lr=0.001)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:38.738837Z","iopub.execute_input":"2025-02-11T19:15:38.739205Z","iopub.status.idle":"2025-02-11T19:15:38.743394Z","shell.execute_reply.started":"2025-02-11T19:15:38.739162Z","shell.execute_reply":"2025-02-11T19:15:38.742457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_losses,valid_losses=[],[]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:38.745292Z","iopub.execute_input":"2025-02-11T19:15:38.745531Z","iopub.status.idle":"2025-02-11T19:15:38.757096Z","shell.execute_reply.started":"2025-02-11T19:15:38.745511Z","shell.execute_reply":"2025-02-11T19:15:38.756290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_epoch():\n    epoch_loss=0\n    for i, (imgs,labels) in enumerate(train_loader):\n        imgs=imgs.to(device)\n        labels=labels.to(device)\n\n        out=model(imgs)\n        loss=criterion(out,labels)\n        epoch_loss+=loss\n\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n    avg_loss=epoch_loss/len(train_loader)\n    return avg_loss\n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:38.758158Z","iopub.execute_input":"2025-02-11T19:15:38.758447Z","iopub.status.idle":"2025-02-11T19:15:38.770587Z","shell.execute_reply.started":"2025-02-11T19:15:38.758419Z","shell.execute_reply":"2025-02-11T19:15:38.769789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def valid_epoch():\n    valid_loss=0\n    for i, (imgs,labels) in enumerate(valid_loader):\n        imgs=imgs.to(device)\n        labels=labels.to(device)\n\n        out=model(imgs)\n        loss=criterion(out,labels)\n        valid_loss +=loss\n    avg_loss=valid_loss/len(valid_loader)\n    valid_losses.append(avg_loss)\n    return avg_loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:38.771365Z","iopub.execute_input":"2025-02-11T19:15:38.771575Z","iopub.status.idle":"2025-02-11T19:15:38.783969Z","shell.execute_reply.started":"2025-02-11T19:15:38.771556Z","shell.execute_reply":"2025-02-11T19:15:38.783244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for epoch in range(5):\n    train_error=train_epoch()\n    val_error=valid_epoch\n    print(f\"epoch {epoch+1} /epochs---training error={train_error} ---validation error={val_error}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:15:38.798922Z","iopub.execute_input":"2025-02-11T19:15:38.799133Z","iopub.status.idle":"2025-02-11T19:23:29.770295Z","shell.execute_reply.started":"2025-02-11T19:15:38.799114Z","shell.execute_reply":"2025-02-11T19:23:29.769386Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torchvision import models","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T19:23:29.771605Z","iopub.execute_input":"2025-02-11T19:23:29.771870Z","iopub.status.idle":"2025-02-11T19:23:29.775520Z","shell.execute_reply.started":"2025-02-11T19:23:29.771848Z","shell.execute_reply":"2025-02-11T19:23:29.774667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model=models.resnet50(weights=\"IMAGENET1K_V1\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T20:01:51.370422Z","iopub.execute_input":"2025-02-11T20:01:51.370724Z","iopub.status.idle":"2025-02-11T20:01:51.837572Z","shell.execute_reply.started":"2025-02-11T20:01:51.370701Z","shell.execute_reply":"2025-02-11T20:01:51.836880Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T20:01:55.685598Z","iopub.execute_input":"2025-02-11T20:01:55.685922Z","iopub.status.idle":"2025-02-11T20:01:55.692410Z","shell.execute_reply.started":"2025-02-11T20:01:55.685872Z","shell.execute_reply":"2025-02-11T20:01:55.691619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for param in model.parameters():\n    param.requires_grad=False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T20:02:03.923002Z","iopub.execute_input":"2025-02-11T20:02:03.923318Z","iopub.status.idle":"2025-02-11T20:02:03.927445Z","shell.execute_reply.started":"2025-02-11T20:02:03.923290Z","shell.execute_reply":"2025-02-11T20:02:03.926637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fc=nn.Linear(2048,2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T20:02:07.708349Z","iopub.execute_input":"2025-02-11T20:02:07.708614Z","iopub.status.idle":"2025-02-11T20:02:07.712964Z","shell.execute_reply.started":"2025-02-11T20:02:07.708593Z","shell.execute_reply":"2025-02-11T20:02:07.712109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T20:02:10.489095Z","iopub.execute_input":"2025-02-11T20:02:10.489384Z","iopub.status.idle":"2025-02-11T20:02:10.495837Z","shell.execute_reply.started":"2025-02-11T20:02:10.489362Z","shell.execute_reply":"2025-02-11T20:02:10.495003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for param in model.fc.parameters():\n    print(param.requires_grad)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T20:02:18.547869Z","iopub.execute_input":"2025-02-11T20:02:18.548224Z","iopub.status.idle":"2025-02-11T20:02:18.553129Z","shell.execute_reply.started":"2025-02-11T20:02:18.548193Z","shell.execute_reply":"2025-02-11T20:02:18.552303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T20:02:41.144944Z","iopub.execute_input":"2025-02-11T20:02:41.145219Z","iopub.status.idle":"2025-02-11T20:02:41.186631Z","shell.execute_reply.started":"2025-02-11T20:02:41.145197Z","shell.execute_reply":"2025-02-11T20:02:41.185980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for epoch in range(5):\n    train_error=train_epoch()\n    val_error=valid_epoch\n    print(f\"epoch {epoch+1} /epochs---training error={train_error} ---validation error={val_error}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-11T20:02:47.378035Z","iopub.execute_input":"2025-02-11T20:02:47.378326Z","iopub.status.idle":"2025-02-11T20:10:24.588942Z","shell.execute_reply.started":"2025-02-11T20:02:47.378305Z","shell.execute_reply":"2025-02-11T20:10:24.588139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}