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¶
Important: Please define all parameters below
Important: The function ImageSlicer.show_slices() will help to visualize the scaling
Important: For the parameters of the functions see Core
Note: The scaling of different datasets can be adjusted -> Improves the stability of the training significantly
slice_range = [300, 600]
Optional:¶
Define quantiles from a slice range
slice_l = ImageSlicer.quantile_from_slice_range(slice_range);slice_l
Input directory (where the images are)
inp_d = Path("./docs/img")
slice_l
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
Note: Depends on the deep learning architecture
Note: In fastai different resized slices can be used for the training
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
Process and status report
ImageSlicer.slice_images(inp_d_2, dir_name , slice_l, resize, overlap)
Note: Multi stage interpolation (Nearest Neighbor + Bicubic combined) will be used
Note: Optional Parameter out_path, cb , half_empty. All other parameter need to be set.
Note: More than half empty slices (half_empty = True) will be ignored. When you dont want this change to half_empty = False .
Example of a saved slice:
sl
Slice masks¶
Definition of the parameters similiar to process of images (ImageSlicer.slice_images).
Important: Added parameter palette (type: dictionary)
Note: For transforming a rgb mask to greyscale and to transforme after the interference back to rgb
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)
Note: Interpolation nearest neighbor will be used
Fastai interference and image mosaicing¶
Note: Not up to date
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