BLIP 模型中 get_image_features() 的输出不一致——如何获得一致的结果?

问题描述 投票:0回答:2

我目前正在使用 BLIP 模型通过其 get_image_features() 方法获取图像嵌入。但是,每次重新加载模型时,此方法都会为相同的输入返回不同的值。有没有办法在每次加载模型时都获得一致的结果?

如有任何指导,我们将不胜感激。

这是我的代码片段。

from PIL import Image
from transformers import AutoProcessor, TFBlipModel
model = TFBlipModel.from_pretrained("Salesforce/blip-image-captioning-base")
processor = AutoProcessor.from_pretrained("salesforce/blip-image-captioning-base")

image = Image.open(img_path)
inputs = processor(images=image, return_tensors="tf")
image_features = model.get_image_features(**inputs).numpy().squeeze()
python feature-extraction huggingface
2个回答
0
投票

简而言之,是因为模型使用的种子。您应该能够设置定义的种子以获得一致的响应。


0
投票

我在我的案例中找到了方法。 这是我的代码片段。

您需要保存并加载模型的权重。

from PIL import Image
import tensorflow as tf
from transformers import AutoProcessor, TFBlipModel

model = TFBlipModel.from_pretrained("Salesforce/blip-image-captioning-base")
# model.save_weights("weights", save_format="tf")
model.load_weights("weights")
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")

image = Image.open("./abc.jpg")
inputs = processor(images=image, return-tensors="tf")
image_features = model.get_image_features(**inputs).numpy().squeeze()
print(image_features)

首先您必须保存权重,然后加载权重。

© www.soinside.com 2019 - 2024. All rights reserved.