awslabs / keras-apache-mxnet

[DEPRECATED] Amazon Deep Learning's Keras with Apache MXNet support
https://github.com/awslabs/keras-apache-mxnet/wiki
Other
290 stars 65 forks source link

Error in Conv1D after Lambda dropping 1 input axis #161

Open michelegalante opened 6 years ago

michelegalante commented 6 years ago

This code uses a Lambda layer to select a subset of the input (data format is channels_first):

import keras
NSTEPS=100
NFEATURES=10
NGROUPS=3
a = keras.layers.Input((NFEATURES,NGROUPS,NSTEPS))
print(a.shape)
b = keras.layers.Lambda(lambda x: x[:,:,1,:], output_shape=(NFEATURES,NSTEPS))(a)
print(b.shape)
c = keras.layers.Conv1D(8, 5)(b)
print(c.shape)

With tensorflow backend I get this output, as expected:

(?, 10, 3, 100)
(?, 10, 100)
(?, 8, 96)

But with mxnet backend I get an error in Conv1D:

(None, 10, 3, 100)
(None, 10, 100)
---------------------------------------------------------------------------
MXNetError                                Traceback (most recent call last)
...
MXNetError: Error in operator conv1d_1/conv1d1: [21:37:32] src/operator/nn/convolution.cc:148: Check failed: dshp.ndim() == 4U (5 vs. 4) Input data should be 4D in batch-num_filter-y-x
roywei commented 6 years ago

@michelegalante Hi, thank you for submitting this issue. We will take a look. Seems like the shape information is wrong when passed to Conv1D after Lambda in MXNet backend.

Mean while, maybe there is some work arounds to explore for your use case? How about creating another input with x[:,:,1,:] and concatenate to your network.

michelegalante commented 6 years ago

Hi roywei,

after some research I found this workaround:

import keras

def select_axis(x):
    return keras.backend.reshape(x[:,:,1,:], (-1,NFEATURES,NSTEPS))

NSTEPS=100
NFEATURES=10
NGROUPS=3
a = keras.layers.Input((NFEATURES,NGROUPS,NSTEPS))
print(a.shape)
b = keras.layers.Lambda(select_axis)(a)
print(b.shape)
c = keras.layers.Conv1D(8, 5)(b)
print(c.shape)

I suspect the problem is related to #113 and #120 (incorrect shape after slice operation).

roywei commented 6 years ago

@michelegalante Yes, the shape information is lost after slicing in Lambda, during infer_shape_partial() on mxnet symbol. Please use the workaround now while we fix it. @sandeep-krishnamurthy could you help verify? Seems #120 still exists

sandeep-krishnamurthy commented 6 years ago

Can you please install MXNet master build to get the fix. You can do with --pre option in pypi

pip install mxnet-mkl --pre # For CPU
pip install mxnet-cu90 --pre # For GPU
roywei commented 6 years ago

On my side with mxnet-mkl --pre it's failing with the following error:

Traceback (most recent call last):
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/backend/test.py", line 86, in <module>
    c = keras.layers.Conv1D(8, 5)(b)
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/engine/base_layer.py", line 432, in __call__
    self.build(input_shapes[0])
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/layers/convolutional.py", line 142, in build
    constraint=self.kernel_constraint)
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/engine/base_layer.py", line 252, in add_weight
    constraint=constraint)
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/backend/mxnet_backend.py", line 235, in variable
    if hasattr(value, "shape") and value.shape == (1,):
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/backend/mxnet_backend.py", line 3946, in shape
    return self._get_shape()
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/backend/mxnet_backend.py", line 3955, in _get_shape
    _, out_shape, _,  _ = self.symbol.infer_shape_partial()
ValueError: not enough values to unpack (expected 4, got 3)