local model
LocalModel Module
This module provides the LocalModel
class that allows loading, inference,
and benchmark testing of models in a local environment. It supports detection
and segmentation tasks, and utilizes ONNXRuntime for model execution.
Classes:
Name | Description |
---|---|
LocalModel |
A class for managing and interacting with local models. |
Functions:
Name | Description |
---|---|
__init__ |
Initializes the LocalModel instance, loading the model, metadata, and setting up the runtime. |
_read_metadata |
Reads the model metadata from a JSON file. |
_annotate |
Annotates the input image with detection or segmentation results. |
infer |
Runs inference on an input image, with optional annotation. |
benchmark |
Benchmarks the model's inference performance over a specified number of iterations and input size. |
LocalModel
#
Source code in focoos/local_model.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
__init__(model_dir, runtime_type=None)
#
Initialize a LocalModel instance.
This class sets up a local model for inference by initializing the runtime environment, loading metadata, and preparing annotation utilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_dir |
Union[str, Path]
|
The path to the directory containing the model files. |
required |
runtime_type |
Optional[RuntimeTypes]
|
Specifies the runtime type to use for inference.
Defaults to the value of |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If no runtime type is provided and |
FileNotFoundError
|
If the specified model directory does not exist. |
Attributes:
Name | Type | Description |
---|---|---|
model_dir |
Union[str, Path]
|
Path to the model directory. |
metadata |
ModelMetadata
|
Metadata information for the model. |
model_ref |
ModelMetadata
|
Reference identifier for the model obtained from metadata. |
label_annotator |
LabelAnnotator
|
Utility for adding labels to the output, initialized with text padding and border radius. |
box_annotator |
BoxAnnotator
|
Utility for annotating bounding boxes. |
mask_annotator |
MaskAnnotator
|
Utility for annotating masks. |
runtime |
ONNXRuntime
|
Inference runtime initialized with the specified runtime type, model path, metadata, and warmup iterations. |
The method verifies the existence of the model directory, reads the model metadata, and initializes the runtime for inference using the provided runtime type. Annotation utilities are also prepared for visualizing model outputs.
Source code in focoos/local_model.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
benchmark(iterations, size)
#
Benchmark the model's inference performance over multiple iterations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterations |
int
|
Number of iterations to run for benchmarking. |
required |
size |
int
|
The input size for each benchmark iteration. |
required |
Returns:
Name | Type | Description |
---|---|---|
LatencyMetrics |
LatencyMetrics
|
Latency metrics including time taken for inference. |
Source code in focoos/local_model.py
197 198 199 200 201 202 203 204 205 206 207 208 |
|
infer(image, threshold=0.5, annotate=False)
#
Run inference on an input image and optionally annotate the results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
Union[bytes, str, Path, ndarray, Image]
|
The input image to infer on. This can be a byte array, file path, or a PIL Image object, or a NumPy array representing the image. |
required |
threshold |
float
|
The confidence threshold for detections. Defaults to 0.5. Detections with confidence scores below this threshold will be discarded. |
0.5
|
annotate |
bool
|
Whether to annotate the image with detection results. Defaults to False. If set to True, the method will return the image with bounding boxes or segmentation masks. |
False
|
Returns:
Type | Description |
---|---|
Tuple[FocoosDetections, Optional[ndarray]]
|
Tuple[FocoosDetections, Optional[np.ndarray]]: A tuple containing:
- |
Raises:
Type | Description |
---|---|
ValueError
|
If the model is not deployed locally (i.e., |
Source code in focoos/local_model.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|