| 
13 | 13 | 
 
  | 
14 | 14 | 
 
  | 
15 | 15 | 
 
  | 
16 |  | -#  url = '10.128.61.7:8001'  | 
17 |  | -url = '127.0.0.1:8001'  | 
18 |  | -model_name = 'bisenetv2'  | 
 | 16 | +url = '10.128.61.8:8001'  | 
 | 17 | +#  url = '127.0.0.1:8001'  | 
 | 18 | +model_name = 'bisenetv1'  | 
19 | 19 | model_version = '1'  | 
20 |  | -inp_name = 'input_image'  | 
 | 20 | +inp_name = 'raw_img_bytes'  | 
21 | 21 | outp_name = 'preds'  | 
22 |  | -inp_dtype = 'FP32'  | 
 | 22 | +inp_dtype = 'UINT8'  | 
23 | 23 | outp_dtype = np.int64  | 
24 |  | -inp_shape = [1, 3, 1024, 2048]  | 
25 |  | -outp_shape = [1024, 2048]  | 
26 | 24 | impth = '../example.png'  | 
27 | 25 | mean = [0.3257, 0.3690, 0.3223] # city, rgb  | 
28 | 26 | std = [0.2112, 0.2148, 0.2115]  | 
29 | 27 | 
 
  | 
30 | 28 | 
 
  | 
 | 29 | +## input data and mean/std  | 
 | 30 | +inp_data = np.fromfile(impth, dtype=np.uint8)[None, ...]  | 
 | 31 | +mean = np.array(mean, dtype=np.float32)[None, ...]  | 
 | 32 | +std = np.array(std, dtype=np.float32)[None, ...]  | 
 | 33 | +inputs = [service_pb2.ModelInferRequest().InferInputTensor() for _ in range(3)]  | 
 | 34 | +inputs[0].name = inp_name  | 
 | 35 | +inputs[0].datatype = inp_dtype  | 
 | 36 | +inputs[0].shape.extend(inp_data.shape)  | 
 | 37 | +inputs[1].name = 'channel_mean'  | 
 | 38 | +inputs[1].datatype = 'FP32'  | 
 | 39 | +inputs[1].shape.extend(mean.shape)  | 
 | 40 | +inputs[2].name = 'channel_std'  | 
 | 41 | +inputs[2].datatype = 'FP32'  | 
 | 42 | +inputs[2].shape.extend(std.shape)  | 
 | 43 | +inp_bytes = [inp_data.tobytes(), mean.tobytes(), std.tobytes()]  | 
 | 44 | + | 
 | 45 | + | 
31 | 46 | option = [  | 
32 | 47 |         ('grpc.max_receive_message_length', 1073741824),  | 
33 | 48 |         ('grpc.max_send_message_length', 1073741824),  | 
 | 
52 | 67 | request.model_name = model_name  | 
53 | 68 | request.model_version = model_version  | 
54 | 69 | 
 
  | 
55 |  | -inp = service_pb2.ModelInferRequest().InferInputTensor()  | 
56 |  | -inp.name = inp_name  | 
57 |  | -inp.datatype = inp_dtype  | 
58 |  | -inp.shape.extend(inp_shape)  | 
59 |  | - | 
60 |  | - | 
61 |  | -mean = np.array(mean).reshape(1, 1, 3)  | 
62 |  | -std = np.array(std).reshape(1, 1, 3)  | 
63 |  | -im = cv2.imread(impth)[:, :, ::-1]  | 
64 |  | -im = cv2.resize(im, dsize=tuple(inp_shape[-1:-3:-1]))  | 
65 |  | -im = ((im / 255.) - mean) / std  | 
66 |  | -im = im[None, ...].transpose(0, 3, 1, 2)  | 
67 |  | -inp_bytes = im.astype(np.float32).tobytes()  | 
68 |  | - | 
69 | 70 | request.ClearField("inputs")  | 
70 | 71 | request.ClearField("raw_input_contents")  | 
71 |  | -request.inputs.extend([inp,])  | 
72 |  | -request.raw_input_contents.extend([inp_bytes,])  | 
73 |  | - | 
 | 72 | +request.inputs.extend(inputs)  | 
 | 73 | +request.raw_input_contents.extend(inp_bytes)  | 
74 | 74 | 
 
  | 
75 |  | -outp = service_pb2.ModelInferRequest().InferRequestedOutputTensor()  | 
76 |  | -outp.name = outp_name  | 
77 |  | -request.outputs.extend([outp,])  | 
78 | 75 | 
 
  | 
79 | 76 | # sync  | 
80 |  | -#  resp = grpc_stub.ModelInfer(request).raw_output_contents[0]  | 
 | 77 | +#  resp = grpc_stub.ModelInfer(request)  | 
81 | 78 | # async  | 
82 | 79 | resp = grpc_stub.ModelInfer.future(request)  | 
83 |  | -resp = resp.result().raw_output_contents[0]  | 
 | 80 | +resp = resp.result()  | 
 | 81 | + | 
 | 82 | +outp_bytes = resp.raw_output_contents[0]  | 
 | 83 | +outp_shape = resp.outputs[0].shape  | 
84 | 84 | 
 
  | 
85 |  | -out = np.frombuffer(resp, dtype=outp_dtype).reshape(*outp_shape)  | 
 | 85 | +out = np.frombuffer(outp_bytes, dtype=outp_dtype).reshape(*outp_shape).squeeze()  | 
86 | 86 | 
 
  | 
87 | 87 | out = palette[out]  | 
88 | 88 | cv2.imwrite('res.png', out)  | 
0 commit comments