AttributeError:'str'对象没有属性'ndim',无法使用model.predict()

时间:2018-05-15 11:36:19

标签: python-3.x tensorflow keras deep-learning captions

我试图以与here

类似的方式制作图像字幕模型

我使用ResNet50代替了VGG16,还必须通过model.fit_generator()方法使用渐进式加载。

我使用了来自here的ResNet50,当我通过设置include_top = False导入它时,它给了我{{key'形状的照片功能:[[[[value1,value2,.... value] 2048]]]]},其中“key”是图像ID。

这是我的字幕生成器功能代码: -

def createCaptions(tokenizer, photoData, MaxLength, model):
    for key, feature in photoData.items():
        inSeq = "START"
        for i in range(MaxLength):
            sequence = tokenizer.texts_to_sequences([inSeq])[0]
            sequence = pad_sequences([sequence], maxlen = MaxLength)
            ID = model.predict([np.array(feature[0][0][0]), inSeq])
            ID = word_for_id(ID)
            if ID is None:
                break
            inSeq += " " + ID
            if ID == "END":
                break
        print(inSeq)

word_for_id函数是: -

def word_for_id(integer, tokenizer):
    for word, index in tokenizer.word_index.items():
        if index == integer:
            return word
    return None

我通过以下方式生成了photoData: -

features = {}
for images in os.listdir(args["image"]):
    filename = args["image"] + '/' + images
    image = load_img(filename, target_size = inputShape)
    image = img_to_array(image)
    image = np.expand_dims(image, axis = 0)
    image = preprocess(image)
    pred = resnet.predict(image)
    image_id = images.split('.')[0]
    features[image_id] = pred
    print('>{}'.format(images))

功能是我的photoData词典。

当我尝试生成字幕时: -

caption = createCaptions(tokenizerTrain, features, 34, model)

我收到以下错误: -

Traceback (most recent call last):
  File "CaptionGenerator.py", line 111, in <module>
caption = createCaptions(tokenizerTrain, features, 34, model)
  File "CaptionGenerator.py", line 101, in createCaptions
ID = model.predict([np.array(feature[0][0][0]), inSeq])
  File "/home/aditya/.virtualenvs/cv/lib/python3.5/site-packages/keras/engine/training.py", line 1817, in predict
check_batch_axis=False)
  File "/home/aditya/.virtualenvs/cv/lib/python3.5/site-packages/keras/engine/training.py", line 76, in _standardize_input_data
data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
  File "/home/aditya/.virtualenvs/cv/lib/python3.5/site-packages/keras/engine/training.py", line 76, in <listcomp>
data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
AttributeError: 'str' object has no attribute 'ndim'

我哪里出错了? 请帮忙。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您将inSeq = "START"作为字符串传递给model.predict

ID = model.predict([np.array(feature[0][0][0]), inSeq])

没有预处理它。您需要将其编码为数组。