ValueError:图表已断开,当我尝试在keras中构建Timedistrubuted模型时

时间:2019-03-11 04:49:53

标签: python tensorflow keras

我试图从this paper构建“ ReHAR”模型

那个模型看起来像这样。 ReHAR

这是我的代码

import keras
from keras.applications.vgg16 import VGG16
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,TimeDistributed,LSTM,concatenate,Dropout,Reshape,Lambda
from keras.layers.pooling import GlobalAveragePooling2D,GlobalMaxPooling2D
from keras.models import Model, Sequential
from keras.utils import plot_model

frame_shape = (224,224,3)
frames_shape =(24,224,224,3)
optical_shape = (224,224,3)
opticals_shape = (24,224,224,3)

def get_model():
    frames_input =  keras.Input(shape=frames_shape)
    opticals_input = keras.Input(shape=opticals_shape)

    # single frame network
    spatial = VGG16(input_shape=frame_shape,weights="imagenet",include_top=False)
    spatial_gm = GlobalMaxPooling2D()(spatial.output)
    spatial_gm = Reshape((1,512))(spatial_gm)
    spatial_ga = GlobalAveragePooling2D()(spatial.output)
    spatial_ga = Reshape((1,512))(spatial_ga)

    temporal = VGG16(input_shape=optical_shape,weights="imagenet",include_top=False)
    for layer in temporal.layers:
        layer.name = layer.name + '_2'
    temporal_gm = GlobalMaxPooling2D()(temporal.output)
    temporal_gm = Reshape((1,512))(temporal_gm)
    temporal_ga = GlobalAveragePooling2D()(temporal.output)
    temporal_ga = Reshape((1,512))(temporal_ga)
    fetures = concatenate([spatial_gm,spatial_ga,temporal_gm,temporal_ga], axis=1)

    lstm1 = LSTM(512, activation='relu', name='LSTM1',kernel_initializer='glorot_normal')(fetures)
    output1 = Dense(11, activation='softmax', name='output1',kernel_initializer='glorot_normal')(lstm1)
    single_frame_model = Model([spatial.input, temporal.input],output1)
    single_frame_model.summary()

    concat = concatenate([frames_input, opticals_input])

    single_frame_outputs = TimeDistributed(Lambda(lambda x: single_frame_model([x[:,:,:,:3], x[:,:,:,3:]])))(concat)

    # activity network

    lstm2 = LSTM(512, activation='relu', name='LSTM2',kernel_initializer='glorot_normal',return_sequences=True)(single_frame_outputs)
    output2 = Dense(11, activation='softmax', name='output2',kernel_initializer='glorot_normal')(lstm2)


    model = Model([frames_input, opticals_input], [output1,output2])
    return model

if __name__ == '__main__':
    model = get_model()
    model.summary()
    #plot_model(model, to_file='model.png')

这是我的错误消息

  

回溯(最近一次通话最近):文件“ ReHAR.py”,第50行,在          模型= get_model()在get_model中的文件“ ReHAR.py”,第46行       型号=型号([frames_input,Opticals_input],[output1,output2])文件   “ /home/hashmymind/.local/lib/python3.6/site-packages/keras/legacy/interfaces.py”,   第91行,在包装器中       返回func(* args,** kwargs)文件“ /home/hashmymind/.local/lib/python3.6/site-packages/keras/engine/network.py”,   第93行,初始化       self._init_graph_network(* args,** kwargs)文件“ /home/hashmymind/.local/lib/python3.6/site-packages/keras/engine/network.py”,   _init_graph_network中的第231行       self.inputs,self.outputs)文件“ /home/hashmymind/.local/lib/python3.6/site-packages/keras/engine/network.py”,   _map_graph_network中的第1443行       str(layers_with_complete_input))ValueError:图形已断开连接:无法获取张量Tensor(“ input_4:0”,shape =(?, 224,224,   3),在“ input_4_2”层的dtype = float32)。以下的前几层   被访问没有问题:[

我试图通过在线搜索类似的问题来解决它,但是没有一个起作用。

欢迎任何想法

0 个答案:

没有答案