Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative dimension size caused by subtracting 50 from 1 for 'depthwise_conv2d_5/depthwise' (op: 'DepthwiseConv2dNative') with input shapes: [?,1,50,8], [50,1,8,2]. #13

Closed
sagihaider opened this issue Nov 6, 2019 · 13 comments

Comments

@sagihaider
Copy link

I am using EEGNet model for classifying 4-class BCI competition IV Dataset 2a for Motor Imagery detection i.e. detecting (SMR)
I read the data for one subject using the MAT file I have. After reading the training data, the x

print(np.shape(X))
print(np.shape(y))

The output is :

(273, 22, 1500)
(273, 1)

where 272 are trials, 22 channels, 1500 samples from each trial

I partitioned and cropped the data. The cropping I did because the data in the region 500:1000 (i.e. from the start of the cue to the 2 sec after cue) is much related for ERD/ERS.

# take 50/25/25 percent of the data to train/validate/test
X_train      = X[0:150,:,500:1000]
Y_train      = y[0:150]
X_validate   = X[151:200,:,500:1000]
Y_validate   = y[151:200]
X_test       = X[201:,:,500:1000]
Y_test       = y[201:]
print(np.shape(X_train))
print(np.shape(Y_train))
print(np.shape(X_validate))
print(np.shape(Y_validate))
print(np.shape(X_test))
print(np.shape(Y_test))

Output:

(150, 22, 500)
(150, 1)
(49, 22, 500)
(49, 1)
(72, 22, 500)
(72, 1)

Following parameter, I used and reshaped the data

kernels, chans, samples = 1, 22, 500
# convert data to NCHW (trials, kernels, channels, samples) format. Data 
# contains 22 channels and 500 time-points. Set the number of kernels to 1.
X_train      = X_train.reshape(X_train.shape[0], kernels, chans, samples)
X_validate   = X_validate.reshape(X_validate.shape[0], kernels, chans, samples)
X_test       = X_test.reshape(X_test.shape[0], kernels, chans, samples)
   
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

Output:

X_train shape: (150, 1, 22, 500)
150 train samples
72 test samples

Now, while creating the model

model = EEGNet(nb_classes = 4, Chans = 22, Samples = 500, 
             dropoutRate = 0.5, kernLength = 25, F1 = 8, 
             D = 2, F2 = 16, norm_rate = 0.25, dropoutType = 'Dropout')

Now the error, I am getting is given as follows:

ValueError: Negative dimension size caused by subtracting 22 from 8 for 'depthwise_conv2d/depthwise' (op: 'DepthwiseConv2dNative') with input shapes: [?,8,22,500], [22,1,500,2].

I am wondering what mistake I am making? any clue, please
Many thanks

@vlawhern
Copy link
Owner

vlawhern commented Nov 7, 2019

You'll need to change the Keras variable "image_data_format" to "channels_first". You can do this in one of two ways:

  1. Change it in your keras.json configuration file (https://keras.io/backend/#switching-from-one-backend-to-another)
  2. Include the following two lines at the top of your code
from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')

@sagihaider
Copy link
Author

Many thanks, working now.

@sagihaider
Copy link
Author

Hi,
I did run your EEGNet model with BCI competition IV Dataset 2A for 4-classes.
In total, there were 288 trials, 22 channels, and 1750 is the trial length.

The input, I have to your model is

273 trials because rest are discarded as bad trials, 22 channels, and from each trial, I only selected the data after the cue from 0s to 3s after cue. In code sample =750. In 273 trials, I kept 200 for training and 73 for validation.

Results: I kept 100 epochs, training accuracy was 91% and validation was 73% which I think is higher than what you have said in your paper. I remember from 4 class you reported 44%. I am wondering is that on Session-1 data or you tested it in session-II data.
If you want, please see my Repo
Colab Notebook

@vlawhern
Copy link
Owner

vlawhern commented Nov 7, 2019

So there are a couple of things to note:

  1. The paper uses [0.5, 2.5]s post cue as the data window. This was chosen primarily to avoid the visual-evoked potential that appears due to presentation of the visual cue. I believe there is some documentation stating that for this data eye movements are more prevalent in the first 0.5s since participants visually attended to the cue. The presence of the VEP probably doesn't effect overall classification performance since every trial would have it, but I've seen [0.5, 2.5]s used in a couple other papers so I decided to use that.

  2. I don't perform any data/trial rejection due to artifacts. I just took the data as-is and filtered it at 4-40Hz according to the pipeline described by @robintibor in (https://onlinelibrary.wiley.com/doi/full/10.1002/hbm.23730), his code-base is at (https://github.com/TNTLFreiburg/braindecode)

  3. I down-sampled the data to 128Hz instead of the default 250Hz; I don't think this really matters much in terms of classification performance.

  4. I did 4-fold block-wise cross-validation, fixing the test set to be the official BCI IV 2A test set. What this means is: take each subjects training data, divide it into three contiguous, non-overlapping blocks. Choose two of those blocks to be the training set, set the remaining block to be the validation set. Perform minibatch gradient descent, minimizing validation set categorical cross-entropy loss. Then evaluate model on the held-out test set. The classification performances reported in the paper are the test set classification accuracies averaged across all folds.

So your results may still be right; having a larger window at [0,3]s could potentially improve classification performance. This is something I haven't really tested.

@sagihaider
Copy link
Author

Dear,

Many thanks for your suggestions. I used Dataset 2A and trained on session 1 data and evaluated it in Session 2 data. The transitioning phase from one session to others has a covariate shift in the data for each subject. But, your model is working very well just changing a few parameters.
Thanks. Good work guys :-)

@vlawhern
Copy link
Owner

Good to hear! Let me know if any other issues arise.

@mariasapantan
Copy link

Hello! I had the same problem, but when I put these lines from tensorflow.keras import backend as K K.set_image_data_format('channels_first'), I receive tensorflow.python.framework.errors_impl.InvalidArgumentError: Default AvgPoolingOp only supports NHWC on device type CPU. How could I solve this?

@vlawhern
Copy link
Owner

@mariasapantan what version of tensorflow are you using and how did you install tensorflow?

The above error comes up if you installed the CPU version of tensorflow through pip; i.e. pip install tensorflow, see Issue #18. There are a couple of ways around this:

  1. Use the GPU version of Tensorflow if you have a supported GPU.
  2. If you want to use the CPU version of tensorflow and using the Anaconda python distribution, install tensorflow through conda as conda install tensorflow. Alternatively, if you're not using the Anaconda python distribution you can install the intel-tensorflow package through pip as pip install intel-tensorflow which is a version of Tensorflow compiled against the Intel MKL which fixes the above issue.

@mariasapantan
Copy link

I'm not using Anaconda distribution and I have CPU version. I installed pip install intel-tensorflow. Now I have to uninstall tensorflow or what's next?

@vlawhern
Copy link
Owner

Yes I believe you should uninstall all previous versions of tensorflow and install intel-tensorflow. Then things "should" work out of the box.

@mariasapantan
Copy link

It seens like the tensorflow is uninstalled when I put pip list , but when I look at project interpreter in PyCharm, tensorflow is there. Intel-tensorflow does not have keras when I try to import it

@vlawhern
Copy link
Owner

So I ran a quick test using the intel-tensorflow package and everything works for me:

from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')

you may have to restart your interpreter after you make package changes? I don't use pycharm so I'm not sure how to troubleshoot there..

Note I'm using Python 3.7, but I doubt this matters much whether you're on Python 3.7/3.8/etc. You should double-check if all tensorflow packages are uninstalled, then try to install intel-tensorflow again.

Otherwise I'm not sure how to help from here..

@mariasapantan
Copy link

I have AMD processor. I tried a lot of things, but it does not work for me. Could you help me via TeamViewer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants