Beispiel #1
0
    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])

        input_contents = grpc_service_pb2.InferTensorContents()
        input_contents.raw_contents = bytes(4 * 'a', 'utf-8')
        input.contents.CopyFrom(input_contents)

        request.inputs.extend([input])

        output = grpc_service_pb2.ModelInferRequest(
        ).InferRequestedOutputTensor()
        output.name = output_name
        request.outputs.extend([output])

        return request
Beispiel #2
0
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")
        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

        input_contents = grpc_service_pb2.InferTensorContents()
        input_contents.raw_contents = input_bytes
        input.contents.CopyFrom(input_contents)
        request.inputs.extend([input])
        result_filenames.append(input_filenames)
        yield request
    request = grpc_service_pb2.ModelConfigRequest(name="resnet_v1_50_graphdef",
                                                  version=model_version)
    response = grpc_stub.ModelConfig(request)
    print("model config:\n{}".format(response))

    # Infer
    request = grpc_service_pb2.ModelInferRequest()
    request.model_name = "resnet_v1_50_graphdef"
    request.model_version = model_version
    request.id = "my request id"

    input = grpc_service_pb2.ModelInferRequest().InferInputTensor()
    input.name = "input"
    input.datatype = "FP32"
    input.shape.extend([1, 224, 224, 3])

    input_contents = grpc_service_pb2.InferTensorContents()
    input_contents.raw_contents = bytes(602112 * 'a', 'utf-8')
    input.contents.CopyFrom(input_contents)

    request.inputs.extend([input])

    output = grpc_service_pb2.ModelInferRequest().InferRequestedOutputTensor()
    output.name = "resnet_v1_50/predictions/Softmax"
    request.outputs.extend([output])

    response = grpc_stub.ModelInfer(request)
    print("model infer:\n{}".format(response))

    print("PASS")