首页 > 解决方案 > (深度学习,rnn,cnn)图像图像 captinoing 与 keras

问题描述

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

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

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

请帮帮我……我只为此花了两周时间……

标签: keraslstmrecurrent-neural-network

解决方案


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

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

尝试将其更改为:

caption_in = Input(shape=(max_len,))

推荐阅读