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']
相关推荐
amazinging7 分钟前
北京-4年功能测试2年空窗-报培训班学测开-第五十天
python·学习·面试
Jamence10 分钟前
多模态大语言模型arxiv论文略读(157)
论文阅读·人工智能·语言模型·自然语言处理·论文笔记
DogDaoDao20 分钟前
Rembg开源项目全面解析:从原理到实践应用
人工智能·深度学习·开源·github·图像分割·背景检测·rembg
汀、人工智能33 分钟前
AI-Compass LLM训练框架生态:整合ms-swift、Unsloth、Megatron-LM等核心框架,涵盖全参数/PEFT训练与分布式优化
人工智能·分布式·sft·swift·大模型训练
ATM00634 分钟前
开源AI Agent开发平台Dify源码剖析系列(二)
人工智能·开源·dify·源码剖析
ATM0062 小时前
人机协作系列(四)AI编程的下一个范式革命——看Factory AI如何重构软件工程?
人工智能·大模型·agent·人机协作·人机协同
读创商闻3 小时前
极狐GitLab CEO 柳钢——极狐 GitLab 打造中国企业专属 AI 编程平台,引领编程新潮流
人工智能·gitlab
kailp3 小时前
语言模型玩转3D生成:LLaMA-Mesh开源项目
人工智能·3d·ai·语言模型·llama·gpu算力
marteker3 小时前
弗兰肯斯坦式的人工智能与GTM策略的崩溃
人工智能·搜索引擎
无心水3 小时前
大语言模型零样本情感分析实战:无需机器学习训练,96%准确率实现指南
人工智能·机器学习·语言模型