How to use the tool
from slicer.core import *
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
import PIL,os,mimetypes, glob, skimage
from PIL import Image
from natsort import natsorted

Parameters

slice_range = [300, 600]

Optional:

Define quantiles from a slice range

slice_l = ImageSlicer.quantile_from_slice_range(slice_range);slice_l
[300, 375, 450, 525, 600]

Input directory (where the images are)

inp_d = Path("./docs/img")
slice_l
[300, 375, 450, 525, 600]
ImageSlicer.show_slices(slice_l, inp_d)

Optional add padding

  • Add padding so that slices at the border are not cut out (depenend on the scaling of the slices)
pad = 300
inp_d = Path("./docs/img")
ImageSlicer.add_padding(inp_d, pad)
img_pad

Slice Images

inp_d_2 = Path("./docs/img/out_padded/") 

Which parts of the image should be unique? The image below can help to find a good parameter.

overlap = 2 
ovl

Output size of the sliced tiles

resize = 224 

Definition of the directory name with the used parameters

dir_name = f'test_{resize}_{overlap}_{slice_range[0]}-{slice_range[1]}';dir_name 
'test_224_2_300-600'

Process and status report

ImageSlicer.slice_images(inp_d_2, dir_name , slice_l, resize, overlap)
Overlap = 2
Resize to = 224*224
----
Process slice: 300
Rows: 16
Columns: 13
----
Finished
----
Process slice: 375
Rows: 13
Columns: 11
----
Finished
----
Process slice: 450
Rows: 11
Columns: 9
----
Finished
----
Process slice: 525
Rows: 9
Columns: 8
----
Finished
----
Process slice: 600
Rows: 8
Columns: 7
----
Finished
----
Saved under: docs\img\out_padded\test_224_2_300-600
Count:  336

Example of a saved slice:

sl

Slice masks

Definition of the parameters similiar to process of images (ImageSlicer.slice_images).

palette = {0 : (255, 255, 255), # Impervious surfaces (white)
           1 : (0, 0, 255),     # Buildings (blue)
           2 : (0, 255, 255),   # Low vegetation (cyan)
           3 : (0, 255, 0),     # Trees (green)
           4 : (255, 255, 0),   # Cars (yellow)
           5 : (255, 0, 0),     # Clutter (red)
           6 : (0, 0, 0)}       # Undefined (black)
ImageSlicer.slice_masks(inp_d_2,dir_name , slice_l, resize, overlap, palette)

Fastai interference and image mosaicing

from fastai import vision
from PIL import Image
from functools import partial

Define individuell metrics from exported fastai model

def acc_isprs(input, target):
    target = target.squeeze(1)
    mask = target != void_code 
    return (input.argmax(dim=1)[mask]==target[mask]).float().mean()
metrics = acc_isprs
f_score1 = partial(fbeta , beta=1)

Path to exported fastai model

path_model  = Path('D:/david/va_po/im_train/va_5-9_po__9-13_448')

Type in the amount of rows and columns to rearrange the image

rows, columns = 7,7
ImageSlicer.predict_from_fastai_model(path_fastai_model= path_model,inp_d= inp_d, dir_name=dir_name, 
palette= palette , rows= rows, columns= columns )
pred