{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch \nimport torchvision\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.transforms.functional as TF\n#-------------------------------------------\nimport os\nimport shutil\nimport zipfile\n#-------------------------------------------\nimport numpy as np\nfrom PIL import Image\nimport albumentations as A\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader\nfrom albumentations.pytorch import ToTensorV2\n#-------------------------------------------\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-14T13:46:33.506772Z","iopub.execute_input":"2023-10-14T13:46:33.507290Z","iopub.status.idle":"2023-10-14T13:46:42.954135Z","shell.execute_reply.started":"2023-10-14T13:46:33.507261Z","shell.execute_reply":"2023-10-14T13:46:42.953157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\" style=\"background-color: #1877f2; color: white; text-align: center;\">\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Объявление путей и работа с архивами</h1>\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Declaring paths and working with archives</h1>\n</div>\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:42:24.700499Z","iopub.execute_input":"2023-10-11T17:42:24.700955Z","iopub.status.idle":"2023-10-11T17:42:24.709911Z","shell.execute_reply.started":"2023-10-11T17:42:24.700923Z","shell.execute_reply":"2023-10-11T17:42:24.707788Z"}}},{"cell_type":"code","source":"path_to_data='/kaggle/input/carvana-image-masking-challenge/'","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:46:53.023220Z","iopub.execute_input":"2023-10-14T13:46:53.023674Z","iopub.status.idle":"2023-10-14T13:46:53.028190Z","shell.execute_reply.started":"2023-10-14T13:46:53.023647Z","shell.execute_reply":"2023-10-14T13:46:53.027195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths=os.listdir('/kaggle/input/carvana-image-masking-challenge')","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:46:53.918853Z","iopub.execute_input":"2023-10-14T13:46:53.919490Z","iopub.status.idle":"2023-10-14T13:46:53.924182Z","shell.execute_reply.started":"2023-10-14T13:46:53.919429Z","shell.execute_reply":"2023-10-14T13:46:53.923312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:46:55.051671Z","iopub.execute_input":"2023-10-14T13:46:55.051994Z","iopub.status.idle":"2023-10-14T13:46:55.058809Z","shell.execute_reply.started":"2023-10-14T13:46:55.051968Z","shell.execute_reply":"2023-10-14T13:46:55.057813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"need_list=[0,3,-3]","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:46:56.043034Z","iopub.execute_input":"2023-10-14T13:46:56.043689Z","iopub.status.idle":"2023-10-14T13:46:56.048316Z","shell.execute_reply.started":"2023-10-14T13:46:56.043659Z","shell.execute_reply":"2023-10-14T13:46:56.047198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_path='/kaggle/working/'\n\nfor indx in tqdm(need_list):\n    with zipfile.ZipFile(path_to_data+paths[indx], 'r') as zip_file:\n        zip_file.extractall(base_path)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:46:56.942777Z","iopub.execute_input":"2023-10-14T13:46:56.943381Z","iopub.status.idle":"2023-10-14T13:49:56.004316Z","shell.execute_reply.started":"2023-10-14T13:46:56.943352Z","shell.execute_reply":"2023-10-14T13:49:56.003421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nos.mkdir('/kaggle/working/saved_imgs')","metadata":{"execution":{"iopub.status.busy":"2023-10-14T14:12:52.633518Z","iopub.execute_input":"2023-10-14T14:12:52.633845Z","iopub.status.idle":"2023-10-14T14:12:52.638933Z","shell.execute_reply.started":"2023-10-14T14:12:52.633820Z","shell.execute_reply":"2023-10-14T14:12:52.637680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/val')\nos.mkdir('/kaggle/working/val_masks')\n\nfor file in sorted(os.listdir(base_path+'train'))[4600:]:\n  shutil.move('/kaggle/working/train/' + file, '/kaggle/working/val')\n\nfor file in sorted(os.listdir(base_path+'train_masks'))[4600:]:\n  shutil.move('/kaggle/working/train_masks/' + file, '/kaggle/working/val_masks')","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:49:58.655929Z","iopub.execute_input":"2023-10-14T13:49:58.656230Z","iopub.status.idle":"2023-10-14T13:49:58.720004Z","shell.execute_reply.started":"2023-10-14T13:49:58.656206Z","shell.execute_reply":"2023-10-14T13:49:58.719053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\" style=\"background-color: #1877f2; color: white; text-align: center;\">\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Создание функций для работы с моделью</h1>\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Creating functions to work with the model</h1>\n</div>\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:18:29.548325Z","iopub.execute_input":"2023-10-11T18:18:29.548758Z","iopub.status.idle":"2023-10-11T18:18:29.556775Z","shell.execute_reply.started":"2023-10-11T18:18:29.548729Z","shell.execute_reply":"2023-10-11T18:18:29.555286Z"}}},{"cell_type":"code","source":"def save_checkpoint(state,filename='model_ch_.pth.tar'):\n    print('====Saving checkpoint====')\n    torch.save(state,filename)\n    \n    \ndef load_checkpoint(checkpoint,model):\n    print('====Loading checkpoint====')\n    model.load_state_dict(checkpoint[\"state_dict\"])","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:50:01.039181Z","iopub.execute_input":"2023-10-14T13:50:01.039539Z","iopub.status.idle":"2023-10-14T13:50:01.044561Z","shell.execute_reply.started":"2023-10-14T13:50:01.039511Z","shell.execute_reply":"2023-10-14T13:50:01.043416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_loaders(Train_dir,\n                Train_mask_dir,\n                Val_img_dir,\n                Val_mask_dir,\n                Batch_size,\n                train_transform,\n                val_transform,\n                Num_workers,\n                PIN_MEMORY):\n    train_ds = CarvanaDataset(image_dir=Train_dir,\n                              mask_dir=Train_mask_dir,\n                              transform=train_transform)\n\n    train_loader = DataLoader(\n        train_ds,\n        batch_size=Batch_size,\n        num_workers=Num_workers,\n        pin_memory=PIN_MEMORY,\n        shuffle=True\n    )\n    Val_ds = CarvanaDataset(image_dir=Val_img_dir,\n                              mask_dir=Val_mask_dir,\n                              transform=val_transform)\n\n    val_loader = DataLoader(\n        Val_ds,\n        batch_size=Batch_size,\n        num_workers=Num_workers,\n        pin_memory=PIN_MEMORY,\n        shuffle=False\n    )\n    \n\n    return train_loader,val_loader\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:50:46.347062Z","iopub.execute_input":"2023-10-14T13:50:46.347608Z","iopub.status.idle":"2023-10-14T13:50:46.357860Z","shell.execute_reply.started":"2023-10-14T13:50:46.347570Z","shell.execute_reply":"2023-10-14T13:50:46.356687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_accuracy(loader, model, device=\"cuda\"):\n    num_correct=0\n    num_pixels=0\n    dice_score=0\n    model.eval()\n    \n    with torch.no_grad():\n        for x,y in loader:\n            x=x.to(device)\n            y=y.to(device)\n            \n            preds = torch.sigmoid(model(x))\n            preds=preds.squeeze(1)\n            #print(y.shape,\"yyyyy\")\n            #print(preds.shape)\n            preds = (preds>0.5).float()\n            num_correct +=(preds==y).sum()\n            \n            num_pixels += torch.numel(preds)\n            \n            dice_score += (2*(preds*y).sum())/(\n                (preds+y).sum()+1e-8\n            )\n            \n            \n    print(\n        f\"Получила {num_correct}/{num_pixels} с точностью {(num_correct/num_pixels)*100:.2f}\")\n    \n    print(f\"Dice_score {dice_score/len(loader)}\")\n    \n    \n    model.train()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-14T15:17:28.225381Z","iopub.execute_input":"2023-10-14T15:17:28.226047Z","iopub.status.idle":"2023-10-14T15:17:28.233015Z","shell.execute_reply.started":"2023-10-14T15:17:28.226018Z","shell.execute_reply":"2023-10-14T15:17:28.231840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_prediction_as_imgs(\n    loader,\n    model,\n    folder='/kaggle/working/',\n    device='cuda'\n):\n    model.eval()\n    \n    for idx, (x,y) in enumerate(loader):\n        x=x.to(device)\n        with torch.no_grad():\n            preds = torch.sigmoid(model(x))\n            preds = (preds>0.5).float()\n            \n        torchvision.utils.save_image(\n            preds,f\"{folder}/pred_{idx}.png\"\n        )\n        \n        torchvision.utils.save_image(y.unsqueeze(1),f\"{folder}/pred_{idx}.png\")\n    \n    \n    model.train()\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-14T14:20:32.118015Z","iopub.execute_input":"2023-10-14T14:20:32.118389Z","iopub.status.idle":"2023-10-14T14:20:32.125201Z","shell.execute_reply.started":"2023-10-14T14:20:32.118361Z","shell.execute_reply":"2023-10-14T14:20:32.124268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\" style=\"background-color: #1877f2; color: white; text-align: center;\">\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Создание датасета</h1>\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Creating a dataset</h1>\n</div>\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:42:39.518833Z","iopub.execute_input":"2023-10-11T17:42:39.519278Z","iopub.status.idle":"2023-10-11T17:42:39.527968Z","shell.execute_reply.started":"2023-10-11T17:42:39.519248Z","shell.execute_reply":"2023-10-11T17:42:39.526063Z"}}},{"cell_type":"code","source":"Learn_rate=1e-4\nDevice='cuda' if torch.cuda.is_available() else 'cpu'\nBatch_size=16\nNum_epoch=15\nNum_workers=2\nIMG_Height=320 #1280\nIMG_Width=480#1918\nPIN_MEMORY=True\nLoad_MOdel=False\nTrain_dir='/kaggle/working/train'\nTrain_mask_dir='/kaggle/working/train_masks'\nVal_dir='/kaggle/working/val'\nVasl_mask_dir='/kaggle/working/val_masks'\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T14:57:47.281768Z","iopub.execute_input":"2023-10-14T14:57:47.282170Z","iopub.status.idle":"2023-10-14T14:57:47.289080Z","shell.execute_reply.started":"2023-10-14T14:57:47.282139Z","shell.execute_reply":"2023-10-14T14:57:47.287807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform=A.Compose(\n    [\n        A.Resize(height=IMG_Height,width=IMG_Width),\n        A.Rotate(limit=35,p=1.0),\n        A.HorizontalFlip(p=0.5),\n        A.VerticalFlip(p=0.1),\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        \n        \n    ]\n)\n\nval_transform=A.Compose(\n    [\n        A.Resize(height=IMG_Height,width=IMG_Width),\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)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:50:55.044055Z","iopub.execute_input":"2023-10-14T13:50:55.045135Z","iopub.status.idle":"2023-10-14T13:50:55.052308Z","shell.execute_reply.started":"2023-10-14T13:50:55.045097Z","shell.execute_reply":"2023-10-14T13:50:55.051340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CarvanaDataset(Dataset):\n    def __init__(self, image_dir,mask_dir,transform=None):\n        self.image_dir=image_dir\n        self.mask_dir=mask_dir\n        self.transform=transform\n        \n        self.images=os.listdir(image_dir)\n#==================================================================================   \n       \n        \n    def __len__(self):\n        return len(self.images)\n    \n#==================================================================================   \n    def __getitem__(self,indx):\n        \n        img_path=os.path.join(self.image_dir,self.images[indx])\n        mask_path=os.path.join(self.mask_dir,self.images[indx].replace('.jpg','_mask.gif'))\n        \n        \n        image=np.array(Image.open(img_path).convert('RGB'))\n        \n        mask=np.array(Image.open(mask_path).convert('L'),dtype=np.float32)\n        \n        #mask[mask_path==255.0]=1.0\n        mask=mask/255.0\n        #self.transform is not None\n        if self.transform:\n            augmentations=self.transform(image=image,mask=mask)\n            image=augmentations['image']\n            \n            mask=augmentations['mask']\n            \n            \n        return image,mask\n            \n            \n        ","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:50:58.205139Z","iopub.execute_input":"2023-10-14T13:50:58.205497Z","iopub.status.idle":"2023-10-14T13:50:58.212465Z","shell.execute_reply.started":"2023-10-14T13:50:58.205463Z","shell.execute_reply":"2023-10-14T13:50:58.211368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndataset=CarvanaDataset(\"/kaggle/working/val\",\"/kaggle/working/val_masks\")\n\n# Получение тензора из датасета\nimage, label = dataset[1]  # Предположим, что у вас есть датасет с именем dataset и индексом idx\n\n# Преобразование тензора обратно в изображение\nimage = TF.to_pil_image(image)\n\n# Отображение изображения\nplt.imshow(image)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T15:14:32.564376Z","iopub.execute_input":"2023-10-14T15:14:32.564752Z","iopub.status.idle":"2023-10-14T15:14:33.054325Z","shell.execute_reply.started":"2023-10-14T15:14:32.564725Z","shell.execute_reply":"2023-10-14T15:14:33.053451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\" style=\"background-color: #1877f2; color: white; text-align: center;\">\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Создание модели</h1>\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Creating a Model</h1>\n</div>\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:39:49.069727Z","iopub.execute_input":"2023-10-11T17:39:49.070400Z","iopub.status.idle":"2023-10-11T17:39:49.078871Z","shell.execute_reply.started":"2023-10-11T17:39:49.070367Z","shell.execute_reply":"2023-10-11T17:39:49.077078Z"}}},{"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,3,1,1,bias=False),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(out_channels,out_channels,3,1,1,bias=False),\n            nn.BatchNorm2d(out_channels),\n            nn.ReLU(inplace=True)\n\n        )\n    def forward(self,x):\n        return self.conv(x)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:50:59.975323Z","iopub.execute_input":"2023-10-14T13:50:59.975981Z","iopub.status.idle":"2023-10-14T13:50:59.981607Z","shell.execute_reply.started":"2023-10-14T13:50:59.975950Z","shell.execute_reply":"2023-10-14T13:50:59.980567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNET(nn.Module):\n    def __init__(self,\n                 in_channels=3,\n                 out_channels=1,\n                features=[64,128,256,512]):\n        \n        super(UNET,self).__init__()\n        self.uppers=nn.ModuleList()\n        self.downs=nn.ModuleList()\n        self.pool=nn.MaxPool2d(kernel_size=2,stride=2)\n        \n        #Нижняя часть сети UNET\n        \n        for feature in features:\n            self.downs.append(ConvBlock(in_channels,\n                                       feature))\n            \n            in_channels=feature\n            \n            \n        #Верх сетки UNET\n        for feature in reversed(features):\n            self.uppers.append(\n                nn.ConvTranspose2d(feature*2,\n                                   feature,\n                                   kernel_size=2,\n                                   stride=2\n                                  )\n            )\n            self.uppers.append(ConvBlock(feature*2,\n                                       feature))\n            \n            \n        self.bottleneck=ConvBlock(features[-1],\n                                       features[-1]*2)\n        \n        \n        \n        self.last_conv=nn.Conv2d(features[0],out_channels,kernel_size=1)\n    \n    #=====================================================================================\n    def forward(self,x):\n        \n        skip_connections=[]\n        \n        for down in self.downs:\n            x= down(x)\n            skip_connections.append(x)\n            x= self.pool(x)\n            \n        x= self.bottleneck(x)\n        skip_connections= skip_connections[::-1]\n        for idx in range(0,len(self.uppers),2):\n            \n            x=self.uppers[idx](x)\n            skip_connection= skip_connections[idx//2]\n            \n            if x.shape != skip_connection.shape:\n                x= TF.resize(x,size=skip_connection[2:])\n            \n            concat_x= torch.cat((skip_connection,x),dim=1)\n            x= self.uppers[idx+1](concat_x)\n            \n            \n        return self.last_conv(x)\n        \n            \n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:51:01.225158Z","iopub.execute_input":"2023-10-14T13:51:01.225817Z","iopub.status.idle":"2023-10-14T13:51:01.235731Z","shell.execute_reply.started":"2023-10-14T13:51:01.225786Z","shell.execute_reply":"2023-10-14T13:51:01.234508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test():\n    x=torch.randn((3,1,224,224))\n    model=UNET(  in_channels=1,\n                 out_channels=1)\n    preds=model(x)\n    print(preds.shape)\n    print(x.shape)\n    \n    assert preds.shape==x.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:51:02.422376Z","iopub.execute_input":"2023-10-14T13:51:02.422741Z","iopub.status.idle":"2023-10-14T13:51:02.427670Z","shell.execute_reply.started":"2023-10-14T13:51:02.422715Z","shell.execute_reply":"2023-10-14T13:51:02.426614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T17:44:00.080906Z","iopub.execute_input":"2023-10-12T17:44:00.082150Z","iopub.status.idle":"2023-10-12T17:44:05.906032Z","shell.execute_reply.started":"2023-10-12T17:44:00.082102Z","shell.execute_reply":"2023-10-12T17:44:05.904846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\" style=\"background-color: #1877f2; color: white; text-align: center;\">\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Обучение на данных</h1>\n    <h1 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: inherit;\">Learning from data</h1>\n</div>\n","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:51:01.026952Z","iopub.execute_input":"2023-10-11T17:51:01.027354Z","iopub.status.idle":"2023-10-11T17:51:01.032931Z","shell.execute_reply.started":"2023-10-11T17:51:01.027327Z","shell.execute_reply":"2023-10-11T17:51:01.032020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_(loader,model,optimizer,loss_fn,scaler):\n    loop=tqdm(loader)\n    \n    for batch_indx,(data,targets) in enumerate(loop):\n        data=data.to(device=Device)\n        targets=targets.float().unsqueeze(1).to(device=Device)\n        \n        \n        \n        #train\n        with torch.cuda.amp.autocast():\n            \n            pred=model(data)\n            loss=loss_fn(pred,targets)\n            \n            \n        \n        optimizer.zero_grad()\n        scaler.scale(loss).backward()\n        scaler.step(optimizer)\n        \n        scaler.update()\n        \n        loop.set_postfix(loss=loss.item())\n        \n        \n        \n        \n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-14T13:53:40.717802Z","iopub.execute_input":"2023-10-14T13:53:40.718156Z","iopub.status.idle":"2023-10-14T13:53:40.725538Z","shell.execute_reply.started":"2023-10-14T13:53:40.718125Z","shell.execute_reply":"2023-10-14T13:53:40.724282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-info\" style=\"background-color: #cce5ff; color: #007bff; text-align: center; border: 1px solid #007bff; border-radius: 5px; padding: 15px;\">\n    <h2 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-weight: bold; color: #007bff;\">Оптимизация Работы Видеокарты</h2>\n    <p style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 16px;\">В данной работе особое внимание уделяется использованию scaler для оптимизации работы видеокарты. Эта возможность играет ключевую роль при переносе весов модели на ПЛИС и может оказаться весьма полезной в контексте оптимизации производительности.</p>\n    <h2 style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-weight: bold; color: #007bff; margin-top: 20px;\">Optimizing GPU Performance</h2>\n    <p style=\"font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 16px;\">A crucial aspect of this work is the utilization of scaler to optimize GPU performance. This feature becomes especially valuable when transferring model weights to FPGA, offering significant advantages in performance optimization.</p>\n</div>","metadata":{}},{"cell_type":"code","source":"model=UNET(in_channels=3,out_channels=1).to(device=Device)\nloss_fn=nn.BCEWithLogitsLoss()\noptimizer=optim.Adam(model.parameters(),lr=Learn_rate)\n\ntrain_loader,val_loader=get_loaders(\n    Train_dir,\n    Train_mask_dir,\n    Val_dir,\n    Vasl_mask_dir,    \n    Batch_size,\n    train_transform,\n    val_transform,\n    Num_workers,\n    PIN_MEMORY\n    \n    \n)\n\n\nscaler=torch.cuda.amp.GradScaler()\n\nfor epoch in range(Num_epoch):\n    train_(train_loader,model,optimizer,loss_fn,scaler)\n    #save model\n    checkpoint={\n        \"state_dict\":model.state_dict(),\n        \"optimizer\":optimizer.state_dict(),\n        \n    }\n    \n    save_checkpoint(checkpoint)\n    #check acc\n    check_accuracy(val_loader,model,device=Device)\n    #print some exmpls\n    save_prediction_as_imgs(val_loader,model,folder=\"saved_imgs/\",device=Device)\n    \n    \n    \n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T15:17:33.132537Z","iopub.execute_input":"2023-10-14T15:17:33.132863Z","iopub.status.idle":"2023-10-14T15:59:48.467926Z","shell.execute_reply.started":"2023-10-14T15:17:33.132838Z","shell.execute_reply":"2023-10-14T15:59:48.466707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" torch.save(model.state_dict(), f\"pytorch_model-e{Num_epoch}.pth\")","metadata":{"execution":{"iopub.status.busy":"2023-10-14T16:09:25.516543Z","iopub.execute_input":"2023-10-14T16:09:25.517759Z","iopub.status.idle":"2023-10-14T16:09:25.804535Z","shell.execute_reply.started":"2023-10-14T16:09:25.517702Z","shell.execute_reply":"2023-10-14T16:09:25.803324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Visual_testing_datas=CarvanaDataset(\"/kaggle/working/val\",\"/kaggle/working/val_masks\",transform=val_transform)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T16:20:30.063280Z","iopub.execute_input":"2023-10-14T16:20:30.063646Z","iopub.status.idle":"2023-10-14T16:20:30.069499Z","shell.execute_reply.started":"2023-10-14T16:20:30.063617Z","shell.execute_reply":"2023-10-14T16:20:30.068227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Visual_testing_datas[0][0].shape","metadata":{"execution":{"iopub.status.busy":"2023-10-14T16:20:42.066978Z","iopub.execute_input":"2023-10-14T16:20:42.067363Z","iopub.status.idle":"2023-10-14T16:20:42.131582Z","shell.execute_reply.started":"2023-10-14T16:20:42.067334Z","shell.execute_reply":"2023-10-14T16:20:42.130714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nindex = os.listdir('/kaggle/working/val')\nimage_i = random.choice(index)\n\nnew_image_path ='/kaggle/working/val/'+ image_i\nnew_image = np.array(Image.open(new_image_path).convert('RGB'))\nprint(new_image_path)\n\n\n\ninput_image = val_transform(image=new_image) # C альбументацией преобразовываем изображение\n\n# Добавил размерность, чтобы был батч\ninput_image = input_image['image'].unsqueeze(0) .to(Device)\n\nwith torch.no_grad():\n    predicted_mask = model(input_image)\n\n#Использование пороговой функции\nthreshold = 0.5\npredicted_mask_binary = (predicted_mask > threshold).float()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T17:03:11.691056Z","iopub.execute_input":"2023-10-14T17:03:11.691568Z","iopub.status.idle":"2023-10-14T17:03:11.723266Z","shell.execute_reply.started":"2023-10-14T17:03:11.691522Z","shell.execute_reply":"2023-10-14T17:03:11.722220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predicted_mask_binary.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T17:03:33.632012Z","iopub.execute_input":"2023-10-14T17:03:33.633089Z","iopub.status.idle":"2023-10-14T17:03:33.638373Z","shell.execute_reply.started":"2023-10-14T17:03:33.633048Z","shell.execute_reply":"2023-10-14T17:03:33.637410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_mask_binary_np=np.array(predicted_mask_binary.squeeze(0).cpu())\npredicted_mask_binary_np=predicted_mask_binary_np.squeeze(0)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T17:05:15.047490Z","iopub.execute_input":"2023-10-14T17:05:15.047832Z","iopub.status.idle":"2023-10-14T17:05:15.053504Z","shell.execute_reply.started":"2023-10-14T17:05:15.047806Z","shell.execute_reply":"2023-10-14T17:05:15.052410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predicted_mask_binary_np.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T17:05:21.025862Z","iopub.execute_input":"2023-10-14T17:05:21.026208Z","iopub.status.idle":"2023-10-14T17:05:21.031510Z","shell.execute_reply.started":"2023-10-14T17:05:21.026179Z","shell.execute_reply":"2023-10-14T17:05:21.030463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_mask_array_3ch = np.repeat(predicted_mask_binary_np[:, :, np.newaxis], 3, axis=2)\npredicted_mask_tensor = torch.tensor(predicted_mask_array_3ch).permute(2, 0, 1)\ninput_image=input_image.cpu().squeeze(0)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T17:05:39.200147Z","iopub.execute_input":"2023-10-14T17:05:39.200535Z","iopub.status.idle":"2023-10-14T17:05:39.207010Z","shell.execute_reply.started":"2023-10-14T17:05:39.200504Z","shell.execute_reply":"2023-10-14T17:05:39.205589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predicted_mask_tensor.shape)\nprint(input_image.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T17:05:44.216102Z","iopub.execute_input":"2023-10-14T17:05:44.216538Z","iopub.status.idle":"2023-10-14T17:05:44.221845Z","shell.execute_reply.started":"2023-10-14T17:05:44.216497Z","shell.execute_reply":"2023-10-14T17:05:44.220691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmented_image = ((input_image + (predicted_mask_tensor * 0.5)).permute(1, 2, 0)).numpy()\npredicted_mask_tensor=predicted_mask_tensor.permute(1, 2, 0).numpy()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T17:06:15.403883Z","iopub.execute_input":"2023-10-14T17:06:15.404231Z","iopub.status.idle":"2023-10-14T17:06:15.409856Z","shell.execute_reply.started":"2023-10-14T17:06:15.404196Z","shell.execute_reply":"2023-10-14T17:06:15.408909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Визуализация оригинального изображения и сегментированного изображения\nfig, axes = plt.subplots(1, 2, figsize=(12, 6))\naxes[0].imshow(predicted_mask_tensor)\naxes[0].set_title('Оригинальное изображение/original')\naxes[0].axis('off')\n\naxes[1].imshow(segmented_image)\naxes[1].set_title('Сегментированное изображение/original with mask')\naxes[1].axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T17:06:41.411972Z","iopub.execute_input":"2023-10-14T17:06:41.412309Z","iopub.status.idle":"2023-10-14T17:06:41.755597Z","shell.execute_reply.started":"2023-10-14T17:06:41.412281Z","shell.execute_reply":"2023-10-14T17:06:41.754674Z"},"trusted":true},"execution_count":null,"outputs":[]}]}