PaddleNLP [Question]: 使用ERNIE-VIL2.0做跨模态检索,如何提速?

knpiaxh1  于 23天前  发布在  其他
关注(0)|答案(4)|浏览(19)

没有提供描述。

ujv3wf0j

ujv3wf0j1#

document_store = ElasticsearchDocumentStore(
        host=args.host,
        port=args.port,
        username="",
        password="",
        embedding_dim=args.embedding_dim,
        index=args.index_name,
    )

    # 文搜图,对image做embedding
retriever_mm = MultiModalRetriever(
        document_store=document_store,
        query_embedding_model=args.query_embedding_model,
        query_type="text",
        document_embedding_models={"image": args.document_embedding_model},
    )

主要耗费时间的是 MultiModalRetriever 这一步骤,大约需要 15-25s 左右。但是如果预加载这两个模型,又会占用大约 5G 的显存,似乎把特征库也加载进去了。有没有什么折中的办法,既能提速又不过多占用显存?

trnvg8h3

trnvg8h32#

可以参考这个PR,使用fastdeploy进行加速。
https://github.com/PaddlePaddle/PaddleNLP/pull/5884/files#diff-c03da1cc4c202ac2af06716ac6ca6b22f18475671f893231d90ee17be650d051

am46iovg

am46iovg3#

可以参考这个pr,使用fastdeploy进行加速。 https://github.com/PaddlePaddle/PaddleNLP/pull/5884/files#diff-c03da1cc4c202ac2af06716ac6ca6b22f18475671f893231d90ee17be650d051
谢谢您的回复,我把taskflow设置为全局变量之后速度就快了很多了,但是遇见了一个问题,用flask进行接口调用时,显存和内存都会不断增大直到溢出程序被异常关闭,这个有解决办法吗?
‘’‘python
def getvideo(url):

获取当前路径

savepath = os.path.join(os.getcwd(), 'videos',get_valid_filename(url))
response=requests.get(url,stream=True)
if response.status_code == 200:
from tqdm import tqdm
with open(savepath, 'wb') as file:
total_size = int(response.headers.get('content-length', 0))
with tqdm(total=total_size, unit='B', unit_scale=True, unit_divisor=1024) as pbar:
for chunk in response.iter_content(1024):
pbar.update(len(chunk))
file.write(chunk)
try:
getivideo(savepath,url)
gettvideo(savepath,url)
except Exception as e:
print("视频问题:",e)
traceback.print_exc()

finally:
          # 无论视频是否损坏,都删除文件
          try:
              os.remove(savepath)
          except:
              pass

def getivideo(savepath,query):
document_storev = ElasticsearchDocumentStore(
host=args.host,
port=args.port,
username="",
password="",
embedding_dim=args.embedding_dim,
index='image2video',
)
retriever_mmv = MultiModalRetriever(
document_store=document_storev,
query_embedding_model=args.query_embedding_model,
query_type="image",
document_embedding_models={"image": args.document_embedding_model},
)
now = datetime.now()
today = now.date()
output_dir=videoframe(savepath)
docs = [
Document(content=f"{output_dir}/{fname}", content_type="image",meta={"detail": query,"date":today})
for fname in os.listdir(output_dir)
]

# raise NotImplementedError

  # 文档数据写入数据库
  document_storev.write_documents(docs)

  # 文搜图,对image做embedding

  document_storev.update_embeddings(retriever_mmv)
  shutil.rmtree(output_dir)
q1qsirdb

q1qsirdb4#

根据您提供的信息,您在Flask接口调用时遇到了显存和内存不断增大直到溢出程序被异常关闭的问题。这可能是由于您的程序在处理大量数据时没有正确释放资源导致的。您可以尝试以下方法来解决这个问题:

  1. 在处理完每个请求后,确保及时关闭文件、数据库连接等资源。例如,在您的代码中,可以在with open(savepath, 'wb') as file:with tqdm(total=total_size, unit='B', unit_scale=True, unit_divisor=1024) as pbar:这两个上下文管理器内部,分别添加file.close()pbar.close()来关闭文件和进度条。

  2. 检查您的程序是否存在内存泄漏。内存泄漏是指程序在申请内存后,无法释放已申请的内存空间,一次内存泄漏危害可以忽略,但内存泄漏堆积后果很严重,无论多少次内存分配操作,最终都会导致系统崩溃。您可以使用Python的tracemalloc库来检测内存泄漏。

  3. 如果问题仍然存在,您可以考虑使用Python的gc库(垃圾回收器)来手动触发垃圾回收。在适当的地方添加gc.collect()即可。

希望这些建议能帮助您解决问题。如果还有其他问题,请随时提问。

相关问题