update
Browse files
README.md
CHANGED
|
@@ -24,6 +24,27 @@ from PIL import Image
|
|
| 24 |
import warnings
|
| 25 |
import numpy as np
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# set device
|
| 28 |
device = 'cuda' # or cpu
|
| 29 |
torch.set_default_device(device)
|
|
@@ -44,8 +65,7 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 44 |
img_path = 'sample/4927.png'
|
| 45 |
prompt = 'Based on the image, first describe what you see in the figure, then predict the construction_cdl and image_cdl and calibrate it.'
|
| 46 |
text = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image>\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
|
| 47 |
-
|
| 48 |
-
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1][1:], dtype=torch.long).unsqueeze(0).to(device)
|
| 49 |
|
| 50 |
# image, sample images can be found in images folder
|
| 51 |
image = Image.open(img_path).convert('RGB')
|
|
|
|
| 24 |
import warnings
|
| 25 |
import numpy as np
|
| 26 |
|
| 27 |
+
def tokenizer_image_token(prompt, tokenizer, image_token_index, return_tensors=None):
|
| 28 |
+
prompt_chunks = [tokenizer(chunk).input_ids for chunk in prompt.split('<image>')]
|
| 29 |
+
|
| 30 |
+
def insert_separator(X, sep):
|
| 31 |
+
return [ele for sublist in zip(X, [sep] * len(X)) for ele in sublist][:-1]
|
| 32 |
+
|
| 33 |
+
input_ids = []
|
| 34 |
+
offset = 0
|
| 35 |
+
if len(prompt_chunks) > 0 and len(prompt_chunks[0]) > 0 and prompt_chunks[0][0] == tokenizer.bos_token_id:
|
| 36 |
+
offset = 1
|
| 37 |
+
input_ids.append(prompt_chunks[0][0])
|
| 38 |
+
|
| 39 |
+
for x in insert_separator(prompt_chunks, [image_token_index] * (offset + 1)):
|
| 40 |
+
input_ids.extend(x[offset:])
|
| 41 |
+
|
| 42 |
+
if return_tensors is not None:
|
| 43 |
+
if return_tensors == 'pt':
|
| 44 |
+
return torch.tensor(input_ids, dtype=torch.long)
|
| 45 |
+
raise ValueError(f'Unsupported tensor type: {return_tensors}')
|
| 46 |
+
return input_ids
|
| 47 |
+
|
| 48 |
# set device
|
| 49 |
device = 'cuda' # or cpu
|
| 50 |
torch.set_default_device(device)
|
|
|
|
| 65 |
img_path = 'sample/4927.png'
|
| 66 |
prompt = 'Based on the image, first describe what you see in the figure, then predict the construction_cdl and image_cdl and calibrate it.'
|
| 67 |
text = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image>\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
|
| 68 |
+
input_ids = tokenizer_image_token(text, tokenizer, -200, return_tensors='pt').unsqueeze(0).cuda()
|
|
|
|
| 69 |
|
| 70 |
# image, sample images can be found in images folder
|
| 71 |
image = Image.open(img_path).convert('RGB')
|