一、简介
上海人工智能实验室的代季峰教授团队最近开发了一种新型多模态大模型Mono-InternVL,该模型在多模态任务中表现卓越,显示出技术上的显著优势。Mono-InternVL通过内嵌视觉专家,优化了视觉感知与理解的集成,大幅提高了处理效率。该模型采用了增量预训练方法,有效降低了训练中的信息遗忘问题,并通过内生视觉预训练方法,增强了模型在复杂任务中的性能。在多项多模态基准测试中,Mono-InternVL展现了优于现有模型的能力,特别是在OCR、问答系统和图表解析等方面表现出色。
二、环境安装
https://huggingface.co/OpenGVLab/Mono-InternVL-2B
conda create -n mono python==3.9conda activate monopip install transformers==4.37.2conda install pytorch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 pytorch-cuda=11.8 -c pytorch -c nvidiapip install decordpip install peftpip install einopspip install sentencepiecepip install fastapipip install python-multipartpip install uvicornexport HF_ENDPOINT=https://hf-mirror.com
三、测试代码
官方给的测试代码:
import numpy as np
import torch
import torchvision.transforms as T
from decord import VideoReader, cpu
from PIL import Image
from torchvision.transforms.functional import InterpolationMode
from transformers import AutoModel, AutoTokenizerIMAGENET_MEAN = (0.485, 0.456, 0.406)
IMAGENET_STD = (0.229, 0.224, 0.225)def build_transform(input_size):MEAN, STD = IMAGENET_MEAN, IMAGENET_STDtransform = T.Compose([T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),T.ToTensor(),T.Normalize(mean=MEAN, std=STD)])return transformdef find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):best_ratio_diff = float('inf')best_ratio = (1, 1)area = width * heightfor ratio in target_ratios:target_aspect_ratio = ratio[0] / ratio[1]ratio_diff = abs(aspect_ratio - target_aspect_ratio)if ratio_diff < best_ratio_diff:best_ratio_diff = ratio_diffbest_ratio = ratioelif ratio_diff == best_ratio_diff:if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:best_ratio = ratioreturn best_ratiodef dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):orig_width, orig_height = image.sizeaspect_ratio = orig_width / orig_height# calculate the existing image aspect ratiotarget_ratios = set((i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) ifi * j <= max_num and i * j >= min_num)target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])# find the closest aspect ratio to the targettarget_aspect_ratio = find_closest_aspect_ratio(aspect_ratio, target_ratios, orig_width, orig_height, image_size)# calculate the target width and heighttarget_width = image_size * target_aspect_ratio[0]target_height = image_size * target_aspect_ratio[1]blocks = target_aspect_ratio[0] * target_aspect_ratio[1]# resize the imageresized_img = image.resize((target_width, target_height))processed_images = []for i in range(blocks):box = ((i % (target_width // image_size)) * image_size,(i // (target_width // image_size)) * image_size,((i % (target_width // image_size)) + 1) * image_size,((i // (target_width // image_size)) + 1) * image_size)# split the imagesplit_img = resized_img.crop(box)processed_images.append(split_img)assert len(processed_images) == blocksif use_thumbnail and len(processed_images) != 1:thumbnail_img = image.resize((image_size, image_size))processed_images.append(thumbnail_img)return processed_imagesdef load_image(image_file, input_size=448, max_num=12):image = Image.open(image_file).convert('RGB')transform = build_transform(input_size=input_size)images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)pixel_values = [transform(image) for image in images]pixel_values = torch.stack(pixel_values)return pixel_valuespath = './'
model = AutoModel.from_pretrained(path,torch_dtype=torch.bfloat16,low_cpu_mem_usage=True,trust_remote_code=True).eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)# set the max number of tiles in `max_num`
pixel_values = load_image('./examples/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
generation_config = dict(max_new_tokens=1024, do_sample=True)# # pure-text conversation (纯文本对话)
# question = 'Hello, who are you?'
# response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
# print(f'User: {question}\nAssistant: {response}')# question = 'Can you tell me a story?'
# response, history = model.chat(tokenizer, None, question, generation_config, history=history, return_history=True)
# print(f'User: {question}\nAssistant: {response}')# # single-image single-round conversation (单图单轮对话)
# question = '<image>\nPlease describe the image shortly.'
# response = model.chat(tokenizer, pixel_values, question, generation_config)
# print(f'User: {question}\nAssistant: {response}')# single-image multi-round conversation (单图多轮对话)
question = '<image>\n识别图片中的文字'
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
print(f'User: {question}\nAssistant: {response}')
四、Mono-InternVL 和qwen2vl-7B 效果测评
目前开源的是:Mono-InternVL-2B 比qwen2vl-7B要小,所以速度更快,显存占用更小,但是效果要差一些。
Mono-InternVL-2B | qwen2vl-7B |
这张图片中的动物是一只红熊猫。红熊猫是一种小型的熊科动物,以其浓密的粉红色毛皮和独特的黑白相间的头部花纹(被称为“熊猫头”)而闻名。图片中的红熊猫正趴在木制的平台上,似乎正在休息或观察周围的环境。红熊猫常见于中国的云贵高原地区,是一种濒危物种,目前已经被记录在《濒危野生动植物种国际贸易公约》(CITES)中,受到严格保护。 | 这张图片展示了一只小熊猫。小熊猫的毛色主要是橙红色,带有白色的面部和耳朵。它正靠在一个木制的平台上,背景是树木和绿色的叶子。小熊猫看起来很可爱,表情温和。 |
从图中可以看到直角ABC,其中AB=15, BC=9,直角三角形ABC中,直角边的长度等于斜边的一半。因为这是一个直角三角形,其中直角边长为9,所以斜边长为:\n\n斜边 = 直角边的1.5倍, \n斜边 = 9 x 1.5 = 13.5\n\n于是,\n\nx = 10.5,\n\n所以x的值为10.5。\n\n这种题目通常是通过找到三角形三个特定边的比值,来求其斜边。 | 这是一个直角三角形,其中一条直角边长为9,斜边长为15。我们可以使用勾股定理来计算未知边长x的值。\n\n勾股定理公式为:\\(a^2 + b^2 = c^2\\),其中c是斜边长,a和b是直角边长。\n\n在这个问题中,a = 9,c = 15,我们需要找到b(即x的值)。\n\n将已知值代入公式中,得到:\n\\[9^2 + x^2 = 15^2\\]\n\\[81 + x^2 = 225\\]\n\\[x^2 = 225 - 81\\]\n\\[x^2 = 144\\]\n\\[x = \\sqrt{144}\\]\n\\[x = 12\\]\n\n所以,x的值是12。 |