【问题标题】:(deep learning, rnn , cnn) image image captinoing with keras(深度学习,rnn,cnn)使用 keras 的图像图像字幕
【发布时间】:2019-05-14 06:58:38
【问题描述】:

我制作了图像字幕教程,但它不起作用。帮帮我……

图像字幕是一种解释人输入图像的模型。

我没有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'])

但是,它不起作用,有人说这段代码是用Sequential设计的。因此,我将它们更改为 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 周时间....

【问题讨论】:

    标签: keras lstm recurrent-neural-network


    【解决方案1】:

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

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

    尝试将其更改为:

    caption_in = Input(shape=(max_len,))
    

    【讨论】:

    • 错误代码:ValueError:您正在尝试将包含 6 层的权重文件加载到具有 4 层的模型中。
    • 当我加载使用教程模型设计的重量时会发生此错误。教程模型设计有 6 层,但我的模型由 4 层组成。