Instructions to use jcwang0602/MLLMSeg_InternVL2_5_1B_RES with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use jcwang0602/MLLMSeg_InternVL2_5_1B_RES with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="jcwang0602/MLLMSeg_InternVL2_5_1B_RES", trust_remote_code=True)# Load model directly from transformers import MLLMSeg model = MLLMSeg.from_pretrained("jcwang0602/MLLMSeg_InternVL2_5_1B_RES", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
Remove code snippet, add base model (#2)
Browse files- Remove code snippet, add base model (ff512191277128d482bc2baaf052ef54155a256e)
Co-authored-by: Niels Rogge <nielsr@users.noreply.huggingface.co>
README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
| 2 |
license: mit
|
| 3 |
pipeline_tag: image-segmentation
|
| 4 |
library_name: transformers
|
|
|
|
|
|
|
| 5 |
---
|
| 6 |
|
| 7 |
# MLLMSeg: Unlocking the Potential of MLLMs in Referring Expression Segmentation via a Light-weight Mask Decoder
|
|
@@ -33,111 +35,9 @@ pip install -r requirements.txt # Assuming requirements.txt from the cloned repo
|
|
| 33 |
pip install flash-attn==2.3.6 --no-build-isolation # Note: requires GPU to install
|
| 34 |
```
|
| 35 |
|
| 36 |
-
##
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
import numpy as np
|
| 40 |
-
import torch
|
| 41 |
-
import torchvision.transforms as T
|
| 42 |
-
from PIL import Image
|
| 43 |
-
from torchvision.transforms.functional import InterpolationMode
|
| 44 |
-
from transformers import AutoModel, AutoTokenizer
|
| 45 |
-
|
| 46 |
-
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
| 47 |
-
IMAGENET_STD = (0.229, 0.224, 0.225)
|
| 48 |
-
|
| 49 |
-
def build_transform(input_size):
|
| 50 |
-
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
|
| 51 |
-
transform = T.Compose([
|
| 52 |
-
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
| 53 |
-
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
|
| 54 |
-
T.ToTensor(),
|
| 55 |
-
T.Normalize(mean=MEAN, std=STD)
|
| 56 |
-
])
|
| 57 |
-
return transform
|
| 58 |
-
|
| 59 |
-
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
| 60 |
-
best_ratio_diff = float('inf')
|
| 61 |
-
best_ratio = (1, 1)
|
| 62 |
-
area = width * height
|
| 63 |
-
for ratio in target_ratios:
|
| 64 |
-
target_aspect_ratio = ratio[0] / ratio[1]
|
| 65 |
-
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
|
| 66 |
-
if ratio_diff < best_ratio_diff:
|
| 67 |
-
best_ratio_diff = ratio_diff
|
| 68 |
-
best_ratio = ratio
|
| 69 |
-
elif ratio_diff == best_ratio_diff:
|
| 70 |
-
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
|
| 71 |
-
best_ratio = ratio
|
| 72 |
-
return best_ratio
|
| 73 |
-
|
| 74 |
-
def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
|
| 75 |
-
orig_width, orig_height = image.size
|
| 76 |
-
aspect_ratio = orig_width / orig_height
|
| 77 |
-
|
| 78 |
-
# calculate the existing image aspect ratio
|
| 79 |
-
target_ratios = set(
|
| 80 |
-
(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) if
|
| 81 |
-
i * j <= max_num and i * j >= min_num)
|
| 82 |
-
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
| 83 |
-
|
| 84 |
-
# find the closest aspect ratio to the target
|
| 85 |
-
target_aspect_ratio = find_closest_aspect_ratio(
|
| 86 |
-
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
|
| 87 |
-
|
| 88 |
-
# calculate the target width and height
|
| 89 |
-
target_width = image_size * target_aspect_ratio[0]
|
| 90 |
-
target_height = image_size * target_aspect_ratio[1]
|
| 91 |
-
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
| 92 |
-
|
| 93 |
-
# resize the image
|
| 94 |
-
resized_img = image.resize((target_width, target_height))
|
| 95 |
-
processed_images = []
|
| 96 |
-
for i in range(blocks):
|
| 97 |
-
box = (
|
| 98 |
-
(i % (target_width // image_size)) * image_size,
|
| 99 |
-
(i // (target_width // image_size)) * image_size,
|
| 100 |
-
((i % (target_width // image_size)) + 1) * image_size,
|
| 101 |
-
((i // (target_width // image_size)) + 1) * image_size
|
| 102 |
-
)
|
| 103 |
-
# split the image
|
| 104 |
-
split_img = resized_img.crop(box)
|
| 105 |
-
processed_images.append(split_img)
|
| 106 |
-
assert len(processed_images) == blocks
|
| 107 |
-
if use_thumbnail and len(processed_images) != 1:
|
| 108 |
-
thumbnail_img = image.resize((image_size, image_size))
|
| 109 |
-
processed_images.append(thumbnail_img)
|
| 110 |
-
return processed_images
|
| 111 |
-
|
| 112 |
-
def load_image(image_file, input_size=448, max_num=12):
|
| 113 |
-
image = Image.open(image_file).convert('RGB')
|
| 114 |
-
transform = build_transform(input_size=input_size)
|
| 115 |
-
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
|
| 116 |
-
pixel_values = [transform(image) for image in images]
|
| 117 |
-
pixel_values = torch.stack(pixel_values)
|
| 118 |
-
return pixel_values
|
| 119 |
-
|
| 120 |
-
# Load the model and tokenizer
|
| 121 |
-
# Note: trust_remote_code=True is required for this model architecture
|
| 122 |
-
model_path = 'jcwang0602/MLLMSeg_InternVL2_5_1B_RES'
|
| 123 |
-
model = AutoModel.from_pretrained(
|
| 124 |
-
model_path,
|
| 125 |
-
torch_dtype=torch.bfloat16,
|
| 126 |
-
low_cpu_mem_usage=True,
|
| 127 |
-
trust_remote_code=True).eval().cuda()
|
| 128 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True, use_fast=False)
|
| 129 |
-
|
| 130 |
-
# Example image (replace with your image path)
|
| 131 |
-
# You can find example images in the GitHub repository of MLLMSeg, e.g., in the 'examples/images' directory.
|
| 132 |
-
image_path = './path/to/your/image.png'
|
| 133 |
-
pixel_values = load_image(image_path, max_num=6).to(torch.bfloat16).cuda()
|
| 134 |
-
generation_config = dict(max_new_tokens=1024, do_sample=True)
|
| 135 |
-
|
| 136 |
-
# Example query for referring expression segmentation
|
| 137 |
-
question = "Please segment the person in the image." # Replace with your specific referring expression
|
| 138 |
-
response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
|
| 139 |
-
print(f'User: {question}
|
| 140 |
-
Assistant: {response}')
|
| 141 |
|
| 142 |
# The 'response' will contain the segmentation mask coordinates in a specific format (normalized 0-1000).
|
| 143 |
# You will need to parse these coordinates and visualize the mask as per the paper's methodology or example scripts.
|
|
|
|
| 2 |
license: mit
|
| 3 |
pipeline_tag: image-segmentation
|
| 4 |
library_name: transformers
|
| 5 |
+
base_model:
|
| 6 |
+
- OpenGVLab/InternVL2_5-2B
|
| 7 |
---
|
| 8 |
|
| 9 |
# MLLMSeg: Unlocking the Potential of MLLMs in Referring Expression Segmentation via a Light-weight Mask Decoder
|
|
|
|
| 35 |
pip install flash-attn==2.3.6 --no-build-isolation # Note: requires GPU to install
|
| 36 |
```
|
| 37 |
|
| 38 |
+
## Usage
|
| 39 |
+
|
| 40 |
+
Refer to the Github README:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# The 'response' will contain the segmentation mask coordinates in a specific format (normalized 0-1000).
|
| 43 |
# You will need to parse these coordinates and visualize the mask as per the paper's methodology or example scripts.
|