I have read a sequence of images (frames) into a numpy array with shape (9135, 200, 200, 4) where 9135 is the sample size, 200 is height and width in 4 channel (R-G-B-Depth) images.
I have a sequential model with an LSTM layer:
x_train=np.reshape(x_train,(x_train.shape[0],x_train.shape[1],x_train.shape[2],x_train.shape[3],1))
#(9135, 200, 200, 4, 1)
x_val=np.reshape(x_val,(x_val.shape[0],x_val.shape[1],x_val.shape[2],x_val.shape[3],1))
#(3046, 200, 200, 4, 1)
model = Sequential()
model.add(TimeDistributed(Conv2D(64, (3,3), activation='relu'), input_shape=(200, 200, 4)))
model.add(TimeDistributed(Conv2D(64, (3,3), activation='relu')))
model.add(TimeDistributed(GlobalAveragePooling2D()))
model.add(LSTM(1024, activation='relu', return_sequences=False))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(10, activation='sigmoid'))
model.compile('adam', loss='categorical_crossentropy')
model.summary()
history = model.fit(x_train, y_train, epochs=epochs,batch_size=batch_size,verbose=verbose, validation_data=(x_val, y_val))
but there is an error in the result:
ValueError: Input 0 of layer conv2d is incompatible with the layer: :
expected min_ndim=4, found ndim=3. Full shape received: [None, 200, 4]
What is the suggested way to input a 4 channel image into an LSTM layer in Keras?
PS: Εach class has different frames so I do not know how to put unstable timestep
You need to reshape
x_train=np.reshape(x_train,(x_train.shape[0],1,x_train.shape[1],x_train.shape[2],x_train.shape[3]))
#(9135,1 200, 200,4)
x_val=np.reshape(x_val,(x_val.shape[0],1,x_val.shape[1],x_val.shape[2],x_val.shape[3]))
#(3046,1 200, 200,4)
and change the input_shape of model to input_shape=(None,200, 200, 4)))
Related
I am working on a dataset with following input shapes of X and Y
>>> print(X_train.shape, Y_train.shape)
(211968, 1024, 2) (211968, 24)
Here's my simple model with summary and the error:
batch_size = 128
hidden_units = 256
dropout = 0.45
model = Sequential()
model.add(Dense(hidden_units,input_shape=(1024,2)))
model.add(Activation('relu'))
model.add(Dropout(dropout))
model.add(Dense(hidden_units))
model.add(Activation('relu'))
model.add(Dropout(dropout))
model.add(Dense(24))
model.add(Activation('softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(X_train, Y_train, epochs=30, batch_size=batch_size)
ValueError: Shapes (128, 24) and (128, 1024, 24) are incompatible
The input to Dense Layer must be one dimensional vector. So a Flatten() operation before the Dense Layer did the trick.
I keep on getting this error related to the input shape. Any help would be very appreciated. Thank you!
def deep_model():
model = Sequential()
model.add(keras.layers.Conv1D(filters=50, kernel_size=9, strides=1, padding='same',
batch_input_shape=(None, Length, 4), activation='relu'))
model.add(keras.layers.Conv2D(32, kernel_size =(9, 9), strides =(1, 1),
activation ='relu'))
model.add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2)))
model.add(keras.layers.Conv2D(64, (9, 9), activation ='relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
model.add(Flatten())
model.add(Dense(100, activation ='relu'))
model.add(Dropout(0.3))
model.add(Dense(3, activation ='softmax'))
# training the model
model.compile(loss = keras.losses.categorical_crossentropy,
optimizer = keras.optimizers.SGD(lr = 0.01),
metrics =['accuracy'])
return model
x_train = x_train.reshape(-1, 400, 4)
raise ValueError('Input ' + str(input_index) + ' of layer ' +
ValueError: Input 0 of layer conv2d is incompatible with the layer: :
expected min_ndim=4, found ndim=3. Full shape received: (None, 400,
50)
I tried to reshape the x_train also but got this error:
x_train = x_train.reshape(-1, 400, 400, 4) ValueError: cannot reshape
array of size 43200000 into shape (400,400,4)
I think you need the input shape to have an extra dimension.
The reason your reshaping failed is because 400 * 400 * 4 is equal to 640000 and not 43200000
This is how you would do it:
x_train = x_train[np.newaxis]
I'm modifying an old code, by adding attention layer to a model. But I'm not able to figure out how to stack the layers with correct input size.
The actual input data would be (200,189,1).
//I'm trying something like this
def mocap_model(optimizer='SGD'):
model = Sequential()
model.add(Conv2D(32, 3, strides=(2, 2), padding ='same', input_shape=(200, 189, 1)))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Conv2D(64, 3, strides=(2, 2), padding ='same'))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Conv2D(64, 3, strides=(2, 2), padding ='same'))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Conv2D(128, 3, strides=(2, 2), padding ='same'))
model.add(Dropout(0.2))
model.add(Flatten())
return model
cnn = mocap_model()
main_input = Input(shape=(200, 189, 1))
rnn = Sequential()
rnn = LSTM(256, return_sequences=True, input_shape=(200,189))
model = TimeDistributed(cnn)(main_input)
model = rnn(model)
att_in=LSTM(256,return_sequences=True,dropout=0.3,recurrent_dropout=0.2)(model)
att_out=attention()(att_in)
output3=Dense(256,activation='relu',trainable=True)(att_out)
output4=Dense(4,activation='softmax',trainable=True)(output3)
model=Model(main_input,output4)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
But I get this error:
----> 8 model = TimeDistributed(cnn)(main_input)
ValueError: Input 0 of layer sequential_40 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 189, 1)
Problem with Input shape. tf.keras.layers.TimeDistributed expects batch size as input. Expects inputs: Input tensor of shape (batch, time, ...).
In the main_input add batch_size
main_input = Input(shape=(10, 200, 189, 1))
Working sample code
import tensorflow as tf
cnn = tf.keras.Sequential()
cnn.add(tf.keras.layers.Conv2D(64, 1, 1, input_shape=(200, 189, 1)))
cnn.add(tf.keras.layers.Flatten())
cnn.output_shape
main_input = tf.keras.Input(shape=(10, 200, 189, 1))
outputs = tf.keras.layers.TimeDistributed(cnn)(main_input)
outputs.shape
Output
TensorShape([None, 10, 2419200])
I have a rnn and want to feed in a sentence with a length of 50, and have the output be the same length. (For a chatbot). Does anyone know why this error:
ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 5000]
keeps appearing? Here is the code:
def model():
model = Sequential()
model.add(Embedding(vocab_size, 100, input_length=l))
model.add(Flatten())
model.add(LSTM(100, return_sequences=True))
model.add(LSTM(100))
model.add(Dense(100, activation='relu'))
model.add(Dense(vocab_size, activation='softmax'))
model.summary()
return model
model = model()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(padded_x, padded_y, batch_size=128, epochs=100)
The shape of both arrays are 5000, 50....5000 sentences with 50 words each. They are already encoded. I first though it was because I was flattening...but this is the error before flattening:
ValueError: A target array with shape (5000, 50) was passed for an output of shape (None, 12097) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.
##BTW the vocab_size is 12097##
Don't flatten. The size of the output you're expecting is 50, so you need 50 neurons in your last dense layer.
def model():
model = Sequential()
model.add(Embedding(vocab_size, 100, input_length=l))
model.add(LSTM(100, return_sequences=True))
model.add(LSTM(100))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='softmax')) # 50 here, not 12,097
model.summary()
return model
I'm trying to run a few different models, which ran fine until I added a GlobalAveragePooling2D() which now throws the following error:
ValueError: Input 0 is incompatible with layer flatten_105: expected min_ndim=3, found ndim=2
I feel it has something to do with a GlobalAveragePooling2D() layer not being compatible with a flatten() layer and my understand is lacking, but I'm not sure.
My code is below. Could anyone enlighten me on what they think is going on? It ran fine without the GlobalAveragePooling2D layer. I was hoping to experiment with it though.
dense_layers = [1,2,3]
layer_sizes = [32, 64, 128]
con_layers = [1,2,3]
con_layer_sizes = [32, 64, 128, 512]
for dense_layer in dense_layers:
for layer_size in layer_sizes:
for conv_layer in con_layers:
for con_layer_size in con_layer_sizes:
img_size = 125
batch_size = 16
K.input_shape = (img_size, img_size)
NAME = "{}-conv-{}-con_layer_sizes-{}-nodes-{}-dense-{}".format(conv_layer, con_layer_size, layer_size, dense_layer, int(time.time()))
print(NAME)
tensorboard = TensorBoard(log_dir= 'logs/{}'.format(NAME))
mcp = ModelCheckpoint(filepath='C:\\Users\\jordan.howell\\models\\'+NAME+'_model.h5',monitor="val_loss"
, save_best_only=True, save_weights_only=False)
reduce_learning_rate = ReduceLROnPlateau(monitor='val_loss', factor=0.3,patience=2,cooldown=2
, min_lr=0.00001, verbose=1)
#start model build
model = Sequential()
model.add(Conv2D(con_layer_size, (3, 3), activation="relu", padding = 'same', input_shape=input_shape))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.15))
for l in range(conv_layer-1):
#Convolution
model.add(Conv2D(con_layer_size, (3, 3), activation="relu", padding = 'same'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.15))
model.add(GlobalAveragePooling2D())
# Flatten the layer
model.add(Flatten())
for l in range(dense_layer):
model.add(Dense(layer_size, activation = 'relu'))
model.add(Dense(activation = 'sigmoid', units = 1))
model.compile(loss ='binary_crossentropy', optimizer = 'adam'
, metrics=[km.binary_precision(), km.binary_recall()])
#generators = Generators(TRAIN_DATA_DIR, VALIDATION_DATA_DIR, TEST_DATA_DIR)
#train_generator = generators.train_generator(150, batch_size=32)
#validation_generator = generators.validation_generator(150, batch_size=16)
model.fit_generator(train_generator, steps_per_epoch=5216 // batch_size
,validation_data=validation_generator, validation_steps=1
, epochs = 50, callbacks = [reduce_learning_rate, tensorboard, mcp])
From Keras docs, GlobalAveragePooling2D input shape is 4D tensor, output shape is 2D tensor. In this case, Flatten is redundant.