BLIP2部署教程

简单记录一下BLIP2部署的流程

主要遇到的问题还是有墙导致模型权重无法下载

环境安装

本文采用Lavis进行BLIP2的部署

1.pip 安装lavis,这里记得换一下清华源,下载会快一点

pip install salesforce-lavis

通过下方代码判断lavis库是否安装成功

python 复制代码
from lavis.models import model_zoo
print(model_zoo)

2.替换安装的库中权重文件路径

Image Captioning任务为例

  • 修改 lavis/configs/models/blip_caption_base_coco.yaml

    该文件在python lavis库中,如果是通过git安装的,则直接更改对应git 项目文件

    yaml 复制代码
    model:
      arch: blip_caption
      load_finetuned: True
    
      pretrained: "https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth"
      finetuned: "https://storage.googleapis.com/sfr-vision-language-research/LAVIS/models/BLIP/blip_coco_caption_base.pth"
    
      # vit encoder
      vit_type: "base"
      vit_grad_ckpt: False
      vit_ckpt_layer: 0
      image_size: 384

    pretrainedfinetuned 中的权重文件下载到本地,然后将其替换成权重的绝对路径

  • 运行报错 OSError: Can't load tokenizer for 'bert-base-uncased'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'bert-base-uncased' is the correct path to a dir

    直接命令行通过huggingface镜像运行脚本,从而下载对应bert模型权重
    HF_ENDPOINT=https://hf-mirror.com python 测试脚本.py

    总体思想就是缺少哪个模型权重文件就下载哪个,然后替换成对应绝对路径

    另附Image Captioning任务 测试脚本

    python 复制代码
    import torch
    from lavis.models import load_model_and_preprocess
    from PIL import Image
    
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    # loads BLIP caption base model, with finetuned checkpoints on MSCOCO captioning dataset.
    # this also loads the associated image processors
    model, vis_processors, _ = load_model_and_preprocess(name="blip_caption", model_type="base_coco", is_eval=True, device=device)
    # preprocess the image
    # vis_processors stores image transforms for "train" and "eval" (validation / testing / inference)
    raw_image = Image.open("./merlion.png").convert("RGB")
    
    image = vis_processors["eval"](raw_image).unsqueeze(0).to(device)
    # generate caption
    res=model.generate({"image": image})
    print(res)
    # ['a large fountain spewing water into the air']
相关推荐
天一生水water5 分钟前
基于FFT的频域故障诊断
人工智能·算法·智慧油田
宇擎智脑科技11 分钟前
OpenClaw:开源多渠道AI个人助手的技术架构与实践分析
人工智能·智能体
码农小韩17 分钟前
AIAgent应用开发——DeepSeek分析(一)
人工智能·python·深度学习·agent·强化学习
【赫兹威客】浩哥27 分钟前
半导体芯片缺陷检测数据集分享及多版本YOLO模型训练验证
人工智能·计算机视觉·目标跟踪
学Linux的语莫28 分钟前
skills的使用
java·数据库·python
硅基流动29 分钟前
硅基流动 × Open Course:更懂你的个性化智能学伴
人工智能
大模型玩家七七32 分钟前
关系记忆不是越完整越好:chunk size 的隐性代价
java·前端·数据库·人工智能·深度学习·算法·oracle
Elastic 中国社区官方博客32 分钟前
Elasticsearch:使用 Workflow 查询天气,发送消息到 Slack
大数据·运维·人工智能·elasticsearch·搜索引擎·ai
康康的AI博客33 分钟前
AI技术驱动电商内容与策略优化:如何提升客户参与度与品牌价值
大数据·人工智能
搞科研的小刘选手41 分钟前
【人工智能专题】2026年人工智能与生成式设计国际学术会议(ICAIGD 2026)
人工智能·算法·aigc·生成式ai·学术会议·计算机工程·生成式设计