ValueError:请使用“图层”实例初始化“ TimeDistributed”图层

时间:2019-12-28 07:52:07

标签: python tensorflow machine-learning keras deep-learning

我正在尝试建立一个可以在音频和视频样本上进行训练的模型,但出现此错误
ValueError: Please initialize `TimeDistributed` layer with a `Layer` instance. You passed: Tensor("input_13:0", shape=(None, 5, 648, 384, 3), dtype=float32)

这是我的三个模型函数:

def build_convnet(shape=(648, 384, 3)):
    momentum = .9 
    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Conv2D(64, (2,2), input_shape=shape,padding='same', activation='relu'))
    model.add(tf.keras.layers.Conv2D(64, (2,2), padding='same', activation='relu'))
    model.add(tf.keras.layers.BatchNormalization(momentum=momentum))
    model.add(tf.keras.layers.MaxPool2D())
    model.add(tf.keras.layers.Conv2D(128, (3,3), padding='same', activation='relu'))
    model.add(tf.keras.layers.Conv2D(128, (3,3), padding='same', activation='relu'))
    model.add(tf.keras.layers.BatchNormalization(momentum=momentum))
    model.add(tf.keras.layers.MaxPool2D())
    model.add(tf.keras.layers.Conv2D(256, (3,3), padding='same', activation='relu'))
    model.add(tf.keras.layers.Conv2D(256, (3,3), padding='same', activation='relu'))
    model.add(tf.keras.layers.BatchNormalization(momentum=momentum))
    model.add(tf.keras.layers.GlobalMaxPool2D())

    print(model.summary())
    return model
def action_model(shape=(5, 648, 384, 3)):
    # Create our convnet with (112, 112, 3) input shape
    convnet = build_convnet(shape[1:])
    # then create our final model
    # model = tf.keras.Sequential()
    # add the convnet with (5, 112, 112, 3) shape
    input_shape = tf.keras.layers.Input(shape)
    TD = tf.keras.layers.TimeDistributed(input_shape)(convnet)
    # here, you can also use tf.keras.layers.GRU or LSTM
    LSTM1 = tf.keras.layers.LSTM(1024)(TD)

    Dense1 = tf.keras.layers.Dense(512, activation='relu')(LSTM1)
    Drop1 = tf.keras.layers.Dropout(.2)(Dense1)
    Dense2 = tf.keras.layers.Dense(128, activation='relu')(Drop1)
    Drop2 = tf.keras.layers.Dropout(.2)(Dense2)
    Dense3 = tf.keras.layers.Dense(64, activation='relu')(Drop2)
    # Dense4 = tf.keras.layers.Dense(2, activation='softmax')(Dense3)

    model = tf.keras.models.Model(inputs=input_shape,outputs=Dense3)

    return model
def audio_and_final_model():
  input_shape = tf.keras.layers.Input(shape=(220941,1))
  Conv1 = tf.keras.layers.Conv1D(16,activation='relu',kernel_size=(10))(input_shape)
  MaxPool1 = tf.keras.layers.MaxPool1D()(Conv1)
  Dropout1 = tf.keras.layers.Dropout(0.2)(MaxPool1)
  Conv2 = tf.keras.layers.Conv1D(32,activation='relu',kernel_size=(10))(Dropout1)
  MaxPool2 = tf.keras.layers.MaxPool1D()(Conv2)
  Dropout2 = tf.keras.layers.Dropout(0.2)(MaxPool2)
  Conv3 = tf.keras.layers.Conv1D(16,activation='relu',kernel_size=(10))(Dropout2)
  MaxPool3 = tf.keras.layers.MaxPool1D()(Conv3)
  Dropout3 = tf.keras.layers.Dropout(0.2)(MaxPool3)
  Flatten = tf.keras.layers.Flatten()(Dropout3)
  Dense1 = tf.keras.layers.Dense(128,activation='relu')(Flatten)
  Dense2 = tf.keras.layers.Dense(64,activation='relu')(Dense1)


  model = tf.keras.models.Model(inputs=input_shape,outputs=Dense2)

  return model
INSHAPEAM = (5, 648, 384, 3)
INSHAPEAFM = (220941,1)
am = action_model()
afm = audio_and_final_model()

combined = tf.keras.layers.Concatenate([am.output,afm.output])
z = tf.keras.layers.Dense(2,activation='softmax')(combined)

model = tf.keras.models.Model(inputs=[INSHAPEAM,INSHAPEAFM],outputs=z)

我尝试搜索,但是我只能找到一个答案here,但我并没有真正理解它,因此如果有人可以在这里为我提供帮助,那将是很大的帮助。预先感谢!

1 个答案:

答案 0 :(得分:0)

问题在下面的部分中。从函数更改此部分: 只需使用功能模型而不是顺序

将部件嵌入到动作模型中的build_convnet上
shipments