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

相关推荐
Jsundoku2 分钟前
第六章 深度学习 -- 循环神经网络
人工智能·深度学习·循环神经网络·1024程序员节
用户14465546834015 分钟前
Spring AI Alibaba Agent 长流程栈溢出:从 Reactor 调用链到修复验证
人工智能
染指111013 分钟前
155.Agent-LangGraph-节点级重试策略
人工智能·langchain·1024程序员节·agents
soft_hub122 分钟前
普通话转写APP怎么判断准确率是否真实
人工智能
xsd2024111834 分钟前
Pascal Editor:开源Web 3D建筑编辑器
人工智能
AI技术新视界35 分钟前
递归自我改进(RSI)是什么
人工智能·llm·1024程序员节·rsi
2601_9556624636 分钟前
小说推文如何制作多人配音?从角色划分到音频生成
人工智能·音视频·语音识别·媒体·视频
林浩杨_44 分钟前
腾讯Hy-Memory:让Agent学会正确记忆
人工智能·强化学习·1024程序员节·长期记忆
zhangfeng11331 小时前
EpipolarPose(常被简写为 epipose) CVPR 2019,**自监督3D人体姿态估计**
人工智能·1024程序员节
weixin_440401691 小时前
大数据技术-人工智能编程题
大数据·人工智能·python·1024程序员节