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
or better image augmentations
or weighted edges
or Test time augmentation
from fastai.imports import *
from fastai.vision 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"
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
type, val, tb = sys.exc_info()
traceback.clear_frames(tb)
raise type(val).with_traceback(tb) from None
return wrapper
os.environ['FASTAI_TB_CLEAR_FRAMES']="1"
from importlib import reload
from lovasz_losses import *
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(f.name) for f in cropped_dir.ls() if f.name.endswith('RGB.tif')]
val_img_ids = [get_img_id(f.name) for f in cropped_val_dir.ls() if f.name.endswith('RGB.tif')]
train_cropped_imgs = [f.name for f in (data_dir / "cropped_training").ls() if f.name.endswith('RGB.tif')]
valid_cropped_imgs = [f.name for f in (data_dir / "cropped_validation").ls() if f.name.endswith('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 ] })
# 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))
size = 128
bs=64
data = (src.transform(get_transforms(do_flip=True,
flip_vert=True,
max_rotate=180,
max_zoom=1.2,
max_lighting=0.5,
max_warp=0.2,
p_affine=0.75,
p_lighting=0.75), size=size, tfm_y=True)
.databunch(bs=bs)
.normalize(imagenet_stats))
def accuracy_pixel(input, target):
target = target.squeeze(1)
mask = target != 0
return (input.argmax(dim=1)[mask] == target[mask]).float().mean()
data.show_batch(8, figsize=(20,20))