Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Tensorflow Keras将CNN与LSTM相结合_Python_Tensorflow_Neural Network_Keras_Lstm - Fatal编程技术网

Python 使用Tensorflow Keras将CNN与LSTM相结合

Python 使用Tensorflow Keras将CNN与LSTM相结合,python,tensorflow,neural-network,keras,lstm,Python,Tensorflow,Neural Network,Keras,Lstm,我正在使用预先训练过的ResNet-50模型,并希望将倒数第二层的输出提供给LSTM网络。以下是我的示例代码,仅包含CNN(ResNet-50): 接下来,我想将其馈送到LSTM网络,如下所示 final_model = Sequential() final_model.add((model)) final_model.add(LSTM(64, return_sequences=True, stateful=True)) final_model.add(Dense(N, activation='

我正在使用预先训练过的ResNet-50模型,并希望将倒数第二层的输出提供给LSTM网络。以下是我的示例代码,仅包含CNN(ResNet-50):

接下来,我想将其馈送到LSTM网络,如下所示

final_model = Sequential()
final_model.add((model))
final_model.add(LSTM(64, return_sequences=True, stateful=True))
final_model.add(Dense(N, activation='softmax'))
但我不知道如何将输出重塑为LSTM输入。我的原始输入是(224*224*3)到CNN。 另外,我应该使用时间分配吗


感谢您的任何帮助。

在CNN之后添加LSTM没有多大意义,因为LSTM主要用于时间/序列信息,而您的数据似乎只是空间数据,但是如果您仍然喜欢使用它,请使用它

x = Reshape((1024,1))(x)
这将把它转换为1024个样本的序列,其中有一个特征


如果您正在谈论时空数据,请在Resnet层上使用
Timedistributed
,然后您可以使用将预训练网络与LSTM结合使用的示例:

inputs = Input(shape=(config.N_FRAMES_IN_SEQUENCE, config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))
cnn = VGG16(include_top=False, weights='imagenet', input_shape=(config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))
x = TimeDistributed(cnn)(inputs)
x = TimeDistributed(Flatten())(x)
x = LSTM(256)(x)

谢谢,你给我指明了方向。。。你的第一个建议非常有效,但我正在考虑你告诉我的事实——这没有多大意义。事实上,是的,我有时空数据。但是我如何使用时间分配,请您详细说明一下,以便我理解。我当时看到了这个答案。响应建议“复制ResNet的代码,并将时间分布添加到所有层,然后将“基本”ResNet的权重加载到我定制的ResNet上。”那么,我是否要执行此操作?是的,确切地说,类似于
input\u layer=input(shape);基本模型=ResNet50(权重=imagenet',包括顶部=False);x=时间分布(基本模型)(输入层);x=convlstm2d(参数)(x);。。。。;x=时间分布(密集(N))(x)你使用imageDataGenerator准备图像序列了吗?@TheJokerAEZ不,我没有使用imageDataGenerator。改为使用预先保存的路径…您测试过吗?它返回一个维度错误。VGG16需要输入形状(批次大小、宽度、高度、维度),而LSTM需要序列(批次大小、时间步长、宽度、高度、维度)。我现在面临这个问题。
inputs = Input(shape=(config.N_FRAMES_IN_SEQUENCE, config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))
cnn = VGG16(include_top=False, weights='imagenet', input_shape=(config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))
x = TimeDistributed(cnn)(inputs)
x = TimeDistributed(Flatten())(x)
x = LSTM(256)(x)