我的模型是一个(trained EfficientNetB0)+LSTM模型,我的数据有两个类。
我知道同样的问题已经被问过了,但答案在我的情况下不起作用。
这就是我创建模型并尝试训练它的地方。但是在model.fit部分,我得到了以下错误:
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)
这一例外的原因是什么?
1条答案
按热度按时间szqfcxe21#
我对这个模型的架构有点困惑。但据我所知,你只是想让你的输入通过CNN模型,变成一个一维向量,其中有32个元素,并希望使用
TimeDistributed
层在2个时间步发生。无论是什么情况,我认为应该做的是将X_train和其他数据集更改为
(2, 224, 224, 3)
形状。例如,如果您看到https://www.tensorflow.org/api_docs/python/tf/keras/layers/TimeDistributed链接,则假定输入为
(32 video sample numbers as batch size, 10 timesteps, 224 image height, 224 image width, 3 image channel)
只需检查您的输入是否适合使用此
TimeDistributed
层。如果您的输入没有序列,只是普通的图像,您可能正在尝试一些TimeDistributed
层无法实现的东西