Image-Text-to-Text
Transformers
Safetensors
mage_vl
multimodal
vision-language-model
mage-vl
video-understanding
streaming
conversational
custom_code
Instructions to use microsoft/Mage-VL with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Mage-VL with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="microsoft/Mage-VL", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoModelForImageTextToText model = AutoModelForImageTextToText.from_pretrained("microsoft/Mage-VL", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Mage-VL with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Mage-VL" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Mage-VL", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/microsoft/Mage-VL
- SGLang
How to use microsoft/Mage-VL with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "microsoft/Mage-VL" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Mage-VL", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "microsoft/Mage-VL" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Mage-VL", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use microsoft/Mage-VL with Docker Model Runner:
docker model run hf.co/microsoft/Mage-VL
File size: 6,756 Bytes
12acbba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import json
import os
from unittest.mock import patch
import torch
from torch.nn.modules.utils import consume_prefix_in_state_dict_if_present
import numpy as np
def str2bool(v):
return str(v).lower() in ("yes", "y", "true", "t", "1")
def set_torch_env():
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ":4096:8"
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
torch.use_deterministic_algorithms(True)
torch.manual_seed(0)
torch.set_num_threads(1)
np.random.seed(seed=0)
try:
# require pytorch >= 2.2.0
torch.utils.deterministic.fill_uninitialized_memory = False
except Exception: # pylint: disable=W0718
pass
def create_folder(path, print_if_create=False):
if not os.path.exists(path):
os.makedirs(path)
if print_if_create:
print(f"created folder: {path}")
def get_state_dict(ckpt_path):
ckpt = torch.load(ckpt_path, map_location=torch.device('cpu'), weights_only=True)
if "state_dict" in ckpt:
ckpt = ckpt['state_dict']
if "net" in ckpt:
ckpt = ckpt["net"]
consume_prefix_in_state_dict_if_present(ckpt, prefix="module.")
return ckpt
@patch('json.encoder.c_make_encoder', None)
def dump_json(obj, fid, float_digits=-1, **kwargs):
of = json.encoder._make_iterencode # pylint: disable=W0212
def inner(*args, **kwargs):
args = list(args)
# fifth argument is float formater which we will replace
args[4] = lambda o: format(o, '.%df' % float_digits)
return of(*args, **kwargs)
with patch('json.encoder._make_iterencode', wraps=inner):
json.dump(obj, fid, **kwargs)
def generate_log_json(frame_num, frame_pixel_num, test_time, frame_types, bits, psnrs, ssims,
verbose=False, avg_encoding_time=None, avg_decoding_time=None):
include_yuv = len(psnrs[0]) > 1
assert not include_yuv or (len(psnrs[0]) == 4 and len(ssims[0]) == 4)
i_bits = 0
i_psnr = 0
i_psnr_y = 0
i_psnr_u = 0
i_psnr_v = 0
i_ssim = 0
i_ssim_y = 0
i_ssim_u = 0
i_ssim_v = 0
p_bits = 0
p_psnr = 0
p_psnr_y = 0
p_psnr_u = 0
p_psnr_v = 0
p_ssim = 0
p_ssim_y = 0
p_ssim_u = 0
p_ssim_v = 0
i_num = 0
p_num = 0
for idx in range(frame_num):
if frame_types[idx] == 0:
i_bits += bits[idx]
i_psnr += psnrs[idx][0]
i_ssim += ssims[idx][0]
i_num += 1
if include_yuv:
i_psnr_y += psnrs[idx][1]
i_psnr_u += psnrs[idx][2]
i_psnr_v += psnrs[idx][3]
i_ssim_y += ssims[idx][1]
i_ssim_u += ssims[idx][2]
i_ssim_v += ssims[idx][3]
else:
p_bits += bits[idx]
p_psnr += psnrs[idx][0]
p_ssim += ssims[idx][0]
p_num += 1
if include_yuv:
p_psnr_y += psnrs[idx][1]
p_psnr_u += psnrs[idx][2]
p_psnr_v += psnrs[idx][3]
p_ssim_y += ssims[idx][1]
p_ssim_u += ssims[idx][2]
p_ssim_v += ssims[idx][3]
log_result = {}
log_result['frame_pixel_num'] = frame_pixel_num
log_result['i_frame_num'] = i_num
log_result['p_frame_num'] = p_num
log_result['ave_i_frame_bpp'] = i_bits / i_num / frame_pixel_num
log_result['ave_i_frame_psnr'] = i_psnr / i_num
log_result['ave_i_frame_msssim'] = i_ssim / i_num
if include_yuv:
log_result['ave_i_frame_psnr_y'] = i_psnr_y / i_num
log_result['ave_i_frame_psnr_u'] = i_psnr_u / i_num
log_result['ave_i_frame_psnr_v'] = i_psnr_v / i_num
log_result['ave_i_frame_msssim_y'] = i_ssim_y / i_num
log_result['ave_i_frame_msssim_u'] = i_ssim_u / i_num
log_result['ave_i_frame_msssim_v'] = i_ssim_v / i_num
if verbose:
log_result['frame_bpp'] = list(np.array(bits) / frame_pixel_num)
log_result['frame_psnr'] = [v[0] for v in psnrs]
log_result['frame_msssim'] = [v[0] for v in ssims]
log_result['frame_type'] = frame_types
if include_yuv:
log_result['frame_psnr_y'] = [v[1] for v in psnrs]
log_result['frame_psnr_u'] = [v[2] for v in psnrs]
log_result['frame_psnr_v'] = [v[3] for v in psnrs]
log_result['frame_msssim_y'] = [v[1] for v in ssims]
log_result['frame_msssim_u'] = [v[2] for v in ssims]
log_result['frame_msssim_v'] = [v[3] for v in ssims]
log_result['test_time'] = test_time
if p_num > 0:
total_p_pixel_num = p_num * frame_pixel_num
log_result['ave_p_frame_bpp'] = p_bits / total_p_pixel_num
log_result['ave_p_frame_psnr'] = p_psnr / p_num
log_result['ave_p_frame_msssim'] = p_ssim / p_num
if include_yuv:
log_result['ave_p_frame_psnr_y'] = p_psnr_y / p_num
log_result['ave_p_frame_psnr_u'] = p_psnr_u / p_num
log_result['ave_p_frame_psnr_v'] = p_psnr_v / p_num
log_result['ave_p_frame_msssim_y'] = p_ssim_y / p_num
log_result['ave_p_frame_msssim_u'] = p_ssim_u / p_num
log_result['ave_p_frame_msssim_v'] = p_ssim_v / p_num
else:
log_result['ave_p_frame_bpp'] = 0
log_result['ave_p_frame_psnr'] = 0
log_result['ave_p_frame_msssim'] = 0
if include_yuv:
log_result['ave_p_frame_psnr_y'] = 0
log_result['ave_p_frame_psnr_u'] = 0
log_result['ave_p_frame_psnr_v'] = 0
log_result['ave_p_frame_msssim_y'] = 0
log_result['ave_p_frame_msssim_u'] = 0
log_result['ave_p_frame_msssim_v'] = 0
log_result['ave_all_frame_bpp'] = (i_bits + p_bits) / (frame_num * frame_pixel_num)
log_result['ave_all_frame_psnr'] = (i_psnr + p_psnr) / frame_num
log_result['ave_all_frame_msssim'] = (i_ssim + p_ssim) / frame_num
if avg_encoding_time is not None and avg_decoding_time is not None:
log_result['avg_frame_encoding_time'] = avg_encoding_time
log_result['avg_frame_decoding_time'] = avg_decoding_time
if include_yuv:
log_result['ave_all_frame_psnr_y'] = (i_psnr_y + p_psnr_y) / frame_num
log_result['ave_all_frame_psnr_u'] = (i_psnr_u + p_psnr_u) / frame_num
log_result['ave_all_frame_psnr_v'] = (i_psnr_v + p_psnr_v) / frame_num
log_result['ave_all_frame_msssim_y'] = (i_ssim_y + p_ssim_y) / frame_num
log_result['ave_all_frame_msssim_u'] = (i_ssim_u + p_ssim_u) / frame_num
log_result['ave_all_frame_msssim_v'] = (i_ssim_v + p_ssim_v) / frame_num
return log_result
|