You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EfficientNetB0+LSTM模型训练输入形状不匹配错误原因排查

问题:EfficientNetB0+LSTM模型训练时输入形状不匹配错误

训练阶段出现如下错误:

ValueError: Input 0 of layer "model_1" is incompatible with the layer: expected shape=(None, 2, 224, 224, 3), found shape=(None, 224, 224, 3)

模型构建及训练代码

import numpy as np
from sklearn.model_selection import StratifiedKFold

def create_model():
    conv_base = tf.keras.applications.EfficientNetB0(weights='imagenet',include_top=False,input_shape=(224, 224, 3))
    conv_base.trainable = True
    
    model = models.Sequential()
    model.add(conv_base)
    model.add(layers.Flatten())
    model.add(layers.Dense(32))
  
    cnn = models.Model(model.input, model.output)
    
    seq_input = Input(shape=(2,224,224,3))
    
    encoded_sequence = TimeDistributed(cnn)(seq_input)
    encoded_sequence = Bidirectional(LSTM(32, return_sequences=True))(encoded_sequence)
    encoded_sequence = Bidirectional(LSTM(32, return_sequences=False))(encoded_sequence)
    out = Dense(2, activation="softmax")(encoded_sequence)
    cnn_lstm = models.Model(seq_input, out)
    cnn_lstm.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(learning_rate=1e-4),metrics=['acc'])
    
    if fold_count == 1:
        cnn_lstm.summary()
    return cnn_lstm
#//////////////////////////////////////////////////////////////////////////////


hist_EfficientNetB0LSTM = {}
seed = 7
np.random.seed(seed)
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)

fold_count=0
for train_index, val_index in skf.split(X_train,Y_train):
    fold_count = fold_count + 1
    
    x_train, x_val = np.array(X_train)[train_index], np.array(X_train)[val_index]
    y_train, y_val = np.array(Y_train)[train_index], np.array(Y_train)[val_index]
    
    y_train = np_utils.to_categorical(y_train, num_classes=2)
    y_val = np_utils.to_categorical(y_val, num_classes=2)
    
    model = create_model()
    # from keras.utils.vis_utils import plot_model
    # plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
    
    print(x_train.dtype, y_train.dtype)

    x_train = x_train.astype(np.float32)
  

history = model.fit(x_train,y_train,epochs=50, batch_size=10, validation_data = (x_val,y_val), shuffle=True)
    plot_results(history)
    hist_EfficientNetB0LSTM['fold'+str(fold_count)+'_history']= history.history
    hist_EfficientNetB0LSTM['fold'+str(fold_count)+'_results'] = calculate_test_accuracy()
    print('fold_'+str(fold_count)+' done!')
    with open(base_dir+'hist_EfficientNetB0LSTM_fold_'+str(fold_count), 'wb') as file_pi1:
        pickle.dump(hist_EfficientNetB0LSTM, file_pi1)

异常成因分析

核心原因是模型期望的输入形状与训练数据的实际形状不匹配:

  • 模型定义中,seq_input = Input(shape=(2,224,224,3))明确指定输入需要是包含2个时序帧的图像序列,完整输入形状为(None, 2, 224, 224, 3)(第一个维度为batch size)。
  • 但训练时传入的x_train是单张图像的集合,形状为(None, 224, 224, 3),缺少模型要求的长度为2的时序维度。

简单来说:模型要的是「每组2张图的序列」,但你给的是「一张张单独的图」,两者维度不匹配就触发了错误。

另外注意代码里的语法问题:model.fit的缩进错误,它应该放在for循环内部,否则会在循环结束后才执行训练,不过这不是当前报错的直接原因。

内容的提问来源于stack exchange,提问作者MohammadReza Safari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 12:37:18