Keras ConvLSTM InvalidArgumentError - keras

I'm new enough to TensorFlow and Keras that I might be missing something obvious, but this is driving me nuts. I inherited an app that trains a custom convolutional LSTM, and I just spent the last two months or so straightening out some really atrocious data wrangling, only to discover I can't get the model to train properly.
Here's the model definition (with hard values substituted for the variables in my actual code):
inputs = layers.Input(shape = (2, 23, 23, 10))
outputs = layers.ConvLSTM2D(filters = 32,
kernel_size = (5,5),
padding = "same",
return_sequences = True,
stateful = False,
activation = "relu")(inputs)
outputs = layers.BatchNormalization()(outputs)
outputs = layers.ConvLSTM2D(filters = 32,
kernel_size = (3,3),
padding = "same",
return_sequences = True,
stateful = False,
activation = "relu")(outputs)
outputs = layers.ConvLSTM2D(filters = 32,
kernel_size = (3,3),
padding = "same",
return_sequences = True,
stateful = False,
activation = "relu")(outputs)
outputs = layers.Conv3D(filters = 1,
kernel_size = (3, 3, 3),
padding = "same",
activation = "sigmoid")(outputs)
If something looks squirrely there, let me know--I didn't create this model (though I did change the input grid from 100 x 100 to 23 x 23, if that makes a difference). The idea is to predict the intensity of a particular weather phenomenon (that's a single number for each grid point at each time).
Here's the model summary produced after the model is defined:
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 2, 23, 23, 10)] 0
conv_lstm2d (ConvLSTM2D) (None, 2, 23, 23, 32) 134528
batch_normalization (BatchN (None, 2, 23, 23, 32) 128
ormalization)
conv_lstm2d_1 (ConvLSTM2D) (None, 2, 23, 23, 32) 73856
conv_lstm2d_2 (ConvLSTM2D) (None, 2, 23, 23, 32) 73856
conv3d (Conv3D) (None, 2, 23, 23, 1) 865
=================================================================
Total params: 283,233
Trainable params: 283,169
Non-trainable params: 64
_________________________________________________________________
The model is fit using data from a custom Sequence sub-class, which produces X input of shape ([batch size], 2, 23, 23, 10) and Y input of shape ([batch size], 2, 23, 23, 1). The batch size is usually 8, but because of the way the data is lazily loaded, the last batch in a particular block of files may be smaller, which is why I don't specify a batch size in model definition. For the record, the original coders had a constant batch size, though, as with mine, it wasn't specified in the model definition.
When I try to fit the model, I get a crash pretty quickly, with this traceback:
Traceback (most recent call last):
File "C:/code/Python/edapts/ConvLSTM2D.py", line 175, in <module>
history = model.fit(training_data,
File "C:\Python\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Python\lib\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'gradient_tape/model/conv_lstm2d/transpose_1/transpose' defined at (most recent call last):
File "C:/code/Python/edapts/ConvLSTM2D.py", line 175, in <module>
history = model.fit(training_data,
File "C:\Python\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1051, in train_function
return step_function(self, iterator)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1030, in run_step
outputs = model.train_step(data)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 893, in train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
File "C:\Python\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 537, in minimize
grads_and_vars = self._compute_gradients(
File "C:\Python\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 590, in _compute_gradients
grads_and_vars = self._get_gradients(tape, loss, var_list, grad_loss)
File "C:\Python\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 471, in _get_gradients
grads = tape.gradient(loss, var_list, grad_loss)
Node: 'gradient_tape/model/conv_lstm2d/transpose_1/transpose'
transpose expects a vector of size 4. But input(1) is a vector of size 5
[[{{node gradient_tape/model/conv_lstm2d/transpose_1/transpose}}]] [Op:__inference_train_function_9880]
I'm so lost. The only thing I can think of is that the original coders made the X and Y features the same, whereas I'm only trying to predict 1 of the 10 (there's not actually a reason to predict the others--not sure why they were trying to). If that's the problem, how do I redefine the model to take account of the different output shape?
EDIT: Well, that's interesting. I just compared the model definition from the original team with the model definition used by the co-worker I inherited the code from. Said co-worker seems to have inserted an extra layer in the outputs, duplicating the ConvLSTM2D with 32 filters and kernel size (3,3). When I remove one of those duplicates, everything runs just fine....
Great, so it's "fixed". But is there someone who can explain why it wasn't working in the first place? My level of understanding the issue at this point is to cross myself and throw salt over my shoulder.
EDIT #2: Does the problem result from having such a small grid (23 x 23)? So either a bigger grid or smaller kernels would solve the problem without deleting a layer? That seems intuitively likely, but I'd like to know the match to calculate how the outputs from each layer match up with the definition of the next layer.

Related

Increasing the number of channels in keras tensor

I want to concatenate two tensors in keras model like this
merge6 = concatenate([drop4, up6], axis=2)
when i run the code this gives me following error
ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concatenation axis. Received: input_shape=[(None, 512, 19, 30, 30), (None, 512, 18, 30, 30)]
How can i make the 18 channels to 19 so that concatenation be done

Having a problem with input_shape on Conv1d Layer

I'm attempting to perform a sentiment classification using CNN. The error seems to be related to the input_shape parameters.
The x data consists of arrays of integers created using tokenizer.texts_to_sequences.
? x_train.shape
(4460, 20)
? x_trains.shape[0]
array([ 49, 472, 4436, 843, 756, 659, 64, 8, 1328, 87, 123,
352, 1329, 148, 2996, 1330, 67, 58, 4437, 144])
The y data consist of one hot encoded values for classification.
y_train.shape
(4460, 2)
y_train[0]
array([1., 0.], dtype=float32)
here is the model:
model.add(layers.Conv1D(filters=256, kernel_size=3, activation='relu', input_shape=(max_seqlen,)))
model.add(layers.SpatialDropout1D(0.2))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(100, activation='relu'))
model.add(layers.Dense(num_classes, activation="softmax"))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=3, batch_size=512,
validation_data=(x_val, y_val), class_weight=label_weights)
An error is thrown adding the Conv1D layer. The message is:
"Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=2"
I have no idea what I'm doing wrong. Any help is greatly appreciated.
Conv1D takes a 2D input (I don't know why this is the case). As your input is only 1D, your dimensions don't match up. I'm afraid that you will probably have to stick to other keras layer types, or alternatively reshape your data so that it is (4460, 20, 1), allowing you to pass a conv1D over it.

keras resnet load_model() fails with "ValueError: Shape must be rank 3 but is rank 4..."

I've created a ResNet1D model with keras-resnet 0.2.0 (python3), and have fit my data over many epochs without any issues, but after saving, then simply trying to read the model back in (via load_model), I get a tensor shape mismatch error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1607, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 3 but is rank 4 for 'padding_conv1_2/Pad' (op: 'Pad') with input shapes: [1,?,30,2], [3,2].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/local/lib/python3.6/site-packages/keras/engine/saving.py", line 661, in model_from_json
return deserialize(config, custom_objects=custom_objects)
File "/usr/local/lib/python3.6/site-packages/keras/layers/__init__.py", line 168, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.6/site-packages/keras/engine/network.py", line 1107, in from_config
return cls(inputs=input_tensors, outputs=output_tensors, name=name)
File "/usr/local/lib/python3.6/site-packages/keras_resnet/models/_1d.py", line 184, in __init__
**kwargs
File "/usr/local/lib/python3.6/site-packages/keras_resnet/models/_1d.py", line 82, in __init__
x = keras.layers.ZeroPadding1D(padding=3, name="padding_conv1")(inputs)
File "/usr/local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 489, in __call__
output = self.call(inputs, **kwargs)
File "/usr/local/lib/python3.6/site-packages/keras/layers/convolutional.py", line 2151, in call
return K.temporal_padding(inputs, padding=self.padding[0])
File "/usr/local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2759, in temporal_padding
return tf.pad(x, pattern)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py", line 2840, in pad
result = gen_array_ops.pad(tensor, paddings, name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py", line 6399, in pad
"Pad", input=input, paddings=paddings, name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py", line 794, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 3357, in create_op
attrs, op_def, compute_device)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 3426, in _create_op_internal
op_def=op_def)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1770, in __init__
control_input_ops)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1610, in _create_c_op
raise ValueError(str(e))
ValueError: Shape must be rank 3 but is rank 4 for 'padding_conv1_2/Pad' (op: 'Pad') with input shapes: [1,?,30,2], [3,2].
I've pared down the model as much as I could and saved the model alone (21kb, without weights) as JSON, in this github repository.
I can duplicate the error with the following snippet. Keras-resnet needs to be installed in python3, and you'll need the ck.json file from the repository. Below, a custom object dictionary is supplied to model_from_json() because the model contains a custom layer.
from keras.models import model_from_json
import keras_resnet
from keras_resnet.models import ResNet1D18
with open('ck.json', 'r') as f:
model = model_from_json(f.read(), {'ResNet1D18': keras_resnet.models.ResNet1D18})
I'm very new at this, so I hope I simply did something silly, but the problem doesn't seem to be shape mismatches in the model itself, because I can create the model, fit data, and save the model with no problems. Reading the saved model back in elicits the error. From the model summary below, the first ZeroPadding1D layer's input shape is (?, 30, 2) but how does the saved model transform that to (1, ?, 30, 2) as in the error above?
Thanks in advance for any help!
The model summary is:
Model: "resnet1d18_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 30, 2) 0
__________________________________________________________________________________________________
padding_conv1 (ZeroPadding1D) (None, 36, 2) 0 input_1[0][0]
__________________________________________________________________________________________________
conv1 (Conv1D) (None, 15, 64) 896 padding_conv1[0][0]
__________________________________________________________________________________________________
bn_conv1 (BatchNormalization) (None, 15, 64) 256 conv1[0][0]
__________________________________________________________________________________________________
conv1_relu (Activation) (None, 15, 64) 0 bn_conv1[0][0]
__________________________________________________________________________________________________
pool1 (MaxPooling1D) (None, 8, 64) 0 conv1_relu[0][0]
__________________________________________________________________________________________________
padding2a_branch2a (ZeroPadding (None, 10, 64) 0 pool1[0][0]
__________________________________________________________________________________________________
res2a_branch2a (Conv1D) (None, 8, 64) 12288 padding2a_branch2a[0][0]
__________________________________________________________________________________________________
bn2a_branch2a (BatchNormalizati (None, 8, 64) 256 res2a_branch2a[0][0]
__________________________________________________________________________________________________
res2a_branch2a_relu (Activation (None, 8, 64) 0 bn2a_branch2a[0][0]
__________________________________________________________________________________________________
padding2a_branch2b (ZeroPadding (None, 10, 64) 0 res2a_branch2a_relu[0][0]
__________________________________________________________________________________________________
res2a_branch2b (Conv1D) (None, 8, 64) 12288 padding2a_branch2b[0][0]
__________________________________________________________________________________________________
res2a_branch1 (Conv1D) (None, 8, 64) 4096 pool1[0][0]
__________________________________________________________________________________________________
bn2a_branch2b (BatchNormalizati (None, 8, 64) 256 res2a_branch2b[0][0]
__________________________________________________________________________________________________
bn2a_branch1 (BatchNormalizatio (None, 8, 64) 256 res2a_branch1[0][0]
__________________________________________________________________________________________________
res2a (Add) (None, 8, 64) 0 bn2a_branch2b[0][0]
bn2a_branch1[0][0]
__________________________________________________________________________________________________
res2a_relu (Activation) (None, 8, 64) 0 res2a[0][0]
__________________________________________________________________________________________________
pool5 (GlobalAveragePooling1D) (None, 64) 0 res2a_relu[0][0]
__________________________________________________________________________________________________
fc1000 (Dense) (None, 1) 65 pool5[0][0]
==================================================================================================
Total params: 30,657
Trainable params: 30,145
Non-trainable params: 512
Look at this line https://github.com/wt18/keras-resnet-json-load-fail/blob/master/ck.json#L11
This makes the input_shape to be (None, 30, 2) instead of (30, 2).
Try delete this line.

How to resolve error when checking model input: expected convolution2d_input_9 to have 4 dimensions but got array with shape

I am using an existing Keras model that a fellow student made for me and trying to implement a CNN, but I keep getting this error
ValueError: Error when checking model input: expected convolution2d_input_9 to have 4 dimensions, but got array with shape (3938, 4, 42)
Here is the log
runfile('C:/Users/a_phi/Downloads/hand-pred-20190324T215735Z-001/hand-pred/model_lstmAshwin-train.py', wdir='C:/Users/a_phi/Downloads/hand-pred-20190324T215735Z-001/hand-pred')
Reloaded modules: myFileHandler
x_train (3938, 4, 42)
y_train (3938, 6)
saving scaler object to Scaler.sav
Compilation Time : 0.026177644729614258
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
convolution2d_24 (Convolution2D) (None, 26, 26, 32) 320 convolution2d_input_11[0][0]
____________________________________________________________________________________________________
activation_10 (Activation) (None, 26, 26, 32) 0 convolution2d_24[0][0]
____________________________________________________________________________________________________
convolution2d_25 (Convolution2D) (None, 24, 24, 64) 18496 activation_10[0][0]
____________________________________________________________________________________________________
maxpooling2d_12 (MaxPooling2D) (None, 12, 12, 64) 0 convolution2d_25[0][0]
____________________________________________________________________________________________________
dropout_22 (Dropout) (None, 12, 12, 64) 0 maxpooling2d_12[0][0]
____________________________________________________________________________________________________
flatten_10 (Flatten) (None, 9216) 0 dropout_22[0][0]
____________________________________________________________________________________________________
dense_20 (Dense) (None, 128) 1179776 flatten_10[0][0]
____________________________________________________________________________________________________
dropout_23 (Dropout) (None, 128) 0 dense_20[0][0]
____________________________________________________________________________________________________
dense_21 (Dense) (None, 10) 1290 dropout_23[0][0]
====================================================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0
____________________________________________________________________________________________________
Now to train the model using the fit() method
Traceback (most recent call last):
File "<ipython-input-19-d0bd53cbab62>", line 1, in <module>
runfile('C:/Users/a_phi/Downloads/hand-pred-20190324T215735Z-001/hand-pred/model_lstmAshwin-train.py', wdir='C:/Users/a_phi/Downloads/hand-pred-20190324T215735Z-001/hand-pred')
File "C:\Users\a_phi\Anaconda3\envs\cpr_lstm07\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "C:\Users\a_phi\Anaconda3\envs\cpr_lstm07\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/a_phi/Downloads/hand-pred-20190324T215735Z-001/hand-pred/model_lstmAshwin-train.py", line 627, in <module>
verbose=2)
File "C:\Users\a_phi\Anaconda3\envs\cpr_lstm07\lib\site-packages\keras\models.py", line 672, in fit
initial_epoch=initial_epoch)
File "C:\Users\a_phi\Anaconda3\envs\cpr_lstm07\lib\site-packages\keras\engine\training.py", line 1116, in fit
batch_size=batch_size)
File "C:\Users\a_phi\Anaconda3\envs\cpr_lstm07\lib\site-packages\keras\engine\training.py", line 1029, in _standardize_user_data
exception_prefix='model input')
File "C:\Users\a_phi\Anaconda3\envs\cpr_lstm07\lib\site-packages\keras\engine\training.py", line 112, in standardize_input_data
str(array.shape))
ValueError: Error when checking model input: expected convolution2d_input_11 to have 4 dimensions, but got array with shape (3938, 4, 42)
I am unfamiliar with Keras and not sure how to correct this.
def build_model2(dataIn, timeWindow, layerOut, outputs, activation,
optimizer):
layerOut = 84
layerOutExpand = 100
dataIn = 42
timeWindow = 20
outputs = 6
# Takes feature data matrix row size as input = 42
# Returns vector of size 20 (timeWindow) as output
num_classes = 10
model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(28, 28, 1)))
model.add(Activation('relu'))
model.add(Conv2D(64,3,3))
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'))
# Compile the model and time how long it takes
start = time.time()
#optimizer = SGD(lr=0.3, momentum=0.9)
model.compile(loss="mse", optimizer=optimizer, metrics=[coeffDetermination, 'accuracy'])
print("Compilation Time : ", time.time() - start)
# Let's see the model details too, and create an image file of the
# network structure
model.summary()
return model
The other solutions that I've seen I could not understand or get it to work.
I am unfamiliar with Keras/TensorFlow so any help would be appreciated.

Memory error while using keras

I am using keras for CNN but the problem is that there is memory leak. The error is
anushreej#cpusrv-gpu-109:~/12EC35005/MTP_Workspace/MTP$ python cnn_implement.py
Using Theano backend.
[INFO] compiling model...
Traceback (most recent call last):
File "cnn_implement.py", line 23, in <module>
model = CNNModel.build(width=150, height=150, depth=3)
File "/home/ms/anushreej/12EC35005/MTP_Workspace/MTP/cnn/networks/model_define.py", line 27, in build
model.add(Dense(depth*height*width))
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/models.py", line 146, in add
output_tensor = layer(self.outputs[0])
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py", line 458, in __call__
self.build(input_shapes[0])
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/layers/core.py", line 604, in build
name='{}_W'.format(self.name))
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/initializations.py", line 61, in glorot_uniform
return uniform(shape, s, name=name)
File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/initializations.py", line 32, in uniform
return K.variable(np.random.uniform(low=-scale, high=scale, size=shape),
File "mtrand.pyx", line 1255, in mtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:13575)
File "mtrand.pyx", line 220, in mtrand.cont2_array_sc (numpy/random/mtrand/mtrand.c:2902)
MemoryError
Now I am unable to understand why is this happening. My training images are very small of the size 150*150*3.
The code is -:
# import the necessary packages
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
class CNNModel:
#staticmethod
def build(width, height, depth):
# initialize the model
model = Sequential()
# first set of CONV => RELU
model.add(Convolution2D(50, 5, 5, border_mode="same", batch_input_shape=(None, depth, height, width)))
model.add(Activation("relu"))
# second set of CONV => RELU
# model.add(Convolution2D(50, 5, 5, border_mode="same"))
# model.add(Activation("relu"))
# third set of CONV => RELU
# model.add(Convolution2D(50, 5, 5, border_mode="same"))
# model.add(Activation("relu"))
model.add(Flatten())
model.add(Dense(depth*height*width))
# if weightsPath is not None:
# model.load_weights(weightsPath)
return model
I faced the same problem, I think the issue is the number data points just before the Flattening layer are more than your system can handle(i tried in difference systems so one with high ram worked and with less ram gave this error). Just add more CNN layers to reduce the size and then add a flattening layer it works.
This gave me and error:
model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96),activation='relu'))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dense(97,activation='softmax'))
This didnt give an error
model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96),activation='relu'))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(Convolution2D(128, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dense(97,activation='softmax')
Hope it helps.

Resources