Python 3.x Tokenizer.word“U索引不包含”;“开始”;或;完",;,“相当含蓄”;“开始”;及;完";

Python 3.x Tokenizer.word“U索引不包含”;“开始”;或;完",;,“相当含蓄”;“开始”;及;完";,python-3.x,tensorflow,keras,deep-learning,captions,Python 3.x,Tensorflow,Keras,Deep Learning,Captions,我当时正试图以类似于中的方式制作一个图像字幕模型 我使用ResNet50代替VGG16,还必须通过model.fit_generator()方法使用渐进加载。 我使用了来自的ResNet50,当我通过设置include_top=False导入它时,它以{'key':[[[[value1,value2,….value 2048]]]}的形式为我提供了照片的特征,其中“key”是图像id。 这是我的captionGenerator函数代码:- def createCaptions(tokenizer

我当时正试图以类似于中的方式制作一个图像字幕模型 我使用ResNet50代替VGG16,还必须通过model.fit_generator()方法使用渐进加载。 我使用了来自的ResNet50,当我通过设置include_top=False导入它时,它以{'key':[[[[value1,value2,….value 2048]]]}的形式为我提供了照片的特征,其中“key”是图像id。 这是我的captionGenerator函数代码:-

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]), sequence])
            ID = np.argmax(ID)
            ID = word_for_id(ID)
            if ID is None:
                break
            inSeq += " " + ID
            if ID == "END":
                break
        print(inSeq)
_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字典

问题是,在我通过以下方式生成的培训数据照片描述中:-

def train_test_data(filename):
    DataFile = open(filename, 'r')
    Data = DataFile.read()
    DataFile.close()

    ImageID = []

    textDataFile = pickle.load(open('descriptions.pkl', 'rb'))

    for line in Data.split('\n'):
        if len(line) < 1:
            continue
        ImageID.append(line.split('.')[0])

    Data = {}

    for key in textDataFile:
        if key in ImageID:
            Data[key] = textDataFile[key]

    for ID in Data:
        for i in range(len(Data[ID])):
            l = Data[ID][i]
            l = "START " + " ".join(l) + " END"
            Data[ID][i] = l

    return Data
这个结果是错误的。 请向我解释为什么会发生这种情况。 如果我这样做:-

k = pickle.load(open('word_index.pkl', 'rb'))
print("start" in k)

答案为真。

这是因为默认情况下,
标记器根据
lower=True
参数进行拟合时会降低单词。创建标记器时,可以使用小写或传递
lower=False

k = pickle.load(open('word_index.pkl', 'rb'))
print("start" in k)