Keras ValueError: 输入0与 conv_lst_m2d_16 层不兼容:期望 ndim=5,但找到的是 ndim=4。

4

我正在尝试将一系列包含5帧图像的序列分类为2类。我已经使用ConvLSTM2D作为第一层,并且出现了上述错误。其中input_shape参数为input_shape = (timesteps, rows, columns, channels)

我生成的数据格式如下:

self.data = np.random.random((self.number_of_samples, 
                                  self.timesteps,
                                  self.rows,
                                  self.columns,
                                  self.channels)) 

第一层实现如下所示:

model = Sequential()

# time distributed is used - working frame by frame
model.add(ConvLSTM2D(filters=10,
                     input_shape=input_shape,
                     kernel_size=(3, 3),
                     activation='relu',
                     data_format="channels_last"))

请问有人能帮我解决这个问题吗?

编辑:以下是我的测试代码:

import numpy as np
from keras.layers import Dense, Dropout, LSTM
from keras.layers import Conv2D, Flatten, ConvLSTM2D
from keras.models import Sequential
from keras.layers.wrappers import TimeDistributed
import time


class Classifier():
    """Classifier model to classify image sequences"""

    def __init__(self, number_of_samples, timesteps, rows, columns, channels, epochs, batch_size):
        self.number_of_samples = number_of_samples
        self.rows = rows
        self.columns = columns
        self.timesteps = timesteps
        self.channels = channels
        self.model = None
        self.data = []
        self.labels = []
        self.epochs = epochs
        self.batch_size = batch_size
        self.X_train = []
        self.X_test = []
        self.y_train = []
        self.y_test = []

    def build_model(self, input_shape, output_label_size):
        """Builds the classification model

        Keyword arguments:
            input_shape -- shape of the image array
            output_label_size -- 1
        """
        # initialize a sequential model
        model = Sequential()

        # time distributed is used - working frame by frame
        model.add(ConvLSTM2D(filters=10,
                             input_shape=input_shape,
                             kernel_size=(3, 3),
                             activation='relu',
                             data_format="channels_last"))
        print("output shape 1:{}".format(model.output_shape))
        print("correct till here")

        model.add(Dropout(0.2))
        model.add(ConvLSTM2D(filters=5,
                             kernel_size=(3, 3),
                             activation='relu'))
        print("correct till here")

        model.add(Dropout(0.2))
        model.add(Flatten())
        # print("output shape 2:{}".format(model.output_shape))
        model.add(LSTM(10))
        print("correct till here")
        # print("output shape 3:{}".format(model.output_shape))
        model.add(Dropout(0.2))
        model.add(LSTM(5))
        model.add(Dropout(0.2))
        # print("output shape 4:{}".format(model.output_shape))
        model.add(Dense(output_label_size,
                        kernel_initializer='uniform',
                        bias_initializer='zeros',
                        activation='sigmoid'))
        model.compile(optimizer='adam', loss='binary_crossentropy')
        print("correct till here")
        # model.summary()

        self.model = model

        print("[INFO] Classifier model generated")

    def split_data(self, data, labels):
        """Returns training and test set after splitting

        Keyword arguments:
            data -- image data
            labels -- 0 or 1
        """

        print("[INFO] split the data into training and testing sets")
        train_test_split = 0.9

        # split the data into train and test sets
        split_index = int(train_test_split * self.number_of_samples)
        # shuffled_indices = np.random.permutation(self.number_of_samples)
        indices = np.arange(self.number_of_samples)
        train_indices = indices[0:split_index]
        test_indices = indices[split_index:]

        X_train = data[train_indices, :, :]
        X_test = data[test_indices, :, :]
        y_train = labels[train_indices]
        y_test = labels[test_indices]

        print('Input shape: ', input_shape)
        print('X_train shape: ', X_train.shape)
        print('X_train[0] shape: ', X_train[0].shape)
        print('X_train[0][0] shape: ', X_train[0][0].shape)
        # print('y_train shape: ', y_train.shape)
        # print('X_test shape: ', X_test.shape)
        # print('y_test shape: ', y_test.shape)

        return X_train, X_test, y_train, y_test

    def load_training_data(self):
        """Load the training data for building the classification model."""

        self.data = np.random.random((self.number_of_samples,
                                      self.timesteps,
                                      self.rows,
                                      self.columns,
                                      self.channels))
        print("shape 1", type(self.data))
        print("shape 2", type(self.data[0]))
        print("shape 3", type(self.data[0][0]))

        # self.labels = np.zeros(self.number_of_samples)
        self.labels = np.ones(self.number_of_samples)

        X_train, X_test, y_train, y_test = self.split_data(self.data, self.labels)

        self.X_train = X_train
        self.X_test = X_test
        self.y_train = y_train
        self.y_test = y_test

        print("loading the training data done")

    def train_model(self):
        """Train the model

        Keyword arguments:
            epochs -- number of training iterations
            batch_size -- number of samples per batch
        """

        self.model.fit(x=self.X_train,
                       y=self.y_train,
                       batch_size=self.batch_size,
                       epochs=self.epochs,
                       verbose=1,
                       validation_data=(self.X_test, self.y_test))

        score = self.model.evaluate(self.X_test, self.y_test,
                                    verbose=1, batch_size=self.batch_size)

        prediction = self.model.predict(self.X_test,
                                        batch_size=self.batch_size,
                                        verbose=1)
        print("Loss:{}".format(score))
        print("Prediction:{}".format(prediction))


if __name__ == "__main__":
    start = time.time()
    number_of_samples = 12
    # number_of_test_samples = 2000
    timesteps = 5
    rows = 14
    columns = 14
    channels = 3
    output_label_size = 1
    epochs = 1
    batch_size = 1
    input_shape = (timesteps, rows, columns, channels)
    # input_shape = (batch_size, timesteps, rows, columns, channels)

    classifier_model = Classifier(number_of_samples,
                                  timesteps,
                                  rows,
                                  columns,
                                  channels,
                                  epochs,
                                  batch_size)

    classifier_model.load_training_data()
    classifier_model.build_model(input_shape, output_label_size)
    classifier_model.train_model()
    end = time.time()

    print("total time:{}".format(end - start))

从我的角度来看,应该是 input_shape=(None, timesteps, rows, columns, channels) 来适应批次维度,这只是我暂时想到的。 - nemo
我尝试使用 input_shape = (None, timesteps, rows, columns, channels),但仍然出现了错误: ValueError: 输入0与层conv_lst_m2d_35不兼容:期望的ndim=5,但发现ndim=6。 - Yadunandan Kini
我认为 input_shape = (timesteps, rows, columns, channels) 没有任何问题。你能提供可重现的代码吗? - rvinas
当然。请告诉我我的回答是否解决了你的问题。 - rvinas
我已经接受并点赞了这个答案,但是我的声望还不够高,所以它还没有显示出来 :D - Yadunandan Kini
显示剩余2条评论
1个回答

6

有几种方法可以指定输入形状。从文档中:

向第一层传递input_shape参数。这是一个形状元组(由整数或None条目组成的元组,其中None表示可以期望任何正整数)。在input_shape中,不包括批量维度

因此,正确的输入形状是:

input_shape = (timesteps, rows, columns, channels)

在修复此错误后,您将遇到下一个错误(与input_shape无关):

ValueError:输入0与层conv_lst_m2d_2不兼容:期望ndim = 5,但发现ndim = 4

当您尝试添加第二个ConvLSTM2D层时会出现此错误。这是因为第一个ConvLSTM2D层的输出是具有形状(samples, output_row, output_col, filters)的4D张量。您可能想设置return_sequences=True,在这种情况下,输出是具有形状(samples, time, output_row, output_col, filters)的5D张量。
在修复此错误后,您将遇到以下行中出现的新错误:
model.add(Flatten())
model.add(LSTM(10))

LSTM 层之前添加 Flatten 层是没有意义的。这样做永远不会起作用,因为 LSTM 要求一个形状为 (samples, time, input_dim) 的 3D 输入张量。

总之,我强烈建议您仔细阅读 Keras 文档,特别是 LSTMConvLSTM2D 层的文档。同时了解这些层的工作原理对于充分利用它们非常重要。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接