Justobe opened a new issue #19717:
URL: https://github.com/apache/incubator-mxnet/issues/19717
## Description
mxnet throws an exception when I try to build my model and use mxnet as the
backend of keras. However, my script runs successfully on other backends of
keras (such as tensorflow and cntk). I further found that the problem may be
caused by batch normalization in the program when using mxnet.
I also noticed that this issue was mentioned in #15721, but this issue still
exists in the latest keras-mxnet 2.2.4.2 and mxnet-cu101 1.7
### Error Message
> Traceback (most recent call last):
File "crash_checker.py", line 67, in <module>
model.add(Dense(10, activation='softmax'))
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/keras/engine/sequential.py",
line 181, in add
output_tensor = layer(self.outputs[0])
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/keras/engine/base_layer.py",
line 470, in __call__
output = self.call(inputs, **kwargs)
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/keras/layers/core.py",
line 893, in call
output = K.bias_add(output, self.bias, data_format='channels_last')
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
line 94, in func_wrapper
train_symbol = func(*args, **kwargs)
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
line 3982, in bias_add
x_dim = ndim(x)
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
line 535, in ndim
shape = x.shape
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
line 4395, in shape
return self._get_shape()
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
line 4404, in _get_shape
_, out_shape, _ = self.symbol.infer_shape_partial()
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/mxnet/symbol/symbol.py",
line 1177, in infer_shape_partial
return self._infer_shape_impl(True, *args, **kwargs)
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/mxnet/symbol/symbol.py",
line 1265, in _infer_shape_impl
ctypes.byref(complete)))
File
"/root/anaconda3/envs/mxnet_170/lib/python3.6/site-packages/mxnet/base.py",
line 246, in check_call
raise get_last_ffi_error()
**mxnet.base.MXNetError: MXNetError: Error in operator batchnorm6:
[16:26:44] include/mxnet/./tuple.h:245: Check failed: i >= 0 && i < ndim():
index = -2 must be in range [0, -1)**
## To Reproduce
I provide a simple script to reproduce the bug, run the following script
such as:
```
import os
import sys
bk = sys.argv[1]
os.environ['KERAS_BACKEND'] = bk
from keras import backend as K
import keras
from keras.models import Sequential
from keras.layers.core import Dense
from keras.layers import
Conv2D,MaxPooling2D,BatchNormalization,Flatten,Dropout
model = Sequential()
model.add(Conv2D(96, (3,3), strides=(2,2), activation='relu',
padding='same', input_shape=(32, 32, 3,)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
# Local Response normalization for Original Alexnet
model.add(BatchNormalization())
model.add(Conv2D(96, (3,3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2,2)))
model.add(BatchNormalization())
model.add(Conv2D(192, (3,3), activation='relu', padding='same'))
model.add(Conv2D(192, (3,3), activation='relu', padding='same'))
model.add(Conv2D(256, (3,3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2,2)))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(512, activation='tanh'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='tanh'))
# Comment out this line of code, mxnet runs successfully
# However, this script runs successfully on both tensorflow and cntk
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
# print the model summary
model.summary()
```
### Steps to reproduce
`python myscript.py mxnet` (change mxnet to tensorflow if you want to test
under backend tensorflow)
## Environment
```
Package Version
------------------- -------------------
cached-property 1.5.2
certifi 2020.12.5
chardet 4.0.0
cycler 0.10.0
decorator 4.4.2
graphviz 0.8.4
h5py 2.10.0
idna 2.10
Keras-Applications 1.0.8
keras-mxnet 2.2.4.2
Keras-Preprocessing 1.1.2
kiwisolver 1.3.1
matplotlib 3.2.2
mxnet-cu101 1.7.0
networkx 2.5
numpy 1.19.4
pandas 0.23.0
Pillow 5.1.0
pip 20.3.3
pyparsing 2.4.7
python-dateutil 2.8.1
pytz 2020.5
PyWavelets 1.1.1
PyYAML 5.3.1
redis 3.3.2
requests 2.25.1
scikit-image 0.13.1
scikit-learn 0.19.1
scipy 1.1.0
setuptools 51.0.0.post20201207
six 1.15.0
urllib3 1.26.2
wheel 0.36.2
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]