


pip install -r requirements.txt
pip install -r requirements_wo_ds.txt
pip install --no-deps 'SwissArmyTransformer>=0.3.6'
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("THUDM/visualglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/visualglm-6b", trust_remote_code=True).half().cuda()
image_path = "your image path"
response, history = model.chat(tokenizer, image_path, "描述这张图片。", history=[])
print(response)
response, history = model.chat(tokenizer, "这张图片可能是在什么场所拍摄的?", history=history)
print(response)
如果使用SwissArmyTransformer库调用模型,方法类似,可以使用环境变量SAT_HOME决定模型下载位置。在本仓库目录下
import argparse
from transformers import AutoTokenizer
"THUDM/chatglm-6b", trust_remote_code=True) tokenizer = AutoTokenizer.from_pretrained(
from model import chat, VisualGLMModel
'visualglm-6b', args=argparse.Namespace(fp16=True, skip_init=True)) model, model_args = VisualGLMModel.from_pretrained(
from sat.model.mixins import CachedAutoregressiveMixin
'auto-regressive', CachedAutoregressiveMixin()) model.add_mixin(
"your image path or URL" image_path =
history, cache_image = chat(image_path, model, tokenizer, "描述这张图片。", history=[]) >> response,
print(response)
history, cache_image = chat(None, model, tokenizer, "这张图片可能是在什么场所拍摄的?", history=history, image=cache_image) >> response,
print(response)
使用 sat 库也可以轻松进行进行参数高效微调。
命令行 Demo
python cli_demo.py

程序提供如下超参数控制生成过程与量化精度:
usage: cli_demo.py [-h] [--max_length MAX_LENGTH] [--top_p TOP_P] [--top_k TOP_K] [--temperature TEMPERATURE] [--english] [--quant {8,4}]
optional arguments:
-h, --help show this help message and exit
--max_length MAX_LENGTH
max length of the total sequence
--top_p TOP_P top p for nucleus sampling
--top_k TOP_K top k for top k sampling
--temperature TEMPERATURE
temperature for sampling
--english only output English
--quant {8,4} quantization bits
我们也提供了继承自ChatGLM-6B的打字机效果命令行工具,此工具使用Huggingface模型:
python cli_demo_hf.py

我们提供了一个基于 Gradio 的网页版 Demo,首先安装 Gradio:pip install gradio。然后下载并进入本仓库运行web_demo.py:
git clone https://github.com/THUDM/VisualGLM-6B
cd VisualGLM-6B
python web_demo.py
程序会自动下载sat模型,并运行一个 Web Server,并输出地址。在浏览器中打开输出的地址即可使用。--quant 4使用4比特量化减少显存占用。
python web_demo_hf.py
python api.py
程序会自动下载sat模型,默认部署在本地的 8080 端口,通过 POST 方法进行调用。下面是用curl请求的例子,一般而言可以也可以使用代码方法进行POST。
echo "{\"image\":\"$(base64 path/to/example.jpg)\",\"text\":\"描述这张图片\",\"history\":[]}" > temp.json
curl -X POST -H "Content-Type: application/json" -d .json http://127.0.0.1:8080
得到的返回值为
{
"response":"这张图片展现了一只可爱的卡通羊驼,它站在一个透明的背景上。这只羊驼长着一张毛茸茸的耳朵和一双大大的眼睛,它的身体是白色的,带有棕色斑点。",
"history":[('描述这张图片', '这张图片展现了一只可爱的卡通羊驼,它站在一个透明的背景上。这只羊驼长着一张毛茸茸的耳朵和一双大大的眼睛,它的身体是白色的,带有棕色斑点。')],
"status":200,
"time":"2023-05-16 20:20:10"
}
在Huggingface实现中,模型默认以 FP16 精度加载,运行上述代码需要大概 15GB 显存。如果你的 GPU 显存有限,可以尝试以量化方式加载模型。使用方法如下:
# 按需修改,目前只支持 4/8 bit 量化。下面将只量化ChatGLM,ViT 量化时误差较大
model = AutoModel.from_pretrained("THUDM/visualglm-6b", trust_remote_code=True).quantize(8).half().cuda()
在sat实现中,需先传参将加载位置改为cpu,再进行量化。方法如下,详见cli_demo.py:
from sat.quantization.kernels import quantize
model = quantize(model.transformer, args.quant).cuda()
# 指定 model.transformer 只量化 ChatGLM,ViT 量化时误差较大
-
图像描述事实性/模型幻觉问题。在生成图像长描述的时候,距离图像较远时,语言模型的将占主导,有一定可能根据上下文生成并不存在于图像的内容。 -
属性错配问题。在多物体的场景中,部分物体的某些属性,经常被错误安插到其他物体上。 -
分辨率问题。本项目使用了224*224的分辨率,也是视觉模型中最为常用的尺寸;然而为了进行更细粒度的理解,更大的分辨率和计算量是必要的。
如果你有好的文章希望通过我们的平台分享给更多人,请通过这个链接与我们联系:
https://bit.ly/hf-tougao
本文分享自微信公众号 - Hugging Face(gh_504339124f0f)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。