简体   繁体   English

Keras ValueError:输入0与图层conv_lst_m2d_16不兼容:预期ndim = 5,发现ndim = 4

[英]Keras ValueError: Input 0 is incompatible with layer conv_lst_m2d_16: expected ndim=5, found ndim=4

I am trying to classify sequences of images into 2 classes. 我试图将图像序列分为2类。 Each sequence has 5 frames. 每个序列有5帧。 I have used ConvLSTM2D as the first layer and I'm getting the above error. 我使用ConvLSTM2D作为第一层,我收到了上述错误。 the input_shape parameter is input_shape = (timesteps, rows, columns, channels) . input_shape参数是input_shape = (timesteps, rows, columns, channels)

The data which I've generated is of this format: 我生成的数据是这种格式:

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

and the first layer is implemented as shown below: 第一层实现如下:

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"))

Can anyone please help me with this? 有人可以帮我这个吗?

Edit: Here is my toying code: 编辑:这是我的玩弄代码:

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))

There are several ways to specify the input shape . 有几种方法可以指定输入形状 From the documentation: 从文档:

Pass an input_shape argument to the first layer. input_shape参数传递给第一个图层。 This is a shape tuple (a tuple of integers or None entries, where None indicates that any positive integer may be expected). 这是一个形状元组(整数或None条目的元组,其中None表示可以预期任何正整数)。 In input_shape , the batch dimension is not included . input_shape不包括批次维度

Therefore, the right input shape is: 因此,正确的输入形状是:

input_shape = (timesteps, rows, columns, channels)

After fixing this error you will encounter the next error (it is not related to input_shape ): 修复这个错误后,您会遇到下一个错误(它是相关input_shape ):

ValueError: Input 0 is incompatible with layer conv_lst_m2d_2: expected ndim=5, found ndim=4 ValueError:输入0与图层conv_lst_m2d_2不兼容:预期ndim = 5,发现ndim = 4

This error occurs when you try to add the second ConvLSTM2D layer. 当您尝试添加第二个ConvLSTM2D图层时会发生此错误。 This happens because the output of the first ConvLSTM2D layer is a 4D tensor with shape (samples, output_row, output_col, filters) . 发生这种情况是因为第一个ConvLSTM2D图层的输出是具有形状的4D张量(samples, output_row, output_col, filters) You might want to set return_sequences=True , in which case the output is a 5D tensor with shape (samples, time, output_row, output_col, filters) . 您可能希望设置return_sequences=True ,在这种情况下,输出是具有形状的5D张量(samples, time, output_row, output_col, filters)

After fixing this error, you will encounter a new error happening in the following lines: 修复此错误后,您将在以下行中遇到新错误:

model.add(Flatten())
model.add(LSTM(10))

It does not make sense to have a Flatten layer right before an LSTM layer. LSTM层之前LSTM Flatten图层是没有意义的。 This will never work as the LSTM requires a 3D input tensor with shape (samples, time, input_dim) . 这将永远不会起作用,因为LSTM需要具有形状的3D输入张量(samples, time, input_dim)

To sum up, I highly recommend you to take a close look at the Keras documentation, in particular for the LSTM and ConvLSTM2D layers. 总而言之,我强烈建议您仔细查看Keras文档,特别是LSTMConvLSTM2D层。 It is also important to understand how these layers work to make a good use of them. 了解这些层如何充分利用它们也很重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras Conv2D - ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3 - Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3 Keras Conv1D ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2 - Keras Conv1D ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 Keras ValueError:输入 0 与层 conv2d_1 不兼容:预期 ndim=4,发现 ndim=5 - Keras ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5 ValueError: 层 conv1d 的输入 0 与层不兼容: : 预期 min_ndim=3, 发现 ndim=2 - ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2 ValueError:输入0与层conv2d_5不兼容:预期ndim = 4,找到的ndim = 2 - ValueError: Input 0 is incompatible with layer conv2d_5: expected ndim=4, found ndim=2 ValueError:输入 0 与层 conv2d_1 不兼容:预期 ndim=4,发现 ndim=3 - ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=3 Keras:输入 0 与层 conv1d_5 不兼容:预期 ndim=3,发现 ndim=2 - Keras: Input 0 is incompatible with layer conv1d_5: expected ndim=3, found ndim=2 Keras Conv1d输入形状问题,conv1d层的输入0与层不兼容::预期min_ndim=3,发现ndim=2 - Keras Conv1d input shape problem, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2 ValueError:层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(28、28、1) - ValueError: Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (28, 28, 1) 在 Tensorflow 中创建 model 时出错。 ValueError: 层 conv2d_1 的输入 0 与层不兼容: : 预期 min_ndim=4, 发现 ndim=3 - Error with creating a model in Tensorflow. ValueError: Input 0 of layer conv2d_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM