unetseg package



unetseg.evaluate module

unetseg.evaluate.plot_data_generator(num_samples: int = 3, fig_size=(20, 10), *, train_config: unetseg.train.TrainConfig, img_ch: int = 3)[source]

Plots some samples from a data generator.

  • num_samples (int) – Number of samples to plot.

  • fig_size (tuple) – Figure size.

  • img_ch (int) – Number of channels.

  • train_config (TrainConfig) – Training configuration object.

unetseg.evaluate.plot_data_results(num_samples: int = 3, fig_size=(20, 10), *, predict_config: unetseg.predict.PredictConfig, img_ch: int = 3, n_bands: int = 3)[source]

Plots some samples from the results directory. :param num_samples: Number of samples to plot. :type num_samples: int :param fig_size: Figure size. :type fig_size: tuple :param img_ch: Number of channels. :type img_ch: int :param predict_config: Prediction onfiguration object. :type predict_config: PredictConfig

unetseg.postprocess module

unetseg.postprocess.crop_image(img: numpy.ndarray, margin_ratio: float) numpy.ndarray[source]

Center crop an image, with a margin of margin_ratio

unetseg.postprocess.get_bounds_from_image_files(image_files: List[str]) Tuple[float][source]

Get bounds from all images, and transform to pixels based on the affine transform

unetseg.postprocess.merge(images_dir: str, output_path: str, crop_margin_ratio: float = 0.125)[source]

Merge all images in images_dir into a single image

unetseg.predict module

class unetseg.predict.PredictConfig(images_path='', results_path='', batch_size=32, model_architecture='unet', model_path='unet.h5', height=320, width=320, n_channels=3, n_classes=1, class_weights=0)[source]

Bases: object

unetseg.predict.predict(cfg: unetseg.predict.PredictConfig)[source]

Performs inference based on a configuration object


cfg (PredictConfig) – Configuration object

unetseg.train module

class unetseg.train.TrainConfig(images_path, masks_path=None, width=200, height=200, n_channels=3, n_classes=1, apply_image_augmentation=True, model_path='unet.h5', model_architecture='unet', validation_split=0.1, test_split=0.1, epochs=15, steps_per_epoch=2000, early_stopping_patience=3, batch_size=32, seed=None, evaluate=True, class_weights=0)[source]

Bases: object

unetseg.train.build_data_generator(image_files: List[str], *, config: unetseg.train.TrainConfig, mask_dir: str)[source]

Build data generator based on a list of images and directory of binary masks.

  • image_files (List[str]) – List of paths to images.

  • config (TrainConfig) – Configuration object.

  • mask_dir (str) – Path to directory with binary masks.


tuple – Tuple of image and mask batch.

unetseg.train.build_model_unet(cfg: unetseg.train.TrainConfig) keras.engine.training.Model[source]

Build U-Net model class.


cfg (TrainConfig) – Configuration for training.


U-Net model class.

Return type


unetseg.train.build_model_unetplusplus(cfg: unetseg.train.TrainConfig) keras.engine.training.Model[source]

Builds a U-Net++ model.


cfg (TrainConfig) – Training configuration.


The U-Net++ model.

Return type


unetseg.train.get_mask_raster(image_path: str, n_channels: Optional[int] = None, *, mask_dir: str) numpy.ndarray[source]

Get respective mask raster from image path.

  • image_path (str) – Path to image.

  • n_channels (int, optional) – Number of channels in image. The default is None.

  • mask_dir (str, optional) – Path to mask directory. The default is None.


Mask image.

Return type


unetseg.train.get_raster(image_path: str, n_channels: Optional[int] = None) numpy.ndarray[source]

Loads a raster image from a file.

  • image_path (str) – Path to the image file.

  • n_channels (int, optional) – Number of channels in the image. If not specified, the number of channels is inferred from the image file.


The loaded image.

Return type


unetseg.train.preprocess_input(image: numpy.ndarray, mask: numpy.ndarray, *, config: unetseg.train.TrainConfig) Tuple[numpy.ndarray, numpy.ndarray][source]

Preprocess input image and masks.

  • image (np.ndarray) – Input image.

  • mask (np.ndarray) – Input mask.

  • config (TrainConfig) – Training configuration.


Preprocessed image and mask.

Return type

Tuple[np.ndarray, np.ndarray]

unetseg.train.train(cfg: unetseg.train.TrainConfig)[source]

Performs training and evaluation of the model based on a configuration object.


cfg (TrainConfig) – Configuration object containing all the necessary parameters for training.

unetseg.utils module

unetseg.utils.grouper(iterable, n, fillvalue=None)[source]

Collect data into fixed-length chunks or blocks

unetseg.utils.load_model(model_path: str) keras.engine.training.Model[source]

Load model from model_path

unetseg.utils.resize(image, size)[source]

Resize multiband image to an image of size (h, w)

Module contents