Upload README.md
Browse files
README.md
CHANGED
|
@@ -7,13 +7,7 @@ license: apache-2.0
|
|
| 7 |
在运行脚本之前,首先安装如下必要的依赖。
|
| 8 |
|
| 9 |
```shell
|
| 10 |
-
pip install
|
| 11 |
-
pip install torch transformers==4.40.0
|
| 12 |
-
pip install sentencepiece protobuf
|
| 13 |
-
pip install accelerate pillow
|
| 14 |
-
pip install ninja
|
| 15 |
-
pip install packaging
|
| 16 |
-
pip install flash-attn --no-build-isolation
|
| 17 |
```
|
| 18 |
|
| 19 |
```python
|
|
@@ -24,27 +18,6 @@ from PIL import Image
|
|
| 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,6 +38,28 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 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
|
|
|
|
| 7 |
在运行脚本之前,首先安装如下必要的依赖。
|
| 8 |
|
| 9 |
```shell
|
| 10 |
+
pip install torch transformers==4.40.0 accelerate pillow sentencepiece
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
```
|
| 12 |
|
| 13 |
```python
|
|
|
|
| 18 |
import warnings
|
| 19 |
import numpy as np
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# set device
|
| 22 |
device = 'cuda' # or cpu
|
| 23 |
torch.set_default_device(device)
|
|
|
|
| 38 |
img_path = 'sample/4927.png'
|
| 39 |
prompt = 'Based on the image, first describe what you see in the figure, then predict the construction_cdl and image_cdl and calibrate it.'
|
| 40 |
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"
|
| 41 |
+
|
| 42 |
+
def tokenizer_image_token(prompt, tokenizer, image_token_index, return_tensors=None):
|
| 43 |
+
prompt_chunks = [tokenizer(chunk).input_ids for chunk in prompt.split('<image>')]
|
| 44 |
+
|
| 45 |
+
def insert_separator(X, sep):
|
| 46 |
+
return [ele for sublist in zip(X, [sep] * len(X)) for ele in sublist][:-1]
|
| 47 |
+
|
| 48 |
+
input_ids = []
|
| 49 |
+
offset = 0
|
| 50 |
+
if len(prompt_chunks) > 0 and len(prompt_chunks[0]) > 0 and prompt_chunks[0][0] == tokenizer.bos_token_id:
|
| 51 |
+
offset = 1
|
| 52 |
+
input_ids.append(prompt_chunks[0][0])
|
| 53 |
+
|
| 54 |
+
for x in insert_separator(prompt_chunks, [image_token_index] * (offset + 1)):
|
| 55 |
+
input_ids.extend(x[offset:])
|
| 56 |
+
|
| 57 |
+
if return_tensors is not None:
|
| 58 |
+
if return_tensors == 'pt':
|
| 59 |
+
return torch.tensor(input_ids, dtype=torch.long)
|
| 60 |
+
raise ValueError(f'Unsupported tensor type: {return_tensors}')
|
| 61 |
+
return input_ids
|
| 62 |
+
|
| 63 |
input_ids = tokenizer_image_token(text, tokenizer, -200, return_tensors='pt').unsqueeze(0).cuda()
|
| 64 |
|
| 65 |
# image, sample images can be found in images folder
|