Adding metadata to ONNX model¶
The plugin allows you to load the meta parameters of the onnx model automatically. Predefined parameter types are designed for the simplicity of the user’s work. The written metadata is especially important for users who are not familiar with the technical details.
List of parameters parsed by plugin¶
Parameter | Type | Example | Description |
||||||
---|---|---|---|---|---|---|
model_type | str | |
||||||
class_names |
dict |
|
A dictionary that maps a class id to its name. |
|||
class_names (mutli outputs) |
list |
|
A dictionary that maps a class id to its name. |
|||
resolution |
float |
|
Real-world resolution of images (centimeters per pixel). |
|||
tiles_size |
int |
|
What size (in pixels) is the tile to crop. |
|||
tiles_overlap |
int |
|
How many percent of the image size overlap. |
|||
standardization_mean |
list |
|
Mean - if you want to standarize input after normalisation |
|||
standardization_std |
list |
|
Std - if you want to standarize input after normalisation |
|||
seg_thresh |
float |
|
Segmentor: class confidence threshold. |
|||
seg_small_segment |
int |
|
Segmentor: remove small occurrences of the class. |
|||
reg_output_scaling |
float |
|
Regressor: scaling factor for the model output. |
|||
det_conf |
float |
|
Detector: object confidence threshold. |
|||
det_iou_thresh |
float |
|
Detector: IOU threshold for NMS. |
|||
det_type |
str |
|
Detector: type of the detector model format |
Available model types:
- Segmentor
- Detector
- Regressor
- Recognition
- Superresolution
Availeble detector types:
- YOLO_v5_or_v7_default
- YOLO_v6
- YOLO_v9
- YOLO_Ultralytics
- YOLO_Ultralytics_segmentation
Example¶
The example below shows how to add string, float, and dictionary metadata into a model. Note that metadata is created while model.metadata_props.add()
is called. Moreover, the metadata value has to be a byte type.
import json
import onnx
model = onnx.load('deeplabv3_landcover_4c.onnx')
class_names = {
0: '_background',
1: 'building',
2: 'woodland',
3: 'water',
4: 'road',
}
m1 = model.metadata_props.add()
m1.key = 'model_type'
m1.value = json.dumps('Segmentor')
m2 = model.metadata_props.add()
m2.key = 'class_names'
m2.value = json.dumps(class_names)
m3 = model.metadata_props.add()
m3.key = 'resolution'
m3.value = json.dumps(50)
# optional, if you want to standarize input after normalisation
m4 = model.metadata_props.add()
m4.key = 'standardization_mean'
m4.value = json.dumps([0.0, 0.0, 0.0])
m5 = model.metadata_props.add()
m5.key = 'standardization_std'
m5.value = json.dumps([1.0, 1.0, 1.0])
onnx.save(model, 'deeplabv3_landcover_4c.onnx')
You can also use a script setting the most important parameters in the plugin repository (file tools/add_model_metadata.py
).