-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata.py
116 lines (95 loc) · 3.25 KB
/
metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
# Written by Uglješa Lukešević (github.com/ukicomputers)
# Script for writing required metadata used in yolonas-cpp library
MODEL_TYPE = "yolo_nas_s" # change this variable to your model type
MODEL_TRAINED_CLASSES = (
2 # change this variable to num. of classes that your model is trained
)
MODEL_PATH = "./model.pth" # change this variable to your model path
CONVERT_TO_ONNX = True # do you want to convert that model to ONNX
#################################################################################
from super_gradients.training import models
import super_gradients.training.processing as processing
import numpy as np
std = None
brp = None
cp = None
dlmr = None
norm = None
def get_preprocessing_steps(preprocessing, processing):
global std, brp, cp, dlmr, norm
if isinstance(preprocessing, processing.StandardizeImage):
std = preprocessing.max_value
elif isinstance(preprocessing, processing.DetectionLongestMaxSizeRescale):
dlmr = True
elif isinstance(preprocessing, processing.DetectionBottomRightPadding):
brp = preprocessing.pad_value
elif isinstance(preprocessing, processing.DetectionCenterPadding):
cp = preprocessing.pad_value
elif isinstance(preprocessing, processing.NormalizeImage):
norm = {preprocessing.mean.toList(), std.mean.toList()}
def main():
global std, brp, cp, dlmr, norm
net = models.get(
MODEL_TYPE, num_classes=MODEL_TRAINED_CLASSES, checkpoint_path=MODEL_PATH
)
dummy = np.random.randint(0, 255, (1000, 800, 3), dtype=np.uint8)
for st in net._image_processor.processings:
get_preprocessing_steps(st, processing)
imgsz = np.expand_dims(net._image_processor.preprocess_image(dummy)[0], 0).shape
file = (
str(net._default_nms_iou) + "\n" +
str(net._default_nms_conf) + "\n" +
str(imgsz[2]) + "\n" +
str(imgsz[3]) + "\n"
)
if std != None:
file += str(std) + "\n"
else:
file += "n" + "\n"
if dlmr != None:
file += "t" + "\n"
else:
file += "n" + "\n"
if brp != None:
file += str(brp) + "\n"
else:
file += "n" + "\n"
if cp != None:
file += str(cp) + "\n"
else:
file += "n" + "\n"
if norm != None:
file += "t" + "\n"
file += str(norm[0][0]) + "\n"
file += str(norm[0][1]) + "\n"
file += str(norm[0][2]) + "\n"
file += str(norm[1][0]) + "\n"
file += str(norm[1][1]) + "\n"
file += str(norm[1][2])
else:
file += "n"
"""
metadata output:
iou thereshold (float)
score thereshold (float)
width (int)
height (int)
standardize (int:string (if n))
detect long max rescale (string t:n, 1:0)
bottom right padding (int:string (if n))
center padding (int:string (if n))
normalize (string t:n, 1:0)
std[0] (int)
std[1] (int)
std[2] (int)
mean[0] (int)
mean[1] (int)
mean[2] (int)
"""
filename = f"metadata"
with open(filename, "w") as f:
f.write(file)
if CONVERT_TO_ONNX == True:
models.convert_to_onnx(model=net, input_shape=imgsz[1:], out_path="model.onnx")
if __name__ == "__main__":
main()