Converter error: list assignment index out of range
See original GitHub issueI was trying to convert a pytorch model into keras via onnx. The pytorch model was successfully converted to onnx format. I tried two versions of the model:
- Model with bilinear upsampling, with align_corners=False and opset 9,
- Model with bilinear upsampling, with align_corners=True and opset 11
In both cases i got same errors, if i set change_ordering=False Code:-
import onnx
from onnx2keras import onnx_to_keras
# Load ONNX model
onnx_model = onnx.load('SINet_320_opbnup.onnx')
# Call the converter (input - is the main model input name, can be different for your model)
k_model = onnx_to_keras(onnx_model, ['data'])
k_model.save('SINet.h5')
Error:-
WARNING:onnx2keras:avgpool:Unable to use `same` padding. Add ZeroPadding2D layer to fix shapes.
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-27-b90de0dc63bc> in <module>()
6
7 # Call the converter (input - is the main model input name, can be different for your model)
----> 8 k_model = onnx_to_keras(onnx_model, ['data'])
9 k_model.save('SINet.h5')
6 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/advanced_activations.py in build(self, input_shape)
141 if self.shared_axes is not None:
142 for i in self.shared_axes:
--> 143 param_shape[i - 1] = 1
144 self.alpha = self.add_weight(
145 shape=param_shape,
IndexError: list assignment index out of range
If i set change_ordering=True, i get another error:-
ValueError Traceback (most recent call last) <ipython-input-29-bf041c502013> in <module>() 6 7 # Call the converter (input - is the main model input name, can be different for your model) ----> 8 k_model = onnx_to_keras(onnx_model, [‘data’], change_ordering=True) 9 k_model.save(‘SINet.h5’)
5 frames /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in set_weights(self, weights) 1824 raise ValueError( 1825 'Layer weight shape %s not compatible with provided weight ’ -> 1826 ‘shape %s’ % (ref_shape, weight.shape)) 1827 weight_value_tuples.append((param, weight)) 1828 weight_index += 1
ValueError: Layer weight shape (1, 1, 160) not compatible with provided weight shape (12, 1, 1)
Models:-
I ran the model checker successfully i.e onnx.checker.check_model(onnx_model) The original pytorch model contains PRelu activations. How can we fix this issue and convert the model into keras format??
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
I think this is primarily a lack of time. ReduceMax operation was added yesterday. I will look better to onnx “Resize” operation and maybe will add its support to the library. There is no similar functionality in keras. I’ll see what can be done about it.
I think the ‘resize’ operator in onnx seems to handle bilinear resize functionality. It seems to give good results during testing/inference, with align corners=True. Earlier it used to be upsample layer(ie. nearest only). Similarly onnx seems to support reduce max operation. Keras also seems to have these layers anyway, so are they just integration issues?