人工智能之基于阿里云快速搭建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提供的操作内容进行快速的模型搭建。

相关推荐
lili-felicity2 分钟前
CANN批处理优化技巧:从动态批处理到流水线并行
人工智能·python
一枕眠秋雨>o<9 分钟前
算子之力:解码CANN ops-nn如何重塑昇腾AI计算范式
人工智能
AI科技10 分钟前
原创音乐人运用AI编曲软件,编曲怎么配和弦的声音
人工智能
dazzle12 分钟前
机器学习算法原理与实践-入门(三):使用数学方法实现KNN
人工智能·算法·机器学习
那个村的李富贵14 分钟前
智能炼金术:CANN加速的新材料AI设计系统
人工智能·算法·aigc·cann
凯子坚持 c15 分钟前
CANN 生态新星:`minddata-dataset-engine` 如何加速 AI 数据 pipeline
人工智能
Fairy要carry17 分钟前
面试-GRPO强化学习
开发语言·人工智能
xiaobaibai15318 分钟前
营销自动化终极形态:AdAgent 自主闭环工作流全解析
大数据·人工智能·自动化
自不量力的A同学25 分钟前
Solon AI v3.9 正式发布:全能 Skill 爆发
java·网络·人工智能
一枕眠秋雨>o<30 分钟前
从抽象到具象:TBE如何重构AI算子的编译哲学
人工智能