keras - (深度学习,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)
请帮帮我……我只为此花了两周时间……
解决方案
如错误消息所示,您对嵌入层的输入具有错误的形状:
caption_in = Input(shape=(max_len, vocab_size))
尝试将其更改为:
caption_in = Input(shape=(max_len,))
推荐阅读
- java - CCC 2018 J4:NoSuchElementException 错误 (Java)
- html - 绝对定位的元素的父元素绝对必须容纳子元素的高度
- java - Gradle : : 在 sourceSets 中设置 outputDir 时找不到方法 main()
- javascript - javascript - 如何在查看文档时检测用户正在使用什么浏览器?
- reactjs - 可以在按钮反应中的onClick中调用异步函数吗
- python - Python的数字字典
- reactjs - React 默认值不显示在输入字段中
- laravel - 我使用开放服务器,当我使用 jquery 时,显示错误 500(内部服务器错误)。如何解决?
- r - 如何在 ggplot cumsum plot 的 x 轴上显示刻度标签
- postgresql - 带有 Hikari 的 Spring JPA 未释放连接