gmalivenko/onnx2keras

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

Opened this issue · 1 comments

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?

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.