Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: Number of inputs is not equal 1 for unsqueeze layer #157

Open
MT010104 opened this issue Jan 2, 2023 · 1 comment
Open

Comments

@MT010104
Copy link

MT010104 commented Jan 2, 2023

image

image

There are two similar LeNet5 models. But when using onnx2keras, their node information is different.

image

image

As a result, an error is reported when converting the second model.

image

VGG16 met the same problem, can someone help me with this?
@adamshaeffer
Copy link

adamshaeffer commented Jun 6, 2023

So I'm not totally certain if what I'm doing is correct, but if I'm not mistaken, onnx2keras relies on an older version of Onnx that has a different number of inputs in the unsqueeze layer, and instead puts it in the params. Something you can do that at least makes it so unsqueeze works is change the convert_unsqueeze method in reshape_layers.py to look like this (approximately line 200):

def convert_unsqueeze(node, params, layers, lambda_func, node_name, keras_name):
    """
    Convert unsqueeze.
    :param node: current operation node
    :param params: operation attributes
    :param layers: available keras layers
    :param lambda_func: function for keras Lambda layer
    :param node_name: internal converter name
    :param keras_name: resulting layer name
    :return: None
    """
    logger = logging.getLogger('onnx2keras:unsqueeze')

    # if len(node.input) != 1:
    #     raise AttributeError('Number of inputs is not equal 1 for unsqueeze layer')

    if is_numpy(layers[node.input[0]]):
        logger.debug('Work with numpy types.')
        layers[node_name] = layers[node.input[0]]
        axes = layers[node.input[1]]
        logger.debug(axes)
        for axis in axes:
            logger.debug(axis)
            layers[node_name] = np.expand_dims(layers[node_name], axis)
    else:

        if len(params['axes']) != 1:
            raise AttributeError('Number of axes is not equal 1. Cannot unsqueeze')

        # if params['axes'][0] != 0:
        #     raise AttributeError('Axes is not 0. Cannot unsqueeze')

        def target_layer(x, axis=params['axes'][0]):
            from tensorflow import keras
            return keras.backend.expand_dims(x, axis)

        lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
        layers[node_name] = lambda_layer(layers[node.input[0]])
        lambda_func[keras_name] = target_layer

You will need to change the axes for loop because it looks for it in params instead of input[1], and I haven't confirmed whether it actually converts it as it was supposed to, but I'm pretty sure it should do the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants