maybeLee opened a new issue #20483:
URL: https://github.com/apache/incubator-mxnet/issues/20483


   ## Description
   MXNet (version: 1.8.0) reports a crash when building a simple network 
containing three layers (Flatten, Reshape, and ZeroPadding2D), I use Keras as 
the front-end to build the model:
   
   ```
   x = layers.Input(input_shape[1:])
   layer_input = layers.Flatten()(x)
   layer_input = layers.Reshape((10,10,1))(layer_input)
   y = layers.ZeroPadding2D()(layer_input)
   model = Model(x, y)
   ```
   However, if I use TensorFlow as the backend to run this model, I can get the 
result fluently.
   
   ### Error Message
   ```
   Using MXNet backend
   
/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/__init__.py:31:
 DeprecationWarning: MXNet support in Keras is going to be discontinued and 
v2.2.4.3 is the last release as multi-backend Keras has been discontinued. It 
is recommended to consider switching to MXNet Gluon. More information can be 
found here: https://github.com/awslabs/keras-apache-mxnet
     "https://github.com/awslabs/keras-apache-mxnet";, DeprecationWarning)
   start building the model
   Traceback (most recent call last):
     File "try.py", line 20, in <module>
       y = layers.ZeroPadding2D()(layer_input)
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/engine/base_layer.py",
 line 470, in __call__
       output = self.call(inputs, **kwargs)
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/layers/convolutional.py",
 line 2252, in call
       data_format=self.data_format)
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
 line 95, in func_wrapper
       train_symbol = func(*args, **kwargs)
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
 line 2361, in spatial_2d_padding
       x = _postprocess_convnd_output(x, data_format)
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
 line 87, in func_wrapper
       train_symbol = func(*args, **kwargs)
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
 line 4803, in _postprocess_convnd_output
       if data_format == 'channels_last' and ndim(x) > 3:
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
 line 536, in ndim
       shape = x.shape
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
 line 4399, in shape
       return self._get_shape()
     File 
"/root/anaconda3/envs/diffcu_mxnet/lib/python3.6/site-packages/keras/backend/mxnet_backend.py",
 line 4408, in _get_shape
       _, out_shape, _ = self.symbol.infer_shape_partial()
     File "/mxnet/incubator-mxnet/python/mxnet/symbol/symbol.py", line 1177, in 
infer_shape_partial
       return self._infer_shape_impl(True, *args, **kwargs)
     File "/mxnet/incubator-mxnet/python/mxnet/symbol/symbol.py", line 1265, in 
_infer_shape_impl
       ctypes.byref(complete)))
     File "/mxnet/incubator-mxnet/python/mxnet/base.py", line 246, in check_call
       raise get_last_ffi_error()
   mxnet.base.MXNetError: MXNetError: Error in operator pad0: [08:36:54] 
src/operator/./pad-inl.h:216: Current implementation only supports 4-D or 5-D 
input.
   ```
   
   ## To Reproduce
   You can reproduce this bug by running following code:
   ```
   import os
   import argparse
   import sys
   import warnings
   parse = argparse.ArgumentParser()
   parse.add_argument("--bk", type=str,default="mxnet", help="the name of 
backend")
   flags, _ = parse.parse_known_args(sys.argv[1:])
   os.environ["KERAS_BACKEND"]=flags.bk
   import keras
   from keras import initializers, layers, Model
   import numpy as np
   warnings.filterwarnings("ignore", category=DeprecationWarning)
   warnings.filterwarnings("ignore", category=UserWarning)
   input_shape = [1,10,10,1]
   print("start building the model")
   x = layers.Input(input_shape[1:])
   layer_input = layers.Flatten()(x)
   layer_input = layers.Reshape((10,10,1))(layer_input)
   y = layers.ZeroPadding2D()(layer_input)
   print("successfully compile the model")
   model = Model(x, y)
   x = np.random.rand(**input_shape)
   pred = model.predict(x)
   print("successfully get the prediction result")
   
   ```
   Run this script (suppose you store the code in the file: `try.py`) using 
backend MXNet, it will directly crash when compiling the model:
   ```
   python try.py --bk mxnet
   ```
   Run this script using backend TensorFlow, everything works fine:
   ```
   python try.py --bk tensorflow
   ```
   
   Related package version used:
   ```
   mxnet==1.8.0
   keras-mxnet==2.2.4.3
   tensorflow==2.0.0
   ```
   
   It seems that this crash is caused by the one bug in the InferShape function 
when using ZeroPadding2D:
   
![image](https://user-images.githubusercontent.com/32777264/127987453-90c01c12-2031-4996-8158-8409997d1ff0.png)
   The rank of the input should be 4 dimensions but this function wrongly 
inferences the dimension.
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to