From 6e7d3c4fa9323835d921454987c354f972fb6a17 Mon Sep 17 00:00:00 2001 From: shradha Date: Sat, 22 Apr 2023 18:18:36 +0530 Subject: [PATCH] postprocess_model() shifted above to fix the issue 23508 in opencv/opencv repository --- testdata/dnn/onnx/generate_onnx_models.py | 61 ++++++++++++----------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/testdata/dnn/onnx/generate_onnx_models.py b/testdata/dnn/onnx/generate_onnx_models.py index d7adfd047..50141f4fe 100644 --- a/testdata/dnn/onnx/generate_onnx_models.py +++ b/testdata/dnn/onnx/generate_onnx_models.py @@ -524,6 +524,37 @@ def generate_slice_neg_starts(): generate_slice_neg_starts() +def postprocess_model(model_path, inputs_shapes): + onnx_model = onnx.load(model_path) + + def update_inputs_dims(model, input_dims): + """ + This function updates the sizes of dimensions of the model's inputs to the values + provided in input_dims. if the dim value provided is negative, a unique dim_param + will be set for that dimension. + """ + def update_dim(tensor, dim, i, j, dim_param_prefix): + dim_proto = tensor.type.tensor_type.shape.dim[j] + if isinstance(dim, int): + if dim >= 0: + dim_proto.dim_value = dim + else: + dim_proto.dim_param = dim_param_prefix + str(i) + '_' + str(j) + elif isinstance(dim, str): + dim_proto.dim_param = dim + else: + raise ValueError('Only int or str is accepted as dimension value, incorrect type: {}'.format(type(dim))) + + for i, input_dim_arr in enumerate(input_dims): + for j, dim in enumerate(input_dim_arr): + update_dim(model.graph.input[i], dim, i, j, 'in_') + + onnx.checker.check_model(model) + return model + + onnx_model = update_inputs_dims(onnx_model, inputs_shapes) + onnx.save(onnx_model, model_path) + input_2 = Variable(torch.randn(6, 6)) custom_slice_list = [ slice(1, 3, 1), @@ -1916,36 +1947,6 @@ def forward(self, x): model = GatherMultiOutput() save_data_and_model("gather_multi_output", x, model) -def postprocess_model(model_path, inputs_shapes): - onnx_model = onnx.load(model_path) - - def update_inputs_dims(model, input_dims): - """ - This function updates the sizes of dimensions of the model's inputs to the values - provided in input_dims. if the dim value provided is negative, a unique dim_param - will be set for that dimension. - """ - def update_dim(tensor, dim, i, j, dim_param_prefix): - dim_proto = tensor.type.tensor_type.shape.dim[j] - if isinstance(dim, int): - if dim >= 0: - dim_proto.dim_value = dim - else: - dim_proto.dim_param = dim_param_prefix + str(i) + '_' + str(j) - elif isinstance(dim, str): - dim_proto.dim_param = dim - else: - raise ValueError('Only int or str is accepted as dimension value, incorrect type: {}'.format(type(dim))) - - for i, input_dim_arr in enumerate(input_dims): - for j, dim in enumerate(input_dim_arr): - update_dim(model.graph.input[i], dim, i, j, 'in_') - - onnx.checker.check_model(model) - return model - - onnx_model = update_inputs_dims(onnx_model, inputs_shapes) - onnx.save(onnx_model, model_path) class UnsqueezeAndConv(nn.Module): def __init__(self):