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

相关推荐
Liue612312313 小时前
基于YOLOv26的口罩佩戴检测与识别系统实现与优化
人工智能·yolo·目标跟踪
小二·4 小时前
Python Web 开发进阶实战 :AI 原生数字孪生 —— 在 Flask + Three.js 中构建物理世界实时仿真与优化平台
前端·人工智能·python
chinesegf4 小时前
文本嵌入模型的比较(一)
人工智能·算法·机器学习
珠海西格电力5 小时前
零碳园区的能源结构优化需要哪些技术支持?
大数据·人工智能·物联网·架构·能源
Black蜡笔小新5 小时前
视频汇聚平台EasyCVR打造校园消防智能监管新防线
网络·人工智能·音视频
珠海西格电力科技5 小时前
双碳目标下,微电网为何成为能源转型核心载体?
网络·人工智能·物联网·云计算·智慧城市·能源
2501_941837265 小时前
【计算机视觉】基于YOLOv26的交通事故检测与交通状况分析系统详解_1
人工智能·yolo·计算机视觉
HyperAI超神经5 小时前
加州大学构建基于全连接神经网络的片上光谱仪,在芯片级尺寸上实现8纳米的光谱分辨率
人工智能·深度学习·神经网络·机器学习·ai编程
badfl5 小时前
AI漫剧技术方案拆解:NanoBanana+Sora视频生成全流程
人工智能·ai·ai作画
杭州杭州杭州5 小时前
李沐动手学深度学习笔记(4)---物体检测基础
人工智能·笔记·深度学习