Keras input shape mismatch error for multi-feature CNN classification model - numpy

Here is my code:
model = Sequential()
model.add(Conv1D(32, kernel_size=3,
activation='relu',
input_shape=(14,1)))
model.add(MaxPooling1D(pool_size=1))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='softmax'))
model.summary()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(X_train.values, y_train.values,
batch_size=4,
epochs=1,
verbose=2,
validation_data=(X_test.values,y_test.values))
And error is:
Error when checking input: expected conv1d_35_input to have 3 dimensions, but got array with shape (13166, 14)
As suggested by other posts, I tweaked with flatten layer before output layer but that did not work.
My X_train.values.shape gives (13166, 14)
Any suggestion how should I fix this?

You need to reshape the X_train.values from (13166, 14) to (13166, 14, 1) as your input shape of CNN network is (None, 14, 1).
This may solve your problem:
X_train.values.reshape([-1,14,1])

Related

ValueError: Shapes (None, 9) and (None, 22000, 9) are incompatible

The following is my code:
The shape of my X_train is TensorShape([600, 22000, 5])
The shape of my Y_train is (600, 9)
Is there an error with the type of data that I am using for this time-series problem?
model = Sequential()
model.add(LSTM(256,return_sequences=True,input_shape=(22000, 5)))
model.add(Dense(9, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
#print(model.summary())
model.fit(allfileswow[:600], features_a1[:600], epochs=100,verbose=0)
Hi just like #Djinn mentioned add a flatten layer to make everything 1-D
model = Sequential()
model.add(LSTM(256,return_sequences=True,input_shape=(22000, 5)))
model.add(Flatten())
model.add(Dense(9, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
Try removing 'return_sequences=True' as you don't need full sequence as the output for the next layer.
Refer lstm

Why the input size ot shape is not valid?

I know that similar questions were asked before, but the solutions didn't helped me.
I have the following model:
model = Sequential()
# CNN
model.add(Conv2D(filters=16, kernel_size=2, input_shape=(40, 2000, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
# CNN
model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
# CNN
model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
# CNN
model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(GlobalAveragePooling2D())
model.add(Dense(num_labels, activation='softmax'))
optimizer = optimizers.SGD(lr=0.002, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
I'm trying to fit the model:
model.fit(X_train, y_train_hot, batch_size=10, epochs=50,
validation_data=(X_test, y_test_hot))
where
X_train.shape = {tuple:3} (246, 40, 2000)
from other post I read (Keras input_shape for conv2d and manually loaded images) it seems that my input is right.
But I'm getting the following error:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 40, 2000]
What am I missing ? and how can I fix it ?
As you saw from the link you posted you must reshape your data to be 4dim.
X_train = X_train.reshape(246, 40, 2000, 1)
or
X_train = X_train.reshape(-1, 40, 2000, 1)
4D: [batch_size, img_height, img_width, number_of_channels]
The error is that you did not include the 4-th axis(i.e. axis=3 or axis -1 for that matter).
Here, you can see the following:
expected min_ndim=4, found ndim=3. Full shape received: [None, 40,
2000]
The None translates to the batch size, which of course is variable and set to None.
Then, you have 40, 2000, which should correspond to height and width respectively.
At the same time, remember that you wrote in your code that the input shape your network expects is input_shape=(40, 2000, 1) not (40,2000).
You need to explicitly add the "color"/"number of channels" axis, the 3-rd channel in this case, so you need to either use reshape or expand_dims to achieve that.
For demonstrative purposes, suppose that X is of shape (40,2000), then reshape X to X = X.reshape(40,2000,1)

Why I'm getting error incompatible with the layer?

I have a cnn network, which I'm trying to test.
I'm getting errors about the input, and I can't figure why
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(99,13,1)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, batch_size=4, epochs=10, verbose=1, validation_data=(x_test, y_test))
where:
x_train / test .shape = {tuple: 3}(30, 99, 13)
y_train / test shape = {tuple: 1}30
Error:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 99, 13]
What's wrong and how can I fix it ?
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(99,13,1)))
Model waiting for shape [batch, (99,13,1)], you are trying to feed [batch, 99, 13].
I think you need change your input to input_shape=(99,13) in order to match train data
Conv2D requires 4D+ shape
Expand dimension of your data to batch,99,13,1

How to load fine-tuned keras model

I am following this tutorial to try fine-tuning using VGG16 model, I trained the model and saved .h5 file using model.save_weights and
vgg_conv = VGG16(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3))
# Freeze the layers except the last 4 layers
for layer in vgg_conv.layers[:-4]:
layer.trainable = False
model = Sequential()
model.add(vgg_conv)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(11, activation='softmax'))
I then tried to rebuild the architecture and load weights using the below
def create_model(self):
model = Sequential()
vgg_model = VGG16(include_top=False, weights='imagenet', input_shape=(150, 150, 3))
model.add(vgg_model)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(11, activation='softmax'))
model.load_weights(self.top_model_weights_path) # throws error
return model
but it then throws this error
ValueError: Cannot feed value of shape (512, 512, 3, 3) for Tensor 'Placeholder:0', which has shape '(3, 3, 3, 64)'
what am I doing wrong?
I am not sure how to intepret the error but you could try saving the model architecture and the weights together model.save("model.h5") after fine tuning.
To load the model you can type
model = load_model('model.h5')
# summarize model.
model.summary()
I think this has the benefeit of not having to rebuild the model and requires only one line to acomplish the same purpose.
The problem comes from the trainable difference between the two models. If you freeze the 4 last layers in the create_model function, it will work.
But as said by Igna, model.save and model.load_model is simpler.

Input dimension mismatch between dense layers and conv layers of imagenet while attempting transfer learning

I am trying to train dense layers on top of the conv layers of InceptionV3.
But I'm unable to initialize the fully connected model. I'm getting a ValueError.
model_inc = applications.InceptionV3(weights='imagenet',
include_top=False)
model = Sequential()
model.add(Flatten(input_shape=model_inc.output_shape[1:]))
model.add(Dense(256, activation= 'relu', kernel_initializer='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(9, activation='softmax'))
I expected the model to compile successfully but I get "ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 2048). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model."
You want your input to go to your model_inc so you have to define the input_shape there. Something like the following should work
model_inc = applications.InceptionV3(input_shape=(224,224,3), weights='imagenet',
include_top=False)
model = Sequential()
# you need to add your base model
model.add(model_inc)
model.add(Flatten())
model.add(Dense(256, activation= 'relu', kernel_initializer='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(9, activation='softmax'))