AttributeError: 'str' object has no attribute 'ndim', unable to use model.predict()
AttributeError: 'str' object has no attribute 'ndim', unable to use model.predict()
我试图以与 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
我通过以下方式生成了照片数据:-
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))
features 是我的 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'
我哪里错了?
请帮忙。
提前致谢。
您将 inSeq = "START"
作为字符串传递给 model.predict
:
ID = model.predict([np.array(feature[0][0][0]), inSeq])
没有对其进行预处理。您需要将其编码为数组。
我试图以与 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
我通过以下方式生成了照片数据:-
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))
features 是我的 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'
我哪里错了? 请帮忙。 提前致谢。
您将 inSeq = "START"
作为字符串传递给 model.predict
:
ID = model.predict([np.array(feature[0][0][0]), inSeq])
没有对其进行预处理。您需要将其编码为数组。