Inference

The dh_segment.inference module implements the function related to the usage of a dhSegment model, for instance to use a trained model to inference on new data.

Loading a model

LoadedModel(model_base_dir[, predict_mode, …])

Loads an exported dhSegment model


class dh_segment.inference.LoadedModel(model_base_dir, predict_mode='filename', num_parallel_predictions=2)

Loads an exported dhSegment model

Parameters
  • model_base_dir – the model directory i.e. containing saved_model.{pb|pbtxt}. If not, it is assumed to be a TF exporter directory, and the latest export directory will be automatically selected.

  • predict_mode – defines the input/output format of the prediction output (see .predict())

  • num_parallel_predictions – limits the number of conccurent calls of predict to avoid Out-Of-Memory issues if predicting on GPU

predict(input_tensor, prediction_key=None)

Performs the prediction from the loaded model according to the prediction mode.

Prediction modes:

prediction_mode

input_tensor

Output prediction dictionnary

Comment

filename

Single filename string

labels, probs, original_shape

Loads the image, resizes it, and predicts

filename_original_shape

Single filename string

labels, probs

Loads the image, resizes it, predicts and scale the output to the original resolution of the file

image

Single input image [1,H,W,3] float32 (0..255)

labels, probs, original_shape

Resizes the image, and predicts

image_original_shape

Single input image [1,H,W,3] float32 (0..255)

labels, probs

Resizes the image, predicts, and scale the output to the original resolution of the input

image_resized

Single input image [1,H,W,3] float32 (0..255)

labels, probs

Predicts from the image input directly

Parameters
  • input_tensor – a single input whose format should match the prediction mode

  • prediction_key – if not None, will returns the value of the corresponding key of the output dictionnary instead of the full dictionnary

Returns

the prediction output

predict_with_tiles(filename, resized_size=None, tile_size=500, min_overlap=0.2, linear_interpolation=True)