{"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":"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-10T16:20:04.641765Z","iopub.execute_input":"2021-07-10T16:20:04.642185Z","iopub.status.idle":"2021-07-10T16:20:04.65038Z","shell.execute_reply.started":"2021-07-10T16:20:04.642152Z","shell.execute_reply":"2021-07-10T16:20:04.64921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-10T15:43:15.130684Z","iopub.execute_input":"2021-07-10T15:43:15.131141Z","iopub.status.idle":"2021-07-10T15:43:25.744282Z","shell.execute_reply.started":"2021-07-10T15:43:15.131096Z","shell.execute_reply":"2021-07-10T15:43:25.742747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append(\"../input/customdataset\")","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:43:25.750544Z","iopub.execute_input":"2021-07-10T15:43:25.753282Z","iopub.status.idle":"2021-07-10T15:43:25.761488Z","shell.execute_reply.started":"2021-07-10T15:43:25.753232Z","shell.execute_reply":"2021-07-10T15:43:25.760136Z"},"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-10T15:43:25.763523Z","iopub.execute_input":"2021-07-10T15:43:25.764238Z","iopub.status.idle":"2021-07-10T15:43:27.362054Z","shell.execute_reply.started":"2021-07-10T15:43:25.764193Z","shell.execute_reply":"2021-07-10T15:43:27.360853Z"},"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-10T15:43:27.366974Z","iopub.execute_input":"2021-07-10T15:43:27.369688Z","iopub.status.idle":"2021-07-10T15:43:27.391262Z","shell.execute_reply.started":"2021-07-10T15:43:27.369642Z","shell.execute_reply":"2021-07-10T15:43:27.390226Z"},"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-10T15:43:27.395645Z","iopub.execute_input":"2021-07-10T15:43:27.398125Z","iopub.status.idle":"2021-07-10T15:43:27.476242Z","shell.execute_reply.started":"2021-07-10T15:43:27.398084Z","shell.execute_reply":"2021-07-10T15:43:27.475232Z"},"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-10T15:43:27.477801Z","iopub.execute_input":"2021-07-10T15:43:27.478265Z","iopub.status.idle":"2021-07-10T15:43:27.521007Z","shell.execute_reply.started":"2021-07-10T15:43:27.478225Z","shell.execute_reply":"2021-07-10T15:43:27.519617Z"},"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-10T15:43:27.524013Z","iopub.execute_input":"2021-07-10T15:43:27.524447Z","iopub.status.idle":"2021-07-10T15:43:27.544088Z","shell.execute_reply.started":"2021-07-10T15:43:27.524407Z","shell.execute_reply":"2021-07-10T15:43:27.542808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data augmentation function\n\nimage_size = [256, 256]\nOUTPUT_CHANNELS = 3\n# increase the diversity for dataset by the flip left to right images way to decrease overfiting\ndef augmentation(input_image, mask_image):\n    \n    # create the random number if it > 0.5 then flip_left_right images\n    if tf.random.uniform(()) > 0.5:\n        input_image = tf.image.flip_left_right(input_image) \n        mask_image = tf.image.flip_left_right(mask_image)\n    \n    return input_image, mask_image","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:43:27.546452Z","iopub.execute_input":"2021-07-10T15:43:27.547031Z","iopub.status.idle":"2021-07-10T15:43:27.554882Z","shell.execute_reply.started":"2021-07-10T15:43:27.546884Z","shell.execute_reply":"2021-07-10T15:43:27.553322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Preprocessing function\n\ndef preprocess(car_path, mask_path):\n    input_image = tf.io.read_file(car_path) # read content of images\n    \n    input_image = tf.image.decode_jpeg(input_image, channels=OUTPUT_CHANNELS) # car = np.array(Image.open(car_path).convert('RGB'))\n    # Decode a JPEG-encoded image to a uint8 tensor.\n    # The attr channels indicates the desired number of color channels for the decoded image.\n    #Accepted values are:\n    #0: Use the number of channels in the JPEG-encoded image.\n    #1: output a grayscale image.\n    #3: output an RGB image.\n    \n    input_image = tf.image.resize(input_image, image_size) # change the size of images follow the require size \n    input_image = tf.cast(input_image, tf.float32) / 255.0 # Casts a tensor to a new type and make the idensity of pixels in range [0;1]\n    \n    \n    mask_image = tf.io.read_file(mask_path)\n    mask_image = tf.image.decode_jpeg(mask_image, channels=OUTPUT_CHANNELS)\n    mask_image = tf.image.resize(mask_image, image_size)\n    mask_image = mask_image[:, :, :1]\n    mask_image = tf.math.sign(mask_image) # if idensity of pixel = 0 then it = 0 , if > 0 then it = 1 and < 0 then it = -1\n    \n    input_image, mask_image = augmentation(input_image, mask_image)\n    \n    return input_image, mask_image","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:43:27.55683Z","iopub.execute_input":"2021-07-10T15:43:27.557564Z","iopub.status.idle":"2021-07-10T15:43:27.573306Z","shell.execute_reply.started":"2021-07-10T15:43:27.557512Z","shell.execute_reply":"2021-07-10T15:43:27.572126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"#create_dataset function\n\ndef create_dataset(df, train = False):\n    if not train:\n        ds = tf.data.Dataset.from_tensor_slices((df[\"car_path\"].values, df[\"mask_path\"].values)) # make the tensor dataset include in 2 struct [car_path;mask_path]\n        ds = ds.map(preprocess, num_parallel_calls=tf.data.AUTOTUNE) # Parallelizing data extraction\n        \n    else:\n        ds = tf.data.Dataset.from_tensor_slices((df[\"car_path\"].values, df[\"mask_path\"].values))\n        ds = ds.map(preprocess, num_parallel_calls=tf.data.AUTOTUNE)\n        ds = ds.map(augmentation, num_parallel_calls=tf.data.AUTOTUNE)\n        \n    return ds","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:43:27.575372Z","iopub.execute_input":"2021-07-10T15:43:27.576102Z","iopub.status.idle":"2021-07-10T15:43:27.586913Z","shell.execute_reply.started":"2021-07-10T15:43:27.576057Z","shell.execute_reply":"2021-07-10T15:43:27.585876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we will split the dataset into train and test\n\ntrain_df, valid_df = train_test_split(df, random_state=42, test_size=.25) # chose random that the number of train_df = 75% df and 25% df for vaid_df\ntrain = create_dataset(train_df, train = True) # create the train set\nvalid = create_dataset(valid_df) # create the validation set\n\nTRAIN_LENGTH = len(train_df) # 3816\nBATCH_SIZE = 16\nBUFFER_SIZE = 1000\n\ntrain_loader = train.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()\n# (train_data # some tf.data.Dataset, likely in the form of tuples (x, y)\n# .cache() # caches the dataset in memory (avoids having to reapply preprocessing transformations to the input)\n# .shuffle(BUFFER_SIZE) # shuffle the samples to have always a random order of samples fed to the network\n# .batch(BATCH_SIZE) # batch samples in chunks of size BATCH_SIZE (except the last one, that may be smaller)\n# .repeat()) # repeat forever, meaning the dataset will keep producing batches and never terminate running out of data.\n\ntrain_loader= train_loader.prefetch(buffer_size=tf.data.AUTOTUNE)\n# Prefetching overlaps the preprocessing and model execution of a training step. While the model is executing training step s,the input pipeline is reading the data for step s+1. Doing so reduces the step time to the maximum (as opposed to the sum) of the training and the time it takes to extract the data.\n\nval_loader = valid.batch(BATCH_SIZE) \n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:43:30.259671Z","iopub.execute_input":"2021-07-10T15:43:30.260052Z","iopub.status.idle":"2021-07-10T15:43:32.926928Z","shell.execute_reply.started":"2021-07-10T15:43:30.260018Z","shell.execute_reply":"2021-07-10T15:43:32.925925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataiter = iter(val_loader)\ncars, masks = next(dataiter)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:43:32.928662Z","iopub.execute_input":"2021-07-10T15:43:32.929065Z","iopub.status.idle":"2021-07-10T15:43:33.42825Z","shell.execute_reply.started":"2021-07-10T15:43:32.929035Z","shell.execute_reply":"2021-07-10T15:43:33.427104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display(display_list):\n    plt.figure(figsize=(15, 15))\n\n    title = ['Input Image', 'True Mask', 'Predicted Mask']\n\n    for i in range(len(display_list)):\n        plt.subplot(1, len(display_list), i+1)\n        plt.title(title[i])\n        plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i])) # Converts a 3D Numpy array to a PIL Image instance\n        plt.axis('off') \n    plt.show()   \nfor i in range(1):\n   for image, mask in valid.take(1):\n        sample_image, sample_mask = image, mask\n        display([sample_image, sample_mask])\n        \n   \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:43:36.609549Z","iopub.execute_input":"2021-07-10T15:43:36.609934Z","iopub.status.idle":"2021-07-10T15:43:36.971984Z","shell.execute_reply.started":"2021-07-10T15:43:36.609904Z","shell.execute_reply":"2021-07-10T15:43:36.971021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model\nimport torch\nimport torch.nn as nn\nimport torchvision.transforms.functional as TF\n\nclass DoubleConv(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super(DoubleConv, 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\nclass UNET(nn.Module):\n    def __init__(\n            self, in_channels=3, out_channels=1, features=[64, 128, 256, 512],\n    ):\n        super(UNET, self).__init__()\n        self.ups = nn.ModuleList() # Holds submodules in a list\n        self.downs = nn.ModuleList()\n        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)\n\n        #down part of Unet\n        for feature in features:\n            self.downs.append(DoubleConv(in_channels, feature)) # add Doubleconv in list downs \n            in_channels = feature # example with 1 -> 64 -> 64 => DoubleConv(1,64) and in_channels = 64 , features=[64, 128, 256, 512]\n\n        #Up part of Unet\n        for feature in reversed(features): # The reversed() method returns the reversed iterator of the given sequence. It is the same as the iter() method but in reverse order\n            self.ups.append(\n                # Transposed convolution that help us encode the previous feature maps into the more details feature \n                nn.ConvTranspose2d(\n                    feature*2, feature, kernel_size=2, stride=2,\n                )\n            )\n            self.ups.append(DoubleConv(feature*2, feature))\n\n        self.bottleneck = DoubleConv(features[-1], features[-1]*2) # features[-1] = 512 \n        self.final_conv = nn.Conv2d(features[0], out_channels, kernel_size=1) # features[0] = 64 \n\n    def forward(self, x):\n        skip_connections = [] # make to copy and crop in Unet\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] # [::-1] = list[<start>:<stop>:<step>]\n\n        for idx in range(0, len(self.ups),2):\n            x = self.ups[idx](x)\n            skip_connection=skip_connections[idx//2] # // chia lam tron\n\n            if x.shape != skip_connection.shape:\n                x = TF.resize(x, size=skip_connection.shape[2:]) # [2:] = reshape hight and width\n\n            concat_skip = torch.cat((skip_connection, x), dim=1)\n            x = self.ups[idx+1](concat_skip)\n\n        return  self.final_conv(x)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:43:49.832424Z","iopub.execute_input":"2021-07-10T15:43:49.832843Z","iopub.status.idle":"2021-07-10T15:43:49.851785Z","shell.execute_reply.started":"2021-07-10T15:43:49.832812Z","shell.execute_reply":"2021-07-10T15:43:49.849399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# utils\nimport torchvision\n\n# Monitoring the process when running model\ndef save_checkpoint(state, filename='my_checkpoint.pth.tar'):\n  print(\"=> Saving checkpoint\")\n  torch.save(state, filename)\n\ndef load_checkpoint(checkpoint, model):\n  print(\"=> Loading checkpoint\")\n  model.load_state_dict(checkpoint[\"state_dict\"])\n\ndef 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).unsqueeze(1)\n      preds = torch.sigmoid(model(x))\n      preds = (preds > 0.5).float()\n      num_correct += (preds == y).sum()\n      num_pixels += torch.numel(preds)\n      dice_score += (2*(preds*y).sum()) / (\n          (preds +y).sum() + 1e-8\n      )\n\n  print(\n      f\"Got {num_correct}/{num_pixels} with acc {num_correct/num_pixels*100:.2f}\"\n  )\n  print(f\"Dice score: {dice_score/len(loader)}\")\n  model.train()\n\n\ndef save_predictions_as_imgs(\n    loader, model, folder=\"saved_image/\" , device=\"cuda\"\n):\n  model.eval()\n  for idx, (x, y) in enumerate(loader):\n    x = x.to(device=device)\n    with torch.no_grad():\n      preds = torch.sigmoid(model(x))\n      preds = (preds > 0.5).float()\n      torchvision.utils.saved_image(\n          preds, f\"{folder}/pred_{idx}.png\"\n      )\n      torchvision.utils.saved_image(y.unsqueeze(1), f\"{folder}/pred_{idx}.png\")\n\n    model.train()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:56:30.735692Z","iopub.status.idle":"2021-07-10T15:56:30.736717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \nimport albumentations as A\nfrom albumentations.pytorch import ToTensor\nfrom tqdm import tqdm\nimport torch.nn as nn\nimport torch.optim as optim\n\n\ndef train_fn(loader, model, optimizer, loss_fn, scaler):\n    # # tqdm là một tiện ích của python, từ này có nghĩa là “tiến trình” trong tiếng Ả Rập (taqadum, تقدّم) . Trong python, nó giúp hiển thị các vòng lặp dưới dạng một giao diện tiến độ một cách thông minh – chỉ cần bọc bất kỳ vòng lặp nào bằng tqdm và bạn không phải lo cài đặt hiển thị tiến trình cho nó nữa.\n    loop = tqdm(loader)\n\n    for batch_idx, (data, targets) in enumerate(loop):\n        data = data.to(DEVICE)\n        targets = targets.float().unsqueeze(1).to(DEVICE) # unsqueeze()Returns a new tensor with a dimension of size one inserted at the specified position and targets is real output\n\n\n        # forward\n        with torch.cuda.amp.autocast():\n            predictions = model(data)\n            loss = loss_fn(predictions, targets)\n\n        # backward\n        optimizer.zero_grad()\n        # Python can be used to optimize parameters in a model to best fit data, increase profitability of a potential engineering design\n        scaler.scale(loss).backward()\n        scaler.step(optimizer)\n        scaler.update()\n\n        # update tqdm loop\n        loop.set_postfix(loss=loss.item())","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:59:56.732552Z","iopub.execute_input":"2021-07-10T15:59:56.732921Z","iopub.status.idle":"2021-07-10T15:59:56.74271Z","shell.execute_reply.started":"2021-07-10T15:59:56.732882Z","shell.execute_reply":"2021-07-10T15:59:56.741454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train\nimport torch \nimport albumentations as A\nfrom albumentations.pytorch import ToTensor\nfrom tqdm import tqdm\nimport torch.nn as nn\nimport torch.optim as optim\n\n\n# Hyperparameters etc.\nLEARNING_RATE = 1e-4\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nBATCH_SIZE = 16\nNUM_EPOCHS = 3\nNUM_WORKERS = 2\nIMAGE_HEIGHT = 256  # 1280 originally\nIMAGE_WIDTH = 256 # 1918 originally\nPIN_MEMORY = True\nLOAD_MODEL = False\n\n\n\ndef main():\n\n    model = UNET(in_channels=3, out_channels=1).to(DEVICE)\n    loss_fn = nn.BCEWithLogitsLoss()\n    optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)\n\n    if LOAD_MODEL:\n        load_checkpoint(torch.load(\"my_checkpoint.pth.tar\"), model)\n\n\n    scaler = torch.cuda.amp.GradScaler()\n\n    for epoch in range(NUM_EPOCHS):\n        train_fn(train_loader, model, optimizer, loss_fn, scaler)\n\n        # save model\n        checkpoint = {\n            \"state_dict\": model.state_dict(),\n            \"optimizer\":optimizer.state_dict(),\n        }\n        save_checkpoint(checkpoint)\n\n        # check accuracy\n        check_accuracy(val_loader, model, DEVICE)\n\n        # print some examples to a folder\n        save_predictions_as_imgs(val_loader, model, folder=\"saved_images/\", device=DEVICE)\n\n\nif __name__ == \"__main__\":\n    main()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T15:59:59.663579Z","iopub.execute_input":"2021-07-10T15:59:59.663959Z","iopub.status.idle":"2021-07-10T16:00:24.855431Z","shell.execute_reply.started":"2021-07-10T15:59:59.663926Z","shell.execute_reply":"2021-07-10T16:00:24.852787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNET(in_channels=3, out_channels=1).to(DEVICE)\nmodel","metadata":{"execution":{"iopub.status.busy":"2021-07-10T16:15:54.190257Z","iopub.execute_input":"2021-07-10T16:15:54.190648Z","iopub.status.idle":"2021-07-10T16:15:54.468377Z","shell.execute_reply.started":"2021-07-10T16:15:54.190617Z","shell.execute_reply":"2021-07-10T16:15:54.467113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install torchsummary","metadata":{"execution":{"iopub.status.busy":"2021-07-10T16:15:58.460303Z","iopub.execute_input":"2021-07-10T16:15:58.460649Z","iopub.status.idle":"2021-07-10T16:16:08.527192Z","shell.execute_reply.started":"2021-07-10T16:15:58.460617Z","shell.execute_reply":"2021-07-10T16:16:08.525945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchsummary import summary","metadata":{"execution":{"iopub.status.busy":"2021-07-10T16:16:12.162988Z","iopub.execute_input":"2021-07-10T16:16:12.163348Z","iopub.status.idle":"2021-07-10T16:16:12.175558Z","shell.execute_reply.started":"2021-07-10T16:16:12.163306Z","shell.execute_reply":"2021-07-10T16:16:12.174366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(model, (3, 256, 256), 1,'cuda')","metadata":{"execution":{"iopub.status.busy":"2021-07-10T16:16:22.28394Z","iopub.execute_input":"2021-07-10T16:16:22.284328Z","iopub.status.idle":"2021-07-10T16:16:23.43282Z","shell.execute_reply.started":"2021-07-10T16:16:22.284296Z","shell.execute_reply":"2021-07-10T16:16:23.43183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-10T16:16:42.778696Z","iopub.execute_input":"2021-07-10T16:16:42.77922Z","iopub.status.idle":"2021-07-10T16:16:42.787268Z","shell.execute_reply.started":"2021-07-10T16:16:42.779162Z","shell.execute_reply":"2021-07-10T16:16:42.786067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion= nn.BCEWithLogitsLoss()\noptimizer= torch.optim.Adam(model.parameters(),lr=1e-3)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T16:17:25.063021Z","iopub.execute_input":"2021-07-10T16:17:25.063373Z","iopub.status.idle":"2021-07-10T16:17:25.072407Z","shell.execute_reply.started":"2021-07-10T16:17:25.063342Z","shell.execute_reply":"2021-07-10T16:17:25.071146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to(DEVICE)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T16:17:43.548709Z","iopub.execute_input":"2021-07-10T16:17:43.549163Z","iopub.status.idle":"2021-07-10T16:17:43.56202Z","shell.execute_reply.started":"2021-07-10T16:17:43.54913Z","shell.execute_reply":"2021-07-10T16:17:43.560509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-07-10T16:18:02.380251Z","iopub.execute_input":"2021-07-10T16:18:02.380599Z","iopub.status.idle":"2021-07-10T16:18:02.385609Z","shell.execute_reply.started":"2021-07-10T16:18:02.380568Z","shell.execute_reply":"2021-07-10T16:18:02.384155Z"},"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-10T16:18:21.131228Z","iopub.execute_input":"2021-07-10T16:18:21.131606Z","iopub.status.idle":"2021-07-10T16:18:21.141519Z","shell.execute_reply.started":"2021-07-10T16:18:21.131575Z","shell.execute_reply":"2021-07-10T16:18:21.139647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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-10T16:18:38.549869Z","iopub.execute_input":"2021-07-10T16:18:38.550278Z","iopub.status.idle":"2021-07-10T16:18:38.560669Z","shell.execute_reply.started":"2021-07-10T16:18:38.550243Z","shell.execute_reply":"2021-07-10T16:18:38.559043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, batch in enumerate(train_loader):\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-10T16:20:22.357145Z","iopub.execute_input":"2021-07-10T16:20:22.35755Z","iopub.status.idle":"2021-07-10T16:20:45.375135Z","shell.execute_reply.started":"2021-07-10T16:20:22.357518Z","shell.execute_reply":"2021-07-10T16:20:45.373572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:51:27.547683Z","iopub.execute_input":"2021-07-10T08:51:27.54812Z","iopub.status.idle":"2021-07-10T08:51:49.98357Z","shell.execute_reply.started":"2021-07-10T08:51:27.548081Z","shell.execute_reply":"2021-07-10T08:51:49.979882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}