【问题标题】:Extracting Features for Image Caption Generator using Cnn & Lstm?使用 Cnn 和 Lstm 提取图像标题生成器的特征?
【发布时间】:2020-04-11 09:22:17
【问题描述】:

1.这部分我得到的输出是 Created a "descriptions.txt" file

# Set these path according to project folder in you system
dataset_text = 'C:\\Users\Srikanth Bhattu\Project\Flickr8k_text\Flickr8k.token.txt'
dataset_images = 'C:\\Users\Srikanth Bhattu\Project\Flickr8k_Dataset\Flicker8k_Dataset'
#we prepare our text data
filename = dataset_text + "/" + 'C:\\Users\Srikanth Bhattu\Project\Flickr8k_text\Flickr8k.token.txt'
#loading the file that contains all data
#mapping them into descriptions dictionary img to 5 captions
descriptions = all_img_captions()
print("Length of descriptions =" ,len(descriptions))
#cleaning the descriptions
clean_descriptions = cleaning_text(descriptions)
#building vocabulary 
vocabulary = text_vocabulary()
print("Length of vocabulary = ", len(vocabulary))
#saving each description to file 
save_descriptions()

2.帮我加载一个图像数据集并生成如下给定的输出

def extract_features(directory):
        model = Xception( include_top=False, pooling='avg' )
        features = {}
        for img in tqdm(os.listdir(directory)):
            filename = directory + "/" + img
            image = Image.open(filename)
            image = image.resize((299,299))
            image = np.expand_dims(image, axis=0)
            #image = preprocess_input(image)
            image = image/127.5
            image = image - 1.0
            feature = model.predict(image)
            features[img] = feature
        return features
#2048 feature vector
features = extract_features(dataset_images)
dump(features, open("features.p","wb"))

错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-246-e6797fb99786> in <module>
      1 #2048 feature vector
----> 2 features = extract_features(dataset_images)
      3 dump(features, open('C:\\Users\Srikanth Bhattu\Project\features.p','wb'))

<ipython-input-242-ab5029ed6c28> in extract_features(directory)
      1 def extract_features(directory):
----> 2         model = Xception( include_top=False, pooling='avg' )
      3         features = {}
      4         for img in tqdm(os.listdir(directory)):
      5             filename = directory + "/" + img

NameError: name 'Xception' is not defined.

我需要这个输出图像:Click here to Open output Image

我正在使用 “使用 CNN 和 LSTM 的图像字幕生成器”。我有所有源代码和数据集,但我无法在我的代码中包含我的数据集和路径。 如果有人有兴趣帮助我完成我的项目,请发表评论,我将向他们支付一些费用。 谢谢...!!!

【问题讨论】:

  • 您能否更新您的问题并提供更多详细信息?这个问题太笼统了。
  • 帖子已更新..!请通过这个 - Rohan Bojja

标签: python python-3.x lstm


【解决方案1】:

在您共享的代码 sn-ps 中,您没有定义名为“Xception”的模型。因此,除非您定义模型或导入它,否则它不会起作用。我猜你正在关注本教程https://data-flair.training/blogs/python-based-project-image-caption-generator-cnn/

仔细阅读,你会发现他们已经导入了文章开头的模型,这样做应该没问题。按顺序阅读文章,您不会遇到问题。

import string
import numpy as np
from PIL import Image
import os
from pickle import dump, load
import numpy as np
from keras.applications.xception import Xception, preprocess_input
from keras.preprocessing.image import load_img, img_to_array
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from keras.layers.merge import add
from keras.models import Model, load_model
from keras.layers import Input, Dense, LSTM, Embedding, Dropout
# small library for seeing the progress of loops.
from tqdm import tqdm_notebook as tqdm
tqdm().pandas()

这些必须在运行 sn-p 之前执行。它会导入 Xception 模型 和您正在使用的其他库。

【讨论】:

  • 是的 - Rohan bojja iam 遵循您上面提到的相同教程。我已经得到了 3 个部分的输出,但我在这个提取特征部分感到震惊。谢谢你的回答,我会照你说的那样做
  • from keras.applications.xception import Xception, preprocess_input 这行应该在运行sn-p之前执行。
  • - Rohan Bojja 在导入“from keras.applications.xception import Xception”后我的代码运行成功。非常感谢您的帮助...!
  • 嗨,请你帮我解决这个“使用 CNN 和 LSTM 生成图像字幕”@Rohan Bojja
  • @SrikanthBhattu 你能告诉我更多关于你面临的问题吗?
猜你喜欢
  • 2022-06-11
  • 2021-10-23
  • 2018-09-24
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
相关资源
最近更新 更多