(深度学习,rnn,cnn)与keras进行图像验证

时间:2019-05-14 06:58:39

标签: keras lstm recurrent-neural-network

我制作了图像字幕教程,但是没有用。帮帮我...

图像字幕是一个模型,用于解释人输入的图像。

我没有GPU,因此我必须在教程中制作相同的模型, 然后,将权重加载到教程目录中。

我复制了图像字幕教程

这是教程培训模型代码:

image_model = Sequential([
    Dense(embedding_size, input_shape=(2048,), activation='relu'),
    RepeatVector(max_len)
    ])

caption_model = Sequential([
    Embedding(vocab_size, embedding_size, input_length=max_len),
    LSTM(256, return_sequences=True),
    TimeDistributed(Dense(300))
])  

final_model = Sequential([
    Merge([image_model, caption_model], mode='concat', concat_axis=1),
    Bidirectional(LSTM(256, return_sequences=False)),
    Dense(vocab_size),
    Activation('softmax')
    ])

final_model.compile(loss='categorical_crossentropy', optimizer=RMSprop(),         
    metrics=['accuracy'])

但是,它不起作用,有人说此代码是按顺序设计的。因此,我将它们更改为Function API。但是我不知道如何更改它们。

这是我的代码:

embedding_size = 300
vocab_size = 8256
max_len = 40

image_model = Sequential([
    Dense(embedding_size, input_shape=(2048,), activation='relu'),
    RepeatVector(max_len)
  ])

caption_model = Sequential([
    Embedding(vocab_size, embedding_size, input_length=max_len),
    LSTM(256, return_sequences=True),
    TimeDistributed(Dense(300))
])

image_in = Input(shape=(2048,))
caption_in = Input(shape=(max_len, vocab_size))
merged = concatenate([image_model(image_in), caption_model(caption_in)], 
       axis=0)
latent = Bidirectional(LSTM(256, return_sequences=False))(merged)
out = Dense(vocab_size, activation='softmax')(latent)
model = Model([image_in(image_in), caption_in(caption_in)], out)
model.compile(loss='categorical_crossentropy', optimizer=RMSprop(),             
    metrics=['accuracy'])

我遇到了错误:

ValueError: "input_length" is 40, but received input has shape (None, 40, 8256)

请帮助我...我只花了2周的时间。...

1 个答案:

答案 0 :(得分:0)

如错误消息所示,您对嵌入层的输入形状错误:

caption_in = Input(shape=(max_len, vocab_size))

尝试将其更改为:

caption_in = Input(shape=(max_len,))