{"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":30747,"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \ntrain_path = \"/kaggle/working/train\"\ndirs = os.listdir(train_path)\nlen(dirs)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:04:41.400008Z","iopub.execute_input":"2024-08-10T16:04:41.400298Z","iopub.status.idle":"2024-08-10T16:04:41.422374Z","shell.execute_reply.started":"2024-08-10T16:04:41.400271Z","shell.execute_reply":"2024-08-10T16:04:41.421654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check for unbalanced\ncat_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\nprint(len(cat_paths)) ,print(len(dog_paths)) ","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:04:41.423280Z","iopub.execute_input":"2024-08-10T16:04:41.423521Z","iopub.status.idle":"2024-08-10T16:04:41.438085Z","shell.execute_reply.started":"2024-08-10T16:04:41.423499Z","shell.execute_reply":"2024-08-10T16:04:41.437164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image \ncat_path = os.path.join(train_path , cat_paths[0])\nimg = Image.open(cat_path)\nimg","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:04:41.439957Z","iopub.execute_input":"2024-08-10T16:04:41.440236Z","iopub.status.idle":"2024-08-10T16:04:41.511263Z","shell.execute_reply.started":"2024-08-10T16:04:41.440213Z","shell.execute_reply":"2024-08-10T16:04:41.510391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_path = os.path.join(train_path , dog_paths[0])\nimg = Image.open(dog_path)\nimg","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:04:41.512291Z","iopub.execute_input":"2024-08-10T16:04:41.512540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset , DataLoader","metadata":{"execution":{"iopub.status.idle":"2024-08-10T16:04:45.539639Z","shell.execute_reply.started":"2024-08-10T16:04:41.627647Z","shell.execute_reply":"2024-08-10T16:04:45.538666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class dataset(Dataset):\n    def __init__(self , paths , transform):\n        # dataloadig\n        self.paths = paths\n        self.transform = transform\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((277,277))\n        label = self.paths[index][:3]\n        \n        if self.transform:\n            img = self.transform(img)\n            \n            \n        return img , int(label ==\"cat\")\n        \n    def __len__(self):\n        return len(self.paths)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:04:45.540897Z","iopub.execute_input":"2024-08-10T16:04:45.541287Z","iopub.status.idle":"2024-08-10T16:04:45.548147Z","shell.execute_reply.started":"2024-08-10T16:04:45.541260Z","shell.execute_reply":"2024-08-10T16:04:45.547315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = dataset(all_paths , transform)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:33.915892Z","iopub.execute_input":"2024-08-10T16:05:33.916322Z","iopub.status.idle":"2024-08-10T16:05:33.921350Z","shell.execute_reply.started":"2024-08-10T16:05:33.916285Z","shell.execute_reply":"2024-08-10T16:05:33.920177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[0]\n# we should convert PIL to tensor","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:34.732887Z","iopub.execute_input":"2024-08-10T16:05:34.733256Z","iopub.status.idle":"2024-08-10T16:05:34.860088Z","shell.execute_reply.started":"2024-08-10T16:05:34.733220Z","shell.execute_reply":"2024-08-10T16:05:34.859263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.transforms import transforms\nfrom torchvision.transforms import v2 # v2 instead of transform","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:30.197992Z","iopub.execute_input":"2024-08-10T16:05:30.198626Z","iopub.status.idle":"2024-08-10T16:05:31.988711Z","shell.execute_reply.started":"2024-08-10T16:05:30.198595Z","shell.execute_reply":"2024-08-10T16:05:31.987777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data augmentation inside transform\ntransform = v2.Compose([\n    v2.PILToTensor(),\n    v2.RandomHorizontalFlip(p=.5),\n    v2.RandomCrop(size=[227, 227]),\n    v2.ToDtype(torch.float32 ,scale = True)\n])","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:31.990307Z","iopub.execute_input":"2024-08-10T16:05:31.990685Z","iopub.status.idle":"2024-08-10T16:05:31.996016Z","shell.execute_reply.started":"2024-08-10T16:05:31.990660Z","shell.execute_reply":"2024-08-10T16:05:31.995056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[0]","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:40.202615Z","iopub.execute_input":"2024-08-10T16:05:40.202982Z","iopub.status.idle":"2024-08-10T16:05:40.219943Z","shell.execute_reply.started":"2024-08-10T16:05:40.202946Z","shell.execute_reply":"2024-08-10T16:05:40.218880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[0][0].shape","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:40.826621Z","iopub.execute_input":"2024-08-10T16:05:40.827563Z","iopub.status.idle":"2024-08-10T16:05:40.839008Z","shell.execute_reply.started":"2024-08-10T16:05:40.827517Z","shell.execute_reply":"2024-08-10T16:05:40.838111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[0][1]","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:41.558814Z","iopub.execute_input":"2024-08-10T16:05:41.559616Z","iopub.status.idle":"2024-08-10T16:05:41.570277Z","shell.execute_reply.started":"2024-08-10T16:05:41.559585Z","shell.execute_reply":"2024-08-10T16:05:41.569386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loader = DataLoader(train , shuffle = True , batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:42.198230Z","iopub.execute_input":"2024-08-10T16:05:42.198558Z","iopub.status.idle":"2024-08-10T16:05:42.203168Z","shell.execute_reply.started":"2024-08-10T16:05:42.198532Z","shell.execute_reply":"2024-08-10T16:05:42.202267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(loader))  # error batch , size ","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:42.857545Z","iopub.execute_input":"2024-08-10T16:05:42.857871Z","iopub.status.idle":"2024-08-10T16:05:43.037407Z","shell.execute_reply.started":"2024-08-10T16:05:42.857843Z","shell.execute_reply":"2024-08-10T16:05:43.036517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(loader))[0].shape # 4d","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:43.460402Z","iopub.execute_input":"2024-08-10T16:05:43.460688Z","iopub.status.idle":"2024-08-10T16:05:43.618144Z","shell.execute_reply.started":"2024-08-10T16:05:43.460665Z","shell.execute_reply":"2024-08-10T16:05:43.617359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"12500*.2","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:44.036078Z","iopub.execute_input":"2024-08-10T16:05:44.036413Z","iopub.status.idle":"2024-08-10T16:05:44.042156Z","shell.execute_reply.started":"2024-08-10T16:05:44.036385Z","shell.execute_reply":"2024-08-10T16:05:44.041256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_paths = cat_paths[:2500] + dog_paths[:2500]\ntrain_paths = cat_paths[2500:] + dog_paths[2500:]","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:44.860633Z","iopub.execute_input":"2024-08-10T16:05:44.860946Z","iopub.status.idle":"2024-08-10T16:05:44.865890Z","shell.execute_reply.started":"2024-08-10T16:05:44.860908Z","shell.execute_reply":"2024-08-10T16:05:44.864989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random \nrandom.seed(42)\nrandom.shuffle(train_paths)\nrandom.shuffle(val_paths)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:45.869157Z","iopub.execute_input":"2024-08-10T16:05:45.870032Z","iopub.status.idle":"2024-08-10T16:05:45.895658Z","shell.execute_reply.started":"2024-08-10T16:05:45.869987Z","shell.execute_reply":"2024-08-10T16:05:45.894809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = dataset(train_paths , transform )\nval_data = dataset(val_paths , transform)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:46.551317Z","iopub.execute_input":"2024-08-10T16:05:46.551920Z","iopub.status.idle":"2024-08-10T16:05:46.556241Z","shell.execute_reply.started":"2024-08-10T16:05:46.551892Z","shell.execute_reply":"2024-08-10T16:05:46.555236Z"},"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 = True , batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:47.077031Z","iopub.execute_input":"2024-08-10T16:05:47.077335Z","iopub.status.idle":"2024-08-10T16:05:47.082162Z","shell.execute_reply.started":"2024-08-10T16:05:47.077310Z","shell.execute_reply":"2024-08-10T16:05:47.081158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(train_loader))[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:47.120837Z","iopub.execute_input":"2024-08-10T16:05:47.121475Z","iopub.status.idle":"2024-08-10T16:05:47.283620Z","shell.execute_reply.started":"2024-08-10T16:05:47.121451Z","shell.execute_reply":"2024-08-10T16:05:47.282623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(train_loader))","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:47.727885Z","iopub.execute_input":"2024-08-10T16:05:47.728189Z","iopub.status.idle":"2024-08-10T16:05:47.888896Z","shell.execute_reply.started":"2024-08-10T16:05:47.728165Z","shell.execute_reply":"2024-08-10T16:05:47.887994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimg = train_data[45][0]\nplt.imshow(img.permute(1,2,0))\n\n# 3 277 , 277 \n# matplotlib need : 277 , 277 ,3\n","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:48.437832Z","iopub.execute_input":"2024-08-10T16:05:48.438151Z","iopub.status.idle":"2024-08-10T16:05:48.804811Z","shell.execute_reply.started":"2024-08-10T16:05:48.438127Z","shell.execute_reply":"2024-08-10T16:05:48.803918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[7][1]","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:49.108004Z","iopub.execute_input":"2024-08-10T16:05:49.108870Z","iopub.status.idle":"2024-08-10T16:05:49.119999Z","shell.execute_reply.started":"2024-08-10T16:05:49.108830Z","shell.execute_reply":"2024-08-10T16:05:49.119083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimg = train_data[89][0]\nplt.imshow(img.permute(1,2,0))","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:49.941086Z","iopub.execute_input":"2024-08-10T16:05:49.941874Z","iopub.status.idle":"2024-08-10T16:05:50.301297Z","shell.execute_reply.started":"2024-08-10T16:05:49.941841Z","shell.execute_reply":"2024-08-10T16:05:50.300399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[89][1]","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:50.920021Z","iopub.execute_input":"2024-08-10T16:05:50.920620Z","iopub.status.idle":"2024-08-10T16:05:50.933040Z","shell.execute_reply.started":"2024-08-10T16:05:50.920594Z","shell.execute_reply":"2024-08-10T16:05:50.932144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building model : AlexNet","metadata":{}},{"cell_type":"code","source":"import torch  \nimport torch.nn as nn  ","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:52.954815Z","iopub.execute_input":"2024-08-10T16:05:52.955177Z","iopub.status.idle":"2024-08-10T16:05:52.959586Z","shell.execute_reply.started":"2024-08-10T16:05:52.955148Z","shell.execute_reply":"2024-08-10T16:05:52.958579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[0][0].shape","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:53.942831Z","iopub.execute_input":"2024-08-10T16:05:53.943184Z","iopub.status.idle":"2024-08-10T16:05:53.953134Z","shell.execute_reply.started":"2024-08-10T16:05:53.943155Z","shell.execute_reply":"2024-08-10T16:05:53.952231Z"},"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        self.conv2 = nn.Conv2d(96, 256, kernel_size=5, padding=2)  \n        self.mx2 = nn.MaxPool2d(kernel_size=3, stride=2)  \n        self.conv3 = nn.Conv2d(256, 384, kernel_size=3, padding=1)  \n        self.conv4 = nn.Conv2d(384, 384, kernel_size=3, padding=1)  \n        self.conv5 = nn.Conv2d(384, 256, kernel_size=3, padding=1)  \n        self.mx3 = nn.MaxPool2d(kernel_size=3, stride=2)  \n        \n        # Adjust the input size based on output after MaxPool3  \n        self.linear1 = nn.Linear(12544, 4096)  \n        self.linear2 = nn.Linear(4096, 4096)  \n        self.linear3 = nn.Linear(4096, num_classes)  \n\n    def forward(self, x):  \n        out = nn.ReLU()(self.conv1(x))  \n        out = self.mx1(out)  \n        out = nn.ReLU()(self.conv2(out))  \n        out = self.mx2(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\n        print(out.shape)  # Print shape for debugging  \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-08-10T16:05:54.872096Z","iopub.execute_input":"2024-08-10T16:05:54.872681Z","iopub.status.idle":"2024-08-10T16:05:54.884457Z","shell.execute_reply.started":"2024-08-10T16:05:54.872653Z","shell.execute_reply":"2024-08-10T16:05:54.883308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import models","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:05:55.613741Z","iopub.execute_input":"2024-08-10T16:05:55.614105Z","iopub.status.idle":"2024-08-10T16:05:55.618418Z","shell.execute_reply.started":"2024-08-10T16:05:55.614076Z","shell.execute_reply":"2024-08-10T16:05:55.617416Z"},"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.SGD(model.parameters() , lr=lr)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:11.975825Z","iopub.execute_input":"2024-08-10T16:06:11.976515Z","iopub.status.idle":"2024-08-10T16:06:12.918681Z","shell.execute_reply.started":"2024-08-10T16:06:11.976480Z","shell.execute_reply":"2024-08-10T16:06:12.917905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import models","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:12.920199Z","iopub.execute_input":"2024-08-10T16:06:12.920526Z","iopub.status.idle":"2024-08-10T16:06:12.924634Z","shell.execute_reply.started":"2024-08-10T16:06:12.920501Z","shell.execute_reply":"2024-08-10T16:06:12.923736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.resnet50(weights = \"IMAGENET1K_V1\")","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:13.671736Z","iopub.execute_input":"2024-08-10T16:06:13.672425Z","iopub.status.idle":"2024-08-10T16:06:15.026234Z","shell.execute_reply.started":"2024-08-10T16:06:13.672394Z","shell.execute_reply":"2024-08-10T16:06:15.025217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:15.027740Z","iopub.execute_input":"2024-08-10T16:06:15.028046Z","iopub.status.idle":"2024-08-10T16:06:15.035700Z","shell.execute_reply.started":"2024-08-10T16:06:15.028022Z","shell.execute_reply":"2024-08-10T16:06:15.034872Z"},"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-08-10T16:06:27.294856Z","iopub.execute_input":"2024-08-10T16:06:27.295697Z","iopub.status.idle":"2024-08-10T16:06:27.302919Z","shell.execute_reply.started":"2024-08-10T16:06:27.295667Z","shell.execute_reply":"2024-08-10T16:06:27.300611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training loop  \ndef train_epoch():  \n    epoch_loss = 0  \n    for i, (images, labels) in enumerate(train_loader):  \n        images = images.float()\n        images = images.to(device)  \n        labels = labels.to(device)  \n\n        # Verify type of images  \n        print(f'Images type: {images.dtype}')  # Should print torch.float32  \n\n        # forward pass  \n        out = model(images)  \n        loss = criterion(out, labels)  \n        epoch_loss += loss.item()  # Make sure to get the loss value, not the tensor  \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    \n    return epoch_loss / len(train_loader)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:28.013282Z","iopub.execute_input":"2024-08-10T16:06:28.013683Z","iopub.status.idle":"2024-08-10T16:06:28.020836Z","shell.execute_reply.started":"2024-08-10T16:06:28.013654Z","shell.execute_reply":"2024-08-10T16:06:28.019859Z"},"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.float()\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-08-10T16:06:28.976786Z","iopub.execute_input":"2024-08-10T16:06:28.977396Z","iopub.status.idle":"2024-08-10T16:06:28.983440Z","shell.execute_reply.started":"2024-08-10T16:06:28.977365Z","shell.execute_reply":"2024-08-10T16:06:28.982407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses = []\nval_losses = []\nbest_score = float(\"inf\")\ntolerance = 0\n\n#for 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    #    tolerance += 1\n     #   if(tolerance == 4):\n     #       break\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-08-10T16:06:29.870368Z","iopub.execute_input":"2024-08-10T16:06:29.871527Z","iopub.status.idle":"2024-08-10T16:06:29.878897Z","shell.execute_reply.started":"2024-08-10T16:06:29.871487Z","shell.execute_reply":"2024-08-10T16:06:29.877840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:30.660878Z","iopub.execute_input":"2024-08-10T16:06:30.661315Z","iopub.status.idle":"2024-08-10T16:06:30.667235Z","shell.execute_reply.started":"2024-08-10T16:06:30.661272Z","shell.execute_reply":"2024-08-10T16:06:30.666365Z"},"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-08-10T16:06:31.439627Z","iopub.execute_input":"2024-08-10T16:06:31.440353Z","iopub.status.idle":"2024-08-10T16:06:31.445987Z","shell.execute_reply.started":"2024-08-10T16:06:31.440323Z","shell.execute_reply":"2024-08-10T16:06:31.444979Z"},"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-08-10T16:06:32.134554Z","iopub.execute_input":"2024-08-10T16:06:32.134872Z","iopub.status.idle":"2024-08-10T16:06:32.335226Z","shell.execute_reply.started":"2024-08-10T16:06:32.134845Z","shell.execute_reply":"2024-08-10T16:06:32.334322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\n#model = AlexNet(2).to(device)\n#model.load_state_dict(torch.load(\"/kaggle/working/model_8\",map_location = device))","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:32.901884Z","iopub.execute_input":"2024-08-10T16:06:32.902696Z","iopub.status.idle":"2024-08-10T16:06:32.906326Z","shell.execute_reply.started":"2024-08-10T16:06:32.902664Z","shell.execute_reply":"2024-08-10T16:06:32.905345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get_val_loss()","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:46.114711Z","iopub.execute_input":"2024-08-10T16:06:46.115433Z","iopub.status.idle":"2024-08-10T16:06:46.119163Z","shell.execute_reply.started":"2024-08-10T16:06:46.115400Z","shell.execute_reply":"2024-08-10T16:06:46.118227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"plt.imshow(train_data[0][0].permute(1,2,0)) # randomhorizonatal in transform","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:06:47.251890Z","iopub.execute_input":"2024-08-10T16:06:47.252566Z","iopub.status.idle":"2024-08-10T16:06:47.609542Z","shell.execute_reply.started":"2024-08-10T16:06:47.252534Z","shell.execute_reply":"2024-08-10T16:06:47.608596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_data[0][0].permute(1,2,0)) # randomhorizonatal in transform","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:07:00.417650Z","iopub.execute_input":"2024-08-10T16:07:00.417982Z","iopub.status.idle":"2024-08-10T16:07:00.773069Z","shell.execute_reply.started":"2024-08-10T16:07:00.417958Z","shell.execute_reply":"2024-08-10T16:07:00.772153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_data[0][0].permute(1,2,0)) # crop in transform     ","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:07:06.854632Z","iopub.execute_input":"2024-08-10T16:07:06.855027Z","iopub.status.idle":"2024-08-10T16:07:07.207668Z","shell.execute_reply.started":"2024-08-10T16:07:06.854995Z","shell.execute_reply":"2024-08-10T16:07:07.206798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# IN EVERY EPOCH : MODEL READ ALL THE DATA , IN EVERY EPOCH IT READ DATA IN DIFFERENT TRANSFORM","metadata":{}},{"cell_type":"markdown","source":"## DATA augmentation on train only not val","metadata":{}},{"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-08-10T16:07:12.564312Z","iopub.execute_input":"2024-08-10T16:07:12.565024Z","iopub.status.idle":"2024-08-10T16:07:12.624773Z","shell.execute_reply.started":"2024-08-10T16:07:12.564990Z","shell.execute_reply":"2024-08-10T16:07:12.624044Z"},"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-08-10T16:07:13.336200Z","iopub.execute_input":"2024-08-10T16:07:13.336561Z","iopub.status.idle":"2024-08-10T16:07:13.344408Z","shell.execute_reply.started":"2024-08-10T16:07:13.336531Z","shell.execute_reply":"2024-08-10T16:07:13.343314Z"},"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-08-10T16:07:14.002917Z","iopub.execute_input":"2024-08-10T16:07:14.003577Z","iopub.status.idle":"2024-08-10T16:07:14.007770Z","shell.execute_reply.started":"2024-08-10T16:07:14.003540Z","shell.execute_reply":"2024-08-10T16:07:14.006845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning","metadata":{}},{"cell_type":"code","source":"model = models.resnet50(weights = \"IMAGENET1K_V1\")","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:07:16.022631Z","iopub.execute_input":"2024-08-10T16:07:16.023309Z","iopub.status.idle":"2024-08-10T16:07:16.566256Z","shell.execute_reply.started":"2024-08-10T16:07:16.023275Z","shell.execute_reply":"2024-08-10T16:07:16.565269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:07:17.332288Z","iopub.execute_input":"2024-08-10T16:07:17.333124Z","iopub.status.idle":"2024-08-10T16:07:17.341419Z","shell.execute_reply.started":"2024-08-10T16:07:17.333094Z","shell.execute_reply":"2024-08-10T16:07:17.340550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for parameter in model.parameters():\n    parameter.requires_grad = False       # freeze weights   2 - with torch.no grad  3-detach","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:29:05.400874Z","iopub.execute_input":"2024-08-10T16:29:05.401734Z","iopub.status.idle":"2024-08-10T16:29:05.406579Z","shell.execute_reply.started":"2024-08-10T16:29:05.401702Z","shell.execute_reply":"2024-08-10T16:29:05.405586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fc == nn.Linear(model.fc.in_features ,2)","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:29:06.095229Z","iopub.execute_input":"2024-08-10T16:29:06.095942Z","iopub.status.idle":"2024-08-10T16:29:06.102456Z","shell.execute_reply.started":"2024-08-10T16:29:06.095899Z","shell.execute_reply":"2024-08-10T16:29:06.101563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fc.in_features","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:29:07.277032Z","iopub.execute_input":"2024-08-10T16:29:07.277386Z","iopub.status.idle":"2024-08-10T16:29:07.283211Z","shell.execute_reply.started":"2024-08-10T16:29:07.277352Z","shell.execute_reply":"2024-08-10T16:29:07.282055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2024-08-10T16:26:16.598471Z","iopub.execute_input":"2024-08-10T16:26:16.598838Z","iopub.status.idle":"2024-08-10T16:26:16.607136Z","shell.execute_reply.started":"2024-08-10T16:26:16.598809Z","shell.execute_reply":"2024-08-10T16:26:16.606257Z"},"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)","metadata":{},"execution_count":null,"outputs":[]}]}