samgis_core.prediction_api package

Submodules

samgis_core.prediction_api.sam_onnx2 module

Define a machine learning model executed by ONNX Runtime (https://onnxruntime.ai/) for Segment Anything (https://segment-anything.com). Modified from - https://github.com/vietanhdev/samexporter/ - https://github.com/AndreyGermanov/sam_onnx_full_export/

Copyright (c) 2023 Viet Anh Nguyen, Andrey Germanov Copyright (c) 2024-today Alessandro Trinca Tornidor

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class samgis_core.prediction_api.sam_onnx2.SegmentAnythingONNX2(encoder_model_path, decoder_model_path)[source]

Bases: object

Segmentation model using SegmentAnything. Compatible with onnxruntime 1.17.x and later

encode(img)[source]

Calculate embedding and metadata for a single image.

Parameters:

img (Image | ndarray) – input image to embed

Return type:

EmbeddingPILImage

Returns:

embedding image dict useful to store and cache image embeddings

static get_input_points(prompt)[source]

Get input points

padding_tensor(img)[source]
predict_masks(embedding, prompt)[source]

Predict masks for a single image.

preprocess_image(img)[source]

Resize image preserving aspect ratio using ‘output_size_target’ as a long side

samgis_core.prediction_api.sam_onnx_inference module

samgis_core.prediction_api.sam_onnx_inference.get_inference_embedding(img, models_instance, model_name, embedding_key, embedding_dict)[source]

add an embedding to the embedding dict if needed

Parameters:
  • img (Image | ndarray) – input PIL Image

  • models_instance (SegmentAnythingONNX2) – SegmentAnythingONNX instance model

  • model_name (str) – model name string

  • embedding_key (str) – embedding id

  • embedding_dict (dict[str, EmbeddingPILImage]) – embedding dict object

Return type:

dict[str, EmbeddingPILImage]

Returns:

raster dict

samgis_core.prediction_api.sam_onnx_inference.get_raster_inference(img, prompt, models_instance, model_name)[source]

Get inference output for a given image using a SegmentAnythingONNX model

Parameters:
  • img (Image | ndarray) – input PIL Image

  • prompt (ListDict) – list of prompt dict

  • models_instance (SegmentAnythingONNX2) – SegmentAnythingONNX instance model

  • model_name (str) – model name string

Return type:

TupleNdarrayInt

Returns:

raster prediction mask, prediction number

samgis_core.prediction_api.sam_onnx_inference.get_raster_inference_using_existing_embedding(embedding, prompt, models_instance)[source]

Get inference output for a given image using a SegmentAnythingONNX model, using an existing embedding instead of a new ndarray or PIL image

Parameters:
  • embedding (dict) – dict

  • prompt (ListDict) – list of prompt dict

  • models_instance (SegmentAnythingONNX2) – SegmentAnythingONNX instance model

Return type:

TupleNdarrayInt

Returns:

raster prediction mask, prediction number

samgis_core.prediction_api.sam_onnx_inference.get_raster_inference_with_embedding_from_dict(img, prompt, models_instance, model_name, embedding_key, embedding_dict)[source]
Get inference output using a SegmentAnythingONNX model, but get the image embedding from the given embedding dict

instead of creating a new embedding. This function needs the img argument to update the embedding dict if necessary

Parameters:
  • img (Image | ndarray) – input PIL Image

  • prompt (ListDict) – list of prompt dict

  • models_instance (SegmentAnythingONNX2) – SegmentAnythingONNX instance model

  • model_name (str) – model name string

  • embedding_key (str) – embedding id

  • embedding_dict (dict) – embedding images dict

Return type:

TupleNdarrayInt

Returns:

raster prediction mask, prediction number

Module contents

functions useful to handle machine learning models