Conv2D + LSTM network giving errors - keras

I have a list of matrices called charMatrixList, of length 40744. I convert this list to numpy array, and the shape changes to (40744,32,30). This numpy array is passed as an input to the neural network.
The errors I'm getting are related to the shape of the Conv2D layer output, when passed as an input into an LSTM layer.
from keras.models import Sequential
from keras.layers import Embedding,LSTM,Flatten,Conv2D,Reshape
import numpy as np
def phase22(charMatrixList ):
model = Sequential()
model.add(Conv2D(32, (3, 3), strides=(1,1) , padding="same", activation="relu",input_shape=(40744,32,30)))
model.add(LSTM(16, return_sequences=True))
model.add(LSTM(16, return_sequences=True))
model.add(Flatten())
model.compile('rmsprop', 'mse')
input_array = charMatrixList
model.compile('rmsprop', 'mse')
output_array = model.predict(input_array)
return output_array
p2out = phase22(charMatrixList)
I'm getting the below error :
Traceback (most recent call last):
File "<ipython-input-56-f615f91b6704>", line 1, in <module>
p2out = phase22(np.array(charMatrixList) )
File "<ipython-input-55-9a4fd292a04f>", line 4, in phase22
model.add(LSTM(16, return_sequences=True))
File "C:\Users\Kishore\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 185, in add
output_tensor = layer(self.outputs[0])
File "C:\Users\Kishore\Anaconda3\lib\site-packages\keras\layers\recurrent.py", line 500, in __call__
return super(RNN, self).__call__(inputs, **kwargs)
File "C:\Users\Kishore\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 414, in __call__
self.assert_input_compatibility(inputs)
File "C:\Users\Kishore\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 311, in assert_input_compatibility
str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer lstm_11: expected ndim=3, found ndim=4

Keras ignores the first dimension when defining input size because that is just the number of training examples, m. Keras is able to work with any m, so it only cares about the actual input dimensions. That is why Kears sees (40744,32,30) as 4 dimensions.
I'm confused by the dimensions of your input, is 40744 the number of training examples? If it is do input_size = (32, 30).
If your input has 3 dimensions include number of training examples in your input, ie. charMatrixList = (m, 40744,32,30)

Related

Convert tflearn to keras

Since tflearn is outdated and I am watching a chatbot tutorial that uses tflearn, I want to write the neural network model in keras. However, I got this error right here:
WARNING:tensorflow:Model was constructed with shape (None, 58) for input KerasTensor(type_spec=TensorSpec(shape=(None, 58), dtype=tf.float32, name='input_22'), name='input_22', description="created by layer 'input_22'"), but it was called on an input with incompatible shape (None,).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-164-1a494613d0d2> in <module>()
1 convert_input("Hello")
----> 2 chat()
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1801, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1790, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1783, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1751, in predict_step
return self(x, training=False)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 228, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "sequential_24" (type Sequential).
Input 0 of layer "dense_59" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)
Call arguments received:
• inputs=('tf.Tensor(shape=(None,), dtype=int64)',)
• training=False
• mask=None
I have tried to set up the keras model myself
model = keras.Sequential()
# model.add(keras.layers.InputLayer(input_shape = len(words)))
model.add(keras.Input(shape=len(training[0])))
model.add(keras.layers.Dense(8, activation = "relu"))
model.add(keras.layers.Dense(8, activation = "relu"))
model.add(keras.layers.Dense(len(output[0]), activation= "softmax"))
model.compile(optimizer = "adam", loss = "categorical_crossentropy", metrics=['accuracy'])
# convert_input(inp) -> return a 1D numpy array filled with 1 and 0
prediction = model.predict([convert_input(inp)])
versus the tflearn model
network = tflearn.input_data(shape=[None, len(training[0])])
network = tflearn.fully_connected(network, 8)
network = tflearn.fully_connected(network, 8)
network = tflearn.fully_connected(network, len(output[0]), activation = "softmax")
network = tflearn.regression(network)
model = tflearn.DNN(network)
model.fit(training, output, n_epoch = 1000, batch_size=8, show_metric=True)
# convert_input(inp) -> return a 1D numpy array filled with 1 and 0
prediction = model.predict([convert_input(inp)])
However, when I call model.predict only the tflearn model works and not the keras. Please help!
I was in a similar predicament. However I was finally able to resolve my problem with the following code:
import numpy as np
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
model=Sequential()
model.add(Dense(8,input_shape=(len(train_x[0]),),kernel_initializer='normal'))
model.add(Dense(8,kernel_initializer='normal'))
model.add(Dense(len(train_y[0]),activation='softmax',kernel_initializer='normal'))
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
model.fit(np.array(train_x),np.array(train_y), epochs=1000, batch_size=8, verbose=1)
During prediction,
model.predict(np.array([p]))
Basically converting the list into an array using np.array() helped me solve the problem.

Keras CNN: Incompatible shapes [batch_size*2,1] vs. [batch_size,1] with any batch_size > 1

I am Fitting a Siamese CNN with the following structure:
def get_siamese_model(input_shape):
"""
Model architecture
"""
# Define the tensors for the three input images
A_input = Input(input_shape)
B_input = Input(input_shape)
C_input = Input(input_shape)
# Convolutional Neural Network
#Initialzers
initializer = 'random_uniform'
initializer0 = 'zeros'
model = Sequential()
model.add(Conv2D(64, (10,10), activation='relu', input_shape=input_shape,
kernel_initializer=initializer , kernel_regularizer=l2(2e-4)))
model.add(MaxPooling2D())
model.add(Conv2D(128, (7,7), activation='relu', kernel_initializer=initializer ,
bias_initializer=initializer0, kernel_regularizer=l2(2e-4)))
model.add(MaxPooling2D())
model.add(Conv2D(128, (4,4), activation='relu', kernel_initializer=initializer ,
bias_initializer=initializer0, kernel_regularizer=l2(2e-4)))
print("C3 shape: ", model.output_shape)
model.add(MaxPooling2D())
print("P3 shape: ", model.output_shape)
model.add(Conv2D(256, (4,4), activation='relu', kernel_initializer=initializer ,
bias_initializer=initializer0, kernel_regularizer=l2(2e-4)))
model.add(Flatten())
model.add(Dense(4096, activation='sigmoid',
kernel_regularizer=l2(1e-3),
kernel_initializer=initializer,
bias_initializer=initializer0))
# Generate the encodings (feature vectors) for the three images
encoded_A = model(A_input)
encoded_B = model(B_input)
encoded_C = model(C_input)
#Custom Layer for L1-norm
L1_layer = Lambda(lambda tensors: K.sum(K.abs(tensors[0] - tensors[1]), axis=1,keepdims=True))
L_layerAB = L1_layer([encoded_A, encoded_B])
L2_layer = Lambda(lambda tensors: K.sum(K.abs(tensors[0] - tensors[1]), axis=1,keepdims=True))
L_layerAC = L2_layer([encoded_A, encoded_C])
merge6 = concatenate([L_layerAB, L_layerAC], axis = 0)
prediction = Dense(1,activation='sigmoid')(merge6)
siamese_net = Model(inputs=[A_input,B_input, C_input],outputs= prediction)
# return the model
return siamese_net
The training data is are triplets of pictures in array form with following dimensions: (128,128,3).
And the target data is a label (0,1).
Then we fit the model:
model = siam.get_siamese_model((128,128,3))
model.fit([tripletA,tripletB, tripletC], targets , epochs=2, verbose=1,
batch_size = 1)
This works for batch_size = 1 but anything over batchsize >1 produces the following error:
Epoch 1/5
Traceback (most recent call last):
File "<ipython-input-147-8959bad9406a>", line 2, in <module>
batch_size = 2)
File "C:\Users\valan\Anaconda3\lib\site-packages\keras\engine\training.py", line 1239, in fit
validation_freq=validation_freq)
File "C:\Users\valan\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 196, in fit_loop
outs = fit_function(ins_batch)
File "C:\Users\valan\Anaconda3\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3727, in _call_
outputs = self._graph_fn(*converted_inputs)
File "C:\Users\valan\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1551, in _call_
return self._call_impl(args, kwargs)
File "C:\Users\valan\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1591, in _call_impl
return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "C:\Users\valan\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1692, in _call_flat
ctx, args, cancellation_manager=cancellation_manager))
File "C:\Users\valan\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 545, in call
ctx=ctx)
File "C:\Users\valan\Anaconda3\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
InvalidArgumentError: Incompatible shapes: [4,1] vs. [2,1]
[[node loss_16/dense_47_loss/binary_crossentropy/logistic_loss/mul (defined at C:\Users\valan\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_33258]
Does anybody know where the Problem is with batch_size > 1?
EDIT1:
We found out that the following lines caused the error:
L1_layer = Lambda(lambda tensors: K.sum(K.abs(tensors[0] - tensors[1]), axis=1,keepdims=True))
L_layerAB = L1_layer([encoded_A, encoded_B])
L2_layer = Lambda(lambda tensors: K.sum(K.abs(tensors[0] - tensors[1]), axis=1,keepdims=True))
L_layerAC = L2_layer([encoded_A, encoded_C])
Removing these lines and just using sigmoid on encoded A and thus, making the model simpler makes it work for batchsizes >1 .
But does anybody know how to re-add those customized layers properly?
Mentioning the solution in this (Answer) section even though it is present in the Comments section, for the benefit of the community.
For the above code, with batch_size > 1, it is resulting in error,
InvalidArgumentError: Incompatible shapes: [4,1] vs. [2,1]
[[node loss_16/dense_47_loss/binary_crossentropy/logistic_loss/mul (defined at C:\Users\valan\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_33258]
Changing the code from
merge6 = concatenate([L_layerAB, L_layerAC], axis = 0)
to
merge6 = Concatenate()([L_layerAB, L_layerAC])
has resolved the error.

Reshape layer after a dense layer in Keras

I am trying to understand why there is a mismatch dimensionality between a Dense Layer and a Reshape Layer. Shouldn't this snippet code be correct? The dimensionality of the Dense Layer output will be image_resize^2 * 128, why is there a conflict in the reshape?
input_shape = (28,28,1)
inputs = Input(shape=input_shape)
image_size = 28
image_resize = image_size // 4
x = Dense(image_resize * image_resize * 128)(inputs)
x = Reshape((image_resize, image_resize, 128))(x)
This is the error that shows up:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/venv/lib/python3.7/site-packages/keras/engine/base_layer.py", line 474, in __call__
output_shape = self.compute_output_shape(input_shape)
File "/Users/venv/lib/python3.7/site-packages/keras/layers/core.py", line 398, in compute_output_shape
input_shape[1:], self.target_shape)
File "/Users/venv/lib/python3.7/site-packages/keras/layers/core.py", line 386, in _fix_unknown_dimension
raise ValueError(msg)
ValueError: total size of new array must be unchanged
Dense layers act on the last dimension of the input data, if you want to give image input to a Dense layer, you should first flatten it:
x = Flatten()(x)
x = Dense(image_resize * image_resize * 128)(x)
x = Reshape((image_resize, image_resize, 128))(x)
Then the Reshape will work.
Your input is 784 and enters Dense layer with 7*7*128
So you will have and output of 784*7*7*128 in Reshape, not 7*7*128

ValueError: An `initial_state` was passed that is not compatible with `cell.state_size`

i m trying to add a custom layer to my model but i m having this illogic error ,what do i need to change ?
if the code of the custom layer is needed let me know
it will mean the world to me if you help me understand what i m doing wrong.and how to fix it ?
from keras.models import Sequential , Model
from keras.layers.convolutional_recurrent import ConvLSTM2D
from keras.layers.normalization import BatchNormalization
from keras.layers import Input ,Activation, Dense , Flatten ,RNN
import tensorflow as tf
from layers.se3 import SE3CompositeLayer # this is the layer that i want to add
batch_size = 500
seq = Sequential()
seq.add(ConvLSTM2D(filters=128, kernel_size=(3, 3),
batch_input_shape=(500,4,34, 17, 768),
padding='same', return_sequences=True,stateful=True,dropout=0.2))
seq.add(BatchNormalization())
seq.add(Activation('relu'))
seq.add(ConvLSTM2D(filters=32, kernel_size=(3, 3),padding='same',dropout=0.2))
seq.add(BatchNormalization())
seq.add(Activation('relu'))
output1 = Flatten()(seq.layers[11].output)
branch1= Dense(64,activation ='relu')(output1)
branch1= Dense(4)(branch1)
branch2= Dense(64,activation='relu')(output1)
branch2= Dense(3)(branch2)
output_conv_lstm =tf.concat([branch2,branch1] ,1)
output_conv_lstm = tf.expand_dims(output_conv_lstm , axis=0)
init_s=tf.placeholder_with_default(tf.eye(4, batch_shape=[batch_size]) , shape=(None, 4 ,4))
l_se3comp = SE3CompositeLayer()
se3_outputs, se3_state =keras.layers.RNN(cell=l_se3comp , dtype=tf.float32 ,unroll=True)(output_conv_lstm ,initial_state=init_s)
model = Model(inputs= seq.layers[0].input ,outputs=se3_outputs )
model.summary()
File "main.py", line 59, in <module>
se3_outputs, se3_state =keras.layers.RNN(cell=l_se3comp , dtype=tf.float32 ,unroll=True)(output_conv_lstm ,initial_state=init_s)
File "/home/ridha/.local/lib/python3.6/site-packages/keras/layers/recurrent.py", line 574, in __call__
return super(RNN, self).__call__(inputs, **kwargs)
File "/home/ridha/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "/home/ridha/.local/lib/python3.6/site-packages/keras/layers/recurrent.py", line 508, in build
'{}'.format(self.state_spec, self.cell.state_size))
ValueError: An `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`=[InputSpec(shape=(None, 4, 4), ndim=3)]; however `cell.state_size` is (?, 4, 4) ```
Not really an answer but possibly a workaround: tensorflow initializers supposedly can be tensors or callables, but I've only every gotten callables work.
For example, if I wanted to initialize a kernel in an RNN as a 2D vector of all 7's,
kernel_initializer = tf.constant([7.,7.])
has never worked for me, but
init_state_getter = lambda shape, dtype: 7*np.ones(*shape)
...
kernel_initializer = init_state_getter
is ok.

Saving Keras model fails after renaming layers

I have a problem with renaming layers. Below is the simplest example illustrating the problem:
from keras.layers import Dense, Conv2D, Flatten, Input
from keras.models import Model
inputs = Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
print(model.summary())
for i, layer in enumerate(model.layers):
layer.name = 'layer' + str(i)
print(model.summary())
model.save('temp')
It fails with the message:
Traceback (most recent call last):
File "scripts/save_load.py", line 24, in <module>
model.save('temp')
File "/lib/python3.6/site-packages/keras/engine/topology.py", line 2416, in save
save_model(self, filepath, overwrite)
File "/lib/python3.6/site-packages/keras/models.py", line 101, in save_model
'config': model.get_config()
File "/lib/python3.6/site-packages/keras/engine/topology.py", line 2281, in get_config
new_node_index = node_conversion_map[node_key]
KeyError: 'layer0_ib-0'
What am I doing wrong?
I know I can pass names to the layer constructor, it seems not to fail in this case, but is there any chance to improve my solution?

Resources