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 Segment Anything. 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:
- Returns:
embedding image dict useful to store and cache image embeddings
- static get_input_points(prompt)[source]¶
Get input points from a prompt dict list.
- Parameters:
prompt (
ListDict
) – dict list- Return type:
tuple
[ndarray
]- Returns:
tuple of points, labels ndarray ready for Segment Anything inference
- padding_tensor(img)[source]¶
Pad an image ndarray/tensor to given instance self.target_size
- Parameters:
img (
Image
|ndarray
) – input ndarray/PIL image- Return type:
ndarray
- Returns:
image ndarray
- predict_masks(embedding, prompt)[source]¶
Predict masks for a single image.
- Parameters:
embedding (
EmbeddingPILImage
) – input image embedding dictprompt (
ListDict
) – Segment Anything input prompt
- Return type:
ndarray
- Returns:
prediction masks ndarray; this should have (1, 1, **image.shape) shape
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 Imagemodels_instance (
SegmentAnythingONNX2
) – SegmentAnythingONNX instance modelmodel_name (
str
) – model name stringembedding_key (
str
) – embedding idembedding_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 Imageprompt (
ListDict
) – list of prompt dictmodels_instance (
SegmentAnythingONNX2
) – SegmentAnythingONNX instance modelmodel_name (
str
) – model name string
- Return type:
- Returns:
raster prediction mask, prediction number
- samgis_core.prediction_api.sam_onnx_inference.get_raster_inference_using_existing_embedding(embedding, prompt, models_instance, folder_write_tmp_on_disk=None, key=None)[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
) – dictprompt (
ListDict
) – list of prompt dictmodels_instance (
SegmentAnythingONNX2
) – SegmentAnythingONNX instance modelfolder_write_tmp_on_disk (
str
, default:None
) – output folder where to write debug imageskey (
str
, default:None
) – embedding key
- Return type:
- 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, folder_write_tmp_on_disk=None)[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 Imageprompt (
ListDict
) – list of prompt dictmodels_instance (
SegmentAnythingONNX2
) – SegmentAnythingONNX instance modelmodel_name (
str
) – model name stringembedding_key (
str
) – embedding idembedding_dict (
dict
) – embedding images dictfolder_write_tmp_on_disk (
str
, default:None
) – output folder where to write debug images
- Return type:
- Returns:
raster prediction mask, prediction number
Module contents¶
functions useful to handle machine learning models