def create_openvino_modelfile(models_dir, model_version, max_batch, dtype,
                              input_shapes, output_shapes):

    assert len(input_shapes) == len(output_shapes)
    batch_dim = [] if max_batch == 0 else [
        max_batch,
    ]
    if not tu.validate_for_openvino_model(
            dtype, dtype, dtype, batch_dim + input_shapes[0],
            batch_dim + input_shapes[0], batch_dim + input_shapes[0]):
        return

    io_cnt = len(input_shapes)

    # Create the model
    model_name = tu.get_zero_model_name(
        "openvino_nobatch" if max_batch == 0 else "openvino", io_cnt, dtype)
    model_version_dir = models_dir + "/" + model_name + "/" + str(
        model_version)

    openvino_inputs = []
    openvino_outputs = []
    for io_num in range(io_cnt):
        in_name = "INPUT{}".format(io_num)
        out_name = "OUTPUT{}".format(io_num)
        openvino_inputs.append(
            ng.parameter(shape=batch_dim + input_shapes[io_num],
                         dtype=dtype,
                         name=in_name))

        openvino_outputs.append(
            ng.reshape(openvino_inputs[io_num],
                       batch_dim + output_shapes[io_num],
                       name=out_name,
                       special_zero=False))

    function = ng.impl.Function(openvino_outputs, openvino_inputs, model_name)
    ie_network = IENetwork(ng.impl.Function.to_capsule(function))

    try:
        os.makedirs(model_version_dir)
    except OSError as ex:
        pass  # ignore existing dir

    ie_network.serialize(model_version_dir + "/model.xml",
                         model_version_dir + "/model.bin")
Exemple #2
0
for i in net.inputs.keys():
    print(f"Input blob: {i} - shape: {net.inputs[i].shape}")
for o in net.outputs.keys():
    print(f"Output blob: {o} - shape: {net.outputs[o].shape}")

if args.reshape is not None:
    m = re.match(r"(\d+)x(\d+)", args.reshape)
    if not m:
        print("Incorrect syntax for 'reshape' argument")
    else:
        h = int(m.group(1))
        w = int(m.group(2))
        h = h - h % args.stride
        w = w - w % args.stride
        print(f"Reshapping to {h}x{w}")
        for i in net.inputs.keys():
            n, c, _, _ = net.inputs[i].shape
            net.reshape({i: (n, c, h, w)})
            print(f"Input blob: {i} - new shape: {net.inputs[i].shape}")
        for o in net.outputs.keys():
            print(f"Output blob: {o} - new shape: {net.outputs[o].shape}")

        # Saving reshaped model in IR files
        model_name = os.path.splitext(os.path.basename(args.model))[0]
        new_model_path = Path(
            args.model).parent / Path(f"{model_name}_{h}x{w}")

        print(f"Saving reshaped model in {new_model_path}")
        net.serialize(str(new_model_path.with_suffix(".xml")),
                      str(new_model_path.with_suffix(".bin")))