Keras: Input shape for CNN is incompatible

Created on 11 Apr 2019  路  4Comments  路  Source: keras-team/keras

I want to train a model to predict one's emotion from the physical signals. I have three physical signals and using it as input features;

ecg(Electrocardiography), gsr(galvanic skin response), temp(temparature)

In my dataset, there are 312 records for each participant and in every record, there are 18000 rows of data. So when I combine them into a single data frame, there are 5616000 rows in total.

Here is my x_train dataframe;

           ecg     gsr   temp
0        0.1912  0.0000  40.10
1        0.3597  0.0000  40.26
2        0.3597  0.0000  40.20
3        0.3597  0.0000  40.20
4        0.3597  0.0000  40.33
5        0.3597  0.0000  40.03
6        0.2739  0.0039  40.13
7        0.1641  0.0031  40.20
8        0.0776  0.0025  40.20
9        0.0005  0.0020  40.26
10      -0.0375  0.0016  40.03
11      -0.0676  0.0013  40.16
12      -0.1071  0.0010  40.20
13      -0.1197  0.0047  40.20
..      .......  ......  .....
..      .......  ......  .....
..      .......  ......  .....
5616000 0.0226  0.1803  38.43

And I have 6 classes which are corresponding to emotions. I have encoded these labels with numbers;

anger = 0, calmness = 1, disgust = 2, fear = 3, happiness = 4, sadness = 5

Here is my y_train;

         emotion
0              0
1              0
2              0
3              0
4              0
.              .
.              .
.              .
18001          1
18002          1
18003          1
.              .
.              .
.              .
360001         2
360002         2
360003         2
.              .
.              .
.              .
.              .
5616000        5

To feed my CNN model, I needed to reshape my train examples. I have done it like this;

train_x = train_x.values.reshape(5616000,3,1) #because I have 5616000 rows and 3 input features
train_y = train_y.values.reshape(5616000,1)

After reshaping, I have created my CNN model;

model = Sequential()
model.add(Conv1D(100,700,activation='relu',input_shape=(5616000,3)))
model.add(Conv1D(100,700,activation='relu'))
model.add(MaxPooling1D(4))
model.add(Conv1D(160,700,activation='relu'))
model.add(Conv1D(160,700,activation='relu'))
model.add(GlobalAveragePooling1D())
model.add(Dropout(0.5))
model.add(Dense(1,activation='softmax'))

model.compile(optimizer = sgd, loss = 'binary_crossentropy', metrics = ['acc'])
model.fit(train_x,train_y,epochs = 300, batch_size = 32, validation_split=0.33, shuffle=False)

And this gave me the following error;

ValueError: Error when checking input: expected conv1d_96_input to have shape (5616000, 3) but got array with shape (3, 1)

Whatever I have tried, I could not make it work. Any help is appreciated, thanks.

All 4 comments

I do not know how much you know about cnn's, but let's go.

you first need to set a default resolution for the neural network

if you look in google: alexnet input size
will find that the alex net's input size is 227x227

then try this:
input_shape = (image_size, image_size, 3)

in the example I gave from alexnet, it should be:
input_shape = (227, 227, 3)

I do not know how much you know about cnn's, but let's go.

you first need to set a default resolution for the neural network

if you look in google: alexnet input size
will find that the alex net's input size is 227x227

then try this:
input_shape = (image_size, image_size, 3)

in the example I gave from alexnet, it should be:
input_shape = (227, 227, 3)

So you are saying, I need to see this signal data as an image? It has 5610000 rows and 3 columns, so the input shape should be (5610000, 3, 1) ?

No, it should be the input dimensions of each image.

Look at this image, the part that says INPUT DATA
Image

227 x 227 x 3

This means that the neural network will resize the images so they have 224 pixels of widht and 224 pixels of height.
3 means 3 layers (RedGreenBlue)

If it were input size: 227 x 227 x 1, it would probably be a black and white image as input.

Try to enter this 227 x 227 x 3

The larger the input dimension (1024x1024x3), this will increase its accuracy, processing time and memory usage

You should test and choose the best situation.

I recommend testing with 64x64x3
and next time 128x128x3.

Always in power of 2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

braingineer picture braingineer  路  3Comments

amityaffliction picture amityaffliction  路  3Comments

farizrahman4u picture farizrahman4u  路  3Comments

KeironO picture KeironO  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments