{"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":"markdown","source":"# Image Segmentation\nAn image is a collection or set of different pixels. We group together the pixels that have similar attributes using image segmentation.. Thus, the task of image segmentation is to train a neural network to output a pixel-wise mask of the image. This helps in understanding the image at a much lower level, i.e., the pixel level.In image segmetation each pixel is given a label.","metadata":{}},{"cell_type":"markdown","source":"Before diving into code, first we will need to import all the required libraries","metadata":{}},{"cell_type":"markdown","source":"# Import libraries","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np \nimport pandas as pd \nimport torch\nimport torchvision\nfrom torch.utils.data import Dataset, DataLoader\nimport torch.nn as nn\nimport torchvision.transforms as transforms\nfrom zipfile import ZipFile \nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport os\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:03:15.437954Z","iopub.execute_input":"2021-07-05T18:03:15.438285Z","iopub.status.idle":"2021-07-05T18:03:18.689134Z","shell.execute_reply.started":"2021-07-05T18:03:15.438256Z","shell.execute_reply":"2021-07-05T18:03:18.688272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset Preparation","metadata":{}},{"cell_type":"code","source":"pip install segmentation-models-pytorch","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:03:22.482618Z","iopub.execute_input":"2021-07-05T18:03:22.482962Z","iopub.status.idle":"2021-07-05T18:03:38.253483Z","shell.execute_reply.started":"2021-07-05T18:03:22.482931Z","shell.execute_reply":"2021-07-05T18:03:38.252623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's unzip all the files","metadata":{}},{"cell_type":"code","source":"train_zip = \"/kaggle/input/carvana-image-masking-challenge/train.zip\"\nwith ZipFile(train_zip, 'r') as zip_: \n    zip_.extractall('/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:03:38.255261Z","iopub.execute_input":"2021-07-05T18:03:38.255634Z","iopub.status.idle":"2021-07-05T18:03:48.923880Z","shell.execute_reply.started":"2021-07-05T18:03:38.255592Z","shell.execute_reply":"2021-07-05T18:03:48.923020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_mask_zip = \"/kaggle/input/carvana-image-masking-challenge/train_masks.zip\"\nwith ZipFile(train_mask_zip, 'r') as zip_: \n    zip_.extractall('/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:03:50.178098Z","iopub.execute_input":"2021-07-05T18:03:50.178414Z","iopub.status.idle":"2021-07-05T18:03:51.025704Z","shell.execute_reply.started":"2021-07-05T18:03:50.178386Z","shell.execute_reply":"2021-07-05T18:03:51.024877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train set:  \", len(os.listdir(\"/kaggle/working/train\")))\nprint(\"Train masks:\", len(os.listdir(\"/kaggle/working/train_masks\")))","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:03:55.472102Z","iopub.execute_input":"2021-07-05T18:03:55.472423Z","iopub.status.idle":"2021-07-05T18:03:55.484694Z","shell.execute_reply.started":"2021-07-05T18:03:55.472392Z","shell.execute_reply":"2021-07-05T18:03:55.483632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"car_ids = []\npaths = []\nfor dirname, _, filenames in os.walk('/kaggle/working/train'):\n    for filename in filenames:\n        path = os.path.join(dirname, filename)    \n        paths.append(path)\n        \n        car_id = filename.split(\".\")[0]\n        car_ids.append(car_id)\n\nd = {\"id\": car_ids, \"car_path\": paths}\ndf = pd.DataFrame(data = d)\ndf = df.set_index('id')\ndf","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:03:59.235857Z","iopub.execute_input":"2021-07-05T18:03:59.236187Z","iopub.status.idle":"2021-07-05T18:03:59.285470Z","shell.execute_reply.started":"2021-07-05T18:03:59.236157Z","shell.execute_reply":"2021-07-05T18:03:59.284408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.iloc[0, :]['car_path']","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:04.218892Z","iopub.execute_input":"2021-07-05T18:04:04.219215Z","iopub.status.idle":"2021-07-05T18:04:04.224912Z","shell.execute_reply.started":"2021-07-05T18:04:04.219187Z","shell.execute_reply":"2021-07-05T18:04:04.224009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"car_ids = []\nmask_path = []\nfor dirname, _, filenames in os.walk('/kaggle/working/train_masks'):\n    for filename in filenames:\n        path = os.path.join(dirname, filename)\n        mask_path.append(path)\n        \n        car_id = filename.split(\".\")[0]\n        car_id = car_id.split(\"_mask\")[0]\n        car_ids.append(car_id)\n\n        \nd = {\"id\": car_ids,\"mask_path\": mask_path}\nmask_df = pd.DataFrame(data = d)\nmask_df = mask_df.set_index('id')\nmask_df","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:07.084563Z","iopub.execute_input":"2021-07-05T18:04:07.084976Z","iopub.status.idle":"2021-07-05T18:04:07.136680Z","shell.execute_reply.started":"2021-07-05T18:04:07.084939Z","shell.execute_reply":"2021-07-05T18:04:07.134336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"mask_path\"] = mask_df[\"mask_path\"]\ndf","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:12.787172Z","iopub.execute_input":"2021-07-05T18:04:12.787483Z","iopub.status.idle":"2021-07-05T18:04:12.804642Z","shell.execute_reply.started":"2021-07-05T18:04:12.787456Z","shell.execute_reply":"2021-07-05T18:04:12.803616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.iloc[0, :]","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:16.593466Z","iopub.execute_input":"2021-07-05T18:04:16.593822Z","iopub.status.idle":"2021-07-05T18:04:16.600713Z","shell.execute_reply.started":"2021-07-05T18:04:16.593779Z","shell.execute_reply":"2021-07-05T18:04:16.599750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, val_df = train_test_split(df, test_size=0.25, shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:20.362441Z","iopub.execute_input":"2021-07-05T18:04:20.362773Z","iopub.status.idle":"2021-07-05T18:04:20.370545Z","shell.execute_reply.started":"2021-07-05T18:04:20.362743Z","shell.execute_reply":"2021-07-05T18:04:20.369570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"dataset preparation","metadata":{}},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self, df, transform = None):\n        super(CustomDataset, self).__init__()\n        self.df = df\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        car_path = self.df.iloc[idx, :]['car_path']\n        mask_path = self.df.iloc[idx, :]['mask_path']\n        car = np.array(Image.open(car_path).convert('RGB'))\n        mask = np.array(Image.open(mask_path).convert('L'))\n        if self.transform:\n            transformed = self.transform(image=np.array(car), mask=np.array(mask))\n            car = transformed['image']\n            mask = transformed['mask']\n        car = transforms.ToTensor()(car)\n        mask = transforms.ToTensor()(mask)\n        return car, mask","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:25.602706Z","iopub.execute_input":"2021-07-05T18:04:25.603050Z","iopub.status.idle":"2021-07-05T18:04:25.612192Z","shell.execute_reply.started":"2021-07-05T18:04:25.603020Z","shell.execute_reply":"2021-07-05T18:04:25.611142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose([\n    A.Resize(512, 512, p=1)\n])","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:29.268425Z","iopub.execute_input":"2021-07-05T18:04:29.268854Z","iopub.status.idle":"2021-07-05T18:04:29.276755Z","shell.execute_reply.started":"2021-07-05T18:04:29.268803Z","shell.execute_reply":"2021-07-05T18:04:29.275861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = CustomDataset(train_df, transform)\nval_dataset = CustomDataset(val_df, transform)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:32.330502Z","iopub.execute_input":"2021-07-05T18:04:32.330881Z","iopub.status.idle":"2021-07-05T18:04:32.335191Z","shell.execute_reply.started":"2021-07-05T18:04:32.330846Z","shell.execute_reply":"2021-07-05T18:04:32.333965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(train_dataset, batch_size=4, shuffle=True, num_workers=4, pin_memory=True )\nval_dataloader = DataLoader(val_dataset, batch_size=4, shuffle=False, num_workers=4, pin_memory=True )","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:35.313892Z","iopub.execute_input":"2021-07-05T18:04:35.314211Z","iopub.status.idle":"2021-07-05T18:04:35.319204Z","shell.execute_reply.started":"2021-07-05T18:04:35.314184Z","shell.execute_reply":"2021-07-05T18:04:35.318084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataiter = iter(val_dataloader)\ncars, masks = next(dataiter)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:04:54.107972Z","iopub.execute_input":"2021-07-05T18:04:54.108291Z","iopub.status.idle":"2021-07-05T18:04:59.461414Z","shell.execute_reply.started":"2021-07-05T18:04:54.108262Z","shell.execute_reply":"2021-07-05T18:04:59.459669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cars.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:05:03.565677Z","iopub.execute_input":"2021-07-05T18:05:03.566090Z","iopub.status.idle":"2021-07-05T18:05:03.573143Z","shell.execute_reply.started":"2021-07-05T18:05:03.566037Z","shell.execute_reply":"2021-07-05T18:05:03.572281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:05:06.179031Z","iopub.execute_input":"2021-07-05T18:05:06.179342Z","iopub.status.idle":"2021-07-05T18:05:06.184520Z","shell.execute_reply.started":"2021-07-05T18:05:06.179314Z","shell.execute_reply":"2021-07-05T18:05:06.183554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.unique(masks)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:05:14.995148Z","iopub.execute_input":"2021-07-05T18:05:14.995466Z","iopub.status.idle":"2021-07-05T18:05:15.048342Z","shell.execute_reply.started":"2021-07-05T18:05:14.995436Z","shell.execute_reply":"2021-07-05T18:05:15.047360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cars","metadata":{"execution":{"iopub.status.busy":"2021-07-05T17:01:13.759991Z","iopub.execute_input":"2021-07-05T17:01:13.76034Z","iopub.status.idle":"2021-07-05T17:01:13.778583Z","shell.execute_reply.started":"2021-07-05T17:01:13.760293Z","shell.execute_reply":"2021-07-05T17:01:13.777809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks","metadata":{"execution":{"iopub.status.busy":"2021-07-05T17:00:58.549557Z","iopub.execute_input":"2021-07-05T17:00:58.549887Z","iopub.status.idle":"2021-07-05T17:00:58.560229Z","shell.execute_reply.started":"2021-07-05T17:00:58.549859Z","shell.execute_reply":"2021-07-05T17:00:58.55938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"car, mask = train_dataset[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:05:35.572886Z","iopub.execute_input":"2021-07-05T18:05:35.573215Z","iopub.status.idle":"2021-07-05T18:05:35.635536Z","shell.execute_reply.started":"2021-07-05T18:05:35.573185Z","shell.execute_reply":"2021-07-05T18:05:35.634653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"car.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:05:38.522907Z","iopub.execute_input":"2021-07-05T18:05:38.523281Z","iopub.status.idle":"2021-07-05T18:05:38.530989Z","shell.execute_reply.started":"2021-07-05T18:05:38.523249Z","shell.execute_reply":"2021-07-05T18:05:38.529922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.unique(mask)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:05:41.707464Z","iopub.execute_input":"2021-07-05T18:05:41.707800Z","iopub.status.idle":"2021-07-05T18:05:41.718675Z","shell.execute_reply.started":"2021-07-05T18:05:41.707769Z","shell.execute_reply":"2021-07-05T18:05:41.717534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask.dtype\ncar.dtype","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:05:45.437927Z","iopub.execute_input":"2021-07-05T18:05:45.438255Z","iopub.status.idle":"2021-07-05T18:05:45.443186Z","shell.execute_reply.started":"2021-07-05T18:05:45.438225Z","shell.execute_reply":"2021-07-05T18:05:45.442212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we will split the dataset into train and test","metadata":{}},{"cell_type":"code","source":"len(train_df)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:05:51.858366Z","iopub.execute_input":"2021-07-05T18:05:51.858682Z","iopub.status.idle":"2021-07-05T18:05:51.863833Z","shell.execute_reply.started":"2021-07-05T18:05:51.858650Z","shell.execute_reply":"2021-07-05T18:05:51.862821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(val_df)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:06:00.296355Z","iopub.execute_input":"2021-07-05T18:06:00.296683Z","iopub.status.idle":"2021-07-05T18:06:00.303878Z","shell.execute_reply.started":"2021-07-05T18:06:00.296652Z","shell.execute_reply":"2021-07-05T18:06:00.302978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's look the image and it's corresponding mask","metadata":{}},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"markdown","source":"We are going to use U-Net model. A U-Net consists of an encoder (downsampler) and decoder (upsampler). In-order to learn robust features, and reduce the number of trainable parameters, a pretrained model can be used as the encoder.The encoder will be a pretrained MobileNetV2 model which is prepared and ready to use in tf.keras.applications. ","metadata":{}},{"cell_type":"code","source":"import segmentation_models_pytorch as smp","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:06:26.275495Z","iopub.execute_input":"2021-07-05T18:06:26.275806Z","iopub.status.idle":"2021-07-05T18:06:28.127287Z","shell.execute_reply.started":"2021-07-05T18:06:26.275776Z","shell.execute_reply":"2021-07-05T18:06:28.126357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = smp.Unet(encoder_name=\"resnet34\", encoder_weights=\"imagenet\", in_channels=3, classes = 1)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:06:31.787793Z","iopub.execute_input":"2021-07-05T18:06:31.788172Z","iopub.status.idle":"2021-07-05T18:06:33.128617Z","shell.execute_reply.started":"2021-07-05T18:06:31.788140Z","shell.execute_reply":"2021-07-05T18:06:33.127789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:06:35.714739Z","iopub.execute_input":"2021-07-05T18:06:35.715074Z","iopub.status.idle":"2021-07-05T18:06:35.722675Z","shell.execute_reply.started":"2021-07-05T18:06:35.715042Z","shell.execute_reply":"2021-07-05T18:06:35.721868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.segmentation_head","metadata":{"execution":{"iopub.status.busy":"2021-07-05T17:17:28.832075Z","iopub.execute_input":"2021-07-05T17:17:28.832426Z","iopub.status.idle":"2021-07-05T17:17:28.837247Z","shell.execute_reply.started":"2021-07-05T17:17:28.832393Z","shell.execute_reply":"2021-07-05T17:17:28.836483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install torchsummary","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:06:53.290657Z","iopub.execute_input":"2021-07-05T18:06:53.291006Z","iopub.status.idle":"2021-07-05T18:07:07.613528Z","shell.execute_reply.started":"2021-07-05T18:06:53.290975Z","shell.execute_reply":"2021-07-05T18:07:07.612583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchsummary import summary","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:07:09.901111Z","iopub.execute_input":"2021-07-05T18:07:09.901434Z","iopub.status.idle":"2021-07-05T18:07:09.910143Z","shell.execute_reply.started":"2021-07-05T18:07:09.901403Z","shell.execute_reply":"2021-07-05T18:07:09.909235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(model, (3, 512, 512), 1,'cpu')","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:07:13.381004Z","iopub.execute_input":"2021-07-05T18:07:13.381318Z","iopub.status.idle":"2021-07-05T18:07:16.147398Z","shell.execute_reply.started":"2021-07-05T18:07:13.381289Z","shell.execute_reply":"2021-07-05T18:07:16.146602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = torch.rand(1, 3, 512, 512)\nmodel(d).shape","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:07:35.606714Z","iopub.execute_input":"2021-07-05T18:07:35.607071Z","iopub.status.idle":"2021-07-05T18:07:36.785976Z","shell.execute_reply.started":"2021-07-05T18:07:35.607034Z","shell.execute_reply":"2021-07-05T18:07:36.785179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the Model","metadata":{}},{"cell_type":"markdown","source":"Now let's compile the model and see the model architecture","metadata":{}},{"cell_type":"code","source":"def dice_coef(y_true, y_pred, smooth=1):\n    intersection = K.sum(y_true * y_pred, axis=[1,2,3])\n    union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3])\n    return K.mean( (2. * intersection + smooth) / (union + smooth), axis=0)\n\ndef dice_loss(in_gt, in_pred):\n    return 1-dice_coef(in_gt, in_pred)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:07:41.812406Z","iopub.execute_input":"2021-07-05T18:07:41.812727Z","iopub.status.idle":"2021-07-05T18:07:41.818205Z","shell.execute_reply.started":"2021-07-05T18:07:41.812696Z","shell.execute_reply":"2021-07-05T18:07:41.817340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's try out the model to see what it predicts before training.","metadata":{}},{"cell_type":"code","source":"criterion= nn.BCEWithLogitsLoss()\noptimizer= torch.optim.Adam(model.parameters(),lr=1e-3)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:07:45.274378Z","iopub.execute_input":"2021-07-05T18:07:45.274683Z","iopub.status.idle":"2021-07-05T18:07:45.281166Z","shell.execute_reply.started":"2021-07-05T18:07:45.274653Z","shell.execute_reply":"2021-07-05T18:07:45.280246Z"},"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":"2021-07-05T18:07:49.212019Z","iopub.execute_input":"2021-07-05T18:07:49.212342Z","iopub.status.idle":"2021-07-05T18:07:49.217765Z","shell.execute_reply.started":"2021-07-05T18:07:49.212312Z","shell.execute_reply":"2021-07-05T18:07:49.216870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to(device)","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:07:52.579775Z","iopub.execute_input":"2021-07-05T18:07:52.580145Z","iopub.status.idle":"2021-07-05T18:07:52.629542Z","shell.execute_reply.started":"2021-07-05T18:07:52.580114Z","shell.execute_reply":"2021-07-05T18:07:52.628582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-07-05T17:33:15.458189Z","iopub.execute_input":"2021-07-05T17:33:15.458543Z","iopub.status.idle":"2021-07-05T17:33:15.46362Z","shell.execute_reply.started":"2021-07-05T17:33:15.458505Z","shell.execute_reply":"2021-07-05T17:33:15.461403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def meanIOU(target, predicted):\n    if target.shape != predicted.shape:\n        print(\"target has dimension\", target.shape, \", predicted values have shape\", predicted.shape)\n        return\n        \n    if target.dim() != 4:\n        print(\"target has dim\", target.dim(), \", Must be 4.\")\n        return\n    \n    iousum = 0\n    for i in range(target.shape[0]):\n        target_arr = target[i, :, :, :].clone().detach().cpu().numpy().argmax(0)\n        predicted_arr = predicted[i, :, :, :].clone().detach().cpu().numpy().argmax(0)\n        \n        intersection = np.logical_and(target_arr, predicted_arr).sum()\n        union = np.logical_or(target_arr, predicted_arr).sum()\n        if union == 0:\n            iou_score = 0\n        else :\n            iou_score = intersection / union\n        iousum +=iou_score\n        \n    miou = iousum/target.shape[0]\n    return miou","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:08:04.064645Z","iopub.execute_input":"2021-07-05T18:08:04.065000Z","iopub.status.idle":"2021-07-05T18:08:04.073380Z","shell.execute_reply.started":"2021-07-05T18:08:04.064971Z","shell.execute_reply":"2021-07-05T18:08:04.072312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef pixelAcc(target, predicted):    \n    if target.shape != predicted.shape:\n        print(\"target has dimension\", target.shape, \", predicted values have shape\", predicted.shape)\n        return\n        \n    if target.dim() != 4:\n        print(\"target has dim\", target.dim(), \", Must be 4.\")\n        return\n    \n    accsum=0\n    for i in range(target.shape[0]):\n        target_arr = target[i, :, :, :].clone().detach().cpu().numpy().argmax(0)\n        predicted_arr = predicted[i, :, :, :].clone().detach().cpu().numpy().argmax(0)\n        \n        same = (target_arr == predicted_arr).sum()\n        a, b = target_arr.shape\n        total = a*b\n        accsum += same/total\n    \n    pixelAccuracy = accsum/target.shape[0]        \n    return pixelAccuracy","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:08:07.631209Z","iopub.execute_input":"2021-07-05T18:08:07.631519Z","iopub.status.idle":"2021-07-05T18:08:07.640354Z","shell.execute_reply.started":"2021-07-05T18:08:07.631490Z","shell.execute_reply":"2021-07-05T18:08:07.637796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, batch in enumerate(train_dataloader):\n    cars, masks = batch\n    cars = cars.to(device)\n    masks = masks.to(device)\n    preds = model(cars)\n    loss = criterion(preds, masks)\n    \n    optimizer.zero_grad()\n    loss.backward()\n    optimizer.step()\n    print(f\"loss: {loss.item()}\")","metadata":{"execution":{"iopub.status.busy":"2021-07-05T18:08:38.189235Z","iopub.execute_input":"2021-07-05T18:08:38.189574Z","iopub.status.idle":"2021-07-05T18:12:41.901404Z","shell.execute_reply.started":"2021-07-05T18:08:38.189535Z","shell.execute_reply":"2021-07-05T18:12:41.899664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}