我已经用Keras训练了一个TPU进行图像字幕。跑步时出现错误: 导入matplotlib.pyplot作为plt
def generateCaption(photo):
in_text = START
for i in range(max_length):
sequence = [wordtoidx[w] for w in in_text.split() if w in wordtoidx]
sequence = pad_sequences([sequence], maxlen=max_length)
yhat = caption_model.predict([photo,sequence], verbose=0)
yhat = np.argmax(yhat)
word = idxtoword[yhat]
in_text += ' ' + word
if word == STOP:
break
final = in_text.split()
final = final[1:-1]
final = ' '.join(final)
return final
for z in range(10):
pic = list(encoding_test.keys())[z]
image = encoding_test[pic].reshape((1,OUTPUT_DIM))
print(os.path.join(root_captioning,'/content/drive/My Drive/ImageCaptioning/Flicker8k Dataset', pic))
x = plt.imread(os.path.join(root_captioning,'/content/drive/My Drive/ImageCaptioning/Flicker8k Dataset', pic))
plt.imshow(x)
plt.show()
print("Caption:",generateCaption(image))
print("_____________________________________")
以下错误:print("Caption:",generateCaption(image))
我尝试了其他重塑选项,也尝试删除了重塑功能,但错误仍然存在。
答案 0 :(得分:0)
当我尝试训练网络时,此错误发生在我身上,因此可能与 LSTM 有关。在这种特定情况下,模型抱怨是因为第三层的尺寸不匹配。您可以尝试检查训练图像的尺寸是否与此处使用的尺寸相同,或者检查图层的参数。