Is there a way to fix the dense layer shape when adding conv2d layer with lstm? - python-3.x

I am trying to fit my data into a conv2d+lstm layers but I got an error in the last dense layer
i already tried to reshape but it gives me the same error .. and because I am new in python I couldn't understand how to fix my error My model is about combining cnn with lstm layer and i have 2892 training images and 1896 testing images with total 4788 images each image with size 128*128
And here is the final model summary
Here some of my code
cnn_model = Sequential()
cnn_model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(128,128,3)))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(32, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(64, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(128, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Flatten())
model = Sequential()
model.add(cnn_model)
model.add(Reshape((4608, 1)))
model.add(LSTM(16, return_sequences=True, dropout=0.5))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
X_data = np.array(X_data)
X_datatest = np.array(X_datatest)
X_data= X_data.astype('float32') / 255.
X_datatest = X_datatest.astype('float32') / 255.
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest))
The error I expected to be in the dense layer as its outputs the following error
Traceback (most recent call last): File
"C:\Users\bdyssm\Desktop\Master\LSTMCNN2.py", line 212, in
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest)) File
"C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py",
line 952, in fit
batch_size=batch_size) File "C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py",
line 789, in _standardize_user_data
exception_prefix='target') File "C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training_utils.py",
line 128, in standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected dense_1 to have 3
dimensions, but got array withshape (2892, 128, 128, 3)
and this is the cnn_model summary

Related

Keras conv2D input shape error. Error when checking input: expected conv2d_4_input to have 4 dimensions, but got array with shape (58, 195, 1)

I face issue regarding inputshape. During the model.fit(), I get an value error
ValueError: Error when checking input: expected conv2d_4_input to have 4 dimensions, but got array
with shape (58, 195, 1)
num_columns=195 #features
train_x = train_x.reshape(train_x.shape[0], num_columns,1)#array of size 11252. i.e 58*195
test_x = test_x.reshape(test_x.shape[0], num_columns,1)#array size 18*195
valid_x=valid_x.reshape(valid_x.shape[0], num_columns,1) #array size 22*195
model=Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=(58,195,1)))
model.add(Conv2D(64, (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_labels, activation='softmax'))
Is there a way to deal with this? I also tried reshaping data as (-1,58,195,1). It didn't work

How should I fit the sample data in model combining CNN and LSTM

I have a sample data of page visits of one page for 803 days. I have extracted features from data like mean, median etc and final shape of data is (803, 25). I had taken a train set of 640 and a test set of 160. I am trying to use CNN+LSTM model using Keras. But I am getting an error in model.fit method.
I have tried permute layer and changed input shapes but still not able to fix it.
trainX.shape = (642, 1, 25)
trainY.shape = (642,)
testX.shape = (161, 1, 25)
testY.shape = (161,)
'''python
# Basic layer
model = Sequential()
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), strides=1, padding='SAME', input_shape = (642, 25, 1), activation = 'relu')))
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Convolution2D(32, 3, 3, activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(Permute((2, 3), input_shape=(1, 25)))
model.add(LSTM(units=54, return_sequences=True))
# To avoid overfitting
model.add(Dropout(0.2))
# Adding 6 more layers
model.add(LSTM(units=25, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=54))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(units = 1, activation='relu', kernel_regularizer=regularizers.l1(0.0001))))
model.add(PReLU(weights=None, alpha_initializer="zero")) # add an advanced activation
model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
model.fit(trainX, trainY, epochs = 50, batch_size = 32)
predictions = model.predict(testX)
'''
#Runtime Error
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-218-86932db86d0b> in <module>()
42
43 model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
---> 44 model.fit(trainX, trainY, epochs = 50, batch_size = 32)
Error - IndexError: list index out of range
The input_shape requires a tuple of length 4 when it using TimeDistributed with Conv2D
see https://keras.io/layers/wrappers/
input_shape=(10, 299, 299, 3))
Dont you think your data field is too little? usually, CNN+LSTM is meant for a more complicated task with thousands of sequential images/videos.

Is there a way to make the of input channels in python equals to dimension of filters?

My problem here that I want to make the number of input channels in python equals to dimension of filters
i already tried to reshape but it gives me the same error .. and because I am new in python I couldn't understand how to fix my error
My model is about combining cnn with lstm layer and i have 2892 training images and 1896 testing images with total 4788 images each image with size 128*128
here some code of what i had tried
cnn_model = Sequential()
cnn_model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(128,128,3)))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(32, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(64, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(128, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Flatten())
model = Sequential()
model.add(TimeDistributed(cnn_model, input_shape=(1,128, 128,3)))
model.add(LSTM(16, return_sequences=True, dropout=0.5))
model.add(Dense(1, activation='softmax'))
model.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy'])
X_data = np.array(X_data)
X_datatest = np.array(X_datatest)
X_data= X_data.astype('float32') / 255.
X_datatest = X_datatest.astype('float32') / 255.
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest))
when trying the previuos code the following error showed up
Traceback (most recent call last): File
"C:\Users\bdyssm\Desktop\Master\LSTMCNN2.py", line 219, in
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest)) File
"C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py",
line 952, in fit
batch_size=batch_size) File "C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py",
line 751, in _standardize_user_data
exception_prefix='input') File "C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training_utils.py",
line 128, in standardize_input_data
'with shape ' + str(data_shape)) ValueError: Error when checking input: expected time_distributed_1_input to have 5 dimensions, but got
array with shape (2892, 28, 28, 3)
This is the model summary
This is the cnn_model summary
The problem is that your cnn_model has changed the shape of your signal to have 128 channels insteal of 3 color channels, but you are not taking this into account when declaring the input shape of model.
Examine the output shape of cnn_model with cnn_model.summary() and make sure to have input shape of model equal to the output shape of cnn_model.

Keras Error - expected block5_pool to have 4 dimensions, but got array with shape (6, 1)?

i am trying to use VGG16 pretrained in Keras, but i keep getting this error:
ValueError: Error when checking target: expected block5_pool to have 4 dimensions, but got array with shape (6, 1)
What is the error mean?
Error when checking target: expected block5_pool to have 4 dimensions,
but got array with shape (6, 1)
This the my actual code:
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 224, 224
train_data_dir = 'database/train'
validation_data_dir = 'database/validation'
nb_train_samples = 2000
nb_validation_samples = 26
epochs = 50
batch_size = 20
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
print('VGG-16 Model loaded.')
top_model = Sequential()
top_model.add(ZeroPadding2D((1,1),input_shape=input_shape))
top_model.add(Conv2D(64, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(64, (3, 3), activation='relu'))
top_model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(128, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(128, (3, 3), activation='relu'))
top_model.add(MaxPooling2D((2,2), strides=(2,2)))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(256, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(256, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(256, (3, 3), activation='relu'))
top_model.add(MaxPooling2D((2,2), strides=(2,2)))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(MaxPooling2D((2,2), strides=(2,2)))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(MaxPooling2D((2,2), strides=(2,2)))
top_model.add(Flatten())
top_model.add(Dense(4096, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(12))
top_model.add(Activation('softmax'))
# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
# top_model.load_weights('./vgg16_face_weights.h5')
# add the model on top of the convolutional base
model.add_update(top_model)
# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:25]:
layer.trainable = False
model.compile(loss='sparse_categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save_weights('first_try.h5')
The terminal run:
VGG-16 Model loaded.
Found 46 images belonging to 12 classes.
Found 26 images belonging to 12 classes.
Epoch 1/50
Traceback (most recent call last):
File "C:/Users/w024029h/PycharmProjects/keras_pretrained/pretrained.py", line 113, in <module>
validation_steps=nb_validation_samples // batch_size)
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 2230, in fit_generator
class_weight=class_weight)
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1877, in train_on_batch
class_weight=class_weight)
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1480, in _standardize_user_data
exception_prefix='target')
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected block5_pool to have 4 dimensions, but got array with shape (6, 1)
I had this same error before. I resolved it by changing the class_mode on the data generators from 'binary' to None
According to the keras docs, setting the class_mode to 'binary' will return a 1-D array.
By changing the class_mode=None, no labels are returned and the generator will only yield batches of image data. This is what you are expecting with your model and this works well with the fit_generator() method. However, when using class_mode None, the data still needs to reside in a subdirectory of directory for it to work correctly.
This page was useful to me
^look at flow_from_directory()
Hope this helps!

Keras ValueError: ValueError: Error when checking target: expected dense_4 to have shape (None, 2) but got array with shape (2592, 1) Python3

I am having an issue when trying to train my model in Keras 2.0.8, Python 3.6.1, and a Tensorflow Backend.
Error Message:
ValueError: Error when checking target: expected dense_4 to have shape (None, 2) but got array with shape (2592, 1)
X_train = numpy.swapaxes(X_train, 1, 3)
X_test = numpy.swapaxes(X_test, 1, 3)
print("X_train shape: ") --> size = (2592, 1, 1366, 96)
print("-----")
print("X_test shape") --> size = (648, 1, 1366, 96)
print("-----")
print(Y_train.shape) --> size = (2592,)
print("-----")
print("Y_test shape") --> size = (648,)
Relevant Code snippets:
K.set_image_dim_ordering('th')
K.set_image_data_format('channels_first')
def create_model(weights_path=None):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu', padding="same", input_shape=(1, 1366, 96)))
model.add(Conv2D(64, (3, 3), activation='relu', dim_ordering="th"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(16, activation='relu'))
model.add(Dense(2, activation='softmax'))
if weights_path:
model.load_weights(weights_path)
return model
model = create_model()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.SGD(lr=0.01),
metrics=['accuracy'])
history = model.fit(X_train, Y_train,
batch_size=32,
epochs=100,
verbose=1,
validation_data=(X_test, Y_test))
Line 142, where I call model.fit() is where I am getting this error
Things I have tried to fix this error
Referenced these stack overflow posts:
I tried to reshape the Y_test and Y_train numpy arrays using the following code:
Y_train.reshape(2592, 2)
Y_test.reshape(648, 2)
However, I get the following error:
ValueError: cannot reshape array of size 2592 into shape (2592,2)
It seems to me you need to change the last layer of the NN:
def create_model(weights_path=None):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu', padding="same", input_shape=(1, 1366, 96)))
model.add(Conv2D(64, (3, 3), activation='relu', dim_ordering="th"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
if weights_path:
model.load_weights(weights_path)
return model
As you are using the categorical_crossentropy loss, you have to use one-hot encoded labels. For this you can use the function to_categorical from keras.utils.np_utils
from keras.utils import np_utils
y_train_onehot = np_utils.to_categorical(y_train)
y_test_onehot = np_utils.to_categorical(y_test)
Then use the one-hot encoded labels to train your model.

Resources