人工智能之基于阿里云快速搭建Llama-3.2-11B-Vision-Instruct

人工智能之基于阿里云快速搭建Llama-3.2-11B-Vision-Instruct

需求描述

  1. 基于阿里云搭建图片生成文字模型,模型名称:LLM-Research/Llama-3.2-11B-Vision-Instruct
  2. 使用上述模型输入图片生成文字,模型路径

业务实现

阿里云配置

阿里云配置如下:

代码验证

python 复制代码
import requests
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor
from modelscope import snapshot_download
model_id = "LLM-Research/Llama-3.2-11B-Vision-Instruct"
model_dir = snapshot_download(model_id, ignore_file_pattern=['*.pth'])

model = MllamaForConditionalGeneration.from_pretrained(
    model_dir,
    torch_dtype=torch.bfloat16,
    device_map="auto",
)
processor = AutoProcessor.from_pretrained(model_dir)

url = "https://vcg01.cfp.cn/creative/vcg/800/new/VCG41519623066.jpg"
image = Image.open(requests.get(url, stream=True).raw)

messages = [
    {"role": "user", "content": [
        {"type": "image"},
        {"type": "text", "text": "如果你需要对图片描述,你会怎么描述?"}
    ]}
]
input_text \
       = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(image, input_text, return_tensors="pt").to(model.device)

output = model.generate(**inputs, max_new_tokens=30)
print(processor.decode(output[0]))

结果展示如下,生成的文本信息如下:

The image shows a baby in a white outfit being held by an adult, with the baby looking at the camera and smiling.

从多次搭建的经验来看,建议在搭建模型相关的内容的时候,优先选择阿里云的相关服务,很多东西阿里云的容器云已经提供底层技术,可以很快的使用modelscope提供的操作内容进行快速的模型搭建。

相关推荐
赫尔·普莱蒂科萨·帕塔1 分钟前
医疗新纪元的开启
人工智能·chatgpt
m0_603888714 分钟前
Scaling Trends for Multi-Hop Contextual Reasoning in Mid-Scale Language Models
人工智能·算法·ai·语言模型·论文速览
飞凌嵌入式4 分钟前
解析一下面向教育领域的RV1126B\RK3506B\RK3576开发板
linux·人工智能
congming20207 分钟前
AI赋能软考高项论文:140天分层突破法(适配新大纲绩效域)
人工智能
萤丰信息10 分钟前
开启园区“生命体”时代——智慧园区系统,定义未来的办公与生活
java·大数据·运维·数据库·人工智能·生活·智慧园区
Toky丶10 分钟前
【文献阅读】Pretraining Large Language Models with NVFP4
人工智能·语言模型·自然语言处理
颜值博主11 分钟前
新一代大模型范式: Inner Tools
人工智能·ai·语言模型
IT_陈寒12 分钟前
Python 3.12 新特性实战:这5个改进让我的开发效率提升40%
前端·人工智能·后端
comli_cn14 分钟前
残差链接(Residual Connection)
人工智能·算法
摸鱼仙人~16 分钟前
在政务公文场景中落地 RAG + Agent:技术难点与系统化解决方案
人工智能·政务