执行 convLSTM2D() 时发生错误 "Function call stack: train_function"

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

执行ConvLSTM2D时出错。

第一次实现时序数据分析。 所以排列数据格式的程序可能是错误的。 我不知道原因和解决办法,所以请帮助我...!

如果你运行这个程序,请准备5张图像“0.png”,“1.png” ...“4.png”在同一层级。 (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)]))

错误信息↓

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:\Users\UserA\anaconda3\lib\site-packages\keras\engine\training.py", line 1158, in fit
    tmp_logs = self.train_function(iterator)
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 950, in _call
    return self._stateless_fn(*args, **kwds)
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3023, in __call__
    return graph_function._call_flat(
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 1960, in _call_flat
    return self._build_call_outputs(self._inference_function.call(
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 591, in call
    outputs = execute.execute(
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\execute.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:\Users\UserA\anaconda3\lib\site-packages\keras\losses.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:\Users\UserA\anaconda3\lib\site-packages\keras\engine\data_adapter.py:1414)
 model/b/BiasAdd (defined at C:\Users\UserA\anaconda3\lib\site-packages\keras\layers\core.py:1233)

Function call stack:
train_function

您的模型输出是 (200,200,1),因为您将超过 2 维的张量传递给密集层,所以您将在输出时获得所有其他维度,而不仅仅是 1 (Dense(1))。

您可以通过在最后一层之前添加 Flatten 层来解决此问题:

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:使用 model.summary() 调查模型的输出。