Error “Function call stack: train_function” occurred in implementation of convLSTM2D()

Solution for Error “Function call stack: train_function” occurred in implementation of convLSTM2D()
is Given Below:

An error occurs in the implementation of ConvLSTM2D.

I implemented time series data analysis for the first time.
So the procedure for arranging the data format may be wrong.
I don’t know the cause and solution, so please help me…!

If you run this program, please prepare 5 images “0.png”,”1.png” … “4.png” in the same hierarchy. (Link here)

from scipy.sparse import dok
import tensorflow as tf
import numpy as np

from matplotlib import pyplot as plt
from keras.models import Model
from keras.preprocessing.image import img_to_array, load_img
from keras.layers import Input, Dense, Reshape ,ConvLSTM2D,BatchNormalization,Activation
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split

imgSize = 200
dataNum = 5

labels = list(range(dataNum, 0, -1))
imgs = []

for i in range(5):
    img = img_to_array(load_img(str(i)+'.png', target_size=(imgSize,imgSize)))
    imgs.append(img)


#-----------------------------------------------------------------------------------
# Arrange in a format that can be learned in Time-series
# original data format: List of the same number of labels(:labels) and images(:imgs)

# imgs:[image1,image2,image3,image4,image5...]
# labels:[100,110,150,140,160...]

n_seq = 2
n_sample = dataNum - n_seq
x = np.zeros((n_sample, n_seq, imgSize, imgSize, 3))
for i in range(n_sample):
    x[i] = imgs[i:i + n_seq]
del labels[0:n_seq]

#-----------------------------------------------------------------------------------
# Data-Split(val,test)
img_train, img_val, Label_train, Label_val = train_test_split(x,labels, test_size=0.6, shuffle=False)

#-----------------------------------------------------------------------------------
# Model and Training

inputL = Input(shape=(n_seq, imgSize, imgSize, 3))
x0 = ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True, data_format="channels_last")(inputL)
x0 = BatchNormalization(momentum=0.6)(x0)
x0 = ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True, data_format="channels_last")(x0)
x0 = BatchNormalization(momentum=0.8)(x0)
x0 = ConvLSTM2D(filters=3, kernel_size=(3, 3), padding="same", return_sequences=False, data_format="channels_last")(x0)
out = Activation('tanh')(x0)
outputL = Dense(1, name="a")(out)

model = Model(inputs=inputL, outputs=outputL)
model.compile(Adam(learning_rate=0.01),loss= {'a': 'mae'},metrics= {'a': 'mae'})

history = model.fit([np.array(img_train)], [np.array(Label_train)],
                    epochs=50, batch_size=16,
                    validation_data=([np.array(img_val)],[np.array(Label_val)]))

Error Message ↓

Traceback (most recent call last):
  File "c:/Users/UserA/StudyAI/LSTM.py", line 189, in <module>
    history = model.fit([np.array(img_train)], [np.array(Label_train)],
  File "C:UsersUserAanaconda3libsite-packageskerasenginetraining.py", line 1158, in fit
    tmp_logs = self.train_function(iterator)
  File "C:UsersUserAanaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 889, in __call__
    result = self._call(*args, **kwds)
  File "C:UsersUserAanaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 950, in _call
    return self._stateless_fn(*args, **kwds)
  File "C:UsersUserAanaconda3libsite-packagestensorflowpythoneagerfunction.py", line 3023, in __call__
    return graph_function._call_flat(
  File "C:UsersUserAanaconda3libsite-packagestensorflowpythoneagerfunction.py", line 1960, in _call_flat
    return self._build_call_outputs(self._inference_function.call(
  File "C:UsersUserAanaconda3libsite-packagestensorflowpythoneagerfunction.py", line 591, in call
    outputs = execute.execute(
  File "C:UsersUserAanaconda3libsite-packagestensorflowpythoneagerexecute.py", line 59, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError:  required broadcastable shapes at loc(unknown)
         [[node mean_absolute_error_1/sub (defined at C:UsersUserAanaconda3libsite-packageskeraslosses.py:1301) ]] [Op:__inference_train_function_10812]

Errors may have originated from an input operation.
Input Source operations connected to node mean_absolute_error_1/sub:
 ExpandDims_1 (defined at C:UsersUserAanaconda3libsite-packageskerasenginedata_adapter.py:1414)
 model/b/BiasAdd (defined at C:UsersUserAanaconda3libsite-packageskeraslayerscore.py:1233)

Function call stack:
train_function

Your model output is (200,200,1), because you are passing tensor with more than 2 dimension to dense layer, so you will get all other dimensions at output, not just 1 (Dense(1)).

You may resolve this issue by adding Flatten layer before the last layer:

inputL = Input(shape=(n_seq, imgSize, imgSize, 3))
x0 = ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True, data_format="channels_last")(inputL)
x0 = BatchNormalization(momentum=0.6)(x0)
x0 = ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True, data_format="channels_last")(x0)
x0 = BatchNormalization(momentum=0.8)(x0)
x0 = ConvLSTM2D(filters=3, kernel_size=(3, 3), padding="same", return_sequences=False, data_format="channels_last")(x0)
x0 = Activation('tanh')(x0)        #Alter this
out = keras.layers.Flatten()(x0)   #Add this
outputL = Dense(1, name="a")(out)

P.S: Investigate your model’s output with model.summary().