-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyolo.go
More file actions
173 lines (143 loc) · 3.91 KB
/
Copy pathyolo.go
File metadata and controls
173 lines (143 loc) · 3.91 KB
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
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 (
ModelTypeYoloFP16 ModelType = "yolofp16"
ModelTypeYoloFP32 ModelType = "yolofp32"
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
}
client := triton.NewGRPCInferenceServiceClient(conn)
return &YoloTriton{
client: client,
conn: conn,
model: model,
cfg: cfg,
modelMetadata: modelMetadata,
}, nil
}
type YoloTriton struct {
model Model
cfg YoloTritonConfig
client triton.GRPCInferenceServiceClient
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)
inferResponse, err := ModelInferRequest(y.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 {
chosen := boxes[0]
result = append(result, chosen)
tmp := []Box{}
for _, box := range boxes[1:] {
if iou(chosen, 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,
}
}
req := &triton.ModelInferRequest{
ModelName: m.modelName,
ModelVersion: m.modelVersion,
Inputs: []*triton.ModelInferRequest_InferInputTensor{
{
Name: input.Name,
Datatype: input.Datatype,
Shape: input.Shape,
},
},
Outputs: outputs,
}
if len(contents.BytesContents) > 0 && input.Datatype == "FP16" {
req.RawInputContents = contents.BytesContents
} else {
req.Inputs[0].Contents = contents
}
return req
}