{"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":"gpu","dataSources":[{"sourceId":6927,"databundleVersionId":45059,"sourceType":"competition"},{"sourceId":10788096,"sourceType":"datasetVersion","datasetId":6694764}],"dockerImageVersionId":30886,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch \nimport torch.nn as nn\nimport torchvision.transforms.functional as fn\nfrom albumentations.pytorch import ToTensorV2\nimport albumentations as A \nfrom tqdm import tqdm\nimport PIL \nimport pandas \nimport numpy as np\nimport scipy\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings \nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:11.778676Z","iopub.execute_input":"2025-02-20T02:32:11.779008Z","iopub.status.idle":"2025-02-20T02:32:19.486733Z","shell.execute_reply.started":"2025-02-20T02:32:11.778984Z","shell.execute_reply":"2025-02-20T02:32:19.486058Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device=\"cuda\" if torch.cuda.is_available() else \"cpu\"\ndevice","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:19.487800Z","iopub.execute_input":"2025-02-20T02:32:19.488269Z","iopub.status.idle":"2025-02-20T02:32:19.544275Z","shell.execute_reply.started":"2025-02-20T02:32:19.488240Z","shell.execute_reply":"2025-02-20T02:32:19.543344Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"text-align: center;\">\n    <strong><span style=\"color: Black;\"><h2> UNET archticture</h2></span></strong>\n</div>","metadata":{}},{"cell_type":"code","source":"from PIL import Image\n\nimg=Image.open(\"/kaggle/input/archimage/arch.png\")\nimg=img.resize((800,500))\nimg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:19.545671Z","iopub.execute_input":"2025-02-20T02:32:19.545904Z","iopub.status.idle":"2025-02-20T02:32:19.695892Z","shell.execute_reply.started":"2025-02-20T02:32:19.545884Z","shell.execute_reply":"2025-02-20T02:32:19.695076Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n<div style=\"text-align: center;\">\n    <strong><span style=\"color: Black;\"><h2> Building the network from scratch</h2></span></strong>\n</div>","metadata":{}},{"cell_type":"code","source":"class ConvBlock(nn.Module):\n    def __init__(self,in_channels,out_channels):\n        super(ConvBlock,self).__init__()\n        self.conv=nn.Sequential(\n        nn.Conv2d(in_channels,out_channels,kernel_size=3,padding=1,stride=1,bias=False),\n        nn.BatchNorm2d(out_channels),\n        nn.ReLU(inplace=True),\n        \n        nn.Conv2d(out_channels,out_channels,kernel_size=3,padding=1,stride=1,bias=False),\n        nn.BatchNorm2d(out_channels),\n        nn.ReLU(inplace=True)\n        )\n    def forward(self,x):\n        return self.conv(x)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:19.697307Z","iopub.execute_input":"2025-02-20T02:32:19.697632Z","iopub.status.idle":"2025-02-20T02:32:19.702632Z","shell.execute_reply.started":"2025-02-20T02:32:19.697603Z","shell.execute_reply":"2025-02-20T02:32:19.701725Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class UNET(nn.Module):\n    def __init__(self,in_channels=3,out_channels=1,feature_maps=[64,128,256,512]):\n        super(UNET,self).__init__()\n        self.pool=nn.MaxPool2d(kernel_size=2,stride=2)\n        self.downs,self.ups=nn.ModuleList(),nn.ModuleList()\n        for feature_map in feature_maps:\n            self.downs.append(ConvBlock(in_channels,feature_map))\n            in_channels=feature_map\n            \n        self.bottom=ConvBlock(feature_maps[-1],feature_maps[-1]*2)\n\n        \n        for feature_map in reversed(feature_maps):\n            self.ups.append(\n                nn.ConvTranspose2d(feature_map*2,feature_map,kernel_size=2,stride=2)\n            )\n            self.ups.append(\n                ConvBlock(feature_map*2,feature_map)\n            )\n        self.final=nn.Conv2d(feature_maps[0],out_channels,kernel_size=1)\n#######################\n\n    def forward(self,x):\n        skip_connections=[]\n        for down in self.downs:\n            x=down(x)\n            skip_connections.append(x)\n            x=self.pool(x)\n        x=self.bottom(x)\n        skip_connections=skip_connections[::-1]\n        for idx in range(0,len(self.ups),2):\n            x=self.ups[idx](x)\n            skip_connection=skip_connections[idx//2]\n            \n            if x.shape !=skip_connections[idx//2]:\n                x=fn.resize(x,size=(skip_connection.shape[2:]))\n                \n            x=torch.cat((skip_connection,x),dim=1)\n            x=self.ups[idx+1](x)\n        return self.final(x)\n\n##############\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:19.703567Z","iopub.execute_input":"2025-02-20T02:32:19.703880Z","iopub.status.idle":"2025-02-20T02:32:19.719425Z","shell.execute_reply.started":"2025-02-20T02:32:19.703851Z","shell.execute_reply":"2025-02-20T02:32:19.718675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def test():\n    x=torch.randn((3,1,160,160))\n    model=UNET(in_channels=1,out_channels=1)\n    preds=model(x)\n    print(preds.shape)\n    print(x.shape)\n    assert preds.shape ==x.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:19.720218Z","iopub.execute_input":"2025-02-20T02:32:19.720436Z","iopub.status.idle":"2025-02-20T02:32:19.737774Z","shell.execute_reply.started":"2025-02-20T02:32:19.720418Z","shell.execute_reply":"2025-02-20T02:32:19.736995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:19.738592Z","iopub.execute_input":"2025-02-20T02:32:19.738875Z","iopub.status.idle":"2025-02-20T02:32:21.080449Z","shell.execute_reply.started":"2025-02-20T02:32:19.738848Z","shell.execute_reply":"2025-02-20T02:32:21.079418Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n<div style=\"text-align: center;\">\n    <strong><span style=\"color: Black;\"><h2> Getting data and building the datasets and dataloaders</h2></span></strong>\n</div>","metadata":{}},{"cell_type":"code","source":"!unzip /kaggle/input/carvana-image-masking-challenge/train.zip","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:21.083021Z","iopub.execute_input":"2025-02-20T02:32:21.083298Z","iopub.status.idle":"2025-02-20T02:32:29.316941Z","shell.execute_reply.started":"2025-02-20T02:32:21.083276Z","shell.execute_reply":"2025-02-20T02:32:29.316065Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os \n\ntrain_dir=os.listdir(\"/kaggle/working/train\")\nprint(len(train_dir))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:29.318659Z","iopub.execute_input":"2025-02-20T02:32:29.318915Z","iopub.status.idle":"2025-02-20T02:32:29.325942Z","shell.execute_reply.started":"2025-02-20T02:32:29.318893Z","shell.execute_reply":"2025-02-20T02:32:29.325194Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"abs_path=\"/kaggle/working/train\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:29.326704Z","iopub.execute_input":"2025-02-20T02:32:29.326927Z","iopub.status.idle":"2025-02-20T02:32:29.339066Z","shell.execute_reply.started":"2025-02-20T02:32:29.326901Z","shell.execute_reply":"2025-02-20T02:32:29.338447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img=Image.open(os.path.join(abs_path,train_dir[0]))\nimg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:29.339824Z","iopub.execute_input":"2025-02-20T02:32:29.340067Z","iopub.status.idle":"2025-02-20T02:32:29.531891Z","shell.execute_reply.started":"2025-02-20T02:32:29.340026Z","shell.execute_reply":"2025-02-20T02:32:29.531015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip /kaggle/input/carvana-image-masking-challenge/train_masks.zip","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:29.532800Z","iopub.execute_input":"2025-02-20T02:32:29.533066Z","iopub.status.idle":"2025-02-20T02:32:30.413286Z","shell.execute_reply.started":"2025-02-20T02:32:29.533025Z","shell.execute_reply":"2025-02-20T02:32:30.412024Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"masks=os.listdir(\"/kaggle/working/train_masks\")\nimg=Image.open(os.path.join(\"/kaggle/working/train_masks\",train_dir[0].replace(\".jpg\",\"_mask.gif\")))\nimg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.414332Z","iopub.execute_input":"2025-02-20T02:32:30.414662Z","iopub.status.idle":"2025-02-20T02:32:30.438674Z","shell.execute_reply.started":"2025-02-20T02:32:30.414630Z","shell.execute_reply":"2025-02-20T02:32:30.437979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torch.utils.data import Dataset,DataLoader\nfrom torchvision.transforms import transforms,v2\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.439570Z","iopub.execute_input":"2025-02-20T02:32:30.439875Z","iopub.status.idle":"2025-02-20T02:32:30.530901Z","shell.execute_reply.started":"2025-02-20T02:32:30.439846Z","shell.execute_reply":"2025-02-20T02:32:30.530257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_transform=A.Compose(\n    [\n        A.Resize(height=256, width=256),  \n        A.HorizontalFlip(p=0.5),\n        A.VerticalFlip(p=0.5),\n        A.Rotate(limit=35,p=0.5),\n        A.Normalize(\n            mean=[0.0,0.0,0.0],\n            std=[1.0,1.0,1.0],\n            max_pixel_value=255.0\n        ),\n        ToTensorV2()\n    ]\n)\n\nvalid_transform=A.Compose(\n    [\n        A.Resize(height=256, width=256),  \n        A.Normalize(\n            mean=[0.0,0.0,0.0],\n            std=[1.0,1.0,1.0],\n            max_pixel_value=255.0\n        ),\n        ToTensorV2()\n    ]\n)\n\nall_transform={\n    \"train_transform\":train_transform,\n    \"valid_transform\":valid_transform\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.531601Z","iopub.execute_input":"2025-02-20T02:32:30.531830Z","iopub.status.idle":"2025-02-20T02:32:30.541115Z","shell.execute_reply.started":"2025-02-20T02:32:30.531810Z","shell.execute_reply":"2025-02-20T02:32:30.540265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(masks)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.542004Z","iopub.execute_input":"2025-02-20T02:32:30.542318Z","iopub.status.idle":"2025-02-20T02:32:30.556907Z","shell.execute_reply.started":"2025-02-20T02:32:30.542290Z","shell.execute_reply":"2025-02-20T02:32:30.556113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"randoms=np.random.choice(range(1, 5088), size=510, replace=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.557905Z","iopub.execute_input":"2025-02-20T02:32:30.558203Z","iopub.status.idle":"2025-02-20T02:32:30.572598Z","shell.execute_reply.started":"2025-02-20T02:32:30.558183Z","shell.execute_reply":"2025-02-20T02:32:30.572028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_dir=\"/kaggle/working/train_masks\"\ntrain_dir=\"/kaggle/working/train\"\nall_paths=os.listdir(train_dir)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.573352Z","iopub.execute_input":"2025-02-20T02:32:30.573627Z","iopub.status.idle":"2025-02-20T02:32:30.588837Z","shell.execute_reply.started":"2025-02-20T02:32:30.573601Z","shell.execute_reply":"2025-02-20T02:32:30.588198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_paths=[all_paths[idx] for idx in randoms]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.589860Z","iopub.execute_input":"2025-02-20T02:32:30.590169Z","iopub.status.idle":"2025-02-20T02:32:30.599590Z","shell.execute_reply.started":"2025-02-20T02:32:30.590141Z","shell.execute_reply":"2025-02-20T02:32:30.599008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_paths = list(set(all_paths) - set(valid_paths))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.600368Z","iopub.execute_input":"2025-02-20T02:32:30.600624Z","iopub.status.idle":"2025-02-20T02:32:30.613778Z","shell.execute_reply.started":"2025-02-20T02:32:30.600593Z","shell.execute_reply":"2025-02-20T02:32:30.613118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Caravana(Dataset):\n    def __init__(self, paths,x_dir, y_dir, transforms=None, is_train=True):\n        self.x_dir = x_dir\n        self.y_dir = y_dir\n        self.transforms = transforms\n        self.is_train = is_train\n        \n        # Get list of image paths\n        self.x_paths = paths\n     \n        \n    def __getitem__(self, index):\n        # Load image and mask\n        img_path=os.path.join(self.x_dir,self.x_paths[index])\n        mask_path=os.path.join(self.y_dir,self.x_paths[index].replace(\".jpg\",\"_mask.gif\"))\n        # Convert to NumPy arrays\n        img = np.array(Image.open(img_path).convert(\"RGB\"))        \n        target = np.array(Image.open(mask_path).convert('L'), dtype=np.float32)\n        \n        # Normalize mask values (255 -> 1.0)\n        target[target == 255.0] = 1.0\n        \n   \n       # print(img.shape,target.shape)\n        if self.transforms is not None:\n            if self.is_train:\n                transformed = self.transforms[\"train_transform\"](image=img, mask=target)\n            else:\n                transformed = self.transforms[\"valid_transform\"](image=img, mask=target)\n            \n            # Extract transformed image and mask\n            img = transformed[\"image\"]\n            target = transformed[\"mask\"]\n        \n        return img, torch.unsqueeze(target,dim=0)\n    \n    def __len__(self):\n        return len(self.x_paths)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.614418Z","iopub.execute_input":"2025-02-20T02:32:30.614614Z","iopub.status.idle":"2025-02-20T02:32:30.624090Z","shell.execute_reply.started":"2025-02-20T02:32:30.614598Z","shell.execute_reply":"2025-02-20T02:32:30.623254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset=Caravana(train_paths,train_dir,target_dir,all_transform)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.624831Z","iopub.execute_input":"2025-02-20T02:32:30.625021Z","iopub.status.idle":"2025-02-20T02:32:30.640229Z","shell.execute_reply.started":"2025-02-20T02:32:30.625004Z","shell.execute_reply":"2025-02-20T02:32:30.639505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loader=DataLoader(dataset,shuffle=True,batch_size=30)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.643574Z","iopub.execute_input":"2025-02-20T02:32:30.643793Z","iopub.status.idle":"2025-02-20T02:32:30.654412Z","shell.execute_reply.started":"2025-02-20T02:32:30.643768Z","shell.execute_reply":"2025-02-20T02:32:30.653626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_set=Caravana(valid_paths,train_dir,target_dir,all_transform,False)\nval_loader=DataLoader(val_set,batch_size=30)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.655601Z","iopub.execute_input":"2025-02-20T02:32:30.655848Z","iopub.status.idle":"2025-02-20T02:32:30.667932Z","shell.execute_reply.started":"2025-02-20T02:32:30.655829Z","shell.execute_reply":"2025-02-20T02:32:30.667116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x,y=dataset[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.668624Z","iopub.execute_input":"2025-02-20T02:32:30.668808Z","iopub.status.idle":"2025-02-20T02:32:30.770451Z","shell.execute_reply.started":"2025-02-20T02:32:30.668791Z","shell.execute_reply":"2025-02-20T02:32:30.769767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x.shape,y.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.771157Z","iopub.execute_input":"2025-02-20T02:32:30.771364Z","iopub.status.idle":"2025-02-20T02:32:30.776274Z","shell.execute_reply.started":"2025-02-20T02:32:30.771346Z","shell.execute_reply":"2025-02-20T02:32:30.775441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(x.permute(1,2,0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:30.777180Z","iopub.execute_input":"2025-02-20T02:32:30.777383Z","iopub.status.idle":"2025-02-20T02:32:31.031909Z","shell.execute_reply.started":"2025-02-20T02:32:30.777365Z","shell.execute_reply":"2025-02-20T02:32:31.031064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(y.permute(1,2,0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:31.032743Z","iopub.execute_input":"2025-02-20T02:32:31.032956Z","iopub.status.idle":"2025-02-20T02:32:31.205837Z","shell.execute_reply.started":"2025-02-20T02:32:31.032937Z","shell.execute_reply":"2025-02-20T02:32:31.205069Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n<div style=\"text-align: center;\">\n    <strong><span style=\"color: Black;\"><h2> Building the training loop and evaluation loop</h2></span></strong>\n</div>","metadata":{}},{"cell_type":"code","source":"model=UNET(in_channels=3,out_channels=1).to(device)\nloss = nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=1e-4)\nscaler=torch.cuda.amp.GradScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:31.206825Z","iopub.execute_input":"2025-02-20T02:32:31.207169Z","iopub.status.idle":"2025-02-20T02:32:31.640672Z","shell.execute_reply.started":"2025-02-20T02:32:31.207137Z","shell.execute_reply":"2025-02-20T02:32:31.639967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def check_accuracy(loader,model):\n    n_correct=0\n    n_pixels=0\n    dice_score=0\n    model.eval()\n    with torch.no_grad():\n        for x,y in loader:\n            x=x.to(device)\n            y=y.to(device)\n            preds=torch.sigmoid(model(x))\n            preds=(preds>0.5).float()\n            n_correct+=(preds==y).sum()\n            n_pixels+=torch.numel(preds)\n            \n           # print(x.shape,y.shape,preds.shape)\n\n            dice_score+=(2*(preds*y).sum())/((preds+y).sum())\n    print(\n        f\"Got {n_correct} out of {n_pixels} true with accuracy {n_correct/n_pixels*100:.2f} and the dice score is {dice_score/len(loader)}\"\n    )\n    model.train()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:31.641468Z","iopub.execute_input":"2025-02-20T02:32:31.641682Z","iopub.status.idle":"2025-02-20T02:32:31.648162Z","shell.execute_reply.started":"2025-02-20T02:32:31.641656Z","shell.execute_reply":"2025-02-20T02:32:31.647395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_iter(loader,model,optimizer,loss,scaler):\n    loop=tqdm(loader)\n    for idx,(data,mask) in enumerate(loop):\n        data=data.to(device)\n        mask=mask.float().to(device)\n        predictions=model(data)\n       # print(predictions.shape,mask.shape)\n        criterion=loss(predictions,mask)\n        \n        optimizer.zero_grad()\n        scaler.scale(criterion).backward()\n        scaler.step(optimizer)\n        scaler.update()\n\n        loop.set_postfix(loss=criterion.item())\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:31.648812Z","iopub.execute_input":"2025-02-20T02:32:31.649027Z","iopub.status.idle":"2025-02-20T02:32:31.666171Z","shell.execute_reply.started":"2025-02-20T02:32:31.648998Z","shell.execute_reply":"2025-02-20T02:32:31.665300Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_epochs(n_epochs):\n    for epoch in range(n_epochs):\n        train_iter(train_loader,model,optimizer,loss,scaler)\n        check_accuracy(val_loader,model)\n        \n\ntrain_epochs(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:32:31.666990Z","iopub.execute_input":"2025-02-20T02:32:31.667280Z","iopub.status.idle":"2025-02-20T02:54:25.431667Z","shell.execute_reply.started":"2025-02-20T02:32:31.667251Z","shell.execute_reply":"2025-02-20T02:54:25.430880Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_epochs(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T02:54:52.087720Z","iopub.execute_input":"2025-02-20T02:54:52.088122Z","iopub.status.idle":"2025-02-20T03:16:36.621080Z","shell.execute_reply.started":"2025-02-20T02:54:52.088088Z","shell.execute_reply":"2025-02-20T03:16:36.620133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test=next(iter(val_loader))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T03:17:10.219910Z","iopub.execute_input":"2025-02-20T03:17:10.220484Z","iopub.status.idle":"2025-02-20T03:17:11.097755Z","shell.execute_reply.started":"2025-02-20T03:17:10.220443Z","shell.execute_reply":"2025-02-20T03:17:11.096790Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x,y=test[0][:10],test[1][:10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T03:18:07.273640Z","iopub.execute_input":"2025-02-20T03:18:07.273967Z","iopub.status.idle":"2025-02-20T03:18:07.277911Z","shell.execute_reply.started":"2025-02-20T03:18:07.273939Z","shell.execute_reply":"2025-02-20T03:18:07.276920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=x.to(device)\npred=model(x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T03:19:24.303312Z","iopub.execute_input":"2025-02-20T03:19:24.303608Z","iopub.status.idle":"2025-02-20T03:19:24.332054Z","shell.execute_reply.started":"2025-02-20T03:19:24.303585Z","shell.execute_reply":"2025-02-20T03:19:24.331391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=x.to(\"cpu\")\npred=pred.to(\"cpu\")\npred=pred.detach()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T03:28:48.354364Z","iopub.execute_input":"2025-02-20T03:28:48.354646Z","iopub.status.idle":"2025-02-20T03:28:48.358743Z","shell.execute_reply.started":"2025-02-20T03:28:48.354624Z","shell.execute_reply":"2025-02-20T03:28:48.357967Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Displaying sample predictions","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig, ax = plt.subplots(nrows=5, ncols=3, figsize=(25, 25))\n\nfor i in range(5):\n    ax[i,0].imshow(x[i].permute(1, 2, 0))  \n    ax[i,0].set_title(\"Image\")\n    ax[i,0].axis(\"off\")  \n    \n    ax[i,1].imshow(pred[i].permute(1, 2, 0), cmap=\"gray\") \n    ax[i,1].set_title(\"prediction\")\n    ax[i,1].axis(\"off\")  \n    \n    ax[i,2].imshow(y[i].permute(1, 2, 0), cmap=\"gray\") \n    ax[i,2].set_title(\"Mask\")\n    ax[i,2].axis(\"off\")\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T03:29:11.665287Z","iopub.execute_input":"2025-02-20T03:29:11.665591Z","iopub.status.idle":"2025-02-20T03:29:13.571777Z","shell.execute_reply.started":"2025-02-20T03:29:11.665568Z","shell.execute_reply":"2025-02-20T03:29:13.570464Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"text-align: center;\">\n    <strong><span style=\"color: Black;\"><h2>Doaa Helal</h2></span></strong>\n</div>","metadata":{}}]}