Still trying to figure out various ways of separating buildings from each other, but in the meantime, can we improve the raw segmentation results with better loss functions?

Some examples are

  • Lovasz-Softmax Loss
  • Dice coefficient (variant of jaccard loss)
  • Binary cross entropy (since background class is much more dominant)

or better image augmentations

  • e.g mixup, cutout etc

or weighted edges

or Test time augmentation

In [1]:
from fastai.imports import *
from import *
from fastai.metrics import dice
from fastai.callbacks import *
import tifffile as tiff
from skimage.external import tifffile as sktif
from joblib import Parallel, delayed
import torch.nn.functional as F
import torch
import functools, traceback
def gpu_mem_restore(func):
    "Reclaim GPU RAM if CUDA out of memory happened, or execution was interrupted"
    def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
            type, val, tb = sys.exc_info()
            raise type(val).with_traceback(tb) from None
    return wrapper

from importlib import reload
In [2]:
from lovasz_losses import *
In [3]:
data_dir = Path("/media/wwymak/Storage/urban-3D-satellite")
train_dir = data_dir / "training"
cropped_dir =  data_dir / "cropped_training"
cropped_val_dir =  data_dir / "cropped_validation"

def get_img_id(fname):
    fname = fname.replace('_RGB.tif', '')
    img_id = "_".join(fname.split('_')[:-1])
    return img_id

train_img_ids = [get_img_id( for f in if'RGB.tif')]
val_img_ids = [get_img_id( for f in if'RGB.tif')]
In [4]:
train_cropped_imgs = [ for f in (data_dir / "cropped_training").ls() if'RGB.tif')]
valid_cropped_imgs = [ for f in (data_dir / "cropped_validation").ls() if'RGB.tif')]

dataset_df = pd.DataFrame({"name":[f"cropped_training/{f}" for f in train_cropped_imgs]  \
   + [f"cropped_validation/{f}" for f in valid_cropped_imgs],
   "label": [f"{str(data_dir)}/cropped_training/{f.replace('RGB', 'pytorch_GTL')}" for f in train_cropped_imgs]  \
   + [f"{str(data_dir)}/cropped_validation/{f.replace('RGB', 'pytorch_GTL')}" for f in valid_cropped_imgs],\
        "is_valid":   [False for i in train_cropped_imgs] + [True for i in valid_cropped_imgs ]   })
In [5]:
# codes = ["building"]
codes = ["background", "building"]
src = (SegmentationItemList.from_df(dataset_df, path=data_dir )
      . split_from_df(col="is_valid")
       .label_from_df(cols="label", classes=codes))
In [6]:
size = 128
data = (src.transform(get_transforms(do_flip=True, 
             p_lighting=0.75), size=size, tfm_y=True)
In [6]:
def accuracy_pixel(input, target):
    target = target.squeeze(1)
    mask = target != 0
    return (input.argmax(dim=1)[mask] == target[mask]).float().mean()
In [11]:
data.show_batch(8, figsize=(20,20))