【问题标题】:Number of channels in convLSTM for classifying videosconvLSTM 中用于视频分类的通道数
【发布时间】:2020-07-17 13:16:23
【问题描述】:

我创建了一个 convLSTM 用于对灰度视频进行分类,这意味着它们只有一个通道。即使我将 1 定义为通道数,我也会得到错误:

ValueError:检查输入时出错:预期的 conv_lst_m2d_1_input 有 5 个维度,但得到了形状为 (128, 176, 256, 256) 的数组

128是训练数据集的大小,176*256是每帧的分辨率,256是每段视频的帧数。

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20, shuffle=True, random_state=0)
 
model = Sequential()
model.add(ConvLSTM2D(filters = 64, kernel_size = (3, 3), return_sequences = False, data_format = "channels_last", input_shape = (seq_len, img_height, img_width, 1)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.3))
model.add(Dense(6, activation = "softmax"))
 
model.summary()
 
opt = keras.optimizers.SGD(lr=0.001)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=["accuracy"])
 
earlystop = EarlyStopping(patience=7)
callbacks = [earlystop]

history = model.fit(x = X_train, y = y_train, epochs=40, batch_size = 8 , shuffle=True, validation_split=0.2, callbacks=callbacks)

【问题讨论】:

    标签: python tensorflow machine-learning keras neural-network


    【解决方案1】:

    您只需要扩展数据的最后一个维度

    batch_dim, seq_len, img_height, img_width = 3, 17, 25, 25
    X = np.random.uniform(0,1, (batch_dim, seq_len, img_height, img_width))
    y = np.random.randint(0,6, batch_dim)
    print(X.shape)
    
    # expand input dimension
    X = X[...,np.newaxis]
    print(X.shape)
    
    model = Sequential()
    model.add(ConvLSTM2D(filters = 64, kernel_size = (3, 3), return_sequences = False, 
                         data_format = "channels_last", 
                         input_shape = (seq_len, img_height, img_width, 1)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(256, activation="relu"))
    model.add(Dropout(0.3))
    model.add(Dense(6, activation = "softmax"))
    model.summary()
    
    model.predict(X).shape
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 2021-11-27
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多