pytorch2keras icon indicating copy to clipboard operation
pytorch2keras copied to clipboard

Error in convert_constant

Open pkdogcom opened this issue 6 years ago • 3 comments

It seems that there is an error in convert.convert_constant function, where it is defined as

def convert_constant(params, w_name, scope_name, inputs, layers, weights):
    def target_layer(params=params):
        return keras.backend.constant(np.float32(params['value']))

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

while I believe the layers[inputs[0]] argument to lambda_layer is supposed to be a dummy argument to bypass compatibility check in keras base_layer and the params variable from the convert_constant scope should be the real argument and contains the actual constant values.

Therefore, the correct way to define target_layer should be:

    def target_layer(x, params=params):
        return keras.backend.constant(np.float32(params['value']))

instead.

pkdogcom avatar Jul 18 '18 19:07 pkdogcom

It turns out that there need to be another two tweaks in order to make it work.

Sample codes that work:

def convert_constant(params, w_name, scope_name, inputs, layers, weights):
    print('Converting constant ...')

    params = params['value'].numpy().tolist()

    def target_layer(x):
        import keras.backend as K # Actually all lambda layers (sum, reduced_sum) need to do this
        return K.constant(params)

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

params need to be converted as list since either torch tensor or numpy array cannot be correctly loaded by Keras after they are serialized. On the other hand, lambda layer also requires keras.backend namespace imported as K, as pointed out here.

P.S. in the convert_reduce_sum function the axis parameter also needs to be passed as list rather than numpy array, as follows:

axis = params['axes']

pkdogcom avatar Jul 20 '18 15:07 pkdogcom

hello! Thank you for research! Yes, constant is a bit confusing layer. You can make a pull request if you want to contribute your changes. Or i can fix that by myself, it's up to you.

gmalivenko avatar Jul 27 '18 09:07 gmalivenko

@nerox8664 I've created a pull request, let me know if that makes sense to you.

pkdogcom avatar Aug 06 '18 15:08 pkdogcom