Add helper functions
#3
by
nielsr
HF Staff
- opened
README.md
CHANGED
@@ -52,13 +52,94 @@ Codebase and datasets can be found at https://github.com/AI4Chem/ChemVlm.
|
|
52 |
## Quick Start
|
53 |
|
54 |
```python
|
55 |
-
from transformers import AutoTokenizer,
|
56 |
import torch
|
57 |
import torchvision.transforms as T
|
|
|
58 |
from torchvision.transforms.functional import InterpolationMode
|
59 |
-
from PIL import Image
|
60 |
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
tokenizer = AutoTokenizer.from_pretrained('AI4Chem/ChemVLM-8B', trust_remote_code=True)
|
64 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
52 |
## Quick Start
|
53 |
|
54 |
```python
|
55 |
+
from transformers import AutoTokenizer, AutoModelforCasualLM
|
56 |
import torch
|
57 |
import torchvision.transforms as T
|
58 |
+
import transformers
|
59 |
from torchvision.transforms.functional import InterpolationMode
|
|
|
60 |
|
61 |
+
|
62 |
+
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
63 |
+
IMAGENET_STD = (0.229, 0.224, 0.225)
|
64 |
+
|
65 |
+
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
66 |
+
IMAGENET_STD = (0.229, 0.224, 0.225)
|
67 |
+
|
68 |
+
|
69 |
+
def build_transform(input_size):
|
70 |
+
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
|
71 |
+
transform = T.Compose([
|
72 |
+
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
73 |
+
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
|
74 |
+
T.ToTensor(),
|
75 |
+
T.Normalize(mean=MEAN, std=STD)
|
76 |
+
])
|
77 |
+
return transform
|
78 |
+
|
79 |
+
|
80 |
+
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
81 |
+
best_ratio_diff = float('inf')
|
82 |
+
best_ratio = (1, 1)
|
83 |
+
area = width * height
|
84 |
+
for ratio in target_ratios:
|
85 |
+
target_aspect_ratio = ratio[0] / ratio[1]
|
86 |
+
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
|
87 |
+
if ratio_diff < best_ratio_diff:
|
88 |
+
best_ratio_diff = ratio_diff
|
89 |
+
best_ratio = ratio
|
90 |
+
elif ratio_diff == best_ratio_diff:
|
91 |
+
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
|
92 |
+
best_ratio = ratio
|
93 |
+
return best_ratio
|
94 |
+
|
95 |
+
|
96 |
+
def dynamic_preprocess(image, min_num=1, max_num=6, image_size=448, use_thumbnail=False):
|
97 |
+
orig_width, orig_height = image.size
|
98 |
+
aspect_ratio = orig_width / orig_height
|
99 |
+
|
100 |
+
# calculate the existing image aspect ratio
|
101 |
+
target_ratios = set(
|
102 |
+
(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
|
103 |
+
i * j <= max_num and i * j >= min_num)
|
104 |
+
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
105 |
+
|
106 |
+
# find the closest aspect ratio to the target
|
107 |
+
target_aspect_ratio = find_closest_aspect_ratio(
|
108 |
+
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
|
109 |
+
|
110 |
+
# calculate the target width and height
|
111 |
+
target_width = image_size * target_aspect_ratio[0]
|
112 |
+
target_height = image_size * target_aspect_ratio[1]
|
113 |
+
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
114 |
+
|
115 |
+
# resize the image
|
116 |
+
resized_img = image.resize((target_width, target_height))
|
117 |
+
processed_images = []
|
118 |
+
for i in range(blocks):
|
119 |
+
box = (
|
120 |
+
(i % (target_width // image_size)) * image_size,
|
121 |
+
(i // (target_width // image_size)) * image_size,
|
122 |
+
((i % (target_width // image_size)) + 1) * image_size,
|
123 |
+
((i // (target_width // image_size)) + 1) * image_size
|
124 |
+
)
|
125 |
+
# split the image
|
126 |
+
split_img = resized_img.crop(box)
|
127 |
+
processed_images.append(split_img)
|
128 |
+
assert len(processed_images) == blocks
|
129 |
+
if use_thumbnail and len(processed_images) != 1:
|
130 |
+
thumbnail_img = image.resize((image_size, image_size))
|
131 |
+
processed_images.append(thumbnail_img)
|
132 |
+
return processed_images
|
133 |
+
|
134 |
+
|
135 |
+
def load_image(image_file, input_size=448, max_num=6):
|
136 |
+
image = Image.open(image_file).convert('RGB')
|
137 |
+
transform = build_transform(input_size=input_size)
|
138 |
+
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
|
139 |
+
pixel_values = [transform(image) for image in images]
|
140 |
+
pixel_values = torch.stack(pixel_values)
|
141 |
+
return pixel_values
|
142 |
+
|
143 |
|
144 |
tokenizer = AutoTokenizer.from_pretrained('AI4Chem/ChemVLM-8B', trust_remote_code=True)
|
145 |
model = AutoModelForCausalLM.from_pretrained(
|