-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolo.go
161 lines (134 loc) · 3.62 KB
/
yolo.go
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
package yolotriton
import (
"image"
_ "image/png"
"sort"
triton "github.com/dev6699/yolotriton/grpc-client"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type ModelType string
const (
ModelTypeYoloV8 ModelType = "yolov8"
ModelTypeYoloNAS ModelType = "yolonas"
ModelTypeYoloNASInt8 ModelType = "yolonasint8"
)
type Model interface {
GetConfig() YoloTritonConfig
PreProcess(img image.Image, targetWidth uint, targetHeight uint) (*triton.InferTensorContents, error)
PostProcess(rawOutputContents [][]byte) ([]Box, error)
}
type YoloTritonConfig struct {
NumClasses int
NumObjects int
ModelName string
ModelVersion string
MinProbability float32
MaxIOU float64
Classes []string
}
func New(url string, model Model) (*YoloTriton, error) {
conn, err := grpc.Dial(url, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
cfg := model.GetConfig()
modelMetadata, err := newModelMetadata(conn, cfg.ModelName, cfg.ModelVersion)
if err != nil {
return nil, err
}
return &YoloTriton{
conn: conn,
model: model,
cfg: cfg,
modelMetadata: modelMetadata,
}, nil
}
type YoloTriton struct {
model Model
cfg YoloTritonConfig
conn *grpc.ClientConn
modelMetadata *modelMetadata
}
func (y *YoloTriton) Close() error {
return y.conn.Close()
}
func (y *YoloTriton) Infer(img image.Image) ([]Box, error) {
inputs, err := y.model.PreProcess(img, y.modelMetadata.inputWidth(), y.modelMetadata.inputHeight())
if err != nil {
return nil, err
}
modelInferRequest := y.modelMetadata.formInferRequest(inputs)
client := triton.NewGRPCInferenceServiceClient(y.conn)
inferResponse, err := ModelInferRequest(client, modelInferRequest)
if err != nil {
return nil, err
}
boxes, err := y.model.PostProcess(inferResponse.RawOutputContents)
if err != nil {
return nil, err
}
sort.Slice(boxes, func(i, j int) bool {
return boxes[i].Probability > boxes[j].Probability
})
result := []Box{}
for len(boxes) > 0 {
result = append(result, boxes[0])
tmp := []Box{}
for _, box := range boxes {
if iou(boxes[0], box) < y.cfg.MaxIOU {
tmp = append(tmp, box)
}
}
boxes = tmp
}
return result, nil
}
type modelMetadata struct {
modelName string
modelVersion string
*triton.ModelMetadataResponse
}
func newModelMetadata(conn *grpc.ClientConn, modelName string, modelVersion string) (*modelMetadata, error) {
client := triton.NewGRPCInferenceServiceClient(conn)
metaResponse, err := ModelMetadataRequest(client, modelName, modelVersion)
if err != nil {
return nil, err
}
return &modelMetadata{
modelName: modelName,
modelVersion: modelVersion,
ModelMetadataResponse: metaResponse,
}, nil
}
func (m *modelMetadata) inputWidth() uint {
return uint(m.Inputs[0].Shape[2])
}
func (m *modelMetadata) inputHeight() uint {
return uint(m.Inputs[0].Shape[3])
}
func (m *modelMetadata) formInferRequest(contents *triton.InferTensorContents) *triton.ModelInferRequest {
input := m.Inputs[0]
if input.Shape[0] == -1 {
input.Shape[0] = 1
}
outputs := make([]*triton.ModelInferRequest_InferRequestedOutputTensor, len(m.Outputs))
for i, o := range m.Outputs {
outputs[i] = &triton.ModelInferRequest_InferRequestedOutputTensor{
Name: o.Name,
}
}
return &triton.ModelInferRequest{
ModelName: m.modelName,
ModelVersion: m.modelVersion,
Inputs: []*triton.ModelInferRequest_InferInputTensor{
{
Name: input.Name,
Datatype: input.Datatype,
Shape: input.Shape,
Contents: contents,
},
},
Outputs: outputs,
}
}