BLIP使用教程

news/2024/10/23 12:38:11/

文章目录

  • 准备
  • 测试
    • 示例一
    • 示例二:
  • 结论
  • 源代码

原理篇: BLIP2-图像文本预训练论文解读

准备

如果无网络需提前下载相关模型
安装torch、transformers

pip install torch trtransformers

测试

测试blip基于图片生成文本描述能力(Caption);
caption分为两种:
有条件:在输入图片基础上,增加文本描述提示
无条件:仅输入图片。
以下是测试以blip-image-captioning-base模型进行

示例一

示例图如下:
在这里插入图片描述
运行结果如下:
有条件:a photography of a woman and her dog on the beach. 其中a photography of为输入文本提示
无条件:a woman sitting on the beach with her dog

示例二:

以网上下载musk图片示例,
在这里插入图片描述
运行结果如下:
有条件:a photography of a man in a suit and tie
无条件:a man in a suit and tie is surrounded by reporters

several people作为文本提示,输出结果:
several people are standing in the street

the man is interviewed作为文本提示,输出结果:
the man is interviewed by the media

结论

可对图片进行大致概括,但无法输出细节信息。比如:图中为马斯克、背景中人也没没有进行描述。

源代码

# coding=utf-8
import torch
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGenerationdef blip(img_path):# processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")# model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")processor = BlipProcessor.from_pretrained("./pretrained_models/blip-image-captioning-base")model = BlipForConditionalGeneration.from_pretrained("./pretrained_models/blip-image-captioning-base", torch_dtype=torch.float16).to("cuda")raw_image = Image.open(img_path).convert('RGB')# conditional image captioningtext = "several people"inputs = processor(raw_image, text, return_tensors="pt").to("cuda", torch.float16)out = model.generate(**inputs)print(processor.decode(out[0], skip_special_tokens=True))# >>> a photography of a woman and her dog# unconditional image captioninginputs = processor(raw_image, return_tensors="pt").to("cuda", torch.float16)out = model.generate(**inputs)print(processor.decode(out[0], skip_special_tokens=True))def main():img_path = "./example/blip/musk.jpg"blip(img_path)if __name__ == "__main__":main()

http://www.ppmy.cn/news/212919.html

相关文章

1132 Cut Integer(17行代码)

分数 20 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z 167334, we have A 167 and B 334. It is intere…

金立S8 线刷

由于金立s8广告太多,想通过线刷刷回以前版本,记录关键内容: 1.进REC方式:按电源键音量上键,选择REC,进入机器人倒地界面,按电源键音量下键五秒后按电源上键,进入REC 2. 线刷方式&a…

金立android在哪里设置密码,金立s5.5手机怎么设置锁屏密码 金立s5.5屏幕密码设置教程...

金立s5.5怎么设置屏幕密码?手机里有隐私,不想让别人看到如何设置屏幕密码呢?下面脚本之家小编为大家介绍下s5.5设置屏幕密码教程!不知道的赶快来看下吧! 1.在手机界面点击“设置”,之后会出现“滑动”、“图…

联发科6758_搭载MTK MT6758!金立M7现身跑分网站

随着流行趋势的变化,当前众手机厂商的目光都盯紧了全面屏领域,已上市的全面屏产品之外,另有多家厂商明确表态下半年会有全面屏新品面世,其中就包括金立手机,金立官方早早便宣布,下半年重点发力全面屏领域&a…

金立(Gionee)金立M7 Power root 大金刚 GN5007 刷机TWRP 面具 XP框架 线刷包

刷机 免解锁BL 刷精简官方系统,如果需要售后刷机软件 请联系获取免费获取 成功安装框架、 成功 root 金立(Gionee)金立M7 Power root 大金刚 GN5007 刷机TWRP 面具 XP框架,手机root刷机包 柒叁伍叁玖肆零零陆 ( 转换成小写数字) 远程ro…

金立android手机怎么截图,金立M6手机怎么截图 金立M6截屏/截图方法(两种)

昨天下午,国产老牌手机厂商金立发布了M6安全长续航手机,这款手机主要有两大亮点,一是超级续航,而是内置安全加密芯片。注重续航和安全的用户可能会更加看中这点准备入手。如今就有网友开始咨询金立M6怎么截图,为此脚本…

金立卒于 16 岁

金立的没落,是技术更迭浪潮里守旧者的残酷结局,是企业难以把控资金流向的失控之殇,也可能是董事长个人行为酿下的苦果。 作者 | Yesse& Yorke 责编 | 胡巍巍 一家2017年上半年还有7.6亿元盈利的公司,如何会在一年内迅速走向…

RTMPose关键点检测实战——笔记3

文章目录 摘要安装MMPose安装虚拟环境安装pytorch安装MMCV安装其他的安装包下载 MMPose下载预训练模型权重文件和视频素材 安装MMDetection安装Pytorch安装MMCV安装其它工具包下载 MMDetection安装MMDetection下载预训练模型权重文件和视频素材 MMPose预训练模型预测命令行的方…