forked from ClintRen/yolov5_convert_weight_to_coreml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion_modules.py
205 lines (177 loc) · 7.81 KB
/
conversion_modules.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
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
import torch
import torch.nn as nn
import coremltools as ct
class ExportModel(nn.Module):
def __init__(self, base_model, img_size):
super(ExportModel, self).__init__()
self.base_model = base_model
self.img_size = img_size
def forward(self, x):
x = self.base_model(x)[0]
x = x.squeeze(0)
# Convert box coords to normalized coordinates [0 ... 1]
w = self.img_size[0]
h = self.img_size[1]
objectness = x[:, 4:5]
class_probs = x[:, 5:] * objectness
boxes = x[:, :4] * torch.tensor([1.0 / w, 1.0 / h, 1.0 / w, 1.0 / h])
return class_probs, boxes
def make_grid(nx, ny):
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
return torch.stack((xv, yv), 2).view((ny, nx, 2)).float()
def detect_export_forward(self, x):
z = [] # inference output
for i in range(self.nl):
x[i] = self.m[i](x[i]) # conv
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x[i] = (
x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
)
if not self.training: # inference
if self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
y = x[i].sigmoid()
if self.inplace:
y[..., 0:2] = (y[..., 0:2] * 2 + self.grid[i]) * self.stride[i] # xy
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
xy, wh, conf = y.split(
(2, 2, self.nc + 1), 4
) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0
xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy
wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh
y = torch.cat((xy, wh, conf), 4)
z.append(y.view(bs, -1, self.no))
return torch.cat(z, 1), x
def createNmsModelSpec(
nnSpec, numberOfClassLabels, classLabels, iouThreshold, confidenceThreshold
):
"""
Create a coreml model with nms to filter the results of the model
"""
nmsSpec = ct.proto.Model_pb2.Model()
nmsSpec.specificationVersion = 4
# Define input and outputs of the model
for i in range(2):
nnOutput = nnSpec.description.output[i].SerializeToString()
nmsSpec.description.input.add()
nmsSpec.description.input[i].ParseFromString(nnOutput)
nmsSpec.description.output.add()
nmsSpec.description.output[i].ParseFromString(nnOutput)
nmsSpec.description.output[0].name = "confidence"
nmsSpec.description.output[1].name = "coordinates"
# Define output shape of the model
outputSizes = [numberOfClassLabels, 4]
for i in range(len(outputSizes)):
maType = nmsSpec.description.output[i].type.multiArrayType
# First dimension of both output is the number of boxes, which should be flexible
maType.shapeRange.sizeRanges.add()
maType.shapeRange.sizeRanges[0].lowerBound = 0
maType.shapeRange.sizeRanges[0].upperBound = -1
# Second dimension is fixed, for "confidence" it's the number of classes, for coordinates it's position (x, y) and size (w, h)
maType.shapeRange.sizeRanges.add()
maType.shapeRange.sizeRanges[1].lowerBound = outputSizes[i]
maType.shapeRange.sizeRanges[1].upperBound = outputSizes[i]
del maType.shape[:]
# Define the model type non maximum supression
nms = nmsSpec.nonMaximumSuppression
nms.confidenceInputFeatureName = "raw_confidence"
nms.coordinatesInputFeatureName = "raw_coordinates"
nms.confidenceOutputFeatureName = "confidence"
nms.coordinatesOutputFeatureName = "coordinates"
nms.iouThresholdInputFeatureName = "iouThreshold"
nms.confidenceThresholdInputFeatureName = "confidenceThreshold"
# Some good default values for the two additional inputs, can be overwritten when using the model
nms.iouThreshold = iouThreshold
nms.confidenceThreshold = confidenceThreshold
nms.stringClassLabels.vector.extend(list(classLabels.values()) if isinstance(classLabels,dict) else classLabels)
return nmsSpec
# Just run to combine the model added decode and the NMS.
def combineModelsAndExport(
builderSpec,
nmsSpec,
fileName,
img_size,
iouThreshold,
confidenceThreshold,
quantize,
description,
author,
version,
license,
):
"""
Combines the coreml model with export logic and the nms to one final model. Optionally save with different quantization (32, 16, 8)
"""
try:
print(f"Combine CoreMl model with nms and export model")
# Combine models to a single one
pipeline = ct.models.pipeline.Pipeline(
input_features=[
("image", ct.models.datatypes.Array(3, img_size[0], img_size[1])),
("iouThreshold", ct.models.datatypes.Double()),
("confidenceThreshold", ct.models.datatypes.Double()),
],
output_features=["confidence", "coordinates"],
)
# Required version (>= ios13) in order for mns to work
pipeline.spec.specificationVersion = 4
pipeline.add_model(builderSpec)
pipeline.add_model(nmsSpec)
pipeline.spec.description.input[0].ParseFromString(
builderSpec.description.input[0].SerializeToString()
)
pipeline.spec.description.output[0].ParseFromString(
nmsSpec.description.output[0].SerializeToString()
)
pipeline.spec.description.output[1].ParseFromString(
nmsSpec.description.output[1].SerializeToString()
)
pipeline.spec.description.input[
0
].shortDescription = "Image to detect objects in"
# Metadata for the model‚
pipeline.spec.description.input[
1
].shortDescription = (
f"(optional) IOU Threshold override (Default: {iouThreshold})"
)
pipeline.spec.description.input[
2
].shortDescription = (
f"(optional) Confidence Threshold override (Default: {confidenceThreshold})"
)
pipeline.spec.description.output[
0
].shortDescription = "Boxes \xd7 Class confidence"
pipeline.spec.description.output[
1
].shortDescription = "Boxes \xd7 [x, y, width, height] (Normalized)"
pipeline.spec.description.metadata.shortDescription = description
pipeline.spec.description.metadata.author = author
pipeline.spec.description.metadata.versionString = version
pipeline.spec.description.metadata.license = license
# Add the list of class labels and the default threshold values too.
# user_defined_metadata = {
# "iou_threshold": str(iouThreshold),
# "confidence_threshold": str(confidenceThreshold),
# }
# pipeline.spec.description.metadata.userDefined.update(user_defined_metadata)
model = ct.models.MLModel(pipeline.spec)
model.save(fileName)
print(f"CoreML export success, saved as {fileName}")
if quantize:
fileName16 = fileName.replace(".mlmodel", "_FP16.mlmodel")
modelFp16 = ct.models.neural_network.quantization_utils.quantize_weights(
model, nbits=16
)
modelFp16.save(fileName16)
print(f"CoreML export success, saved as {fileName16}")
fileName8 = fileName.replace(".mlmodel", "_Int8.mlmodel")
modelFp8 = ct.models.neural_network.quantization_utils.quantize_weights(
model, nbits=8
)
modelFp8.save(fileName8)
print(f"CoreML export success, saved as {fileName8}")
except Exception as e:
print(f"CoreML export failure: {e}")