-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTritonNETClient.cs
More file actions
180 lines (136 loc) · 5.99 KB
/
Copy pathTritonNETClient.cs
File metadata and controls
180 lines (136 loc) · 5.99 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
174
175
176
177
178
179
180
using Google.Protobuf;
using Google.Protobuf.Collections;
using Grpc.Core;
using Inference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Inference.ModelInferRequest.Types;
namespace TritonNET
{
internal class TritonNETClient : IDisposable
{
private string url;
private string port;
private Channel channel;
private GRPCInferenceService.GRPCInferenceServiceClient client;
public TritonNETClient(string url,string port)
{
this.url = url;
this.port = port;
}
public void Connect()
{
try
{
var channelOptions = new ChannelOption[] { new ChannelOption(ChannelOptions.MaxReceiveMessageLength, 99999999) };
this.channel = new Channel(this.url +':'+ this.port, ChannelCredentials.Insecure,channelOptions);
this.client = new GRPCInferenceService.GRPCInferenceServiceClient(channel);
}
catch (Exception ex)
{
}
}
public async Task<ModelMetadataResponse> ModelInfo(string model, string version)
{
if (channel != null && (channel.State == ChannelState.Ready || channel.State == ChannelState.Idle)) {
try
{
var modelInfoReq = new ModelMetadataRequest()
{
Name = model,
Version = version
};
var modelInfoResp = await client.ModelMetadataAsync(modelInfoReq);
return modelInfoResp;
}
catch (Exception ex)
{
throw ex;
}
}
else
{
throw new Exception("Channel not ready");
}
}
public async Task<ModelInferResponse> ModelInfer(ModelMetadataResponse modelInfo, byte[] inputData, int width, int height)
{
if (channel != null && (channel.State == ChannelState.Ready || channel.State == ChannelState.Idle))
{
try{
//var dt = ByteString.CopyFrom(inputData);
//var uints = new uint[inputData.Length];
//for (int i = 0; i < uints.Length; i++)
//{
// uints[i] = inputData[i];
//}
//var data = BitConverter.ToUInt16(inputData, 0);
//var conts = new InferTensorContents()
//{
// UintContents = {uints}
//};
var input = new InferInputTensor() {
Name = modelInfo.Inputs[0].Name,
Datatype = modelInfo.Inputs[0].Datatype,//TritonTypeMap.TypeMap[modelInfo.Inputs[0].Datatype],
//Contents = conts,
Shape = { 1, width, height, 3 }//{1,512,512,3}
};
var outputs = new List<InferRequestedOutputTensor>();
foreach (var item in modelInfo.Outputs)
{
outputs.Add(new InferRequestedOutputTensor()
{
Name = item.Name,
});
}
/* var num_detections = new InferRequestedOutputTensor() { Name = "num_detections" };
var det_boxes = new InferRequestedOutputTensor() { Name = "detection_boxes" };
var det_scores = new InferRequestedOutputTensor() { Name = "detection_scores" };
var det_classes = new InferRequestedOutputTensor() { Name = "detection_classes" };
var det_multiclass_scores = new InferRequestedOutputTensor() { Name = "detection_multiclass_scores" };
var det_anchor_indices = new InferRequestedOutputTensor() { Name = "detection_anchor_indices" };
var raw_detection_boxes = new InferRequestedOutputTensor() { Name = "raw_detection_boxes" };
var raw_detection_scores = new InferRequestedOutputTensor() { Name = "raw_detection_scores" };*/
var inferRequest = new ModelInferRequest()
{
ModelName = modelInfo.Name,
ModelVersion = modelInfo.Versions.Last(),
Inputs = {input},
RawInputContents = { ByteString.CopyFrom(inputData) },
Outputs = {outputs}
/* Outputs = {
num_detections,
det_boxes,
det_scores,
det_classes,
det_multiclass_scores,
det_anchor_indices,
raw_detection_boxes,
raw_detection_scores},*/
};
var response = await client.ModelInferAsync(inferRequest);
return response;
}catch (Exception ex)
{
throw ex;
}
}
else
{
throw new Exception("Channel not ready.");
}
}
public void Dispose()
{
if (channel != null && channel.State == ChannelState.Ready)
{
channel.ShutdownAsync().Wait();
channel = null;
client = null;
}
}
}
}