def requestGenerator(self, model_name, output_name): request = grpc_service_pb2.ModelInferRequest() request.model_name = model_name request.id = "output name validation" input = grpc_service_pb2.ModelInferRequest().InferInputTensor() input.name = "INPUT0" input.datatype = "FP32" input.shape.extend([1]) request.inputs.extend([input]) output = grpc_service_pb2.ModelInferRequest( ).InferRequestedOutputTensor() output.name = output_name request.outputs.extend([output]) request.raw_input_contents.extend([bytes(4 * 'a', 'utf-8')]) return request
FLAGS = parser.parse_args() # We use a simple model that takes 2 input tensors of 16 integers # each and returns 2 output tensors of 16 integers each. One # output tensor is the element-wise sum of the inputs and one # output is the element-wise difference. model_name = "simple_string" model_version = "" batch_size = 1 # Create gRPC stub for communicating with the server channel = grpc.insecure_channel(FLAGS.url) grpc_stub = grpc_service_pb2_grpc.GRPCInferenceServiceStub(channel) # Generate the request request = grpc_service_pb2.ModelInferRequest() request.model_name = model_name request.model_version = model_version # Populate the inputs in inference request input0 = grpc_service_pb2.ModelInferRequest().InferInputTensor() input0.name = "INPUT0" input0.datatype = "BYTES" input0.shape.extend([1, 16]) for i in range(16): input0.contents.byte_contents.append(('{}'.format(i)).encode('utf-8')) input1 = grpc_service_pb2.ModelInferRequest().InferInputTensor() input1.name = "INPUT1" input1.datatype = "BYTES" input1.shape.extend([1, 16])
def requestGenerator(input_name, output_name, c, h, w, format, dtype, FLAGS, result_filenames): request = grpc_service_pb2.ModelInferRequest() request.model_name = FLAGS.model_name request.model_version = FLAGS.model_version filenames = [] if os.path.isdir(FLAGS.image_filename): filenames = [ os.path.join(FLAGS.image_filename, f) for f in os.listdir(FLAGS.image_filename) if os.path.isfile(os.path.join(FLAGS.image_filename, f)) ] else: filenames = [ FLAGS.image_filename, ] filenames.sort() output = grpc_service_pb2.ModelInferRequest().InferRequestedOutputTensor() output.name = output_name output.parameters['classification'].int64_param = FLAGS.classes request.outputs.extend([output]) input = grpc_service_pb2.ModelInferRequest().InferInputTensor() input.name = input_name input.datatype = dtype if format == mc.ModelInput.FORMAT_NHWC: input.shape.extend([FLAGS.batch_size, h, w, c]) else: input.shape.extend([FLAGS.batch_size, c, h, w]) # Preprocess image into input data according to model requirements # Preprocess the images into input data according to model # requirements image_data = [] for filename in filenames: img = Image.open(filename) image_data.append(preprocess(img, format, dtype, c, h, w, FLAGS.scaling)) # Send requests of FLAGS.batch_size images. If the number of # images isn't an exact multiple of FLAGS.batch_size then just # start over with the first images until the batch is filled. image_idx = 0 last_request = False while not last_request: input_bytes = None input_filenames = [] request.ClearField("inputs") request.ClearField("raw_input_contents") for idx in range(FLAGS.batch_size): input_filenames.append(filenames[image_idx]) if input_bytes is None: input_bytes = image_data[image_idx].tobytes() else: input_bytes += image_data[image_idx].tobytes() image_idx = (image_idx + 1) % len(image_data) if image_idx == 0: last_request = True request.inputs.extend([input]) result_filenames.append(input_filenames) request.raw_input_contents.extend([input_bytes]) yield request