Keras CNN model predicting same output values for every example

161 views
Skip to first unread message

Sagnik Acharya

unread,
Feb 28, 2021, 10:55:57 AM2/28/21
to Keras-users
Hi,
I'm trying to build a CNN using Keras. The inputs are (128, 128) images and I need to predict 7 parameters from these images (not a classification problem, I need values of all 7 parameters).

The input data has been normalized to be in the range (-1, 1). The 7 output labels have also been normalized the same way. My model code is shown as below:

from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten

model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=(128,128,1)))
model.add(MaxPooling2D((2,2), padding="valid"))
model.add(Conv2D(32, kernel_size=3, activation='relu'))
model.add(MaxPooling2D((2,2), padding="valid"))
model.add(Conv2D(16, kernel_size=3, activation='relu'))
model.add(MaxPooling2D((2,2), padding="valid"))
model.add(Flatten())
model.add(Dense(7, activation='tanh'))

model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

sol = model.fit(X_train, Y_train, epochs=5, batch_size=128)

The problem now is that the model always predicts the same 7 parameters for any example in the test set. Even for the training set, the predicted values are not satisfactory (I'm getting an accuracy of 11%). My question is that is there some problem with my implementation of this CNN? Is it some problem with the activation functions or loss function?

If not, then it must be that my data is not properly fed into this model. Any suggestions on that will also be very helpful.

Best regards

Lance Norskog

unread,
Mar 1, 2021, 1:24:52 AM3/1/21
to Sagnik Acharya, Keras-users
I think that binary_crossentropy assumes output values of 0->1.

How large are the training and validation sets? 

Also, 5 epochs might not be enough.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/03219dde-ed03-44ba-a8f2-690a5feec5e0n%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA
Message has been deleted

Sagnik Acharya

unread,
Mar 1, 2021, 3:51:52 AM3/1/21
to Keras-users
Oh, I totally overlooked the fact that binary crossentropy assumes outputs between 0 and 1. Thanks for clarifying that.

I have a total of 5000 examples, I'm using 4700 of them for training and 300 for validation.

I know that 5 epochs is too small for training. I'm just using that to test if my model improves even a little bit every epoch.

I'll try out your suggestions and update this thread with my observations. Thanks!
Reply all
Reply to author
Forward
0 new messages