Beispiel #1
0
def onnx1_4to1_6(model: onnx.ModelProto) -> onnx.ModelProto:
    """Update ir_version from 4 to 6 and update opset from 9 to 11.

    Args:
        model (onnx.ModelProto): input onnx model.

    Returns:
        onnx.ModelProto: updated onnx model.
    """
    graph = model.graph

    if model.opset_import[0].version == 11:
        print("(Stop) the input model is already opset 11, no need to upgrade")
        exit(1)

    # deal with empty node name issue
    other.add_name_to_node(graph)
    # simplify the node param type from initializer to constant
    replacing.replace_initializer_with_Constant(graph)

    # Modify the nodes.
    replace_min_max_attribute_to_const_node_in_clip_node(graph)
    replace_all_attribute_to_const_node_in_slice_node(graph)
    replace_all_attribute_to_const_node_in_pad_node(graph)
    upsampling_to_resize(graph)
    other.topological_sort(graph)

    # Change model properties.
    model.ir_version = 6
    model.opset_import[0].version = 11

    model = onnx.utils.polish_model(model)
    return model
def fuse_bias_in_consecutive_1x1_conv(g):
    for second in g.node:
        # Find two conv
        if second.op_type != 'Conv':
            continue
        first = helper.find_node_by_output_name(g, second.input[0])
        if first is None or first.op_type != 'Conv':
            continue
        # Check if the first one has only one folloing node
        if len(
                helper.find_following_nodes_by_input_value_name(
                    g, first.output[0])) != 1:
            continue
        # If first node has no bias, continue
        if len(first.input) == 2:
            continue
        # Check their kernel size
        first_kernel_shape = helper.get_list_attribute_by_name(
            first, 'kernel_shape', 'int')
        second_kernel_shape = helper.get_list_attribute_by_name(
            second, 'kernel_shape', 'int')
        prod = first_kernel_shape[0] * first_kernel_shape[
            1] * second_kernel_shape[0] * second_kernel_shape[1]
        if prod != 1:
            continue
        print('Found: ', first.name, ' ', second.name)
        # Get bias of the nodes
        first_bias_node = helper.find_node_by_output_name(g, first.input[2])
        second_weight_node = helper.find_node_by_output_name(
            g, second.input[1])
        second_bias_node = helper.find_node_by_output_name(g, second.input[2])
        first_bias = helper.constant_to_numpy(first_bias_node)
        second_weight = helper.constant_to_numpy(second_weight_node)
        second_bias = helper.constant_to_numpy(second_bias_node)
        # Calculate the weight for second node
        first_bias = np.reshape(first_bias, (1, first_bias.size))
        second_weight = np.reshape(
            second_weight, (second_weight.shape[0], second_weight.shape[1]))
        second_weight = np.transpose(second_weight)
        new_second_bias = second_bias + np.matmul(first_bias, second_weight)
        new_second_bias = np.reshape(new_second_bias, (new_second_bias.size, ))
        # Generate new weight
        new_first_bias = np.reshape(first_bias, (first_bias.size, ))
        for i in range(new_first_bias.shape[0]):
            new_first_bias[i] = 0.0
        new_first_bias_node = helper.numpy_to_constant(
            first_bias_node.output[0], new_first_bias)
        new_second_bias_node = helper.numpy_to_constant(
            second_bias_node.output[0], new_second_bias)
        # Delete old weight and add new weights
        g.node.remove(first_bias_node)
        g.node.remove(second_bias_node)
        g.node.extend([new_first_bias_node, new_second_bias_node])
    topological_sort(g)
def do_convert(m):
    graph = m.graph
    
    # Modify the nodes.
    remove_BN_spatial(graph)
    upsample_attribute_to_const(graph)
    relu6_to_clip(graph)
    PRelu_weight_reshape(graph)
    other.topological_sort(graph)

    # Change model properties.
    m.ir_version = 4
    m.opset_import[0].version = 9
    return m
Beispiel #4
0
                    help='add nop bn using specific input')
parser.add_argument(
    '--rename-output',
    dest='rename_output',
    type=str,
    nargs='+',
    help='Rename the specific output(e.g. --rename-output old_name new_name)')

args = parser.parse_args()

# Load model and polish
m = onnx.load(args.in_file)
m = onnx.utils.polish_model(m)
g = m.graph
replacing.replace_initializer_with_Constant(g)
other.topological_sort(g)

# Remove nodes according to the given arguments.
if args.delete_node is not None:
    helper.delete_nodes(g, args.delete_node)

if args.delete_input is not None:
    helper.delete_input(g, args.delete_input)

if args.delete_output is not None:
    helper.delete_output(g, args.delete_output)

# Add do-nothing Conv node
if args.add_conv is not None:
    other.add_nop_conv_after(g, args.add_conv)
    other.topological_sort(g)
            new_input = onnx.helper.make_tensor_value_info(
                node.input[1], input_value.type.tensor_type.elem_type,
                (1, slope.dims[1], 1, 1))
            g.input.remove(input_value)
            g.input.append(new_input)
        value_info = helper.find_value_by_name(g, node.input[1])
        if value_info is not None:
            g.value_info.remove(value_info)


if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage:{} file_in file_out".format(sys.argv[0]))
        exit(1)

    model = onnx.load(sys.argv[1])
    graph = model.graph

    # Modify the nodes.
    remove_BN_spatial(graph)
    upsample_attribute_to_const(graph)
    relu6_to_clip(graph)
    PRelu_weight_reshape(graph)
    other.topological_sort(graph)

    # Change model properties.
    model.ir_version = 4
    model.opset_import[0].version = 9

    onnx.save(model, sys.argv[2])